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 |
|
|
-- Console logging:
|
9 |
|
|
--
|
10 |
|
|
-- Console output (at addresses compatible to Plasma's) is logged to text file
|
11 |
|
|
-- "hw_sim_console_log.txt".
|
12 |
|
|
--
|
13 |
|
|
-- IMPORTANT: The code that echoes UART TX data to the simulation console does
|
14 |
|
|
-- line buffering; it will not print anything until it gets a CR (0x0d), and
|
15 |
|
|
-- will ifnore LFs (0x0a). Bear this in mind if you see no output when you
|
16 |
|
|
-- expect it.
|
17 |
|
|
--
|
18 |
|
|
-- Console logging is done by monitoring CPU writes to the UART, NOT by looking
|
19 |
|
|
-- at the TxD pin. It will NOT catch baud-related problems, etc.
|
20 |
|
|
--------------------------------------------------------------------------------
|
21 |
|
|
-- WARNING: Will only work on Modelsim; uses custom library SignalSpy.
|
22 |
|
|
--##############################################################################
|
23 |
|
|
|
24 |
|
|
library ieee;
|
25 |
|
|
use ieee.std_logic_1164.all;
|
26 |
|
|
use ieee.std_logic_arith.all;
|
27 |
|
|
use ieee.std_logic_unsigned.all;
|
28 |
|
|
use std.textio.all;
|
29 |
|
|
|
30 |
|
|
use work.mips_pkg.all;
|
31 |
|
|
use work.mips_tb_pkg.all;
|
32 |
|
|
use work.sim_params_pkg.all;
|
33 |
|
|
use work.txt_util.all;
|
34 |
|
|
|
35 |
|
|
entity mips_tb is
|
36 |
|
|
end;
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
architecture testbench of mips_tb is
|
40 |
|
|
|
41 |
|
|
-- NOTE: simulation parameters are defined in sim_params_pkg
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
-- External SRAM and interface signals -----------------------------------------
|
45 |
|
|
|
46 |
|
|
signal sram1 : t_sram := ( others => X"00");
|
47 |
|
|
signal sram0 : t_sram := ( others => X"00");
|
48 |
|
|
|
49 |
|
|
signal sram_chip_addr : std_logic_vector(SRAM_ADDR_SIZE downto 1);
|
50 |
|
|
signal sram_output : std_logic_vector(15 downto 0);
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
-- PROM table and interface signals --------------------------------------------
|
54 |
|
|
|
55 |
|
|
-- We'll simulate a 16-bit-wide static PROM (e.g. a Flash) with some serious
|
56 |
|
|
-- cycle time (70 or 90 ns).
|
57 |
|
|
|
58 |
|
|
signal prom_rd_addr : t_prom_address;
|
59 |
|
|
signal prom_output : std_logic_vector(7 downto 0);
|
60 |
|
|
signal prom_oe_n : std_logic;
|
61 |
|
|
|
62 |
|
|
signal prom : t_prom := ( PROM_DATA );
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
-- I/O devices -----------------------------------------------------------------
|
67 |
|
|
|
68 |
|
|
signal data_uart : std_logic_vector(31 downto 0);
|
69 |
|
|
signal data_uart_status : std_logic_vector(31 downto 0);
|
70 |
|
|
signal uart_tx_rdy : std_logic := '1';
|
71 |
|
|
signal uart_rx_rdy : std_logic := '1';
|
72 |
|
|
|
73 |
|
|
--------------------------------------------------------------------------------
|
74 |
|
|
|
75 |
|
|
signal clk : std_logic := '0';
|
76 |
|
|
signal reset : std_logic := '1';
|
77 |
|
|
signal interrupt : std_logic := '0';
|
78 |
|
|
signal done : std_logic := '0';
|
79 |
|
|
|
80 |
|
|
-- interface to asynchronous 16-bit-wide external SRAM
|
81 |
|
|
signal mpu_sram_address : t_word;
|
82 |
|
|
signal mpu_sram_data_rd : std_logic_vector(15 downto 0);
|
83 |
|
|
signal mpu_sram_data_wr : std_logic_vector(15 downto 0);
|
84 |
|
|
signal mpu_sram_byte_we_n : std_logic_vector(1 downto 0);
|
85 |
|
|
signal mpu_sram_oe_n : std_logic;
|
86 |
|
|
|
87 |
|
|
-- interface to i/o
|
88 |
|
|
signal io_rd_data : std_logic_vector(31 downto 0);
|
89 |
|
|
signal io_wr_data : std_logic_vector(31 downto 0);
|
90 |
|
|
signal io_rd_addr : std_logic_vector(31 downto 2);
|
91 |
|
|
signal io_wr_addr : std_logic_vector(31 downto 2);
|
92 |
|
|
signal io_rd_vma : std_logic;
|
93 |
|
|
signal io_byte_we : std_logic_vector(3 downto 0);
|
94 |
|
|
|
95 |
|
|
signal rxd : std_logic;
|
96 |
|
|
signal txd : std_logic;
|
97 |
|
|
|
98 |
200 |
ja_rd |
-- Other CPU signals
|
99 |
|
|
signal cpu_irq : std_logic_vector(7 downto 0);
|
100 |
193 |
ja_rd |
|
101 |
|
|
--------------------------------------------------------------------------------
|
102 |
|
|
-- Logging signals
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
-- Log file
|
106 |
|
|
file log_file: TEXT open write_mode is "hw_sim_log.txt";
|
107 |
|
|
|
108 |
|
|
-- Console output log file
|
109 |
|
|
file con_file: TEXT open write_mode is "hw_sim_console_log.txt";
|
110 |
|
|
|
111 |
|
|
-- All the info needed by the logger is here
|
112 |
|
|
signal log_info : t_log_info;
|
113 |
|
|
|
114 |
200 |
ja_rd |
-- IRQ trigger simulation ------------------------------------------------------
|
115 |
193 |
ja_rd |
|
116 |
200 |
ja_rd |
signal irq_trigger_addr : std_logic_vector(2 downto 0);
|
117 |
|
|
signal irq_trigger_data : std_logic_vector(31 downto 0);
|
118 |
|
|
signal irq_trigger_load : std_logic;
|
119 |
193 |
ja_rd |
|
120 |
200 |
ja_rd |
subtype t_irq_countdown is std_logic_vector(31 downto 0);
|
121 |
|
|
type t_irq_countdown_array is array(0 to 7) of t_irq_countdown;
|
122 |
193 |
ja_rd |
|
123 |
200 |
ja_rd |
signal irq_countdown : t_irq_countdown_array;
|
124 |
|
|
|
125 |
|
|
|
126 |
193 |
ja_rd |
begin
|
127 |
|
|
|
128 |
|
|
-- UUT instantiation -------------------------------------------------------
|
129 |
|
|
mpu: entity work.mips_mpu
|
130 |
|
|
generic map (
|
131 |
|
|
CLOCK_FREQ => 50000000,
|
132 |
|
|
SRAM_ADDR_SIZE => 32
|
133 |
|
|
)
|
134 |
|
|
port map (
|
135 |
200 |
ja_rd |
interrupt => cpu_irq(0),
|
136 |
193 |
ja_rd |
|
137 |
|
|
-- interface to FPGA i/o devices
|
138 |
|
|
io_rd_data => io_rd_data,
|
139 |
|
|
io_rd_addr => io_rd_addr,
|
140 |
|
|
io_wr_addr => io_wr_addr,
|
141 |
|
|
io_wr_data => io_wr_data,
|
142 |
|
|
io_rd_vma => io_rd_vma,
|
143 |
|
|
io_byte_we => io_byte_we,
|
144 |
|
|
|
145 |
|
|
-- interface to asynchronous 16-bit-wide EXTERNAL SRAM
|
146 |
|
|
sram_address => mpu_sram_address,
|
147 |
|
|
sram_data_rd => mpu_sram_data_rd,
|
148 |
|
|
sram_data_wr => mpu_sram_data_wr,
|
149 |
|
|
sram_byte_we_n => mpu_sram_byte_we_n,
|
150 |
|
|
sram_oe_n => mpu_sram_oe_n,
|
151 |
|
|
|
152 |
|
|
uart_rxd => rxd,
|
153 |
|
|
uart_txd => txd,
|
154 |
|
|
|
155 |
|
|
debug_info => OPEN,
|
156 |
|
|
|
157 |
|
|
clk => clk,
|
158 |
|
|
reset => reset
|
159 |
|
|
);
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
-- Master clock: free running clock used as main module clock --------------
|
163 |
|
|
run_master_clock:
|
164 |
|
|
process(done, clk)
|
165 |
|
|
begin
|
166 |
|
|
if done = '0' then
|
167 |
|
|
clk <= not clk after T/2;
|
168 |
|
|
end if;
|
169 |
|
|
end process run_master_clock;
|
170 |
|
|
|
171 |
|
|
-- Main simulation process: reset MCU and wait for fixed period ------------
|
172 |
|
|
drive_uut:
|
173 |
|
|
process
|
174 |
|
|
variable l : line;
|
175 |
|
|
begin
|
176 |
|
|
wait for T*4;
|
177 |
|
|
reset <= '0';
|
178 |
|
|
|
179 |
|
|
wait for T*SIMULATION_LENGTH;
|
180 |
|
|
|
181 |
|
|
-- Flush console output to log console file (in case the end of the
|
182 |
|
|
-- simulation caugh an unterminated line in the buffer)
|
183 |
|
|
if log_info.con_line_ix > 1 then
|
184 |
|
|
write(l, log_info.con_line_buf(1 to log_info.con_line_ix));
|
185 |
|
|
writeline(con_file, l);
|
186 |
|
|
end if;
|
187 |
|
|
|
188 |
|
|
print("TB finished");
|
189 |
|
|
done <= '1';
|
190 |
|
|
wait;
|
191 |
|
|
|
192 |
|
|
end process drive_uut;
|
193 |
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
-- SRAM/FLASH mux (on a real board this would be a simple address decoder)
|
197 |
|
|
mpu_sram_data_rd <=
|
198 |
|
|
X"00" & prom_output when mpu_sram_address(31 downto 27)="10110" else
|
199 |
|
|
sram_output;
|
200 |
|
|
|
201 |
|
|
|
202 |
|
|
-- Do a very basic simulation of an external SRAM --------------------------
|
203 |
|
|
|
204 |
|
|
sram_chip_addr <= mpu_sram_address(SRAM_ADDR_SIZE downto 1);
|
205 |
|
|
|
206 |
|
|
-- FIXME should add some verification of /WE
|
207 |
|
|
sram_output <=
|
208 |
|
|
sram1(conv_integer(unsigned(sram_chip_addr))) &
|
209 |
|
|
sram0(conv_integer(unsigned(sram_chip_addr))) when mpu_sram_oe_n='0'
|
210 |
|
|
else (others => 'Z');
|
211 |
|
|
|
212 |
|
|
simulated_sram_write:
|
213 |
|
|
process(mpu_sram_byte_we_n, mpu_sram_address, mpu_sram_oe_n)
|
214 |
|
|
begin
|
215 |
|
|
-- Write cycle
|
216 |
|
|
-- FIXME should add OE\ to write control logic
|
217 |
|
|
if mpu_sram_byte_we_n'event or mpu_sram_address'event then
|
218 |
|
|
if mpu_sram_byte_we_n(1)='0' then
|
219 |
|
|
sram1(conv_integer(unsigned(sram_chip_addr))) <= mpu_sram_data_wr(15 downto 8);
|
220 |
|
|
end if;
|
221 |
|
|
if mpu_sram_byte_we_n(0)='0' then
|
222 |
|
|
sram0(conv_integer(unsigned(sram_chip_addr))) <= mpu_sram_data_wr( 7 downto 0);
|
223 |
|
|
end if;
|
224 |
|
|
end if;
|
225 |
|
|
end process simulated_sram_write;
|
226 |
|
|
|
227 |
|
|
|
228 |
|
|
-- Do a very basic simulation of an external PROM (FLASH) ------------------
|
229 |
|
|
-- (wired to the same bus as the sram and both are static).
|
230 |
|
|
|
231 |
|
|
prom_rd_addr <= mpu_sram_address(PROM_ADDR_SIZE+1 downto 2);
|
232 |
|
|
|
233 |
|
|
prom_oe_n <= mpu_sram_oe_n;
|
234 |
|
|
|
235 |
|
|
prom_output <=
|
236 |
|
|
prom(conv_integer(unsigned(prom_rd_addr)))(31 downto 24) when prom_oe_n='0' and mpu_sram_address(1 downto 0)="00" else
|
237 |
|
|
prom(conv_integer(unsigned(prom_rd_addr)))(23 downto 16) when prom_oe_n='0' and mpu_sram_address(1 downto 0)="01" else
|
238 |
|
|
prom(conv_integer(unsigned(prom_rd_addr)))(15 downto 8) when prom_oe_n='0' and mpu_sram_address(1 downto 0)="10" else
|
239 |
|
|
prom(conv_integer(unsigned(prom_rd_addr)))( 7 downto 0) when prom_oe_n='0' and mpu_sram_address(1 downto 0)="11" else
|
240 |
|
|
(others => 'Z');
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
-- Simulate dummy I/O traffic external to the MCU --------------------------
|
244 |
200 |
ja_rd |
-- the only IO present is the test interrupt trigger registers
|
245 |
|
|
simulated_io:
|
246 |
|
|
process(clk)
|
247 |
|
|
variable i : integer;
|
248 |
|
|
variable uart_data : integer;
|
249 |
|
|
begin
|
250 |
|
|
if clk'event and clk='1' then
|
251 |
|
|
if io_byte_we /= "0000" then
|
252 |
|
|
if io_wr_addr(31 downto 16)=X"2001" then
|
253 |
|
|
irq_trigger_load <= '1';
|
254 |
|
|
irq_trigger_data <= io_wr_data;
|
255 |
|
|
irq_trigger_addr <= io_wr_addr(4 downto 2);
|
256 |
|
|
else
|
257 |
|
|
irq_trigger_load <= '0';
|
258 |
|
|
end if;
|
259 |
|
|
else
|
260 |
|
|
irq_trigger_load <= '0';
|
261 |
|
|
end if;
|
262 |
|
|
end if;
|
263 |
|
|
end process simulated_io;
|
264 |
|
|
|
265 |
|
|
-- Simulate IRQs -----------------------------------------------------------
|
266 |
|
|
irq_trigger_registers:
|
267 |
|
|
process(clk)
|
268 |
|
|
variable index : integer range 0 to 7;
|
269 |
|
|
begin
|
270 |
|
|
if clk'event and clk='1' then
|
271 |
|
|
if reset='1' then
|
272 |
|
|
cpu_irq <= "00000000";
|
273 |
|
|
else
|
274 |
|
|
if irq_trigger_load='1' then
|
275 |
|
|
index := conv_integer(irq_trigger_addr);
|
276 |
|
|
irq_countdown(index) <= irq_trigger_data;
|
277 |
|
|
else
|
278 |
|
|
for index in 0 to 7 loop
|
279 |
|
|
if irq_countdown(index) = X"00000001" then
|
280 |
|
|
cpu_irq(index) <= '1';
|
281 |
|
|
irq_countdown(index) <= irq_countdown(index) - 1;
|
282 |
|
|
elsif irq_countdown(index)/=X"00000000" then
|
283 |
|
|
irq_countdown(index) <= irq_countdown(index) - 1;
|
284 |
|
|
cpu_irq(index) <= '0';
|
285 |
|
|
else
|
286 |
|
|
cpu_irq(index) <= '0';
|
287 |
|
|
end if;
|
288 |
|
|
end loop;
|
289 |
|
|
end if;
|
290 |
|
|
end if;
|
291 |
|
|
end if;
|
292 |
|
|
end process irq_trigger_registers;
|
293 |
193 |
ja_rd |
|
294 |
200 |
ja_rd |
|
295 |
193 |
ja_rd |
-- This is useless (the simulated UART will not be actually used)
|
296 |
|
|
-- but at least prevents the simulator from optimizing the logic away.
|
297 |
|
|
rxd <= txd;
|
298 |
|
|
|
299 |
|
|
|
300 |
|
|
-- Logging process: launch logger function ---------------------------------
|
301 |
|
|
log_execution:
|
302 |
|
|
process
|
303 |
|
|
begin
|
304 |
|
|
log_cpu_activity(clk, reset, done,
|
305 |
|
|
"mips_tb/mpu", "cpu",
|
306 |
|
|
log_info, "log_info",
|
307 |
|
|
LOG_TRIGGER_ADDRESS, log_file, con_file);
|
308 |
|
|
wait;
|
309 |
|
|
end process log_execution;
|
310 |
|
|
|
311 |
|
|
end architecture testbench;
|