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

Subversion Repositories neorv32

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

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Simple Testbench with UART-to-Console module >>                                  #
3
-- # ********************************************************************************************* #
4
-- # BSD 3-Clause License                                                                          #
5
-- #                                                                                               #
6
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
-- #                                                                                               #
8
-- # Redistribution and use in source and binary forms, with or without modification, are          #
9
-- # permitted provided that the following conditions are met:                                     #
10
-- #                                                                                               #
11
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
-- #    conditions and the following disclaimer.                                                   #
13
-- #                                                                                               #
14
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
16
-- #    provided with the distribution.                                                            #
17
-- #                                                                                               #
18
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
-- #    endorse or promote products derived from this software without specific prior written      #
20
-- #    permission.                                                                                #
21
-- #                                                                                               #
22
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
-- # ********************************************************************************************* #
32
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
-- #################################################################################################
34
 
35
library ieee;
36
use ieee.std_logic_1164.all;
37
use ieee.numeric_std.all;
38
use ieee.math_real.all;
39
 
40
library neorv32;
41
use neorv32.neorv32_package.all;
42
use std.textio.all;
43
 
44
entity neorv32_tb is
45
end neorv32_tb;
46
 
47
architecture neorv32_tb_rtl of neorv32_tb is
48
 
49
  -- User Configuration ---------------------------------------------------------------------
50
  -- -------------------------------------------------------------------------------------------
51
  constant t_clock_c          : time := 10 ns; -- main clock period
52
  constant f_clock_c          : real := 100000000.0; -- main clock in Hz
53
  constant f_clock_nat_c      : natural := 100000000; -- main clock in Hz
54
  constant baud_rate_c        : real := 19200.0; -- standard UART baudrate
55
  constant wb_mem_size_c      : natural := 256; -- wishbone memory size in bytes
56
  constant wb_mem_base_addr_c : std_ulogic_vector(31 downto 0) := x"F0000000"; -- wishbone memory base address
57
  -- -------------------------------------------------------------------------------------------
58
 
59
  -- textio --
60
  file file_uart_tx_out : text open write_mode is "neorv32.sim_uart.out";
61
 
62
  -- internal configuration --
63
  constant baud_val_c : real    := f_clock_c / baud_rate_c;
64
  constant f_clk_c    : natural := natural(f_clock_c);
65
 
66
  -- reduced ASCII table --
67
  type ascii_t is array (0 to 94) of character;
