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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sim/] [neorv32_tb.vhd] - Blame information for rev 19

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2 3 zero_gravi
-- # << NEORV32 - Simple Testbench >>                                                              #
3 2 zero_gravi
-- # ********************************************************************************************* #
4 3 zero_gravi
-- # This testbench provides a virtual UART receiver connected to the processor's uart_txd_o       #
5
-- # signals. The received chars are shown in the simulator console and also written to a file     #
6
-- # ("neorv32.testbench_uart.out").                                                               #
7
-- # Futhermore, this testbench provides a simple RAM connected to the external Wishbone bus.      #
8 11 zero_gravi
-- # The testbench configures the processor with all optional element enabled by default.          #
9 3 zero_gravi
-- # ********************************************************************************************* #
10 2 zero_gravi
-- # BSD 3-Clause License                                                                          #
11
-- #                                                                                               #
12
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
13
-- #                                                                                               #
14
-- # Redistribution and use in source and binary forms, with or without modification, are          #
15
-- # permitted provided that the following conditions are met:                                     #
16
-- #                                                                                               #
17
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
18
-- #    conditions and the following disclaimer.                                                   #
19
-- #                                                                                               #
20
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
21
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
22
-- #    provided with the distribution.                                                            #
23
-- #                                                                                               #
24
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
25
-- #    endorse or promote products derived from this software without specific prior written      #
26
-- #    permission.                                                                                #
27
-- #                                                                                               #
28
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
29
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
30
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
31
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
32
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
33
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
34
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
35
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
36
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
37
-- # ********************************************************************************************* #
38
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
39
-- #################################################################################################
40
 
41
library ieee;
42
use ieee.std_logic_1164.all;
43
use ieee.numeric_std.all;
44
use ieee.math_real.all;
45
 
46
library neorv32;
47
use neorv32.neorv32_package.all;
48
use std.textio.all;
49
 
50
entity neorv32_tb is
51
end neorv32_tb;
52
 
53
architecture neorv32_tb_rtl of neorv32_tb is
54
 
55
  -- User Configuration ---------------------------------------------------------------------
56
  -- -------------------------------------------------------------------------------------------
57
  constant t_clock_c          : time := 10 ns; -- main clock period
58
  constant f_clock_c          : real := 100000000.0; -- main clock in Hz
59
  constant f_clock_nat_c      : natural := 100000000; -- main clock in Hz
60
  constant baud_rate_c        : real := 19200.0; -- standard UART baudrate
61 3 zero_gravi
  constant wb_mem_base_addr_c : std_ulogic_vector(31 downto 0) := x"F0000000"; -- wishbone memory base address
62 2 zero_gravi
  constant wb_mem_size_c      : natural := 256; -- wishbone memory size in bytes
63
  -- -------------------------------------------------------------------------------------------
64
 
65 3 zero_gravi
  -- text.io --
66
  file file_uart_tx_out : text open write_mode is "neorv32.testbench_uart.out";
67 2 zero_gravi
 
68
  -- internal configuration --
69
  constant baud_val_c : real    := f_clock_c / baud_rate_c;
70
  constant f_clk_c    : natural := natural(f_clock_c);
71
 
72
  -- generators --
73
  signal clk_gen, rst_gen : std_ulogic := '0';
74
 
75
  -- simulation uart receiver --
76
  signal uart_txd         : std_ulogic;
77
  signal uart_rx_sync     : std_ulogic_vector(04 downto 0) := (others => '1');
78
  signal uart_rx_busy     : std_ulogic := '0';
79
  signal uart_rx_sreg     : std_ulogic_vector(08 downto 0) := (others => '0');
80
  signal uart_rx_baud_cnt : real;
81
  signal uart_rx_bitcnt   : natural;
82
 
83
  -- gpio --
84
  signal gpio : std_ulogic_vector(15 downto 0);
85
 
86
  -- twi --
87
  signal twi_scl, twi_sda : std_logic;
88
 
89
  -- spi --
90
  signal spi_data : std_logic;
91
 
92
  -- Wishbone bus --
93
  type wishbone_t is record
94
    addr  : std_ulogic_vector(31 downto 0); -- address
95
    wdata : std_ulogic_vector(31 downto 0); -- master write data
96
    rdata : std_ulogic_vector(31 downto 0); -- master read data
