1 |
193 |
ja_rd |
--##############################################################################
|
2 |
|
|
-- Simulation test bench -- not synthesizable.
|
3 |
|
|
--
|
4 |
|
|
-- Simulates the MCU core connected to a simulated external static RAM on a
|
5 |
|
|
-- 16-bit bus, plus an optional 8-bit static ROM. This setup is more or less
|
6 |
|
|
-- that of develoment board DE-1 from Terasic.
|
7 |
|
|
--------------------------------------------------------------------------------
|
8 |
226 |
ja_rd |
-- Simulated I/O
|
9 |
|
|
-- Apart from the io devices within the SoC module, this test bench simulates
|
10 |
|
|
-- the following ports:
|
11 |
|
|
--
|
12 |
|
|
-- 20010000: HW IRQ 0 countdown register (R/o).
|
13 |
|
|
-- 20010004: HW IRQ 1 countdown register (R/o).
|
14 |
|
|
-- 20010008: HW IRQ 2 countdown register (R/o).
|
15 |
|
|
-- 2001000c: HW IRQ 3 countdown register (R/o).
|
16 |
|
|
-- 20010010: HW IRQ 4 countdown register (R/o).
|
17 |
|
|
-- 20010014: HW IRQ 5 countdown register (R/o).
|
18 |
|
|
-- 20010018: HW IRQ 6 countdown register (R/o).
|
19 |
|
|
-- 2001001c: HW IRQ 7 countdown register (R/o).
|
20 |
|
|
-- 20010020: Debug register 0 (R/W).
|
21 |
|
|
-- 20010024: Debug register 1 (R/W).
|
22 |
|
|
-- 20010028: Debug register 2 (R/W).
|
23 |
|
|
-- 2001002c: Debug register 3 (R/W).
|
24 |
|
|
--
|
25 |
|
|
-- NOTE: these addresses are for write accesses only. for read accesses, the
|
26 |
|
|
-- debug registers 0..3 are mirrored over all the io address range 2001xxxxh.
|
27 |
|
|
--
|
28 |
|
|
-- Writing N to an IRQ X countdown register will trigger hardware interrupt X
|
29 |
|
|
-- N clock cycles later. The interrupt line will be asserted for 1 clock cycle.
|
30 |
|
|
--
|
31 |
|
|
-- The debug registers 0 to 3 can only be used to test 32-bit i/o.
|
32 |
|
|
-- All of these registers can only be addressed as 32-bit words. Any other type
|
33 |
|
|
-- of access will yield undefined results.
|
34 |
|
|
--------------------------------------------------------------------------------
|
35 |
193 |
ja_rd |
-- Console logging:
|
36 |
|
|
--
|
37 |
|
|
-- Console output (at addresses compatible to Plasma's) is logged to text file
|
38 |
|
|
-- "hw_sim_console_log.txt".
|
39 |
|
|
--
|
40 |
|
|
-- IMPORTANT: The code that echoes UART TX data to the simulation console does
|
41 |
|
|
-- line buffering; it will not print anything until it gets a CR (0x0d), and
|
42 |
|
|
-- will ifnore LFs (0x0a). Bear this in mind if you see no output when you
|
43 |
|
|
-- expect it.
|
44 |
|
|
--
|
45 |
|
|
-- Console logging is done by monitoring CPU writes to the UART, NOT by looking
|
46 |
|
|
-- at the TxD pin. It will NOT catch baud-related problems, etc.
|
47 |
|
|
--------------------------------------------------------------------------------
|
48 |
|
|
-- WARNING: Will only work on Modelsim; uses custom library SignalSpy.
|
49 |
|
|
--##############################################################################
|
50 |
|
|
|
51 |
|
|
library ieee;
|
52 |
|
|
use ieee.std_logic_1164.all;
|
53 |
|
|
use ieee.std_logic_arith.all;
|
54 |
|
|
use ieee.std_logic_unsigned.all;
|
55 |
|
|
use std.textio.all;
|
56 |
|
|
|
57 |
226 |
ja_rd |
use work.txt_util.all;
|
58 |
193 |
ja_rd |
use work.mips_pkg.all;
|
59 |
|
|
use work.mips_tb_pkg.all;
|
60 |
|
|
use work.sim_params_pkg.all;
|
61 |
237 |
ja_rd |
--use work.obj_code_pkg.obj_code;
|
62 |
193 |
ja_rd |
|
63 |
226 |
ja_rd |
|
64 |
193 |
ja_rd |
entity mips_tb is
|
65 |
|
|
end;
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
architecture testbench of mips_tb is
|
69 |
|
|
|
70 |
226 |
ja_rd |
-- External 16-bit SRAM and interface signals ----------------------------------
|
71 |
193 |
ja_rd |
|
72 |
226 |
ja_rd |
-- External SRAM address length -- these are 16-bit word addresses.
|
73 |
|
|
constant SRAM_ADDR_SIZE : integer := log2(SRAM_SIZE);
|
74 |
193 |
ja_rd |
|
75 |
226 |
ja_rd |
-- Static 16-bit wide RAM.
|
76 |
207 |
ja_rd |
-- Using shared variables for big memory arrays speeds up simulation a lot;
|
77 |
|
|
-- see Modelsim 6.3 User Manual, section on 'Modelling Memory'.
|
78 |
|
|
-- WARNING: I have only tested this construct with Modelsim SE 6.3.
|
79 |
226 |
ja_rd |
shared variable sram : t_hword_table(0 to SRAM_SIZE-1) := objcode_to_htable(SRAM_INIT, SRAM_SIZE);
|
80 |
193 |
ja_rd |
|
81 |
226 |
ja_rd |
|
82 |
193 |
ja_rd |
signal sram_chip_addr : std_logic_vector(SRAM_ADDR_SIZE downto 1);
|
83 |
226 |
ja_rd |
signal sram_output : t_halfword;
|
84 |
193 |
ja_rd |
|
85 |
|
|
|
86 |
|
|
-- PROM table and interface signals --------------------------------------------
|
87 |
|
|
|
88 |
226 |
ja_rd |
constant PROM_ADDR_SIZE : integer := log2(PROM_SIZE);
|
89 |
|
|
subtype t_prom_address is std_logic_vector(PROM_ADDR_SIZE-1 downto 0);
|
90 |
|
|
|
91 |
193 |
ja_rd |
-- We'll simulate a 16-bit-wide static PROM (e.g. a Flash) with some serious
|
92 |
|
|
-- cycle time (70 or 90 ns).
|
93 |
207 |
ja_rd |
-- FIXME FLASH read cycle time not modelled yet.
|
94 |
193 |
ja_rd |
signal prom_rd_addr : t_prom_address;
|
95 |
226 |
ja_rd |
signal prom_output : t_byte;
|
96 |
193 |
ja_rd |
signal prom_oe_n : std_logic;
|
97 |
|
|
|
98 |
207 |
ja_rd |
-- 8-bit wide FLASH modelled as read only block.
|
99 |
|
|
-- We don't simulate the actual FLASH chip: no FLASH writes, control regs, etc.
|
100 |
226 |
ja_rd |
shared variable prom : t_byte_table(0 to PROM_SIZE-1) := objcode_to_btable(PROM_INIT, PROM_SIZE);
|
101 |
193 |
ja_rd |
|
102 |
|
|
|
103 |
|
|
-- I/O devices -----------------------------------------------------------------
|
104 |
|
|
|
105 |
|
|
signal data_uart : std_logic_vector(31 downto 0);
|
106 |
|
|
signal data_uart_status : std_logic_vector(31 downto 0);
|
107 |
|
|
signal uart_tx_rdy : std_logic := '1';
|
108 |
|
|
signal uart_rx_rdy : std_logic := '1';
|
109 |
|
|
|
110 |
|
|
--------------------------------------------------------------------------------
|
111 |
|
|
|
112 |
|
|
signal clk : std_logic := '0';
|
113 |
|
|
signal reset : std_logic := '1';
|
114 |
|
|
signal interrupt : std_logic := '0';
|
115 |
|
|
signal done : std_logic := '0';
|
116 |
|
|
|
117 |
|
|
-- interface to asynchronous 16-bit-wide external SRAM
|
118 |
226 |
ja_rd |
signal mpu_sram_address : std_logic_vector(31 downto 0);
|
119 |
|
|
signal mpu_sram_data_rd : t_halfword;
|
120 |
|
|
signal mpu_sram_data_wr : t_halfword;
|
121 |
193 |
ja_rd |
signal mpu_sram_byte_we_n : std_logic_vector(1 downto 0);
|
122 |
|
|
signal mpu_sram_oe_n : std_logic;
|
123 |
|
|
|
124 |
|
|
-- interface to i/o
|
125 |
|
|
signal io_rd_data : std_logic_vector(31 downto 0);
|
126 |
|
|
signal io_wr_data : std_logic_vector(31 downto 0);
|
127 |
|
|
signal io_rd_addr : std_logic_vector(31 downto 2);
|
128 |
|
|
signal io_wr_addr : std_logic_vector(31 downto 2);
|
129 |
|
|
signal io_rd_vma : std_logic;
|
130 |
|
|
signal io_byte_we : std_logic_vector(3 downto 0);
|
131 |
|
|
|
132 |
|
|
signal rxd : std_logic;
|
133 |
|
|
signal txd : std_logic;
|
134 |
|
|
|
135 |
200 |
ja_rd |
-- Other CPU signals
|
136 |
|
|
signal cpu_irq : std_logic_vector(7 downto 0);
|
137 |
193 |
ja_rd |
|
138 |
|
|
--------------------------------------------------------------------------------
|
139 |
|
|
-- Logging signals
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
-- Log file
|
143 |
|
|
file log_file: TEXT open write_mode is "hw_sim_log.txt";
|
144 |
|
|
|
145 |
|
|
-- Console output log file
|
146 |
|
|
file con_file: TEXT open write_mode is "hw_sim_console_log.txt";
|
147 |
|
|
|
148 |
|
|
-- All the info needed by the logger is here
|
149 |
|
|
signal log_info : t_log_info;
|
150 |
|
|
|
151 |
200 |
ja_rd |
-- IRQ trigger simulation ------------------------------------------------------
|
152 |
193 |
ja_rd |
|
153 |
200 |
ja_rd |
signal irq_trigger_addr : std_logic_vector(2 downto 0);
|
154 |
|
|
signal irq_trigger_data : std_logic_vector(31 downto 0);
|
155 |
|
|
signal irq_trigger_load : std_logic;
|
156 |
193 |
ja_rd |
|
157 |
200 |
ja_rd |
subtype t_irq_countdown is std_logic_vector(31 downto 0);
|
158 |
|
|
type t_irq_countdown_array is array(0 to 7) of t_irq_countdown;
|
159 |
193 |
ja_rd |
|
160 |
200 |
ja_rd |
signal irq_countdown : t_irq_countdown_array;
|
161 |
|
|
|
162 |
211 |
ja_rd |
-- Simulated block of 4 read/write, 32-bit I/O registers, used in cache test.
|
163 |
|
|
type t_debug_reg_block is array(0 to 3) of t_word;
|
164 |
|
|
signal debug_reg_block : t_debug_reg_block;
|
165 |
200 |
ja_rd |
|
166 |
211 |
ja_rd |
|
167 |
193 |
ja_rd |
begin
|
168 |
|
|
|
169 |
|
|
-- UUT instantiation -------------------------------------------------------
|
170 |
226 |
ja_rd |
mpu: entity work.mips_soc
|
171 |
193 |
ja_rd |
generic map (
|
172 |
226 |
ja_rd |
BOOT_BRAM_SIZE => bram_size,
|
173 |
237 |
ja_rd |
OBJECT_CODE => obj_code,
|
174 |
193 |
ja_rd |
CLOCK_FREQ => 50000000,
|
175 |
|
|
SRAM_ADDR_SIZE => 32
|
176 |
|
|
)
|
177 |
|
|
port map (
|
178 |
205 |
ja_rd |
interrupt => cpu_irq,
|
179 |
193 |
ja_rd |
|
180 |
|
|
-- interface to FPGA i/o devices
|
181 |
|
|
io_rd_data => io_rd_data,
|
182 |
|
|
io_rd_addr => io_rd_addr,
|
183 |
|
|
io_wr_addr => io_wr_addr,
|
184 |
|
|
io_wr_data => io_wr_data,
|
185 |
|
|
io_rd_vma => io_rd_vma,
|
186 |
|
|
io_byte_we => io_byte_we,
|
187 |
|
|
|
188 |
|
|
-- interface to asynchronous 16-bit-wide EXTERNAL SRAM
|
189 |
|
|
sram_address => mpu_sram_address,
|
190 |
|
|
sram_data_rd => mpu_sram_data_rd,
|
191 |
|
|
sram_data_wr => mpu_sram_data_wr,
|
192 |
|
|
sram_byte_we_n => mpu_sram_byte_we_n,
|
193 |
|
|
sram_oe_n => mpu_sram_oe_n,
|
194 |
|
|
|
195 |
|
|
uart_rxd => rxd,
|
196 |
|
|
uart_txd => txd,
|
197 |
|
|
|
198 |
237 |
ja_rd |
p0_out => OPEN,
|
199 |
|
|
p1_in => X"00000000",
|
200 |
|
|
|
201 |
193 |
ja_rd |
debug_info => OPEN,
|
202 |
|
|
|
203 |
|
|
clk => clk,
|
204 |
|
|
reset => reset
|
205 |
|
|
);
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
-- Master clock: free running clock used as main module clock --------------
|
209 |
|
|
run_master_clock:
|
210 |
|
|
process(done, clk)
|
211 |
|
|
begin
|
212 |
|
|
if done = '0' then
|
213 |
|
|
clk <= not clk after T/2;
|
214 |
|
|
end if;
|
215 |
|
|
end process run_master_clock;
|
216 |
|
|
|
217 |
|
|
-- Main simulation process: reset MCU and wait for fixed period ------------
|
218 |
|
|
drive_uut:
|
219 |
|
|
process
|
220 |
|
|
variable l : line;
|
221 |
|
|
begin
|
222 |
|
|
wait for T*4;
|
223 |
|
|
reset <= '0';
|
224 |
|
|
|
225 |
|
|
wait for T*SIMULATION_LENGTH;
|
226 |
|
|
|
227 |
|
|
-- Flush console output to log console file (in case the end of the
|
228 |
|
|
-- simulation caugh an unterminated line in the buffer)
|
229 |
|
|
if log_info.con_line_ix > 1 then
|
230 |
|
|
write(l, log_info.con_line_buf(1 to log_info.con_line_ix));
|
231 |
|
|
writeline(con_file, l);
|
232 |
|
|
end if;
|
233 |
|
|
|
234 |
|
|
print("TB finished");
|
235 |
|
|
done <= '1';
|
236 |
|
|
wait;
|
237 |
|
|
|
238 |
|
|
end process drive_uut;
|
239 |
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
|
|
-- SRAM/FLASH mux (on a real board this would be a simple address decoder)
|
243 |
|
|
mpu_sram_data_rd <=
|
244 |
|
|
X"00" & prom_output when mpu_sram_address(31 downto 27)="10110" else
|
245 |
|
|
sram_output;
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
-- Do a very basic simulation of an external SRAM --------------------------
|
249 |
|
|
|
250 |
|
|
sram_chip_addr <= mpu_sram_address(SRAM_ADDR_SIZE downto 1);
|
251 |
|
|
|
252 |
|
|
-- FIXME should add some verification of /WE
|
253 |
|
|
sram_output <=
|
254 |
226 |
ja_rd |
sram(conv_integer(unsigned(sram_chip_addr))) when mpu_sram_oe_n='0'
|
255 |
193 |
ja_rd |
else (others => 'Z');
|
256 |
|
|
|
257 |
|
|
simulated_sram_write:
|
258 |
|
|
process(mpu_sram_byte_we_n, mpu_sram_address, mpu_sram_oe_n)
|
259 |
|
|
begin
|
260 |
|
|
-- Write cycle
|
261 |
|
|
-- FIXME should add OE\ to write control logic
|
262 |
|
|
if mpu_sram_byte_we_n'event or mpu_sram_address'event then
|
263 |
|
|
if mpu_sram_byte_we_n(1)='0' then
|
264 |
226 |
ja_rd |
sram(conv_integer(unsigned(sram_chip_addr)))(15 downto 8) := mpu_sram_data_wr(15 downto 8);
|
265 |
193 |
ja_rd |
end if;
|
266 |
|
|
if mpu_sram_byte_we_n(0)='0' then
|
267 |
226 |
ja_rd |
sram(conv_integer(unsigned(sram_chip_addr)))( 7 downto 0) := mpu_sram_data_wr( 7 downto 0);
|
268 |
193 |
ja_rd |
end if;
|
269 |
|
|
end if;
|
270 |
|
|
end process simulated_sram_write;
|
271 |
|
|
|
272 |
|
|
|
273 |
|
|
-- Do a very basic simulation of an external PROM (FLASH) ------------------
|
274 |
|
|
-- (wired to the same bus as the sram and both are static).
|
275 |
|
|
|
276 |
226 |
ja_rd |
prom_rd_addr <= mpu_sram_address(PROM_ADDR_SIZE-1 downto 0);
|
277 |
193 |
ja_rd |
|
278 |
|
|
prom_oe_n <= mpu_sram_oe_n;
|
279 |
226 |
ja_rd |
|
280 |
|
|
simulated_flash:
|
281 |
|
|
if PROM_SIZE > 0 generate
|
282 |
|
|
prom_output <=
|
283 |
|
|
prom(conv_integer(unsigned(prom_rd_addr))) when prom_oe_n='0' else
|
284 |
|
|
(others => 'Z');
|
285 |
|
|
end generate;
|
286 |
|
|
|
287 |
|
|
unused_flash:
|
288 |
|
|
if PROM_SIZE <= 0 generate
|
289 |
|
|
prom_output <= (others => 'Z');
|
290 |
|
|
end generate;
|
291 |
193 |
ja_rd |
|
292 |
|
|
-- Simulate dummy I/O traffic external to the MCU --------------------------
|
293 |
226 |
ja_rd |
-- The only IO present is the test interrupt trigger registers and the
|
294 |
|
|
-- debug register block.
|
295 |
200 |
ja_rd |
simulated_io:
|
296 |
|
|
process(clk)
|
297 |
|
|
variable i : integer;
|
298 |
|
|
variable uart_data : integer;
|
299 |
|
|
begin
|
300 |
|
|
if clk'event and clk='1' then
|
301 |
|
|
if io_byte_we /= "0000" then
|
302 |
|
|
if io_wr_addr(31 downto 16)=X"2001" then
|
303 |
226 |
ja_rd |
if io_wr_addr(5)='0' then
|
304 |
|
|
-- IRQ trigger register block (write only)
|
305 |
|
|
irq_trigger_load <= '1';
|
306 |
|
|
irq_trigger_data <= io_wr_data;
|
307 |
|
|
irq_trigger_addr <= io_wr_addr(4 downto 2);
|
308 |
|
|
else
|
309 |
|
|
-- Debug register block (read/write)
|
310 |
|
|
debug_reg_block(conv_integer(unsigned(io_wr_addr(3 downto 2)))) <= io_wr_data;
|
311 |
|
|
end if;
|
312 |
200 |
ja_rd |
else
|
313 |
|
|
irq_trigger_load <= '0';
|
314 |
|
|
end if;
|
315 |
|
|
else
|
316 |
|
|
irq_trigger_load <= '0';
|
317 |
|
|
end if;
|
318 |
|
|
end if;
|
319 |
|
|
end process simulated_io;
|
320 |
|
|
|
321 |
211 |
ja_rd |
-- The only readable i/o is the debug reg block. We simulate an asynchronous
|
322 |
226 |
ja_rd |
-- read port (a mux).
|
323 |
|
|
-- For read accesses, this register block is mirrored all over the io
|
324 |
|
|
--- address space 2001xxxxh.
|
325 |
211 |
ja_rd |
io_rd_data <= debug_reg_block(conv_integer(unsigned(io_rd_addr(3 downto 2))));
|
326 |
|
|
|
327 |
200 |
ja_rd |
-- Simulate IRQs -----------------------------------------------------------
|
328 |
|
|
irq_trigger_registers:
|
329 |
|
|
process(clk)
|
330 |
|
|
variable index : integer range 0 to 7;
|
331 |
|
|
begin
|
332 |
|
|
if clk'event and clk='1' then
|
333 |
|
|
if reset='1' then
|
334 |
|
|
cpu_irq <= "00000000";
|
335 |
|
|
else
|
336 |
|
|
if irq_trigger_load='1' then
|
337 |
|
|
index := conv_integer(irq_trigger_addr);
|
338 |
|
|
irq_countdown(index) <= irq_trigger_data;
|
339 |
|
|
else
|
340 |
|
|
for index in 0 to 7 loop
|
341 |
|
|
if irq_countdown(index) = X"00000001" then
|
342 |
|
|
cpu_irq(index) <= '1';
|
343 |
|
|
irq_countdown(index) <= irq_countdown(index) - 1;
|
344 |
|
|
elsif irq_countdown(index)/=X"00000000" then
|
345 |
|
|
irq_countdown(index) <= irq_countdown(index) - 1;
|
346 |
|
|
cpu_irq(index) <= '0';
|
347 |
|
|
else
|
348 |
|
|
cpu_irq(index) <= '0';
|
349 |
|
|
end if;
|
350 |
|
|
end loop;
|
351 |
|
|
end if;
|
352 |
|
|
end if;
|
353 |
|
|
end if;
|
354 |
|
|
end process irq_trigger_registers;
|
355 |
193 |
ja_rd |
|
356 |
200 |
ja_rd |
|
357 |
193 |
ja_rd |
-- This is useless (the simulated UART will not be actually used)
|
358 |
|
|
-- but at least prevents the simulator from optimizing the logic away.
|
359 |
|
|
rxd <= txd;
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
-- Logging process: launch logger function ---------------------------------
|
363 |
|
|
log_execution:
|
364 |
|
|
process
|
365 |
|
|
begin
|
366 |
|
|
log_cpu_activity(clk, reset, done,
|
367 |
|
|
"mips_tb/mpu", "cpu",
|
368 |
|
|
log_info, "log_info",
|
369 |
|
|
LOG_TRIGGER_ADDRESS, log_file, con_file);
|
370 |
|
|
wait;
|
371 |
|
|
end process log_execution;
|
372 |
|
|
|
373 |
|
|
end architecture testbench;
|