OpenCores
URL https://opencores.org/ocsvn/ion/ion/trunk

Subversion Repositories ion

[/] [ion/] [trunk/] [vhdl/] [tb/] [mips_tb.vhdl] - Blame information for rev 226

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
 
62 226 ja_rd
 
63 193 ja_rd
entity mips_tb is
64
end;
65
 
66
 
67
architecture testbench of mips_tb is
68
 
69 226 ja_rd
-- External 16-bit SRAM and interface signals ----------------------------------
70 193 ja_rd
 
71 226 ja_rd
-- External SRAM address length -- these are 16-bit word addresses.
72
constant SRAM_ADDR_SIZE : integer := log2(SRAM_SIZE);
73 193 ja_rd
 
74 226 ja_rd
-- Static 16-bit wide RAM.
75 207 ja_rd
-- Using shared variables for big memory arrays speeds up simulation a lot;
76
-- see Modelsim 6.3 User Manual, section on 'Modelling Memory'.
77
-- WARNING: I have only tested this construct with Modelsim SE 6.3.
78 226 ja_rd
shared variable sram : t_hword_table(0 to SRAM_SIZE-1) := objcode_to_htable(SRAM_INIT, SRAM_SIZE);
79 193 ja_rd
 
80 226 ja_rd
 
81 193 ja_rd
signal sram_chip_addr :     std_logic_vector(SRAM_ADDR_SIZE downto 1);
82 226 ja_rd
signal sram_output :        t_halfword;
83 193 ja_rd
 
84
 
85
-- PROM table and interface signals --------------------------------------------
86
 
87 226 ja_rd
constant PROM_ADDR_SIZE : integer := log2(PROM_SIZE);
88
subtype t_prom_address is std_logic_vector(PROM_ADDR_SIZE-1 downto 0);
89
 
90 193 ja_rd
-- We'll simulate a 16-bit-wide static PROM (e.g. a Flash) with some serious
91
-- cycle time (70 or 90 ns).
92 207 ja_rd
-- FIXME FLASH read cycle time not modelled yet.
93 193 ja_rd
signal prom_rd_addr :       t_prom_address;
94 226 ja_rd
signal prom_output :        t_byte;
95 193 ja_rd
signal prom_oe_n :          std_logic;
96
 
97 207 ja_rd
-- 8-bit wide FLASH modelled as read only block.
98
-- We don't simulate the actual FLASH chip: no FLASH writes, control regs, etc.
99 226 ja_rd
shared variable prom : t_byte_table(0 to PROM_SIZE-1) := objcode_to_btable(PROM_INIT, PROM_SIZE);
100 193 ja_rd
 
101
 
102
-- I/O devices -----------------------------------------------------------------
103
 
104
signal data_uart :          std_logic_vector(31 downto 0);
105
signal data_uart_status :   std_logic_vector(31 downto 0);
106
signal uart_tx_rdy :        std_logic := '1';
107
signal uart_rx_rdy :        std_logic := '1';
108
 
109
--------------------------------------------------------------------------------
110
 
111
signal clk :                std_logic := '0';
112
signal reset :              std_logic := '1';
113
signal interrupt :          std_logic := '0';
114
signal done :               std_logic := '0';
115
 
116
-- interface to asynchronous 16-bit-wide external SRAM
117 226 ja_rd
signal mpu_sram_address :   std_logic_vector(31 downto 0);
118
signal mpu_sram_data_rd :   t_halfword;
119
signal mpu_sram_data_wr :   t_halfword;
120 193 ja_rd
signal mpu_sram_byte_we_n : std_logic_vector(1 downto 0);
121
signal mpu_sram_oe_n :      std_logic;
122
 
123
-- interface to i/o
124
signal io_rd_data :         std_logic_vector(31 downto 0);
125
signal io_wr_data :         std_logic_vector(31 downto 0);
126
signal io_rd_addr :         std_logic_vector(31 downto 2);
127
signal io_wr_addr :         std_logic_vector(31 downto 2);
128
signal io_rd_vma :          std_logic;
129
signal io_byte_we :         std_logic_vector(3 downto 0);
130
 
131
signal rxd :                std_logic;
132
signal txd :                std_logic;
133
 
134 200 ja_rd
-- Other CPU signals 
135
signal cpu_irq :            std_logic_vector(7 downto 0);
136 193 ja_rd
 
137
--------------------------------------------------------------------------------
138
-- Logging signals
139
 
140
 
141
-- Log file
142
file log_file: TEXT open write_mode is "hw_sim_log.txt";
143
 
144
-- Console output log file
145
file con_file: TEXT open write_mode is "hw_sim_console_log.txt";
146
 
147
-- All the info needed by the logger is here
148
signal log_info :           t_log_info;
149
 