97
    we    : std_ulogic; -- write enable
98
    sel   : std_ulogic_vector(03 downto 0); -- byte enable
99
    stb   : std_ulogic; -- strobe
100
    cyc   : std_ulogic; -- valid cycle
101
    ack   : std_ulogic; -- transfer acknowledge
102
    err   : std_ulogic; -- transfer error
103
  end record;
104
  signal wb_cpu : wishbone_t;
105
 
106
 
107 3 zero_gravi
  -- Wishbone memory, SimCom --
108 2 zero_gravi
  type wb_mem_file_t is array (0 to wb_mem_size_c/4-1) of std_ulogic_vector(31 downto 0);
109 3 zero_gravi
  signal wb_mem_file  : wb_mem_file_t := (others => (others => '0'));
110
  signal rb_en        : std_ulogic;
111
  signal r_data       : std_ulogic_vector(31 downto 0);
112
  signal wb_acc_en    : std_ulogic;
113
  signal wb_mem_rdata : std_ulogic_vector(31 downto 0);
114
  signal wb_mem_ack   : std_ulogic;
115 2 zero_gravi
 
116
begin
117
 
118
  -- Clock/Reset Generator ------------------------------------------------------------------
119
  -- -------------------------------------------------------------------------------------------
120
  clk_gen <= not clk_gen after (t_clock_c/2);
121
  rst_gen <= '0', '1' after 60*(t_clock_c/2);
122
 
123
 
124
  -- CPU Core -------------------------------------------------------------------------------
125
  -- -------------------------------------------------------------------------------------------
126
  neorv32_top_inst: neorv32_top
127
  generic map (
128
    -- General --
129 8 zero_gravi
    CLOCK_FREQUENCY              => f_clock_nat_c, -- clock frequency of clk_i in Hz
130
    BOOTLOADER_USE               => false,         -- implement processor-internal bootloader?
131 12 zero_gravi
    USER_CODE                    => x"19880704",   -- custom user code
132 2 zero_gravi
    -- RISC-V CPU Extensions --
133 8 zero_gravi
    CPU_EXTENSION_RISCV_C        => true,          -- implement compressed extension?
134
    CPU_EXTENSION_RISCV_E        => false,         -- implement embedded RF extension?
135
    CPU_EXTENSION_RISCV_M        => true,          -- implement muld/div extension?
136 15 zero_gravi
    CPU_EXTENSION_RISCV_U        => true,          -- implement user mode extension?
137 8 zero_gravi
    CPU_EXTENSION_RISCV_Zicsr    => true,          -- implement CSR system?
138
    CPU_EXTENSION_RISCV_Zifencei => true,          -- implement instruction stream sync.?
139 19 zero_gravi
    -- Extension Options --
140
    CSR_COUNTERS_USE             => true,          -- implement RISC-V perf. counters ([m]instret[h], [m]cycle[h], time[h])?
141
    FAST_MUL_EN                  => false,         -- use DSPs for M extension's multiplier
142 15 zero_gravi
    -- Physical Memory Protection (PMP) --
143
    PMP_USE                      => true,          -- implement PMP?
144
    PMP_NUM_REGIONS              => 4,             -- number of regions (max 16)
145 16 zero_gravi
    PMP_GRANULARITY              => 14,            -- minimal region granularity (1=8B, 2=16B, 3=32B, ...) default is 64k
146 2 zero_gravi
    -- Memory configuration: Instruction memory --
147 8 zero_gravi
    MEM_ISPACE_BASE              => x"00000000",   -- base address of instruction memory space
148
    MEM_ISPACE_SIZE              => 16*1024,       -- total size of instruction memory space in byte
149
    MEM_INT_IMEM_USE             => true,          -- implement processor-internal instruction memory
150
    MEM_INT_IMEM_SIZE            => 16*1024,       -- size of processor-internal instruction memory in bytes
151
    MEM_INT_IMEM_ROM             => false,         -- implement processor-internal instruction memory as ROM
152 2 zero_gravi
    -- Memory configuration: Data memory --
153 8 zero_gravi
    MEM_DSPACE_BASE              => x"80000000",   -- base address of data memory space
154
    MEM_DSPACE_SIZE              => 8*1024,        -- total size of data memory space in byte
155
    MEM_INT_DMEM_USE             => true,          -- implement processor-internal data memory
156
    MEM_INT_DMEM_SIZE            => 8*1024,        -- size of processor-internal data memory in bytes
157 2 zero_gravi
    -- Memory configuration: External memory interface --
158 8 zero_gravi
    MEM_EXT_USE                  => true,          -- implement external memory bus interface?
159
    MEM_EXT_REG_STAGES           => 2,             -- number of interface register stages (0,1,2)
160
    MEM_EXT_TIMEOUT              => 15,            -- cycles after which a valid bus access will timeout
161 2 zero_gravi
    -- Processor peripherals --
162 8 zero_gravi
    IO_GPIO_USE                  => true,          -- implement general purpose input/output port unit (GPIO)?
163
    IO_MTIME_USE                 => true,          -- implement machine system timer (MTIME)?
164
    IO_UART_USE                  => true,          -- implement universal asynchronous receiver/transmitter (UART)?
165
    IO_SPI_USE                   => true,          -- implement serial peripheral interface (SPI)?
166
    IO_TWI_USE                   => true,          -- implement two-wire interface (TWI)?
167
    IO_PWM_USE                   => true,          -- implement pulse-width modulation unit (PWM)?
168
    IO_WDT_USE                   => true,          -- implement watch dog timer (WDT)?
169 11 zero_gravi
    IO_TRNG_USE                  => false,         -- CANNOT BE SIMULATED!
170 8 zero_gravi
    IO_DEVNULL_USE               => true           -- implement dummy device (DEVNULL)?
171 2 zero_gravi
  )
