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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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