150 200 ja_rd
-- IRQ trigger simulation ------------------------------------------------------
151 193 ja_rd
 
152 200 ja_rd
signal irq_trigger_addr :   std_logic_vector(2 downto 0);
153
signal irq_trigger_data :   std_logic_vector(31 downto 0);
154
signal irq_trigger_load :   std_logic;
155 193 ja_rd
 
156 200 ja_rd
subtype t_irq_countdown     is std_logic_vector(31 downto 0);
157
type t_irq_countdown_array  is array(0 to 7) of t_irq_countdown;
158 193 ja_rd
 
159 200 ja_rd
signal irq_countdown :      t_irq_countdown_array;
160
 
161 211 ja_rd
-- Simulated block of 4 read/write, 32-bit I/O registers, used in cache test. 
162
type t_debug_reg_block is array(0 to 3) of t_word;
163
signal debug_reg_block :    t_debug_reg_block;
164 200 ja_rd
 
165 211 ja_rd
 
166 193 ja_rd
begin
167
 
168
    -- UUT instantiation -------------------------------------------------------
169 226 ja_rd
    mpu: entity work.mips_soc
170 193 ja_rd
    generic map (
171 226 ja_rd
        BOOT_BRAM_SIZE => bram_size,
172
        OBJ_CODE       => obj_code,
173 193 ja_rd
        CLOCK_FREQ     => 50000000,
174
        SRAM_ADDR_SIZE => 32
175
    )
176
    port map (
177 205 ja_rd
        interrupt       => cpu_irq,
178 193 ja_rd
 
179
        -- interface to FPGA i/o devices
180
        io_rd_data      => io_rd_data,
181
        io_rd_addr      => io_rd_addr,
182
        io_wr_addr      => io_wr_addr,
183
        io_wr_data      => io_wr_data,
184
        io_rd_vma       => io_rd_vma,
185
        io_byte_we      => io_byte_we,
186
 
187
        -- interface to asynchronous 16-bit-wide EXTERNAL SRAM
188
        sram_address    => mpu_sram_address,
189
        sram_data_rd    => mpu_sram_data_rd,
190
        sram_data_wr    => mpu_sram_data_wr,
191
        sram_byte_we_n  => mpu_sram_byte_we_n,
192
        sram_oe_n       => mpu_sram_oe_n,
193
 
194
        uart_rxd        => rxd,
195
        uart_txd        => txd,
196
 
197
        debug_info      => OPEN,
198
 
199
        clk             => clk,
200
        reset           => reset
201
    );
202
 
203
 
204
    -- Master clock: free running clock used as main module clock --------------
205
    run_master_clock:
206
    process(done, clk)
207
    begin
208
        if done = '0' then
209
            clk <= not clk after T/2;
210
        end if;
211
    end process run_master_clock;
212
 
213
    -- Main simulation process: reset MCU and wait for fixed period ------------
214
    drive_uut:
215
    process
216
    variable l : line;
217
    begin
218
        wait for T*4;
219
        reset <= '0';
220
 
221
        wait for T*SIMULATION_LENGTH;
222
 
223
        -- Flush console output to log console file (in case the end of the
224
        -- simulation caugh an unterminated line in the buffer)
225
        if log_info.con_line_ix > 1 then
226
            write(l, log_info.con_line_buf(1 to log_info.con_line_ix));
227
            writeline(con_file, l);
228
        end if;
229
 
230
        print("TB finished");
231
        done <= '1';
232
        wait;
233
 
234
    end process drive_uut;
235
 
236
 
237
 
238
    -- SRAM/FLASH mux (on a real board this would be a simple address decoder)
239
    mpu_sram_data_rd <=
240
        X"00" & prom_output when mpu_sram_address(31 downto 27)="10110" else
241
        sram_output;
242
 
243
 
244
    -- Do a very basic simulation of an external SRAM --------------------------
245
 
246
    sram_chip_addr <= mpu_sram_address(SRAM_ADDR_SIZE downto 1);
247
 
248
    -- FIXME should add some verification of /WE 
249
    sram_output <=
250 226 ja_rd
        sram(conv_integer(unsigned(sram_chip_addr))) when mpu_sram_oe_n='0'
251 193 ja_rd
        else (others => 'Z');
252
 
253
    simulated_sram_write:
254
    process(mpu_sram_byte_we_n, mpu_sram_address, mpu_sram_oe_n)
255
    begin
256
        -- Write cycle
257
        -- FIXME should add OE\ to write control logic
258
        if mpu_sram_byte_we_n'event or mpu_sram_address'event then
259
            if mpu_sram_byte_we_n(1)='0' then