172
  port map (
173
    -- Global control --
174
    clk_i      => clk_gen,         -- global clock, rising edge
175
    rstn_i     => rst_gen,         -- global reset, low-active, async
176
    -- Wishbone bus interface --
177
    wb_adr_o   => wb_cpu.addr,     -- address
178
    wb_dat_i   => wb_cpu.rdata,    -- read data
179
    wb_dat_o   => wb_cpu.wdata,    -- write data
180
    wb_we_o    => wb_cpu.we,       -- read/write
181
    wb_sel_o   => wb_cpu.sel,      -- byte enable
182
    wb_stb_o   => wb_cpu.stb,      -- strobe
183
    wb_cyc_o   => wb_cpu.cyc,      -- valid cycle
184
    wb_ack_i   => wb_cpu.ack,      -- transfer acknowledge
185
    wb_err_i   => wb_cpu.err,      -- transfer error
186 12 zero_gravi
    -- Advanced memory control signals --
187
    fence_o    => open,            -- indicates an executed FENCE operation
188
    fencei_o   => open,            -- indicates an executed FENCEI operation
189 2 zero_gravi
    -- GPIO --
190
    gpio_o     => gpio,            -- parallel output
191
    gpio_i     => gpio,            -- parallel input
192
    -- UART --
193
    uart_txd_o => uart_txd,        -- UART send data
194
    uart_rxd_i => uart_txd,        -- UART receive data
195
    -- SPI --
196 6 zero_gravi
    spi_sck_o  => open,            -- SPI serial clock
197
    spi_sdo_o  => spi_data,        -- controller data out, peripheral data in
198
    spi_sdi_i  => spi_data,        -- controller data in, peripheral data out
199 2 zero_gravi
    spi_csn_o  => open,            -- SPI CS
200
    -- TWI --
201
    twi_sda_io => twi_sda,         -- twi serial data line
202
    twi_scl_io => twi_scl,         -- twi serial clock line
203
    -- PWM --
204
    pwm_o      => open,            -- pwm channels
205
    -- Interrupts --
206 14 zero_gravi
    mext_irq_i => '0'              -- machine external interrupt
207 2 zero_gravi
  );
208
 
209 3 zero_gravi
  -- TWI termination --
210 2 zero_gravi
  twi_scl <= 'H';
211
  twi_sda <= 'H';
212
 
213 3 zero_gravi
  -- Wishbone read-back --
214
  wb_cpu.rdata <= wb_mem_rdata;
215
  wb_cpu.ack   <= wb_mem_ack;
216
  wb_cpu.err   <= '0';
217 2 zero_gravi
 
218 3 zero_gravi
 
219 2 zero_gravi
  -- Console UART Receiver ------------------------------------------------------------------
220
  -- -------------------------------------------------------------------------------------------
221
  uart_rx_console: process(clk_gen)
