| 1 |
20 |
ja_rd |
--------------------------------------------------------------------------------
|
| 2 |
|
|
-- Light8080 simulation test bench.
|
| 3 |
|
|
--------------------------------------------------------------------------------
|
| 4 |
40 |
ja_rd |
-- This test bench was built from a generic template. The details on what tests
|
| 5 |
|
|
-- are performed by this test bench can be found in the assembly source for the
|
| 6 |
|
|
-- 8080 program, in file asm\@PROGNAME@.asm.
|
| 7 |
20 |
ja_rd |
--------------------------------------------------------------------------------
|
| 8 |
|
|
--
|
| 9 |
|
|
-- This test bench provides a simulated CPU system to test programs. This test
|
| 10 |
|
|
-- bench does not do any assertions or checks, all assertions are left to the
|
| 11 |
|
|
-- software.
|
| 12 |
|
|
--
|
| 13 |
|
|
-- The simulated environment has 2KB of RAM, mirror-mapped to all the memory
|
| 14 |
|
|
-- map of the 8080, initialized with the test program object code. See the perl
|
| 15 |
|
|
-- script 'util\hexconv.pl' and BAT files in the asm directory.
|
| 16 |
|
|
--
|
| 17 |
|
|
-- Besides, it provides some means to trigger hardware irq from software,
|
| 18 |
|
|
-- including the specification of the instructions fed to the CPU as interrupt
|
| 19 |
|
|
-- vectors during inta cycles.
|
| 20 |
|
|
--
|
| 21 |
|
|
-- We will simulate 8 possible irq sources. The software can trigger any one of
|
| 22 |
40 |
ja_rd |
-- them by writing at ports 0x010 to 0x011. Port 0x010 holds the irq source to
|
| 23 |
|
|
-- be triggered (0 to 7) and port 0x011 holds the number of clock cycles that
|
| 24 |
|
|
-- will elapse from the end of the instruction that writes to the register to
|
| 25 |
|
|
-- the assertion of intr. Port 0x012 holds the number of cycles intr will remain
|
| 26 |
|
|
-- high. Intr will be asserted for 1 cycle at least, so writing a 0 here is the
|
| 27 |
|
|
-- same as writing 1.
|
| 28 |
20 |
ja_rd |
--
|
| 29 |
|
|
-- When the interrupt is acknowledged and inta is asserted, the test bench reads
|
| 30 |
|
|
-- the value at register 0x010 as the irq source, and feeds an instruction to
|
| 31 |
|
|
-- the CPU starting from the RAM address 0040h+source*4.
|
| 32 |
|
|
-- That is, address range 0040h-005fh is reserved for the simulated 'interrupt
|
| 33 |
|
|
-- vectors', a total of 4 bytes for each of the 8 sources. This allows the
|
| 34 |
|
|
-- software to easily test different interrupt vectors without any hand
|
| 35 |
|
|
-- assembly. All of this is strictly simulation-only stuff.
|
| 36 |
|
|
--
|
| 37 |
40 |
ja_rd |
-- Upon completion, the software must write a value to register 0x020. Writing
|
| 38 |
|
|
-- a 0x055 means 'success', writing a 0x0aa means 'failure'. The write operation
|
| 39 |
|
|
-- will stop the simulation. Success and failure conditions are defined by the
|
| 40 |
|
|
-- software.
|
| 41 |
20 |
ja_rd |
--
|
| 42 |
40 |
ja_rd |
-- If a time period defined as constant MAX_SIM_LENGTH passes before anything
|
| 43 |
|
|
-- is written to io address 0x020, the test bench assumes the software ran away
|
| 44 |
|
|
-- and quits with an error message.
|
| 45 |
20 |
ja_rd |
--------------------------------------------------------------------------------
|
| 46 |
|
|
|
| 47 |
|
|
library ieee;
|
| 48 |
|
|
use ieee.std_logic_1164.ALL;
|
| 49 |
|
|
use ieee.std_logic_unsigned.all;
|
| 50 |
|
|
use ieee.numeric_std.ALL;
|
| 51 |
|
|
|
| 52 |
|
|
entity light8080_@PROGNAME@ is
|
| 53 |
|
|
end entity light8080_@PROGNAME@;
|
| 54 |
|
|
|
| 55 |
|
|
architecture behavior of light8080_@PROGNAME@ is
|
| 56 |
|
|
|
| 57 |
|
|
--------------------------------------------------------------------------------
|
| 58 |
|
|
-- Simulation parameters
|
| 59 |
|
|
|
| 60 |
|
|
-- T: simulated clock period
|
| 61 |
|
|
constant T : time := 100 ns;
|
| 62 |
|
|
|
| 63 |
|
|
-- MAX_SIM_LENGTH: maximum simulation time
|
| 64 |
|
|
constant MAX_SIM_LENGTH : time := T*7000; -- enough for the tb0
|
| 65 |
|
|
|
| 66 |
|
|
|
| 67 |
|
|
--------------------------------------------------------------------------------
|
| 68 |
|
|
|
| 69 |
40 |
ja_rd |
-- Component Declaration for the Unit Under Test (UUT)
|
| 70 |
20 |
ja_rd |
component light8080
|
| 71 |
|
|
port (
|
| 72 |
|
|
addr_out : out std_logic_vector(15 downto 0);
|
| 73 |
|
|
|
| 74 |
|
|
inta : out std_logic;
|
| 75 |
|
|
inte : out std_logic;
|
| 76 |
|
|
halt : out std_logic;
|
| 77 |
|
|
intr : in std_logic;
|
| 78 |
|
|
|
| 79 |
|
|
vma : out std_logic;
|
| 80 |
|
|
io : out std_logic;
|
| 81 |
|
|
rd : out std_logic;
|
| 82 |
|
|
wr : out std_logic;
|
| 83 |
|
|
fetch : out std_logic;
|
| 84 |
|
|
data_in : in std_logic_vector(7 downto 0);
|
| 85 |
|
|
data_out : out std_logic_vector(7 downto 0);
|
| 86 |
|
|
|
| 87 |
|
|
clk : in std_logic;
|
| 88 |
|
|
reset : in std_logic );
|
| 89 |
|
|
end component;
|
| 90 |
|
|
|
| 91 |
|
|
|
| 92 |
|
|
signal data_i : std_logic_vector(7 downto 0) := (others=>'0');
|
| 93 |
|
|
signal vma_o : std_logic;
|
| 94 |
|
|
signal rd_o : std_logic;
|
| 95 |
|
|
signal wr_o : std_logic;
|
| 96 |
|
|
signal io_o : std_logic;
|
| 97 |
|
|
signal data_o : std_logic_vector(7 downto 0);
|
| 98 |
|
|
signal data_mem : std_logic_vector(7 downto 0);
|
| 99 |
|
|
signal addr_o : std_logic_vector(15 downto 0);
|
| 100 |
|
|
signal fetch_o : std_logic;
|
| 101 |
|
|
signal inta_o : std_logic;
|
| 102 |
|
|
signal inte_o : std_logic;
|
| 103 |
|
|
signal intr_i : std_logic := '0';
|
| 104 |
|
|
signal halt_o : std_logic;
|
| 105 |
|
|
|
| 106 |
|
|
signal reset : std_logic := '0';
|
| 107 |
|
|
signal clk : std_logic := '1';
|
| 108 |
|
|
signal done : std_logic := '0';
|
| 109 |
|
|
|
| 110 |
|
|
type t_rom is array(0 to 2047) of std_logic_vector(7 downto 0);
|
| 111 |
|
|
|
| 112 |
|
|
signal rom : t_rom := (
|
| 113 |
|
|
|
| 114 |
|
|
--@rom_data
|
| 115 |
|
|
|
| 116 |
|
|
);
|
| 117 |
|
|
|
| 118 |
|
|
signal irq_vector_byte: std_logic_vector(7 downto 0);
|
| 119 |
|
|
signal irq_source : integer range 0 to 7;
|
| 120 |
|
|
signal cycles_to_intr : integer range -10 to 255;
|
| 121 |
40 |
ja_rd |
signal intr_width : integer range 0 to 255;
|
| 122 |
20 |
ja_rd |
signal int_vector_index : integer range 0 to 3;
|
| 123 |
|
|
signal addr_vector_table: integer range 0 to 65535;
|
| 124 |
|
|
|
| 125 |
|
|
begin
|
| 126 |
|
|
|
| 127 |
59 |
ja_rd |
-- Instantiate the Unit Under Test (UUT)
|
| 128 |
|
|
uut: light8080 PORT MAP(
|
| 129 |
|
|
clk => clk,
|
| 130 |
|
|
reset => reset,
|
| 131 |
|
|
vma => vma_o,
|
| 132 |
|
|
rd => rd_o,
|
| 133 |
|
|
wr => wr_o,
|
| 134 |
|
|
io => io_o,
|
| 135 |
|
|
fetch => fetch_o,
|
| 136 |
|
|
addr_out => addr_o,
|
| 137 |
|
|
data_in => data_i,
|
| 138 |
|
|
data_out => data_o,
|
| 139 |
|
|
|
| 140 |
|
|
intr => intr_i,
|
| 141 |
|
|
inte => inte_o,
|
| 142 |
|
|
inta => inta_o,
|
| 143 |
|
|
halt => halt_o
|
| 144 |
|
|
);
|
| 145 |
20 |
ja_rd |
|
| 146 |
|
|
|
| 147 |
|
|
-- clock: run clock until test is done
|
| 148 |
|
|
clock:
|
| 149 |
|
|
process(done, clk)
|
| 150 |
|
|
begin
|
| 151 |
59 |
ja_rd |
if done = '0' then
|
| 152 |
|
|
clk <= not clk after T/2;
|
| 153 |
|
|
end if;
|
| 154 |
20 |
ja_rd |
end process clock;
|
| 155 |
|
|
|
| 156 |
|
|
|
| 157 |
|
|
-- Drive reset and done
|
| 158 |
|
|
main_test:
|
| 159 |
|
|
process
|
| 160 |
|
|
begin
|
| 161 |
59 |
ja_rd |
-- Assert reset for at least one full clk period
|
| 162 |
|
|
reset <= '1';
|
| 163 |
|
|
wait until clk = '1';
|
| 164 |
|
|
wait for T/2;
|
| 165 |
|
|
reset <= '0';
|
| 166 |
20 |
ja_rd |
|
| 167 |
59 |
ja_rd |
-- Remember to 'cut away' the preceding 3 clk semiperiods from
|
| 168 |
|
|
-- the wait statement...
|
| 169 |
|
|
wait for (MAX_SIM_LENGTH - T*1.5);
|
| 170 |
20 |
ja_rd |
|
| 171 |
59 |
ja_rd |
-- Maximum sim time elapsed, assume the program ran away and
|
| 172 |
|
|
-- stop the clk process asserting 'done' (which will stop the simulation)
|
| 173 |
|
|
done <= '1';
|
| 174 |
|
|
|
| 175 |
20 |
ja_rd |
assert (done = '1')
|
| 176 |
59 |
ja_rd |
report "Test timed out."
|
| 177 |
|
|
severity failure;
|
| 178 |
|
|
|
| 179 |
|
|
wait;
|
| 180 |
20 |
ja_rd |
end process main_test;
|
| 181 |
|
|
|
| 182 |
|
|
|
| 183 |
|
|
-- Synchronous RAM; 2KB mirrored everywhere
|
| 184 |
|
|
synchronous_ram:
|
| 185 |
|
|
process(clk)
|
| 186 |
|
|
begin
|
| 187 |
|
|
if (clk'event and clk='1') then
|
| 188 |
|
|
data_mem <= rom(conv_integer(addr_o(10 downto 0)));
|
| 189 |
|
|
if wr_o = '1' and addr_o(15 downto 11)="00000" then
|
| 190 |
|
|
rom(conv_integer(addr_o(10 downto 0))) <= data_o;
|
| 191 |
|
|
end if;
|
| 192 |
|
|
end if;
|
| 193 |
|
|
end process synchronous_ram;
|
| 194 |
|
|
|
| 195 |
|
|
|
| 196 |
|
|
irq_trigger_register:
|
| 197 |
|
|
process(clk)
|
| 198 |
|
|
begin
|
| 199 |
|
|
if (clk'event and clk='1') then
|
| 200 |
|
|
if reset='1' then
|
| 201 |
|
|
cycles_to_intr <= -10; -- meaning no interrupt pending
|
| 202 |
|
|
else
|
| 203 |
|
|
if io_o='1' and wr_o='1' and addr_o(7 downto 0)=X"11" then
|
| 204 |
|
|
cycles_to_intr <= conv_integer(data_o) + 1;
|
| 205 |
|
|
else
|
| 206 |
|
|
if cycles_to_intr >= 0 then
|
| 207 |
|
|
cycles_to_intr <= cycles_to_intr - 1;
|
| 208 |
|
|
end if;
|
| 209 |
|
|
end if;
|
| 210 |
|
|
end if;
|
| 211 |
|
|
end if;
|
| 212 |
|
|
end process irq_trigger_register;
|
| 213 |
|
|
|
| 214 |
40 |
ja_rd |
irq_pulse_width_register:
|
| 215 |
|
|
process(clk)
|
| 216 |
|
|
variable intr_pulse_countdown : integer;
|
| 217 |
|
|
begin
|
| 218 |
|
|
if (clk'event and clk='1') then
|
| 219 |
|
|
if reset='1' then
|
| 220 |
|
|
intr_width <= 1;
|
| 221 |
|
|
intr_pulse_countdown := 0;
|
| 222 |
|
|
intr_i <= '0';
|
| 223 |
|
|
else
|
| 224 |
|
|
if io_o='1' and wr_o='1' and addr_o(7 downto 0)=X"12" then
|
| 225 |
|
|
intr_width <= conv_integer(data_o) + 1;
|
| 226 |
|
|
end if;
|
| 227 |
20 |
ja_rd |
|
| 228 |
40 |
ja_rd |
if cycles_to_intr = 0 then
|
| 229 |
|
|
intr_i <= '1';
|
| 230 |
|
|
intr_pulse_countdown := intr_width;
|
| 231 |
|
|
elsif intr_pulse_countdown <= 1 then
|
| 232 |
|
|
intr_i <= '0';
|
| 233 |
|
|
else
|
| 234 |
|
|
intr_pulse_countdown := intr_pulse_countdown - 1;
|
| 235 |
|
|
end if;
|
| 236 |
|
|
end if;
|
| 237 |
|
|
end if;
|
| 238 |
|
|
end process irq_pulse_width_register;
|
| 239 |
|
|
|
| 240 |
20 |
ja_rd |
irq_source_register:
|
| 241 |
|
|
process(clk)
|
| 242 |
|
|
begin
|
| 243 |
|
|
if (clk'event and clk='1') then
|
| 244 |
|
|
if reset='1' then
|
| 245 |
|
|
irq_source <= 0;
|
| 246 |
|
|
else
|
| 247 |
|
|
if io_o='1' and wr_o='1' and addr_o(7 downto 0)=X"10" then
|
| 248 |
|
|
irq_source <= conv_integer(data_o(2 downto 0));
|
| 249 |
|
|
end if;
|
| 250 |
|
|
end if;
|
| 251 |
|
|
end if;
|
| 252 |
|
|
end process irq_source_register;
|
| 253 |
|
|
|
| 254 |
|
|
|
| 255 |
|
|
-- 'interrupt vector' logic.
|
| 256 |
|
|
irq_vector_table:
|
| 257 |
|
|
process(clk)
|
| 258 |
|
|
begin
|
| 259 |
|
|
if (clk'event and clk='1') then
|
| 260 |
|
|
if vma_o = '1' and rd_o='1' then
|
| 261 |
|
|
if inta_o = '1' then
|
| 262 |
|
|
int_vector_index <= int_vector_index + 1;
|
| 263 |
|
|
else
|
| 264 |
|
|
int_vector_index <= 0;
|
| 265 |
|
|
end if;
|
| 266 |
|
|
end if;
|
| 267 |
|
|
-- this is the address of the byte we'll feed to the CPU
|
| 268 |
|
|
addr_vector_table <= 64+irq_source*4+int_vector_index;
|
| 269 |
|
|
end if;
|
| 270 |
|
|
end process irq_vector_table;
|
| 271 |
|
|
irq_vector_byte <= rom(addr_vector_table);
|
| 272 |
|
|
|
| 273 |
|
|
data_i <= data_mem when inta_o='0' else irq_vector_byte;
|
| 274 |
|
|
|
| 275 |
|
|
|
| 276 |
|
|
test_outcome_register:
|
| 277 |
|
|
process(clk)
|
| 278 |
|
|
variable outcome : std_logic_vector(7 downto 0);
|
| 279 |
|
|
begin
|
| 280 |
|
|
if (clk'event and clk='1') then
|
| 281 |
|
|
if io_o='1' and wr_o='1' and addr_o(7 downto 0)=X"20" then
|
| 282 |
|
|
assert (data_o /= X"55") report "Software reports SUCCESS" severity failure;
|
| 283 |
|
|
assert (data_o /= X"aa") report "Software reports FAILURE" severity failure;
|
| 284 |
|
|
assert ((data_o = X"aa") or (data_o = X"55"))
|
| 285 |
|
|
report "Software reports unexpected outcome value."
|
| 286 |
|
|
severity failure;
|
| 287 |
|
|
end if;
|
| 288 |
|
|
end if;
|
| 289 |
|
|
end process test_outcome_register;
|
| 290 |
|
|
|
| 291 |
|
|
|
| 292 |
|
|
end;
|