260 226 ja_rd
                sram(conv_integer(unsigned(sram_chip_addr)))(15 downto 8) := mpu_sram_data_wr(15 downto  8);
261 193 ja_rd
            end if;
262
            if mpu_sram_byte_we_n(0)='0' then
263 226 ja_rd
                sram(conv_integer(unsigned(sram_chip_addr)))( 7 downto 0) := mpu_sram_data_wr( 7 downto  0);
264 193 ja_rd
            end if;
265
        end if;
266
    end process simulated_sram_write;
267
 
268
 
269
    -- Do a very basic simulation of an external PROM (FLASH) ------------------
270
    -- (wired to the same bus as the sram and both are static).
271
 
272 226 ja_rd
    prom_rd_addr <= mpu_sram_address(PROM_ADDR_SIZE-1 downto 0);
273 193 ja_rd
 
274
    prom_oe_n <= mpu_sram_oe_n;
275 226 ja_rd
 
276
    simulated_flash:
277
    if PROM_SIZE > 0 generate
278
        prom_output <=
279
            prom(conv_integer(unsigned(prom_rd_addr))) when prom_oe_n='0' else
280
            (others => 'Z');
281
    end generate;
282
 
283
    unused_flash:
284
    if PROM_SIZE <= 0 generate
285
        prom_output <= (others => 'Z');
286
    end generate;
287 193 ja_rd
 
288
    -- Simulate dummy I/O traffic external to the MCU --------------------------
289 226 ja_rd
    -- The only IO present is the test interrupt trigger registers and the
290
    -- debug register block.
291 200 ja_rd
    simulated_io:
292
    process(clk)
293
    variable i : integer;
294
    variable uart_data : integer;
295
    begin
296
        if clk'event and clk='1' then
297
            if io_byte_we /= "0000" then
298
                if io_wr_addr(31 downto 16)=X"2001" then
299 226 ja_rd
                    if io_wr_addr(5)='0' then
300
                        -- IRQ trigger register block (write only)
301
                        irq_trigger_load <= '1';
302
                        irq_trigger_data <= io_wr_data;
303
                        irq_trigger_addr <= io_wr_addr(4 downto 2);
304
                    else
305
                        -- Debug register block (read/write)
306
                        debug_reg_block(conv_integer(unsigned(io_wr_addr(3 downto 2)))) <= io_wr_data;
307
                    end if;
308 200 ja_rd
                else
309
                    irq_trigger_load <= '0';
310
                end if;
311
            else
312
                irq_trigger_load <= '0';
313
            end if;
314
        end if;
315
    end process simulated_io;
316
 
317 211 ja_rd
    -- The only readable i/o is the debug reg block. We simulate an asynchronous
318 226 ja_rd
    -- read port (a mux). 
319
    -- For read accesses, this register block is mirrored all over the io 
320
    --- address space 2001xxxxh.
321 211 ja_rd
    io_rd_data <= debug_reg_block(conv_integer(unsigned(io_rd_addr(3 downto 2))));
322
 
323 200 ja_rd
    -- Simulate IRQs -----------------------------------------------------------
324
    irq_trigger_registers:
325
    process(clk)
326
    variable index : integer range 0 to 7;
327
    begin
328
        if clk'event and clk='1' then
329
            if reset='1' then
330
                cpu_irq <= "00000000";
331
            else
332
                if irq_trigger_load='1' then
333
                    index := conv_integer(irq_trigger_addr);
334
                    irq_countdown(index) <= irq_trigger_data;
335
                else
336
                    for index in 0 to 7 loop
337
                        if irq_countdown(index) = X"00000001" then
338
                            cpu_irq(index) <= '1';
339
                            irq_countdown(index) <= irq_countdown(index) - 1;
340
                        elsif irq_countdown(index)/=X"00000000" then
341
                            irq_countdown(index) <= irq_countdown(index) - 1;
342
                            cpu_irq(index) <= '0';
343
                        else
344
                            cpu_irq(index) <= '0';
345
                        end if;
346
                    end loop;
347
                end if;
348
            end if;
349
        end if;
350
    end process irq_trigger_registers;
351 193 ja_rd
 
352 200 ja_rd
 
353 193 ja_rd
    -- This is useless (the simulated UART will not be actually used)
354
    -- but at least prevents the simulator from optimizing the logic away.
355
    rxd <= txd;
356
 
357
 
358
    -- Logging process: launch logger function ---------------------------------
359
    log_execution:
360
    process
361
    begin
362
        log_cpu_activity(clk, reset, done,
363
                         "mips_tb/mpu", "cpu",
364
                         log_info, "log_info",
365
                         LOG_TRIGGER_ADDRESS, log_file, con_file);
366
        wait;
367
    end process log_execution;
368
 
369
end architecture testbench;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.