222 3 zero_gravi
    variable i : integer;
223
    variable l : line;
224 2 zero_gravi
  begin
225
    -- "UART" --
226
    if rising_edge(clk_gen) then
227
      -- synchronizer --
228
      uart_rx_sync <= uart_rx_sync(3 downto 0) & uart_txd;
229
      -- arbiter --
230
      if (uart_rx_busy = '0') then -- idle
231
        uart_rx_busy     <= '0';
232
        uart_rx_baud_cnt <= round(0.5 * baud_val_c);
233
        uart_rx_bitcnt   <= 9;
234
        if (uart_rx_sync(4 downto 1) = "1100") then -- start bit? (falling edge)
235
          uart_rx_busy <= '1';
236
        end if;
237
      else
238
        if (uart_rx_baud_cnt = 0.0) then
239
          if (uart_rx_bitcnt = 1) then
240
            uart_rx_baud_cnt <= round(0.5 * baud_val_c);
241
          else
242
            uart_rx_baud_cnt <= round(baud_val_c);
243
          end if;
244
          if (uart_rx_bitcnt = 0) then
245
            uart_rx_busy <= '0'; -- done
246
            i := to_integer(unsigned(uart_rx_sreg(8 downto 1)));
247
 
248 3 zero_gravi
            if (i < 32) or (i > 32+95) then -- printable char?
249
              report "SIM_UART TX: (" & integer'image(i) & ")"; -- print code
250 2 zero_gravi
            else
251 3 zero_gravi
              report "SIM_UART TX: " & character'val(i); -- print ASCII
252 2 zero_gravi
            end if;
253
 
254
            if (i = 10) then -- Linux line break
255 3 zero_gravi
              writeline(file_uart_tx_out, l);
256 2 zero_gravi
            elsif (i /= 13) then -- Remove additional carriage return
257 3 zero_gravi
              write(l, character'val(i));
258 2 zero_gravi
            end if;
259
          else
260
            uart_rx_sreg   <= uart_rx_sync(4) & uart_rx_sreg(8 downto 1);
261
            uart_rx_bitcnt <= uart_rx_bitcnt - 1;
262
          end if;
263
        else
264
          uart_rx_baud_cnt <= uart_rx_baud_cnt - 1.0;
265
        end if;
266
      end if;
267
    end if;
268
  end process uart_rx_console;
269
 
270
 
271
  -- Wishbone Memory ------------------------------------------------------------------------
272
  -- -------------------------------------------------------------------------------------------
273
    wb_mem_file_access: process(clk_gen)
274
    begin
275
      if rising_edge(clk_gen) then
276
        rb_en <= wb_cpu.cyc and wb_cpu.stb and wb_acc_en and (not wb_cpu.we); -- read-back control
277 3 zero_gravi
        wb_mem_ack <= wb_cpu.cyc and wb_cpu.stb and wb_acc_en; -- wishbone acknowledge
278 2 zero_gravi
        if ((wb_cpu.cyc and wb_cpu.stb and wb_acc_en and wb_cpu.we) = '1') then -- valid write access
279
          for i in 0 to 3 loop
280
            if (wb_cpu.sel(i) = '1') then
281
              wb_mem_file(to_integer(unsigned(wb_cpu.addr(index_size_f(wb_mem_size_c/4)+1 downto 2))))(7+i*8 downto 0+i*8) <= wb_cpu.wdata(7+i*8 downto 0+i*8);
282
            end if;
283
          end loop; -- i
284
        end if;
285
        r_data <= wb_mem_file(to_integer(unsigned(wb_cpu.addr(index_size_f(wb_mem_size_c/4)+1 downto 2)))); -- word aligned
286
      end if;
287
    end process wb_mem_file_access;
288
 
289
  -- wb mem access --
290
  wb_acc_en <= '1' when (wb_cpu.addr >= wb_mem_base_addr_c) and (wb_cpu.addr < std_ulogic_vector(unsigned(wb_mem_base_addr_c) + wb_mem_size_c)) else '0';
291
 
292
  -- output gate --
293 3 zero_gravi
  wb_mem_rdata <= r_data when (rb_en = '1') else (others=> '0');
294 2 zero_gravi
 
295 3 zero_gravi
 
296 2 zero_gravi
end neorv32_tb_rtl;

powered by: WebSVN 2.1.0

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