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

Subversion Repositories ion

[/] [ion/] [trunk/] [src/] [mips_tb2_template.vhdl] - Blame information for rev 160

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

Line No. Rev Author Line
1 42 ja_rd
--##############################################################################
2
-- This file was generated automatically from '/src/mips_tb2_template.vhdl'.
3
-- 
4
--------------------------------------------------------------------------------
5
-- Simulation test bench TB2 -- not synthesizable.
6
--
7
-- Simulates the CPU core connected to a simulated external static RAM and an
8
-- internal BRAM block through a stub (i.e. empty).
9
-- BRAM is initialized with the program object code, and SRAM is initialized 
10
-- with data secions from program. 
11
-- The makefile for the source samples include targets to build simulation test 
12
-- benches using this template, use them as usage examples.
13
--
14
-- The memory setup is meant to test the basic 'dummy' cache. 
15
-- 
16
-- Console output (at addresses compatible to Plasma's) is logged to text file
17
-- "hw_sim_console_log.txt".
18
-- IMPORTANT: The code that echoes UART TX data to the simulation console does
19
-- line buffering; it will not print anything until it gets a CR (0x0d), and
20
-- will ifnore LFs (0x0a). Bear this in mind if you see no output when you 
21
-- expect it.
22
--
23
-- WARNING: Will only work on Modelsim; uses custom library SignalSpy.
24
--##############################################################################
25
 
26 51 ja_rd
library ieee;
27 42 ja_rd
use ieee.std_logic_1164.all;
28
use ieee.std_logic_arith.all;
29
use ieee.std_logic_unsigned.all;
30 51 ja_rd
use std.textio.all;
31 42 ja_rd
 
32
use work.mips_pkg.all;
33 51 ja_rd
use work.mips_tb_pkg.all;
34 42 ja_rd
use work.txt_util.all;
35
 
36
entity @entity_name@ is
37
end;
38
 
39
 
40
architecture @arch_name@ of @entity_name@ is
41
 
42
-------------------------------------------------------------------------------
43
-- Simulation parameters
44
 
45
-- Master clock period
46
constant T : time           := 20 ns;
47
-- Time the UART is unavailable after writing to the TX register
48
-- WARNING: slite does not simulate this. The logs may be different when > 0.0!
49
constant SIMULATED_UART_TX_TIME : time := 0.0 us;
50
 
51 102 ja_rd
-- Simulation length in clock cycles, should be long enough (you have to try...)
52 42 ja_rd
constant SIMULATION_LENGTH : integer := @sim_len@;
53
 
54
-- Simulated external SRAM size in 32-bit words 
55
constant SRAM_SIZE : integer := @xram_size@;
56
-- Ext. SRAM address length (memory is 16 bits wide so it needs an extra address bit)
57
constant SRAM_ADDR_SIZE : integer := log2(SRAM_SIZE)+1;
58
 
59
 
60
-- BRAM table and interface signals --------------------------------------------
61
constant BRAM_SIZE : integer := @code_table_size@;
62
constant BRAM_ADDR_SIZE : integer := @code_addr_size@;
63
subtype t_bram_address is std_logic_vector(BRAM_ADDR_SIZE-1 downto 0);
64
-- (this table holds one byte-slice; the RAM will have 4 of these)
65
type t_bram is array(0 to BRAM_SIZE-1) of std_logic_vector(7 downto 0);
66
 
67
signal bram_rd_addr :       t_bram_address;
68
signal bram_wr_addr :       t_bram_address;
69
signal bram_rd_data :       t_word;
70
signal bram_wr_data :       t_word;
71
signal bram_byte_we :       std_logic_vector(3 downto 0);
72 51 ja_rd
signal bram_data_rd_vma :   std_logic;
73 42 ja_rd
 
74
-- bram0 is LSB, bram3 is MSB
75
signal bram3 : t_bram := (@code3@);
76
signal bram2 : t_bram := (@code2@);
77
signal bram1 : t_bram := (@code1@);
78
signal bram0 : t_bram := (@code0@);
79
 
80
-- This is a 16-bit SRAM split in 2 byte slices; so each slice will have two
81
-- bytes for each word of SRAM_SIZE
82
type t_sram is array(0 to SRAM_SIZE*2-1) of std_logic_vector(7 downto 0);
83
signal sram1 : t_sram := (@data31@);
84
signal sram0 : t_sram := (@data20@);
85
 
86 77 ja_rd
signal sram_chip_addr :     std_logic_vector(SRAM_ADDR_SIZE downto 1);
87
signal sram_output :        std_logic_vector(15 downto 0);
88
 
89 74 ja_rd
-- PROM table and interface signals --------------------------------------------
90 42 ja_rd
 
91 74 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
 
94 77 ja_rd
constant PROM_SIZE : integer := @prom_size@;
95
constant PROM_ADDR_SIZE : integer := log2(PROM_SIZE);
96 74 ja_rd
 
97 77 ja_rd
subtype t_prom_address is std_logic_vector(PROM_ADDR_SIZE-1 downto 0);
98
type t_prom is array(0 to PROM_SIZE-1) of t_word;
99 74 ja_rd
 
100 77 ja_rd
signal prom_rd_addr :       t_prom_address;
101
signal prom_output :        std_logic_vector(7 downto 0);
102
signal prom_oe_n :          std_logic;
103
 
104
-- bram0 is LSB, bram3 is MSB
105
signal prom : t_prom := (@flash@);
106
 
107
 
108
 
109 74 ja_rd
-- I/O devices -----------------------------------------------------------------
110
 
111 42 ja_rd
signal data_uart :          std_logic_vector(31 downto 0);
112
signal data_uart_status :   std_logic_vector(31 downto 0);
113
signal uart_tx_rdy :        std_logic := '1';
114
signal uart_rx_rdy :        std_logic := '1';
115
 
116
--------------------------------------------------------------------------------
117
 
118
signal clk :                std_logic := '0';
119
signal reset :              std_logic := '1';
120
signal interrupt :          std_logic := '0';
121
signal done :               std_logic := '0';
122
 
123
-- interface to asynchronous 16-bit-wide external SRAM
124 77 ja_rd
signal sram_address :       std_logic_vector(31 downto 0);
125
signal sram_data_rd :       std_logic_vector(15 downto 0);
126
signal sram_data_wr :       std_logic_vector(15 downto 0);
127 42 ja_rd
signal sram_byte_we_n :     std_logic_vector(1 downto 0);
128
signal sram_oe_n :          std_logic;
129
 
130
-- interface cpu-cache
131 97 ja_rd
signal cpu_data_addr :      t_word;
132 42 ja_rd
signal cpu_data_rd_vma :    std_logic;
133
signal cpu_data_rd :        t_word;
134
signal cpu_code_rd_addr :   t_pc;
135
signal cpu_code_rd :        t_word;
136
signal cpu_code_rd_vma :    std_logic;
137
signal cpu_data_wr :        t_word;
138
signal cpu_byte_we :        std_logic_vector(3 downto 0);
139
signal cpu_mem_wait :       std_logic;
140 102 ja_rd
signal cpu_ic_invalidate :  std_logic;
141
signal cpu_cache_enable :   std_logic;
142 42 ja_rd
 
143
-- interface to i/o
144
signal io_rd_data :         std_logic_vector(31 downto 0);
145
signal io_wr_data :         std_logic_vector(31 downto 0);
146
signal io_rd_addr :         std_logic_vector(31 downto 2);
147
signal io_wr_addr :         std_logic_vector(31 downto 2);
148
signal io_rd_vma :          std_logic;
149
signal io_byte_we :         std_logic_vector(3 downto 0);
150
 
151
 
152
--------------------------------------------------------------------------------
153
-- Logging signals
154
 
155
 
156
-- Log file
157 51 ja_rd
file log_file: TEXT open write_mode is "hw_sim_log.txt";
158 42 ja_rd
 
159
-- Console output log file
160
file con_file: TEXT open write_mode is "hw_sim_console_log.txt";
161
 
162
-- Maximum line size of for console output log. Lines longer than this will be
163
-- truncated.
164
constant CONSOLE_LOG_LINE_SIZE : integer := 1024*4;
165
 
166
-- Console log line buffer
167
signal con_line_buf :       string(1 to CONSOLE_LOG_LINE_SIZE);
168
signal con_line_ix :        integer := 1;
169
 
170 51 ja_rd
signal log_info :           t_log_info;
171
 
172 42 ja_rd
-- Debug signals ---------------------------------------------------------------
173
 
174
 
175
signal full_rd_addr :       std_logic_vector(31 downto 0);
176
signal full_wr_addr :       std_logic_vector(31 downto 0);
177
signal full_code_addr :     std_logic_vector(31 downto 0);
178
 
179
 
180
begin
181
 
182
    cpu: entity work.mips_cpu
183
    port map (
184
        interrupt   => '0',
185
 
186 97 ja_rd
        data_addr   => cpu_data_addr,
187 42 ja_rd
        data_rd_vma => cpu_data_rd_vma,
188
        data_rd     => cpu_data_rd,
189
 
190
        code_rd_addr=> cpu_code_rd_addr,
191
        code_rd     => cpu_code_rd,
192
        code_rd_vma => cpu_code_rd_vma,
193
 
194
        data_wr     => cpu_data_wr,
195
        byte_we     => cpu_byte_we,
196
 
197
        mem_wait    => cpu_mem_wait,
198 102 ja_rd
        cache_enable=> cpu_cache_enable,
199
        ic_invalidate=>cpu_ic_invalidate,
200 42 ja_rd
 
201
        clk         => clk,
202
        reset       => reset
203
    );
204
 
205 97 ja_rd
 
206 125 ja_rd
    cache: entity work.mips_cache
207 42 ja_rd
    generic map (
208
        BRAM_ADDR_SIZE => BRAM_ADDR_SIZE,
209 97 ja_rd
        SRAM_ADDR_SIZE => 32,-- we need the full address to decode sram vs flash
210
        LINE_SIZE =>      4,
211
        CACHE_SIZE =>     256
212 42 ja_rd
    )
213
    port map (
214
        clk             => clk,
215
        reset           => reset,
216
 
217
        -- Interface to CPU core
218 97 ja_rd
        data_addr       => cpu_data_addr,
219 42 ja_rd
        data_rd         => cpu_data_rd,
220
        data_rd_vma     => cpu_data_rd_vma,
221
 
222
        code_rd_addr    => cpu_code_rd_addr,
223
        code_rd         => cpu_code_rd,
224
        code_rd_vma     => cpu_code_rd_vma,
225 97 ja_rd
 
226 42 ja_rd
        byte_we         => cpu_byte_we,
227
        data_wr         => cpu_data_wr,
228
 
229
        mem_wait        => cpu_mem_wait,
230 102 ja_rd
        cache_enable    => cpu_cache_enable,
231
        ic_invalidate   => cpu_ic_invalidate,
232 137 ja_rd
        unmapped        => OPEN,
233 42 ja_rd
 
234
        -- interface to FPGA i/o devices
235
        io_rd_data      => io_rd_data,
236
        io_wr_data      => io_wr_data,
237
        io_rd_addr      => io_rd_addr,
238
        io_wr_addr      => io_wr_addr,
239
        io_rd_vma       => io_rd_vma,
240
        io_byte_we      => io_byte_we,
241
 
242
        -- interface to synchronous 32-bit-wide FPGA BRAM
243
        bram_rd_data    => bram_rd_data,
244
        bram_wr_data    => bram_wr_data,
245
        bram_rd_addr    => bram_rd_addr,
246
        bram_wr_addr    => bram_wr_addr,
247
        bram_byte_we    => bram_byte_we,
248 51 ja_rd
        bram_data_rd_vma=> bram_data_rd_vma,
249 42 ja_rd
 
250
        -- interface to asynchronous 16-bit-wide external SRAM
251
        sram_address    => sram_address,
252 77 ja_rd
        sram_data_rd    => sram_data_rd,
253
        sram_data_wr    => sram_data_wr,
254 42 ja_rd
        sram_byte_we_n  => sram_byte_we_n,
255
        sram_oe_n       => sram_oe_n
256
    );
257
 
258
    ---------------------------------------------------------------------------
259
    -- Master clock: free running clock used as main module clock
260
    run_master_clock:
261
    process(done, clk)
262
    begin
263
        if done = '0' then
264
            clk <= not clk after T/2;
265
        end if;
266
    end process run_master_clock;
267
 
268
    drive_uut:
269
    process
270
    variable l : line;
271
    begin
272
        wait for T*4;
273
        reset <= '0';
274
 
275
        wait for T*SIMULATION_LENGTH;
276
 
277
        -- Flush console output to log console file (in case the end of the
278
        -- simulation caugh an unterminated line in the buffer)
279
        if con_line_ix > 1 then
280
            write(l, con_line_buf(1 to con_line_ix));
281
            writeline(con_file, l);
282
        end if;
283
 
284
        print("TB0 finished");
285
        done <= '1';
286
        wait;
287
 
288
    end process drive_uut;
289
 
290 97 ja_rd
    full_rd_addr <= cpu_data_addr;
291
    full_wr_addr <= cpu_data_addr(31 downto 2) & "00";
292 42 ja_rd
    full_code_addr <= cpu_code_rd_addr & "00";
293
 
294
    data_ram_block:
295
    process(clk)
296
    begin
297
        if clk'event and clk='1' then
298
            if reset='0' then
299
                bram_rd_data <=
300
                    bram3(conv_integer(unsigned(bram_rd_addr))) &
301
                    bram2(conv_integer(unsigned(bram_rd_addr))) &
302
                    bram1(conv_integer(unsigned(bram_rd_addr))) &
303
                    bram0(conv_integer(unsigned(bram_rd_addr)));
304
 
305
                if bram_byte_we(3)='1' then
306
                    bram3(conv_integer(unsigned(bram_wr_addr))) <= cpu_data_wr(31 downto 24);
307
                end if;
308
                if bram_byte_we(2)='1' then
309
                    bram2(conv_integer(unsigned(bram_wr_addr))) <= cpu_data_wr(23 downto 16);
310
                end if;
311
                if bram_byte_we(1)='1' then
312
                    bram1(conv_integer(unsigned(bram_wr_addr))) <= cpu_data_wr(15 downto  8);
313
                end if;
314
                if bram_byte_we(0)='1' then
315
                    bram0(conv_integer(unsigned(bram_wr_addr))) <= cpu_data_wr( 7 downto  0);
316
                end if;
317
            end if;
318
        end if;
319
    end process data_ram_block;
320
 
321 77 ja_rd
    sram_data_rd <=
322
        X"00" & prom_output when sram_address(31 downto 27)="10110" else
323
        sram_output;
324
 
325
 
326
 
327
    -- Do a very basic simulation of an external SRAM ---------------
328
 
329
    sram_chip_addr <= sram_address(SRAM_ADDR_SIZE downto 1);
330
 
331
    -- FIXME should add some verification of /WE 
332
    sram_output <=
333
        sram1(conv_integer(unsigned(sram_chip_addr))) &
334
        sram0(conv_integer(unsigned(sram_chip_addr)))   when sram_oe_n='0'
335 42 ja_rd
        else (others => 'Z');
336
 
337 77 ja_rd
    simulated_sram_write:
338 51 ja_rd
    process(sram_byte_we_n, sram_address, sram_oe_n)
339 42 ja_rd
    begin
340 51 ja_rd
        -- Write cycle
341
        -- FIXME should add OE\ to write control logic
342 42 ja_rd
        if sram_byte_we_n'event or sram_address'event then
343
            if sram_byte_we_n(1)='0' then
344 77 ja_rd
                sram1(conv_integer(unsigned(sram_chip_addr))) <= sram_data_wr(15 downto  8);
345 42 ja_rd
            end if;
346
            if sram_byte_we_n(0)='0' then
347 77 ja_rd
                sram0(conv_integer(unsigned(sram_chip_addr))) <= sram_data_wr( 7 downto  0);
348 51 ja_rd
            end if;
349
        end if;
350 77 ja_rd
    end process simulated_sram_write;
351 51 ja_rd
 
352
 
353 74 ja_rd
    -- Do a very basic simulation of an external PROM wired to the same bus 
354
    -- as the sram (both are static).
355
 
356 77 ja_rd
    prom_rd_addr <= sram_address(PROM_ADDR_SIZE+1 downto 2);
357 74 ja_rd
 
358 77 ja_rd
    prom_oe_n <= sram_oe_n;
359
 
360
    prom_output <=
361
        prom(conv_integer(unsigned(prom_rd_addr)))(31 downto 24) when prom_oe_n='0' and sram_address(1 downto 0)="00" else
362
        prom(conv_integer(unsigned(prom_rd_addr)))(23 downto 16) when prom_oe_n='0' and sram_address(1 downto 0)="01" else
363
        prom(conv_integer(unsigned(prom_rd_addr)))(15 downto  8) when prom_oe_n='0' and sram_address(1 downto 0)="10" else
364
        prom(conv_integer(unsigned(prom_rd_addr)))( 7 downto  0) when prom_oe_n='0' and sram_address(1 downto 0)="11" else
365 86 ja_rd
        (others => 'Z');
366 77 ja_rd
 
367
 
368 42 ja_rd
    simulated_io:
369
    process(clk)
370
    variable i : integer;
371
    variable uart_data : integer;
372
    begin
373
        if clk'event and clk='1' then
374
 
375
            if io_byte_we/="0000" then
376
                if io_wr_addr(31 downto 28)=X"2" then
377
                    -- Write to UART
378
 
379
                    -- If we're simulating the UART TX time, pulse RDY low
380
                    if SIMULATED_UART_TX_TIME > 0 us then
381
                        uart_tx_rdy <= '0', '1' after SIMULATED_UART_TX_TIME;
382
                    end if;
383
 
384
                    -- TX data may come from the high or low byte (opcodes.s
385
                    -- uses high byte, no_op.c uses low)
386
                    if io_byte_we(0)='1' then
387
                        uart_data := conv_integer(unsigned(io_wr_data(7 downto 0)));
388
                    else
389
                        uart_data := conv_integer(unsigned(io_wr_data(31 downto 24)));
390
                    end if;
391
 
392
                    -- UART TX data goes to output after a bit of line-buffering
393
                    -- and editing
394
                    if uart_data = 10 then
395
                        -- CR received: print output string and clear it
396
                        print(con_file, con_line_buf(1 to con_line_ix));
397
                        con_line_ix <= 1;
398
                        for i in 1 to con_line_buf'high loop
399
                           con_line_buf(i) <= ' ';
400
                        end loop;
401
                    elsif uart_data = 13 then
402
                        -- ignore LF
403
                    else
404
                        -- append char to output string
405
                        if con_line_ix < con_line_buf'high then
406
                            con_line_buf(con_line_ix) <= character'val(uart_data);
407
                            con_line_ix <= con_line_ix + 1;
408
                        end if;
409
                    end if;
410
                end if;
411
            end if;
412
        end if;
413
    end process simulated_io;
414
 
415
    -- UART read registers; only status, and hardwired, for the time being
416 51 ja_rd
    io_rd_data <= X"00000003";
417 42 ja_rd
    data_uart <= data_uart_status;
418
    data_uart_status <= X"0000000" & "00" & uart_tx_rdy & uart_rx_rdy;
419
 
420 51 ja_rd
    log_execution:
421 42 ja_rd
    process
422
    begin
423 51 ja_rd
        log_cpu_activity(clk, reset, done,
424 86 ja_rd
                         "@entity_name@/cpu", log_info, "log_info",
425
                         @log_trigger_addr@, log_file);
426 42 ja_rd
        wait;
427 51 ja_rd
    end process log_execution;
428 42 ja_rd
 
429
 
430
end architecture @arch_name@;

powered by: WebSVN 2.1.0

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