68
  constant ascii_lut : ascii_t := (' ', '!', '"', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-',
69
  '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A',
70
  'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
71
  'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
72
  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~');
73
 
74
  -- generators --
75
  signal clk_gen, rst_gen : std_ulogic := '0';
76
 
77
  -- simulation uart receiver --
78
  signal uart_txd         : std_ulogic;
79
  signal uart_rx_sync     : std_ulogic_vector(04 downto 0) := (others => '1');
80
  signal uart_rx_busy     : std_ulogic := '0';
81
  signal uart_rx_sreg     : std_ulogic_vector(08 downto 0) := (others => '0');
82
  signal uart_rx_baud_cnt : real;
83
  signal uart_rx_bitcnt   : natural;
84
 
85
  -- gpio --
86
  signal gpio : std_ulogic_vector(15 downto 0);
87
 
88
  -- twi --
89
  signal twi_scl, twi_sda : std_logic;
90
 
91
  -- spi --
92
  signal spi_data : std_logic;
93
 
94
  -- Wishbone bus --
95
  type wishbone_t is record
96
    addr  : std_ulogic_vector(31 downto 0); -- address
97
    wdata : std_ulogic_vector(31 downto 0); -- master write data
98
    rdata : std_ulogic_vector(31 downto 0); -- master read data
99
    we    : std_ulogic; -- write enable
100
    sel   : std_ulogic_vector(03 downto 0); -- byte enable
101
    stb   : std_ulogic; -- strobe
102
    cyc   : std_ulogic; -- valid cycle
103
    ack   : std_ulogic; -- transfer acknowledge
104
    err   : std_ulogic; -- transfer error
105
  end record;
106
  signal wb_cpu : wishbone_t;
107
 
108
 
109
  -- Wishbone memory --
110
  type wb_mem_file_t is array (0 to wb_mem_size_c/4-1) of std_ulogic_vector(31 downto 0);
111
  signal wb_mem_file : wb_mem_file_t := (others => (others => '0'));
112
  signal rb_en       : std_ulogic;
113
  signal r_data      : std_ulogic_vector(31 downto 0);
114
  signal wb_acc_en   : std_ulogic;
115
 
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
    CLOCK_FREQUENCY           => f_clock_nat_c, -- clock frequency of clk_i in Hz
130
    HART_ID                   => x"ABCD1234",   -- custom hardware thread ID
131
    BOOTLOADER_USE            => false,         -- implement processor-internal bootloader?
132
    -- RISC-V CPU Extensions --
133
    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
    CPU_EXTENSION_RISCV_Zicsr => true,          -- implement CSR system?
137
    -- Memory configuration: Instruction memory --
138
    MEM_ISPACE_BASE           => x"00000000",   -- base address of instruction memory space
139
    MEM_ISPACE_SIZE           => 16*1024,       -- total size of instruction memory space in byte
140
    MEM_INT_IMEM_USE          => true,          -- implement processor-internal instruction memory
141
    MEM_INT_IMEM_SIZE         => 16*1024,       -- size of processor-internal instruction memory in bytes
142
    MEM_INT_IMEM_ROM          => false,         -- implement processor-internal instruction memory as ROM
143
    -- Memory configuration: Data memory --
144
    MEM_DSPACE_BASE           => x"80000000",   -- base address of data memory space
145
    MEM_DSPACE_SIZE           => 8*1024,        -- total size of data memory space in byte
146
    MEM_INT_DMEM_USE          => true,          -- implement processor-internal data memory
147
    MEM_INT_DMEM_SIZE         => 8*1024,        -- size of processor-internal data memory in bytes
148
    -- Memory configuration: External memory interface --
149
    MEM_EXT_USE               => true,          -- implement external memory bus interface?
150
    MEM_EXT_REG_STAGES        => 2,             -- number of interface register stages (0,1,2)
151
    MEM_EXT_TIMEOUT           => 15,            -- cycles after which a valid bus access will timeout
152
    -- Processor peripherals --
153
    IO_GPIO_USE               => true,          -- implement general purpose input/output port unit (GPIO)?
154
    IO_MTIME_USE              => true,          -- implement machine system timer (MTIME)?
155
    IO_UART_USE               => true,          -- implement universal asynchronous receiver/transmitter (UART)?
156
    IO_SPI_USE                => true,          -- implement serial peripheral interface (SPI)?
157
    IO_TWI_USE                => true,          -- implement two-wire interface (TWI)?
158
    IO_PWM_USE                => true,          -- implement pulse-width modulation unit (PWM)?
159
    IO_WDT_USE                => true,          -- implement watch dog timer (WDT)?
160
    IO_CLIC_USE               => true,          -- implement core local interrupt controller (CLIC)?
161
    IO_TRNG_USE               => false          -- implement true random number generator (TRNG)?
162
  )
163
  port map (
164
    -- Global control --
165
    clk_i      => clk_gen,         -- global clock, rising edge
166
    rstn_i     => rst_gen,         -- global reset, low-active, async
167
    -- Wishbone bus interface --
168
    wb_adr_o   => wb_cpu.addr,     -- address
169
    wb_dat_i   => wb_cpu.rdata,    -- read data
170
    wb_dat_o   => wb_cpu.wdata,    -- write data
171
    wb_we_o    => wb_cpu.we,       -- read/write
172
    wb_sel_o   => wb_cpu.sel,      -- byte enable
173
    wb_stb_o   => wb_cpu.stb,      -- strobe
174
    wb_cyc_o   => wb_cpu.cyc,      -- valid cycle
175
    wb_ack_i   => wb_cpu.ack,      -- transfer acknowledge
176
    wb_err_i   => wb_cpu.err,      -- transfer error
177
    -- GPIO --
178
    gpio_o     => gpio,            -- parallel output
179
    gpio_i     => gpio,            -- parallel input
180
    -- UART --
181
    uart_txd_o => uart_txd,        -- UART send data
182
    uart_rxd_i => uart_txd,        -- UART receive data
183
    -- SPI --
184
    spi_sclk_o => open,            -- serial clock line
185
    spi_mosi_o => spi_data,        -- serial data line out
186
    spi_miso_i => spi_data,        -- serial data line in
187
    spi_csn_o  => open,            -- SPI CS
188
    -- TWI --
189
    twi_sda_io => twi_sda,         -- twi serial data line
190
    twi_scl_io => twi_scl,         -- twi serial clock line
191
    -- PWM --
192
    pwm_o      => open,            -- pwm channels
193
    -- Interrupts --
194
    ext_irq_i  => (others => '0'), -- external interrupt request
195
    ext_ack_o  => open             -- external interrupt request acknowledge
196
  );
