| 1 |
2 |
zero_gravi |
-- #################################################################################################
|
| 2 |
36 |
zero_gravi |
-- # << NEORV32 - Default Testbench >> #
|
| 3 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
| 4 |
38 |
zero_gravi |
-- # Use the "User Configuration" section to configure the testbench according to your need. #
|
| 5 |
40 |
zero_gravi |
-- # See NEORV32 data sheet (docs/NEORV32.pdf) for more information. #
|
| 6 |
3 |
zero_gravi |
-- # ********************************************************************************************* #
|
| 7 |
2 |
zero_gravi |
-- # BSD 3-Clause License #
|
| 8 |
|
|
-- # #
|
| 9 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
| 10 |
|
|
-- # #
|
| 11 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
| 12 |
|
|
-- # permitted provided that the following conditions are met: #
|
| 13 |
|
|
-- # #
|
| 14 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
| 15 |
|
|
-- # conditions and the following disclaimer. #
|
| 16 |
|
|
-- # #
|
| 17 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
| 18 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
| 19 |
|
|
-- # provided with the distribution. #
|
| 20 |
|
|
-- # #
|
| 21 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
| 22 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
| 23 |
|
|
-- # permission. #
|
| 24 |
|
|
-- # #
|
| 25 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
| 26 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
| 27 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
| 28 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
| 29 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
| 30 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
| 31 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
| 32 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
| 33 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
| 34 |
|
|
-- # ********************************************************************************************* #
|
| 35 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
| 36 |
|
|
-- #################################################################################################
|
| 37 |
|
|
|
| 38 |
|
|
library ieee;
|
| 39 |
|
|
use ieee.std_logic_1164.all;
|
| 40 |
|
|
use ieee.numeric_std.all;
|
| 41 |
|
|
use ieee.math_real.all;
|
| 42 |
|
|
|
| 43 |
|
|
library neorv32;
|
| 44 |
|
|
use neorv32.neorv32_package.all;
|
| 45 |
30 |
zero_gravi |
use neorv32.neorv32_application_image.all; -- this file is generated by the image generator
|
| 46 |
2 |
zero_gravi |
use std.textio.all;
|
| 47 |
|
|
|
| 48 |
|
|
entity neorv32_tb is
|
| 49 |
|
|
end neorv32_tb;
|
| 50 |
|
|
|
| 51 |
|
|
architecture neorv32_tb_rtl of neorv32_tb is
|
| 52 |
|
|
|
| 53 |
|
|
-- User Configuration ---------------------------------------------------------------------
|
| 54 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 55 |
38 |
zero_gravi |
-- general --
|
| 56 |
39 |
zero_gravi |
constant ext_imem_c : boolean := false; -- false: use and boot from proc-internal IMEM, true: use and boot from external (initialized) simulated IMEM (ext. mem A)
|
| 57 |
|
|
constant ext_dmem_c : boolean := false; -- false: use proc-internal DMEM, true: use external simulated DMEM (ext. mem B)
|
| 58 |
41 |
zero_gravi |
constant icache_use_c : boolean := false; -- set true to use processor-internal instruction cache
|
| 59 |
38 |
zero_gravi |
constant imem_size_c : natural := 16*1024; -- size in bytes of processor-internal IMEM / external mem A
|
| 60 |
39 |
zero_gravi |
constant dmem_size_c : natural := 8*1024; -- size in bytes of processor-internal DMEM / external mem B
|
| 61 |
38 |
zero_gravi |
constant f_clock_c : natural := 100000000; -- main clock in Hz
|
| 62 |
39 |
zero_gravi |
constant baud_rate_c : natural := 19200; -- simulation UART output baudrate
|
| 63 |
38 |
zero_gravi |
-- simulated external Wishbone memory A (can be used as external IMEM) --
|
| 64 |
39 |
zero_gravi |
constant ext_mem_a_base_addr_c : std_ulogic_vector(31 downto 0) := x"00000000"; -- wishbone memory base address (external IMEM base)
|
| 65 |
38 |
zero_gravi |
constant ext_mem_a_size_c : natural := imem_size_c; -- wishbone memory size in bytes
|
| 66 |
40 |
zero_gravi |
constant ext_mem_a_latency_c : natural := 8; -- latency in clock cycles (min 1, max 255), plus 1 cycle initial delay
|
| 67 |
39 |
zero_gravi |
-- simulated external Wishbone memory B (can be used as external DMEM) --
|
| 68 |
|
|
constant ext_mem_b_base_addr_c : std_ulogic_vector(31 downto 0) := x"80000000"; -- wishbone memory base address (external DMEM base)
|
| 69 |
|
|
constant ext_mem_b_size_c : natural := dmem_size_c; -- wishbone memory size in bytes
|
| 70 |
40 |
zero_gravi |
constant ext_mem_b_latency_c : natural := 8; -- latency in clock cycles (min 1, max 255), plus 1 cycle initial delay
|
| 71 |
39 |
zero_gravi |
-- simulated external Wishbone memory C (can be used as external IO) --
|
| 72 |
|
|
constant ext_mem_c_base_addr_c : std_ulogic_vector(31 downto 0) := x"F0000000"; -- wishbone memory base address (default begin of EXTERNAL IO area)
|
| 73 |
|
|
constant ext_mem_c_size_c : natural := 64; -- wishbone memory size in bytes
|
| 74 |
40 |
zero_gravi |
constant ext_mem_c_latency_c : natural := 3; -- latency in clock cycles (min 1, max 255), plus 1 cycle initial delay
|
| 75 |
|
|
-- machine interrupt triggers --
|
| 76 |
|
|
constant msi_trigger_c : std_ulogic_vector(31 downto 0) := x"FF000000"; -- machine software interrupt
|
| 77 |
|
|
constant mei_trigger_c : std_ulogic_vector(31 downto 0) := x"FF000004"; -- machine external interrupt
|
| 78 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 79 |
|
|
|
| 80 |
38 |
zero_gravi |
-- internals - hands off! --
|
| 81 |
39 |
zero_gravi |
constant int_imem_c : boolean := not ext_imem_c;
|
| 82 |
|
|
constant int_dmem_c : boolean := not ext_dmem_c;
|
| 83 |
|
|
constant baud_val_c : real := real(f_clock_c) / real(baud_rate_c);
|
| 84 |
|
|
constant t_clock_c : time := (1 sec) / f_clock_c;
|
| 85 |
38 |
zero_gravi |
|
| 86 |
3 |
zero_gravi |
-- text.io --
|
| 87 |
|
|
file file_uart_tx_out : text open write_mode is "neorv32.testbench_uart.out";
|
| 88 |
2 |
zero_gravi |
|
| 89 |
|
|
-- generators --
|
| 90 |
|
|
signal clk_gen, rst_gen : std_ulogic := '0';
|
| 91 |
|
|
|
| 92 |
|
|
-- simulation uart receiver --
|
| 93 |
|
|
signal uart_txd : std_ulogic;
|
| 94 |
|
|
signal uart_rx_sync : std_ulogic_vector(04 downto 0) := (others => '1');
|
| 95 |
|
|
signal uart_rx_busy : std_ulogic := '0';
|
| 96 |
|
|
signal uart_rx_sreg : std_ulogic_vector(08 downto 0) := (others => '0');
|
| 97 |
|
|
signal uart_rx_baud_cnt : real;
|
| 98 |
|
|
signal uart_rx_bitcnt : natural;
|
| 99 |
|
|
|
| 100 |
|
|
-- gpio --
|
| 101 |
22 |
zero_gravi |
signal gpio : std_ulogic_vector(31 downto 0);
|
| 102 |
2 |
zero_gravi |
|
| 103 |
|
|
-- twi --
|
| 104 |
|
|
signal twi_scl, twi_sda : std_logic;
|
| 105 |
|
|
|
| 106 |
|
|
-- spi --
|
| 107 |
40 |
zero_gravi |
signal spi_data : std_ulogic;
|
| 108 |
2 |
zero_gravi |
|
| 109 |
40 |
zero_gravi |
-- irq --
|
| 110 |
|
|
signal msi_ring, mei_ring : std_ulogic;
|
| 111 |
|
|
|
| 112 |
2 |
zero_gravi |
-- Wishbone bus --
|
| 113 |
|
|
type wishbone_t is record
|
| 114 |
|
|
addr : std_ulogic_vector(31 downto 0); -- address
|
| 115 |
|
|
wdata : std_ulogic_vector(31 downto 0); -- master write data
|
| 116 |
|
|
rdata : std_ulogic_vector(31 downto 0); -- master read data
|
| 117 |
|
|
we : std_ulogic; -- write enable
|
| 118 |
|
|
sel : std_ulogic_vector(03 downto 0); -- byte enable
|
| 119 |
|
|
stb : std_ulogic; -- strobe
|
| 120 |
|
|
cyc : std_ulogic; -- valid cycle
|
| 121 |
|
|
ack : std_ulogic; -- transfer acknowledge
|
| 122 |
|
|
err : std_ulogic; -- transfer error
|
| 123 |
36 |
zero_gravi |
tag : std_ulogic_vector(2 downto 0); -- tag
|
| 124 |
39 |
zero_gravi |
lock : std_ulogic; -- locked/exclusive bus access
|
| 125 |
2 |
zero_gravi |
end record;
|
| 126 |
40 |
zero_gravi |
signal wb_cpu, wb_mem_a, wb_mem_b, wb_mem_c, wb_msi, wb_mei : wishbone_t;
|
| 127 |
2 |
zero_gravi |
|
| 128 |
38 |
zero_gravi |
-- Wishbone memories --
|
| 129 |
|
|
type ext_mem_a_ram_t is array (0 to ext_mem_a_size_c/4-1) of std_ulogic_vector(31 downto 0);
|
| 130 |
|
|
type ext_mem_b_ram_t is array (0 to ext_mem_b_size_c/4-1) of std_ulogic_vector(31 downto 0);
|
| 131 |
39 |
zero_gravi |
type ext_mem_c_ram_t is array (0 to ext_mem_c_size_c/4-1) of std_ulogic_vector(31 downto 0);
|
| 132 |
38 |
zero_gravi |
type ext_mem_read_latency_t is array (0 to 255) of std_ulogic_vector(31 downto 0);
|
| 133 |
30 |
zero_gravi |
|
| 134 |
|
|
-- init function --
|
| 135 |
|
|
-- impure function: returns NOT the same result every time it is evaluated with the same arguments since the source file might have changed
|
| 136 |
38 |
zero_gravi |
impure function init_wbmem(init : application_init_image_t) return ext_mem_a_ram_t is
|
| 137 |
|
|
variable mem_v : ext_mem_a_ram_t;
|
| 138 |
30 |
zero_gravi |
begin
|
| 139 |
|
|
mem_v := (others => (others => '0'));
|
| 140 |
|
|
for i in 0 to init'length-1 loop -- init only in range of source data array
|
| 141 |
40 |
zero_gravi |
if (xbus_big_endian_c = true) then
|
| 142 |
30 |
zero_gravi |
mem_v(i) := init(i);
|
| 143 |
40 |
zero_gravi |
else
|
| 144 |
|
|
mem_v(i) := bswap32_f(init(i));
|
| 145 |
|
|
end if;
|
| 146 |
30 |
zero_gravi |
end loop; -- i
|
| 147 |
|
|
return mem_v;
|
| 148 |
|
|
end function init_wbmem;
|
| 149 |
|
|
|
| 150 |
38 |
zero_gravi |
-- external memory components --
|
| 151 |
39 |
zero_gravi |
signal ext_ram_a : ext_mem_a_ram_t := init_wbmem(application_init_image); -- initialized, used to simulate external IMEM
|
| 152 |
|
|
signal ext_ram_b : ext_mem_b_ram_t := (others => (others => '0')); -- zero, used to simulate external DMEM
|
| 153 |
|
|
signal ext_ram_c : ext_mem_c_ram_t; -- uninitialized, used to simulate external IO
|
| 154 |
30 |
zero_gravi |
|
| 155 |
38 |
zero_gravi |
type ext_mem_t is record
|
| 156 |
|
|
rdata : ext_mem_read_latency_t;
|
| 157 |
23 |
zero_gravi |
acc_en : std_ulogic;
|
| 158 |
38 |
zero_gravi |
ack : std_ulogic_vector(ext_mem_a_latency_c-1 downto 0);
|
| 159 |
23 |
zero_gravi |
end record;
|
| 160 |
39 |
zero_gravi |
signal ext_mem_a, ext_mem_b, ext_mem_c : ext_mem_t;
|
| 161 |
2 |
zero_gravi |
|
| 162 |
|
|
begin
|
| 163 |
|
|
|
| 164 |
|
|
-- Clock/Reset Generator ------------------------------------------------------------------
|
| 165 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 166 |
|
|
clk_gen <= not clk_gen after (t_clock_c/2);
|
| 167 |
|
|
rst_gen <= '0', '1' after 60*(t_clock_c/2);
|
| 168 |
|
|
|
| 169 |
|
|
|
| 170 |
|
|
-- CPU Core -------------------------------------------------------------------------------
|
| 171 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 172 |
|
|
neorv32_top_inst: neorv32_top
|
| 173 |
|
|
generic map (
|
| 174 |
|
|
-- General --
|
| 175 |
38 |
zero_gravi |
CLOCK_FREQUENCY => f_clock_c, -- clock frequency of clk_i in Hz
|
| 176 |
8 |
zero_gravi |
BOOTLOADER_USE => false, -- implement processor-internal bootloader?
|
| 177 |
36 |
zero_gravi |
USER_CODE => x"12345678", -- custom user code
|
| 178 |
|
|
HW_THREAD_ID => x"00000000", -- hardware thread id (hartid)
|
| 179 |
2 |
zero_gravi |
-- RISC-V CPU Extensions --
|
| 180 |
39 |
zero_gravi |
CPU_EXTENSION_RISCV_A => true, -- implement atomic extension?
|
| 181 |
8 |
zero_gravi |
CPU_EXTENSION_RISCV_C => true, -- implement compressed extension?
|
| 182 |
|
|
CPU_EXTENSION_RISCV_E => false, -- implement embedded RF extension?
|
| 183 |
|
|
CPU_EXTENSION_RISCV_M => true, -- implement muld/div extension?
|
| 184 |
15 |
zero_gravi |
CPU_EXTENSION_RISCV_U => true, -- implement user mode extension?
|
| 185 |
8 |
zero_gravi |
CPU_EXTENSION_RISCV_Zicsr => true, -- implement CSR system?
|
| 186 |
|
|
CPU_EXTENSION_RISCV_Zifencei => true, -- implement instruction stream sync.?
|
| 187 |
19 |
zero_gravi |
-- Extension Options --
|
| 188 |
|
|
FAST_MUL_EN => false, -- use DSPs for M extension's multiplier
|
| 189 |
34 |
zero_gravi |
FAST_SHIFT_EN => false, -- use barrel shifter for shift operations
|
| 190 |
15 |
zero_gravi |
-- Physical Memory Protection (PMP) --
|
| 191 |
|
|
PMP_USE => true, -- implement PMP?
|
| 192 |
23 |
zero_gravi |
-- Internal Instruction memory --
|
| 193 |
39 |
zero_gravi |
MEM_INT_IMEM_USE => int_imem_c , -- implement processor-internal instruction memory
|
| 194 |
38 |
zero_gravi |
MEM_INT_IMEM_SIZE => imem_size_c, -- size of processor-internal instruction memory in bytes
|
| 195 |
8 |
zero_gravi |
MEM_INT_IMEM_ROM => false, -- implement processor-internal instruction memory as ROM
|
| 196 |
23 |
zero_gravi |
-- Internal Data memory --
|
| 197 |
39 |
zero_gravi |
MEM_INT_DMEM_USE => int_dmem_c, -- implement processor-internal data memory
|
| 198 |
|
|
MEM_INT_DMEM_SIZE => dmem_size_c, -- size of processor-internal data memory in bytes
|
| 199 |
41 |
zero_gravi |
-- Internal Cache memory --
|
| 200 |
|
|
ICACHE_USE => icache_use_c, -- implement instruction cache
|
| 201 |
|
|
ICACHE_NUM_BLOCKS => 8, -- i-cache: number of blocks (min 2), has to be a power of 2
|
| 202 |
|
|
ICACHE_BLOCK_SIZE => 64, -- i-cache: block size in bytes (min 4), has to be a power of 2
|
| 203 |
23 |
zero_gravi |
-- External memory interface --
|
| 204 |
8 |
zero_gravi |
MEM_EXT_USE => true, -- implement external memory bus interface?
|
| 205 |
2 |
zero_gravi |
-- Processor peripherals --
|
| 206 |
8 |
zero_gravi |
IO_GPIO_USE => true, -- implement general purpose input/output port unit (GPIO)?
|
| 207 |
|
|
IO_MTIME_USE => true, -- implement machine system timer (MTIME)?
|
| 208 |
|
|
IO_UART_USE => true, -- implement universal asynchronous receiver/transmitter (UART)?
|
| 209 |
|
|
IO_SPI_USE => true, -- implement serial peripheral interface (SPI)?
|
| 210 |
|
|
IO_TWI_USE => true, -- implement two-wire interface (TWI)?
|
| 211 |
|
|
IO_PWM_USE => true, -- implement pulse-width modulation unit (PWM)?
|
| 212 |
|
|
IO_WDT_USE => true, -- implement watch dog timer (WDT)?
|
| 213 |
39 |
zero_gravi |
IO_TRNG_USE => false, -- trng cannot be simulated
|
| 214 |
34 |
zero_gravi |
IO_CFU0_USE => true, -- implement custom functions unit 0 (CFU0)?
|
| 215 |
|
|
IO_CFU1_USE => true -- implement custom functions unit 1 (CFU1)?
|
| 216 |
2 |
zero_gravi |
)
|
| 217 |
|
|
port map (
|
| 218 |
|
|
-- Global control --
|
| 219 |
34 |
zero_gravi |
clk_i => clk_gen, -- global clock, rising edge
|
| 220 |
|
|
rstn_i => rst_gen, -- global reset, low-active, async
|
| 221 |
2 |
zero_gravi |
-- Wishbone bus interface --
|
| 222 |
36 |
zero_gravi |
wb_tag_o => wb_cpu.tag, -- tag
|
| 223 |
34 |
zero_gravi |
wb_adr_o => wb_cpu.addr, -- address
|
| 224 |
|
|
wb_dat_i => wb_cpu.rdata, -- read data
|
| 225 |
|
|
wb_dat_o => wb_cpu.wdata, -- write data
|
| 226 |
|
|
wb_we_o => wb_cpu.we, -- read/write
|
| 227 |
|
|
wb_sel_o => wb_cpu.sel, -- byte enable
|
| 228 |
|
|
wb_stb_o => wb_cpu.stb, -- strobe
|
| 229 |
|
|
wb_cyc_o => wb_cpu.cyc, -- valid cycle
|
| 230 |
39 |
zero_gravi |
wb_lock_o => wb_cpu.lock, -- locked/exclusive bus access
|
| 231 |
34 |
zero_gravi |
wb_ack_i => wb_cpu.ack, -- transfer acknowledge
|
| 232 |
|
|
wb_err_i => wb_cpu.err, -- transfer error
|
| 233 |
12 |
zero_gravi |
-- Advanced memory control signals --
|
| 234 |
34 |
zero_gravi |
fence_o => open, -- indicates an executed FENCE operation
|
| 235 |
|
|
fencei_o => open, -- indicates an executed FENCEI operation
|
| 236 |
2 |
zero_gravi |
-- GPIO --
|
| 237 |
34 |
zero_gravi |
gpio_o => gpio, -- parallel output
|
| 238 |
|
|
gpio_i => gpio, -- parallel input
|
| 239 |
2 |
zero_gravi |
-- UART --
|
| 240 |
34 |
zero_gravi |
uart_txd_o => uart_txd, -- UART send data
|
| 241 |
|
|
uart_rxd_i => uart_txd, -- UART receive data
|
| 242 |
2 |
zero_gravi |
-- SPI --
|
| 243 |
34 |
zero_gravi |
spi_sck_o => open, -- SPI serial clock
|
| 244 |
|
|
spi_sdo_o => spi_data, -- controller data out, peripheral data in
|
| 245 |
|
|
spi_sdi_i => spi_data, -- controller data in, peripheral data out
|
| 246 |
|
|
spi_csn_o => open, -- SPI CS
|
| 247 |
2 |
zero_gravi |
-- TWI --
|
| 248 |
34 |
zero_gravi |
twi_sda_io => twi_sda, -- twi serial data line
|
| 249 |
|
|
twi_scl_io => twi_scl, -- twi serial clock line
|
| 250 |
2 |
zero_gravi |
-- PWM --
|
| 251 |
34 |
zero_gravi |
pwm_o => open, -- pwm channels
|
| 252 |
40 |
zero_gravi |
-- system time input from external MTIME (available if IO_MTIME_USE = false) --
|
| 253 |
|
|
mtime_i => (others => '0'), -- current system time
|
| 254 |
2 |
zero_gravi |
-- Interrupts --
|
| 255 |
34 |
zero_gravi |
mtime_irq_i => '0', -- machine software interrupt, available if IO_MTIME_USE = false
|
| 256 |
40 |
zero_gravi |
msw_irq_i => msi_ring, -- machine software interrupt
|
| 257 |
|
|
mext_irq_i => mei_ring -- machine external interrupt
|
| 258 |
2 |
zero_gravi |
);
|
| 259 |
|
|
|
| 260 |
36 |
zero_gravi |
-- TWI termination (pull-ups) --
|
| 261 |
2 |
zero_gravi |
twi_scl <= 'H';
|
| 262 |
|
|
twi_sda <= 'H';
|
| 263 |
|
|
|
| 264 |
|
|
|
| 265 |
|
|
-- Console UART Receiver ------------------------------------------------------------------
|
| 266 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 267 |
|
|
uart_rx_console: process(clk_gen)
|
| 268 |
3 |
zero_gravi |
variable i : integer;
|
| 269 |
|
|
variable l : line;
|
| 270 |
2 |
zero_gravi |
begin
|
| 271 |
|
|
-- "UART" --
|
| 272 |
|
|
if rising_edge(clk_gen) then
|
| 273 |
|
|
-- synchronizer --
|
| 274 |
|
|
uart_rx_sync <= uart_rx_sync(3 downto 0) & uart_txd;
|
| 275 |
|
|
-- arbiter --
|
| 276 |
|
|
if (uart_rx_busy = '0') then -- idle
|
| 277 |
|
|
uart_rx_busy <= '0';
|
| 278 |
|
|
uart_rx_baud_cnt <= round(0.5 * baud_val_c);
|
| 279 |
|
|
uart_rx_bitcnt <= 9;
|
| 280 |
|
|
if (uart_rx_sync(4 downto 1) = "1100") then -- start bit? (falling edge)
|
| 281 |
|
|
uart_rx_busy <= '1';
|
| 282 |
|
|
end if;
|
| 283 |
|
|
else
|
| 284 |
38 |
zero_gravi |
if (uart_rx_baud_cnt <= 0.0) then
|
| 285 |
2 |
zero_gravi |
if (uart_rx_bitcnt = 1) then
|
| 286 |
|
|
uart_rx_baud_cnt <= round(0.5 * baud_val_c);
|
| 287 |
|
|
else
|
| 288 |
|
|
uart_rx_baud_cnt <= round(baud_val_c);
|
| 289 |
|
|
end if;
|
| 290 |
|
|
if (uart_rx_bitcnt = 0) then
|
| 291 |
|
|
uart_rx_busy <= '0'; -- done
|
| 292 |
|
|
i := to_integer(unsigned(uart_rx_sreg(8 downto 1)));
|
| 293 |
|
|
|
| 294 |
3 |
zero_gravi |
if (i < 32) or (i > 32+95) then -- printable char?
|
| 295 |
33 |
zero_gravi |
report "NEORV32_TB_UART.TX: (" & integer'image(i) & ")"; -- print code
|
| 296 |
2 |
zero_gravi |
else
|
| 297 |
33 |
zero_gravi |
report "NEORV32_TB_UART.TX: " & character'val(i); -- print ASCII
|
| 298 |
2 |
zero_gravi |
end if;
|
| 299 |
|
|
|
| 300 |
|
|
if (i = 10) then -- Linux line break
|
| 301 |
3 |
zero_gravi |
writeline(file_uart_tx_out, l);
|
| 302 |
2 |
zero_gravi |
elsif (i /= 13) then -- Remove additional carriage return
|
| 303 |
3 |
zero_gravi |
write(l, character'val(i));
|
| 304 |
2 |
zero_gravi |
end if;
|
| 305 |
|
|
else
|
| 306 |
|
|
uart_rx_sreg <= uart_rx_sync(4) & uart_rx_sreg(8 downto 1);
|
| 307 |
|
|
uart_rx_bitcnt <= uart_rx_bitcnt - 1;
|
| 308 |
|
|
end if;
|
| 309 |
|
|
else
|
| 310 |
|
|
uart_rx_baud_cnt <= uart_rx_baud_cnt - 1.0;
|
| 311 |
|
|
end if;
|
| 312 |
|
|
end if;
|
| 313 |
|
|
end if;
|
| 314 |
|
|
end process uart_rx_console;
|
| 315 |
|
|
|
| 316 |
|
|
|
| 317 |
38 |
zero_gravi |
-- Wishbone Fabric ------------------------------------------------------------------------
|
| 318 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 319 |
38 |
zero_gravi |
-- CPU broadcast signals --
|
| 320 |
|
|
wb_mem_a.addr <= wb_cpu.addr;
|
| 321 |
39 |
zero_gravi |
wb_mem_a.wdata <= wb_cpu.wdata;
|
| 322 |
|
|
wb_mem_a.we <= wb_cpu.we;
|
| 323 |
|
|
wb_mem_a.sel <= wb_cpu.sel;
|
| 324 |
|
|
wb_mem_a.tag <= wb_cpu.tag;
|
| 325 |
|
|
wb_mem_a.cyc <= wb_cpu.cyc;
|
| 326 |
|
|
wb_mem_a.lock <= wb_cpu.lock;
|
| 327 |
|
|
|
| 328 |
38 |
zero_gravi |
wb_mem_b.addr <= wb_cpu.addr;
|
| 329 |
|
|
wb_mem_b.wdata <= wb_cpu.wdata;
|
| 330 |
|
|
wb_mem_b.we <= wb_cpu.we;
|
| 331 |
|
|
wb_mem_b.sel <= wb_cpu.sel;
|
| 332 |
|
|
wb_mem_b.tag <= wb_cpu.tag;
|
| 333 |
|
|
wb_mem_b.cyc <= wb_cpu.cyc;
|
| 334 |
39 |
zero_gravi |
wb_mem_b.lock <= wb_cpu.lock;
|
| 335 |
|
|
|
| 336 |
|
|
wb_mem_c.addr <= wb_cpu.addr;
|
| 337 |
|
|
wb_mem_c.wdata <= wb_cpu.wdata;
|
| 338 |
|
|
wb_mem_c.we <= wb_cpu.we;
|
| 339 |
|
|
wb_mem_c.sel <= wb_cpu.sel;
|
| 340 |
|
|
wb_mem_c.tag <= wb_cpu.tag;
|
| 341 |
|
|
wb_mem_c.cyc <= wb_cpu.cyc;
|
| 342 |
|
|
wb_mem_c.lock <= wb_cpu.lock;
|
| 343 |
|
|
|
| 344 |
40 |
zero_gravi |
wb_msi.addr <= wb_cpu.addr;
|
| 345 |
|
|
wb_msi.wdata <= wb_cpu.wdata;
|
| 346 |
|
|
wb_msi.we <= wb_cpu.we;
|
| 347 |
|
|
wb_msi.sel <= wb_cpu.sel;
|
| 348 |
|
|
wb_msi.tag <= wb_cpu.tag;
|
| 349 |
|
|
wb_msi.cyc <= wb_cpu.cyc;
|
| 350 |
|
|
wb_msi.lock <= wb_cpu.lock;
|
| 351 |
|
|
|
| 352 |
|
|
wb_mei.addr <= wb_cpu.addr;
|
| 353 |
|
|
wb_mei.wdata <= wb_cpu.wdata;
|
| 354 |
|
|
wb_mei.we <= wb_cpu.we;
|
| 355 |
|
|
wb_mei.sel <= wb_cpu.sel;
|
| 356 |
|
|
wb_mei.tag <= wb_cpu.tag;
|
| 357 |
|
|
wb_mei.cyc <= wb_cpu.cyc;
|
| 358 |
|
|
wb_mei.lock <= wb_cpu.lock;
|
| 359 |
|
|
|
| 360 |
38 |
zero_gravi |
-- CPU read-back signals (no mux here since peripherals have "output gates") --
|
| 361 |
40 |
zero_gravi |
wb_cpu.rdata <= wb_mem_a.rdata or wb_mem_b.rdata or wb_mem_c.rdata or wb_mei.rdata or wb_msi.rdata;
|
| 362 |
|
|
wb_cpu.ack <= wb_mem_a.ack or wb_mem_b.ack or wb_mem_c.ack or wb_mei.ack or wb_msi.ack;
|
| 363 |
|
|
wb_cpu.err <= wb_mem_a.err or wb_mem_b.err or wb_mem_c.err or wb_mei.err or wb_msi.err;
|
| 364 |
38 |
zero_gravi |
|
| 365 |
|
|
-- peripheral select via STROBE signal --
|
| 366 |
|
|
wb_mem_a.stb <= wb_cpu.stb when (wb_cpu.addr >= ext_mem_a_base_addr_c) and (wb_cpu.addr < std_ulogic_vector(unsigned(ext_mem_a_base_addr_c) + ext_mem_a_size_c)) else '0';
|
| 367 |
|
|
wb_mem_b.stb <= wb_cpu.stb when (wb_cpu.addr >= ext_mem_b_base_addr_c) and (wb_cpu.addr < std_ulogic_vector(unsigned(ext_mem_b_base_addr_c) + ext_mem_b_size_c)) else '0';
|
| 368 |
39 |
zero_gravi |
wb_mem_c.stb <= wb_cpu.stb when (wb_cpu.addr >= ext_mem_c_base_addr_c) and (wb_cpu.addr < std_ulogic_vector(unsigned(ext_mem_c_base_addr_c) + ext_mem_c_size_c)) else '0';
|
| 369 |
40 |
zero_gravi |
wb_msi.stb <= wb_cpu.stb when (wb_cpu.addr = msi_trigger_c) else '0';
|
| 370 |
|
|
wb_mei.stb <= wb_cpu.stb when (wb_cpu.addr = mei_trigger_c) else '0';
|
| 371 |
38 |
zero_gravi |
|
| 372 |
|
|
|
| 373 |
39 |
zero_gravi |
-- Wishbone Memory A (simulated external IMEM) --------------------------------------------
|
| 374 |
38 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 375 |
|
|
ext_mem_a_access: process(clk_gen)
|
| 376 |
23 |
zero_gravi |
begin
|
| 377 |
|
|
if rising_edge(clk_gen) then
|
| 378 |
|
|
-- control --
|
| 379 |
38 |
zero_gravi |
ext_mem_a.ack(0) <= wb_mem_a.cyc and wb_mem_a.stb; -- wishbone acknowledge
|
| 380 |
|
|
|
| 381 |
23 |
zero_gravi |
-- write access --
|
| 382 |
38 |
zero_gravi |
if ((wb_mem_a.cyc and wb_mem_a.stb and wb_mem_a.we) = '1') then -- valid write access
|
| 383 |
23 |
zero_gravi |
for i in 0 to 3 loop
|
| 384 |
38 |
zero_gravi |
if (wb_mem_a.sel(i) = '1') then
|
| 385 |
|
|
ext_ram_a(to_integer(unsigned(wb_mem_a.addr(index_size_f(ext_mem_a_size_c/4)+1 downto 2))))(7+i*8 downto 0+i*8) <= wb_mem_a.wdata(7+i*8 downto 0+i*8);
|
| 386 |
23 |
zero_gravi |
end if;
|
| 387 |
|
|
end loop; -- i
|
| 388 |
2 |
zero_gravi |
end if;
|
| 389 |
38 |
zero_gravi |
|
| 390 |
23 |
zero_gravi |
-- read access --
|
| 391 |
38 |
zero_gravi |
ext_mem_a.rdata(0) <= ext_ram_a(to_integer(unsigned(wb_mem_a.addr(index_size_f(ext_mem_a_size_c/4)+1 downto 2)))); -- word aligned
|
| 392 |
23 |
zero_gravi |
-- virtual read and ack latency --
|
| 393 |
38 |
zero_gravi |
if (ext_mem_a_latency_c > 1) then
|
| 394 |
|
|
for i in 1 to ext_mem_a_latency_c-1 loop
|
| 395 |
|
|
ext_mem_a.rdata(i) <= ext_mem_a.rdata(i-1);
|
| 396 |
|
|
ext_mem_a.ack(i) <= ext_mem_a.ack(i-1) and wb_mem_a.cyc;
|
| 397 |
23 |
zero_gravi |
end loop;
|
| 398 |
|
|
end if;
|
| 399 |
38 |
zero_gravi |
|
| 400 |
|
|
-- bus output register --
|
| 401 |
|
|
wb_mem_a.err <= '0';
|
| 402 |
|
|
if (ext_mem_a.ack(ext_mem_a_latency_c-1) = '1') and (wb_mem_b.cyc = '1') then
|
| 403 |
|
|
wb_mem_a.rdata <= ext_mem_a.rdata(ext_mem_a_latency_c-1);
|
| 404 |
|
|
wb_mem_a.ack <= '1';
|
| 405 |
|
|
else
|
| 406 |
|
|
wb_mem_a.rdata <= (others => '0');
|
| 407 |
|
|
wb_mem_a.ack <= '0';
|
| 408 |
|
|
end if;
|
| 409 |
23 |
zero_gravi |
end if;
|
| 410 |
38 |
zero_gravi |
end process ext_mem_a_access;
|
| 411 |
2 |
zero_gravi |
|
| 412 |
|
|
|
| 413 |
39 |
zero_gravi |
-- Wishbone Memory B (simulated external DMEM) --------------------------------------------
|
| 414 |
38 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
| 415 |
|
|
ext_mem_b_access: process(clk_gen)
|
| 416 |
|
|
begin
|
| 417 |
|
|
if rising_edge(clk_gen) then
|
| 418 |
|
|
-- control --
|
| 419 |
|
|
ext_mem_b.ack(0) <= wb_mem_b.cyc and wb_mem_b.stb; -- wishbone acknowledge
|
| 420 |
2 |
zero_gravi |
|
| 421 |
38 |
zero_gravi |
-- write access --
|
| 422 |
|
|
if ((wb_mem_b.cyc and wb_mem_b.stb and wb_mem_b.we) = '1') then -- valid write access
|
| 423 |
|
|
for i in 0 to 3 loop
|
| 424 |
|
|
if (wb_mem_b.sel(i) = '1') then
|
| 425 |
|
|
ext_ram_b(to_integer(unsigned(wb_mem_b.addr(index_size_f(ext_mem_b_size_c/4)+1 downto 2))))(7+i*8 downto 0+i*8) <= wb_mem_b.wdata(7+i*8 downto 0+i*8);
|
| 426 |
|
|
end if;
|
| 427 |
|
|
end loop; -- i
|
| 428 |
|
|
end if;
|
| 429 |
3 |
zero_gravi |
|
| 430 |
38 |
zero_gravi |
-- read access --
|
| 431 |
|
|
ext_mem_b.rdata(0) <= ext_ram_b(to_integer(unsigned(wb_mem_b.addr(index_size_f(ext_mem_b_size_c/4)+1 downto 2)))); -- word aligned
|
| 432 |
|
|
-- virtual read and ack latency --
|
| 433 |
|
|
if (ext_mem_b_latency_c > 1) then
|
| 434 |
|
|
for i in 1 to ext_mem_b_latency_c-1 loop
|
| 435 |
|
|
ext_mem_b.rdata(i) <= ext_mem_b.rdata(i-1);
|
| 436 |
|
|
ext_mem_b.ack(i) <= ext_mem_b.ack(i-1) and wb_mem_b.cyc;
|
| 437 |
|
|
end loop;
|
| 438 |
|
|
end if;
|
| 439 |
|
|
|
| 440 |
|
|
-- bus output register --
|
| 441 |
|
|
wb_mem_b.err <= '0';
|
| 442 |
|
|
if (ext_mem_b.ack(ext_mem_b_latency_c-1) = '1') and (wb_mem_b.cyc = '1') then
|
| 443 |
|
|
wb_mem_b.rdata <= ext_mem_b.rdata(ext_mem_b_latency_c-1);
|
| 444 |
|
|
wb_mem_b.ack <= '1';
|
| 445 |
|
|
else
|
| 446 |
|
|
wb_mem_b.rdata <= (others => '0');
|
| 447 |
|
|
wb_mem_b.ack <= '0';
|
| 448 |
|
|
end if;
|
| 449 |
|
|
end if;
|
| 450 |
|
|
end process ext_mem_b_access;
|
| 451 |
|
|
|
| 452 |
|
|
|
| 453 |
39 |
zero_gravi |
-- Wishbone Memory C (simulated external IO) ----------------------------------------------
|
| 454 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 455 |
|
|
ext_mem_c_access: process(clk_gen)
|
| 456 |
|
|
begin
|
| 457 |
|
|
if rising_edge(clk_gen) then
|
| 458 |
|
|
-- control --
|
| 459 |
|
|
ext_mem_c.ack(0) <= wb_mem_c.cyc and wb_mem_c.stb; -- wishbone acknowledge
|
| 460 |
|
|
|
| 461 |
|
|
-- write access --
|
| 462 |
|
|
if ((wb_mem_c.cyc and wb_mem_c.stb and wb_mem_c.we) = '1') then -- valid write access
|
| 463 |
|
|
for i in 0 to 3 loop
|
| 464 |
|
|
if (wb_mem_c.sel(i) = '1') then
|
| 465 |
|
|
ext_ram_c(to_integer(unsigned(wb_mem_c.addr(index_size_f(ext_mem_c_size_c/4)+1 downto 2))))(7+i*8 downto 0+i*8) <= wb_mem_c.wdata(7+i*8 downto 0+i*8);
|
| 466 |
|
|
end if;
|
| 467 |
|
|
end loop; -- i
|
| 468 |
|
|
end if;
|
| 469 |
|
|
|
| 470 |
|
|
-- read access --
|
| 471 |
|
|
ext_mem_c.rdata(0) <= ext_ram_c(to_integer(unsigned(wb_mem_c.addr(index_size_f(ext_mem_c_size_c/4)+1 downto 2)))); -- word aligned
|
| 472 |
|
|
-- virtual read and ack latency --
|
| 473 |
|
|
if (ext_mem_c_latency_c > 1) then
|
| 474 |
|
|
for i in 1 to ext_mem_c_latency_c-1 loop
|
| 475 |
|
|
ext_mem_c.rdata(i) <= ext_mem_c.rdata(i-1);
|
| 476 |
|
|
ext_mem_c.ack(i) <= ext_mem_c.ack(i-1) and wb_mem_c.cyc;
|
| 477 |
|
|
end loop;
|
| 478 |
|
|
end if;
|
| 479 |
|
|
|
| 480 |
|
|
-- error to simulate interrupted LOCKED/EXCLUSIVE bus access --
|
| 481 |
|
|
wb_mem_c.err <= wb_mem_c.cyc and wb_mem_c.stb and wb_mem_c.lock and wb_mem_c.addr(2); -- locked access to odd word-addresses will fail
|
| 482 |
|
|
|
| 483 |
|
|
-- bus output register --
|
| 484 |
|
|
if (ext_mem_c.ack(ext_mem_c_latency_c-1) = '1') and (wb_mem_c.cyc = '1') then
|
| 485 |
|
|
wb_mem_c.rdata <= ext_mem_c.rdata(ext_mem_c_latency_c-1);
|
| 486 |
|
|
wb_mem_c.ack <= '1';
|
| 487 |
|
|
else
|
| 488 |
|
|
wb_mem_c.rdata <= (others => '0');
|
| 489 |
|
|
wb_mem_c.ack <= '0';
|
| 490 |
|
|
end if;
|
| 491 |
|
|
end if;
|
| 492 |
|
|
end process ext_mem_c_access;
|
| 493 |
|
|
|
| 494 |
|
|
|
| 495 |
40 |
zero_gravi |
-- Wishbone IRQ Triggers ------------------------------------------------------------------
|
| 496 |
|
|
-- -------------------------------------------------------------------------------------------
|
| 497 |
|
|
ext_irq_trigger: process(clk_gen)
|
| 498 |
|
|
begin
|
| 499 |
|
|
if rising_edge(clk_gen) then
|
| 500 |
|
|
-- default --
|
| 501 |
|
|
msi_ring <= '0';
|
| 502 |
|
|
wb_msi.rdata <= (others => '0');
|
| 503 |
|
|
wb_msi.ack <= '0';
|
| 504 |
|
|
wb_msi.err <= '0';
|
| 505 |
|
|
mei_ring <= '0';
|
| 506 |
|
|
wb_mei.rdata <= (others => '0');
|
| 507 |
|
|
wb_mei.ack <= '0';
|
| 508 |
|
|
wb_mei.err <= '0';
|
| 509 |
|
|
|
| 510 |
|
|
-- machine software interrupt --
|
| 511 |
|
|
if ((wb_msi.cyc and wb_msi.stb and wb_msi.we) = '1') then
|
| 512 |
|
|
msi_ring <= '1';
|
| 513 |
|
|
wb_msi.ack <= '1';
|
| 514 |
|
|
end if;
|
| 515 |
|
|
|
| 516 |
|
|
-- machine external interrupt --
|
| 517 |
|
|
if ((wb_mei.cyc and wb_mei.stb and wb_mei.we) = '1') then
|
| 518 |
|
|
mei_ring <= '1';
|
| 519 |
|
|
wb_mei.ack <= '1';
|
| 520 |
|
|
end if;
|
| 521 |
|
|
end if;
|
| 522 |
|
|
end process ext_irq_trigger;
|
| 523 |
|
|
|
| 524 |
|
|
|
| 525 |
2 |
zero_gravi |
end neorv32_tb_rtl;
|