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

Subversion Repositories ion

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

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

powered by: WebSVN 2.1.0

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