197
 
198
  -- twi termination --
199
  twi_scl <= 'H';
200
  twi_sda <= 'H';
201
 
202
 
203
  -- Console UART Receiver ------------------------------------------------------------------
204
  -- -------------------------------------------------------------------------------------------
205
  uart_rx_console: process(clk_gen)
206
    variable i, j     : integer;
207
    variable line_tmp : line;
208
  begin
209
 
210
    -- "UART" --
211
    if rising_edge(clk_gen) then
212
      -- synchronizer --
213
      uart_rx_sync <= uart_rx_sync(3 downto 0) & uart_txd;
214
      -- arbiter --
215
      if (uart_rx_busy = '0') then -- idle
216
        uart_rx_busy     <= '0';
217
        uart_rx_baud_cnt <= round(0.5 * baud_val_c);
218
        uart_rx_bitcnt   <= 9;
219
        if (uart_rx_sync(4 downto 1) = "1100") then -- start bit? (falling edge)
220
          uart_rx_busy <= '1';
221
        end if;
222
      else
223
        if (uart_rx_baud_cnt = 0.0) then
224
          -- adapt to the inter-frame pause - which is not implemented in the neo430 uart ;)
225
          if (uart_rx_bitcnt = 1) then
226
            uart_rx_baud_cnt <= round(0.5 * baud_val_c);
227
          else
228
            uart_rx_baud_cnt <= round(baud_val_c);
229
          end if;
230
          if (uart_rx_bitcnt = 0) then
231
            uart_rx_busy <= '0'; -- done
232
            i := to_integer(unsigned(uart_rx_sreg(8 downto 1)));
233
            j := i - 32;
234
            if (j < 0) or (j > 95) then
235
              j := 0; -- undefined = SPACE
236
            end if;
237
 
238
            if (i < 32) or (j > 32+95) then
239
              report "UART TX: (" & integer'image(i) & ")"; -- print code
240
            else
241
              report "UART TX: " & ascii_lut(j); -- print ASCII
242
            end if;
243
 
244
            if (i = 10) then -- Linux line break
245
              writeline(file_uart_tx_out, line_tmp);
246
            elsif (i /= 13) then -- Remove additional carriage return
247
              write(line_tmp, ascii_lut(j));
248
            end if;
249
          else
250
            uart_rx_sreg   <= uart_rx_sync(4) & uart_rx_sreg(8 downto 1);
251
            uart_rx_bitcnt <= uart_rx_bitcnt - 1;
252
          end if;
253
        else
254
          uart_rx_baud_cnt <= uart_rx_baud_cnt - 1.0;
255
        end if;
256
      end if;
257
    end if;
258
  end process uart_rx_console;
259
 
260
 
261
  -- Wishbone Memory ------------------------------------------------------------------------
262
  -- -------------------------------------------------------------------------------------------
263
    wb_mem_file_access: process(clk_gen)
264
    begin
265
      if rising_edge(clk_gen) then
266
        rb_en <= wb_cpu.cyc and wb_cpu.stb and wb_acc_en and (not wb_cpu.we); -- read-back control
267
        wb_cpu.ack <= wb_cpu.cyc and wb_cpu.stb and wb_acc_en; -- wishbone acknowledge
268
        if ((wb_cpu.cyc and wb_cpu.stb and wb_acc_en and wb_cpu.we) = '1') then -- valid write access
269
          for i in 0 to 3 loop
270
            if (wb_cpu.sel(i) = '1') then
271
              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);
272
            end if;
273
          end loop; -- i
274
        end if;
275
        r_data <= wb_mem_file(to_integer(unsigned(wb_cpu.addr(index_size_f(wb_mem_size_c/4)+1 downto 2)))); -- word aligned
276
      end if;
277
    end process wb_mem_file_access;
278
 
279
  -- wb mem access --
280
  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';
281
 
282
  -- output gate --
283
  wb_cpu.rdata <= r_data when (rb_en = '1') else (others=> '0');
284
  wb_cpu.err <= '0';
285
 
286
end neorv32_tb_rtl;

powered by: WebSVN 2.1.0

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