Line 1... |
Line 1... |
-- #################################################################################################
|
-- #################################################################################################
|
-- # << NEORV32 - External Bus Interface (WISHBONE) >> #
|
-- # << NEORV32 - External Bus Interface (WISHBONE) >> #
|
-- # ********************************************************************************************* #
|
-- # ********************************************************************************************* #
|
-- # The interface provides registers for all outgoing and for all incoming signals. If the host #
|
-- # All bus accesses from the CPU, which do not target the internal IO region / the internal #
|
-- # cancels an active transfer, the Wishbone arbiter still waits some time for the bus system to #
|
-- # bootloader / the internal instruction or data memories (if implemented), are delegated via #
|
-- # ACK/ERR the transfer before the arbiter forces termination. #
|
-- # this Wishbone gateway to the external bus interface. Accessed peripherals can have a response #
|
|
-- # latency of up to BUS_TIMEOUT - 1 cycles. #
|
-- # #
|
-- # #
|
-- # Even when all processor-internal memories and IO devices are disabled, the EXTERNAL address #
|
-- # Even when all processor-internal memories and IO devices are disabled, the EXTERNAL address #
|
-- # space ENDS at address 0xffff0000 (begin of internal BOOTROM address space). #
|
-- # space ENDS at address 0xffff0000 (begin of internal BOOTROM address space). #
|
-- # #
|
-- # #
|
-- # All bus accesses from the CPU, which do not target the internal IO region / the internal #
|
-- # The interface uses registers for ALL OUTGOING AND FOR ALL INCOMING signals. Hence, an access #
|
-- # bootloader / the internal instruction or data memories (if implemented), are delegated via #
|
-- # latency of (at least) 2 cycles is added. #
|
-- # this Wishbone gateway to the external bus interface. Accessed peripherals can have a response #
|
|
-- # latency of up to BUS_TIMEOUT - 2 cycles. #
|
|
-- # #
|
-- # #
|
-- # This interface supports classic/standard Wishbone transactions (WB_PIPELINED_MODE = false) #
|
-- # This interface supports classic/standard Wishbone transactions (pkg.wb_pipe_mode_c = false) #
|
-- # and also pipelined transactions (WB_PIPELINED_MODE = true). #
|
-- # and also pipelined transactions (pkg.wb_pipe_mode_c = true). #
|
-- # ********************************************************************************************* #
|
-- # ********************************************************************************************* #
|
-- # BSD 3-Clause License #
|
-- # BSD 3-Clause License #
|
-- # #
|
-- # #
|
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
-- # #
|
-- # #
|
Line 54... |
Line 53... |
library neorv32;
|
library neorv32;
|
use neorv32.neorv32_package.all;
|
use neorv32.neorv32_package.all;
|
|
|
entity neorv32_wishbone is
|
entity neorv32_wishbone is
|
generic (
|
generic (
|
WB_PIPELINED_MODE : boolean := false; -- false: classic/standard wishbone mode, true: pipelined wishbone mode
|
|
-- Internal instruction memory --
|
-- Internal instruction memory --
|
MEM_INT_IMEM_EN : boolean := true; -- implement processor-internal instruction memory
|
MEM_INT_IMEM_EN : boolean := true; -- implement processor-internal instruction memory
|
MEM_INT_IMEM_SIZE : natural := 8*1024; -- size of processor-internal instruction memory in bytes
|
MEM_INT_IMEM_SIZE : natural := 8*1024; -- size of processor-internal instruction memory in bytes
|
-- Internal data memory --
|
-- Internal data memory --
|
MEM_INT_DMEM_EN : boolean := true; -- implement processor-internal data memory
|
MEM_INT_DMEM_EN : boolean := true; -- implement processor-internal data memory
|
Line 107... |
Line 105... |
signal int_dmem_acc : std_ulogic;
|
signal int_dmem_acc : std_ulogic;
|
signal int_boot_acc : std_ulogic;
|
signal int_boot_acc : std_ulogic;
|
signal xbus_access : std_ulogic;
|
signal xbus_access : std_ulogic;
|
|
|
-- bus arbiter
|
-- bus arbiter
|
type ctrl_state_t is (IDLE, BUSY, RESYNC);
|
type ctrl_state_t is (IDLE, BUSY);
|
type ctrl_t is record
|
type ctrl_t is record
|
state : ctrl_state_t;
|
state : ctrl_state_t;
|
we : std_ulogic;
|
we : std_ulogic;
|
rd_req : std_ulogic;
|
|
wr_req : std_ulogic;
|
|
adr : std_ulogic_vector(31 downto 0);
|
adr : std_ulogic_vector(31 downto 0);
|
wdat : std_ulogic_vector(31 downto 0);
|
wdat : std_ulogic_vector(31 downto 0);
|
rdat : std_ulogic_vector(31 downto 0);
|
rdat : std_ulogic_vector(31 downto 0);
|
sel : std_ulogic_vector(3 downto 0);
|
sel : std_ulogic_vector(03 downto 0);
|
ack : std_ulogic;
|
ack : std_ulogic;
|
err : std_ulogic;
|
err : std_ulogic;
|
timeout : std_ulogic_vector(index_size_f(BUS_TIMEOUT)-1 downto 0);
|
timeout : std_ulogic_vector(index_size_f(BUS_TIMEOUT)-1 downto 0);
|
src : std_ulogic;
|
src : std_ulogic;
|
lock : std_ulogic;
|
lock : std_ulogic;
|
priv : std_ulogic_vector(1 downto 0);
|
priv : std_ulogic_vector(01 downto 0);
|
end record;
|
end record;
|
signal ctrl : ctrl_t;
|
signal ctrl : ctrl_t;
|
signal stb_int : std_ulogic;
|
signal stb_int : std_ulogic;
|
signal cyc_int : std_ulogic;
|
signal cyc_int : std_ulogic;
|
|
signal rdata : std_ulogic_vector(31 downto 0);
|
|
|
|
-- async RX mode --
|
|
signal ack_gated : std_ulogic;
|
|
signal rdata_gated : std_ulogic_vector(31 downto 0);
|
|
|
begin
|
begin
|
|
|
-- Sanity Checks --------------------------------------------------------------------------
|
-- Sanity Checks --------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- bus timeout --
|
-- protocol --
|
assert not (BUS_TIMEOUT /= 0) report "NEORV32 PROCESSOR CONFIG NOTE: Using auto-timeout for external bus interface (" & integer'image(BUS_TIMEOUT) & " cycles)." severity note;
|
assert not (wb_pipe_mode_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing STANDARD Wishbone protocol." severity note;
|
assert not (BUS_TIMEOUT = 0) report "NEORV32 PROCESSOR CONFIG NOTE: Using no auto-timeout for external bus interface (might cause permanent CPU stall)." severity note;
|
assert not (wb_pipe_mode_c = true) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing PIEPLINED Wishbone protocol." severity note;
|
|
|
-- external memory interface protocol + max timeout latency notifier (warning) --
|
-- bus timeout --
|
assert not (wb_pipe_mode_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: Implementing external memory interface using STANDARD Wishbone protocol." severity note;
|
assert not (BUS_TIMEOUT /= 0) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing auto-timeout (" & integer'image(BUS_TIMEOUT) & " cycles)." severity note;
|
assert not (wb_pipe_mode_c = true) report "NEORV32 PROCESSOR CONFIG NOTE: Implementing external memory interface using PIEPLINED Wishbone protocol." severity note;
|
assert not (BUS_TIMEOUT = 0) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing no auto-timeout (can cause permanent CPU stall!)." severity note;
|
|
|
-- endianness --
|
-- endianness --
|
assert not (xbus_big_endian_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: Using LITTLE-ENDIAN byte order for external memory interface." severity note;
|
assert not (wb_big_endian_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing LITTLE-endian byte order." severity note;
|
assert not (xbus_big_endian_c = true) report "NEORV32 PROCESSOR CONFIG NOTE: Using BIG-ENDIAN byte order for external memory interface." severity note;
|
assert not (wb_big_endian_c = true) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing BIG-endian byte." severity note;
|
|
|
|
-- async RC --
|
|
assert not (wb_rx_buffer_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing ASYNC RX path." severity note;
|
|
|
|
|
-- Access Control -------------------------------------------------------------------------
|
-- Access Control -------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- access to processor-internal IMEM or DMEM? --
|
-- access to processor-internal IMEM or DMEM? --
|
Line 155... |
Line 159... |
-- access to processor-internal BOOTROM or IO devices? --
|
-- access to processor-internal BOOTROM or IO devices? --
|
int_boot_acc <= '1' when (addr_i(31 downto 16) = boot_rom_base_c(31 downto 16)) else '0'; -- hacky!
|
int_boot_acc <= '1' when (addr_i(31 downto 16) = boot_rom_base_c(31 downto 16)) else '0'; -- hacky!
|
-- actual external bus access? --
|
-- actual external bus access? --
|
xbus_access <= (not int_imem_acc) and (not int_dmem_acc) and (not int_boot_acc);
|
xbus_access <= (not int_imem_acc) and (not int_dmem_acc) and (not int_boot_acc);
|
|
|
|
|
-- Bus Arbiter -----------------------------------------------------------------------------
|
-- Bus Arbiter -----------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
bus_arbiter: process(rstn_i, clk_i)
|
bus_arbiter: process(rstn_i, clk_i)
|
begin
|
begin
|
if (rstn_i = '0') then
|
if (rstn_i = '0') then
|
ctrl.state <= IDLE;
|
ctrl.state <= IDLE;
|
ctrl.we <= def_rst_val_c;
|
ctrl.we <= def_rst_val_c;
|
ctrl.rd_req <= '0';
|
|
ctrl.wr_req <= '0';
|
|
ctrl.adr <= (others => def_rst_val_c);
|
ctrl.adr <= (others => def_rst_val_c);
|
ctrl.wdat <= (others => def_rst_val_c);
|
ctrl.wdat <= (others => def_rst_val_c);
|
ctrl.rdat <= (others => def_rst_val_c);
|
ctrl.rdat <= (others => def_rst_val_c);
|
ctrl.sel <= (others => def_rst_val_c);
|
ctrl.sel <= (others => def_rst_val_c);
|
ctrl.timeout <= (others => def_rst_val_c);
|
ctrl.timeout <= (others => def_rst_val_c);
|
Line 176... |
Line 179... |
ctrl.src <= def_rst_val_c;
|
ctrl.src <= def_rst_val_c;
|
ctrl.lock <= def_rst_val_c;
|
ctrl.lock <= def_rst_val_c;
|
ctrl.priv <= (others => def_rst_val_c);
|
ctrl.priv <= (others => def_rst_val_c);
|
elsif rising_edge(clk_i) then
|
elsif rising_edge(clk_i) then
|
-- defaults --
|
-- defaults --
|
ctrl.rdat <= (others => '0');
|
ctrl.rdat <= (others => '0'); -- required for internal output gating
|
ctrl.ack <= '0';
|
ctrl.ack <= '0';
|
ctrl.err <= '0';
|
ctrl.err <= '0';
|
ctrl.timeout <= std_ulogic_vector(to_unsigned(BUS_TIMEOUT, index_size_f(BUS_TIMEOUT)));
|
ctrl.timeout <= std_ulogic_vector(to_unsigned(BUS_TIMEOUT, index_size_f(BUS_TIMEOUT)));
|
|
|
-- state machine --
|
-- state machine --
|
case ctrl.state is
|
case ctrl.state is
|
|
|
when IDLE => -- waiting for host request
|
when IDLE => -- waiting for host request
|
-- ------------------------------------------------------------
|
-- ------------------------------------------------------------
|
ctrl.rd_req <= '0';
|
|
ctrl.wr_req <= '0';
|
|
-- buffer all outgoing signals --
|
-- buffer all outgoing signals --
|
ctrl.we <= wren_i or ctrl.wr_req;
|
ctrl.we <= wren_i;
|
ctrl.adr <= addr_i;
|
ctrl.adr <= addr_i;
|
if (xbus_big_endian_c = true) then -- big-endian
|
if (wb_big_endian_c = true) then -- big-endian
|
ctrl.wdat <= bswap32_f(data_i);
|
ctrl.wdat <= bswap32_f(data_i);
|
ctrl.sel <= bit_rev_f(ben_i);
|
ctrl.sel <= bit_rev_f(ben_i);
|
else -- little-endian
|
else -- little-endian
|
ctrl.wdat <= data_i;
|
ctrl.wdat <= data_i;
|
ctrl.sel <= ben_i;
|
ctrl.sel <= ben_i;
|
end if;
|
end if;
|
ctrl.src <= src_i;
|
ctrl.src <= src_i;
|
ctrl.lock <= lock_i;
|
ctrl.lock <= lock_i;
|
ctrl.priv <= priv_i;
|
ctrl.priv <= priv_i;
|
-- valid new or buffered read/write request --
|
-- valid new or buffered read/write request --
|
if ((xbus_access and (wren_i or ctrl.wr_req or rden_i or ctrl.rd_req)) = '1') then
|
if ((xbus_access and (wren_i or rden_i)) = '1') then
|
ctrl.state <= BUSY;
|
ctrl.state <= BUSY;
|
end if;
|
end if;
|
|
|
when BUSY => -- transfer in progress
|
when BUSY => -- transfer in progress
|
-- ------------------------------------------------------------
|
-- ------------------------------------------------------------
|
ctrl.rdat <= wb_dat_i;
|
ctrl.rdat <= wb_dat_i;
|
if (wb_err_i = '1') then -- abnormal bus termination
|
if (wb_err_i = '1') or -- abnormal bus termination
|
|
((timeout_en_c = true) and (or_reduce_f(ctrl.timeout) = '0')) then -- valid timeout
|
ctrl.err <= '1';
|
ctrl.err <= '1';
|
ctrl.state <= IDLE;
|
ctrl.state <= IDLE;
|
elsif (wb_ack_i = '1') then -- normal bus termination
|
elsif (wb_ack_i = '1') then -- normal bus termination
|
ctrl.ack <= '1';
|
ctrl.ack <= '1';
|
ctrl.state <= IDLE;
|
ctrl.state <= IDLE;
|
elsif (timeout_en_c = true) and (or_reduce_f(ctrl.timeout) = '0') then -- valid timeout
|
|
ctrl.err <= '1';
|
|
ctrl.state <= IDLE;
|
|
end if;
|
end if;
|
-- timeout counter --
|
-- timeout counter --
|
if (timeout_en_c = true) then
|
if (timeout_en_c = true) then
|
ctrl.timeout <= std_ulogic_vector(unsigned(ctrl.timeout) - 1); -- timeout counter
|
ctrl.timeout <= std_ulogic_vector(unsigned(ctrl.timeout) - 1); -- timeout counter
|
end if;
|
end if;
|
|
|
when RESYNC => -- make sure transfer is done!
|
|
-- ------------------------------------------------------------
|
|
ctrl.wr_req <= ctrl.wr_req or wren_i; -- buffer new request
|
|
ctrl.rd_req <= ctrl.rd_req or rden_i; -- buffer new request
|
|
if (wb_ack_i = '0') then
|
|
ctrl.state <= IDLE;
|
|
end if;
|
|
|
|
when others => -- undefined
|
when others => -- undefined
|
-- ------------------------------------------------------------
|
-- ------------------------------------------------------------
|
ctrl.state <= IDLE;
|
ctrl.state <= IDLE;
|
|
|
end case;
|
end case;
|
end if;
|
end if;
|
end process bus_arbiter;
|
end process bus_arbiter;
|
|
|
-- host access --
|
-- host access --
|
data_o <= ctrl.rdat when (xbus_big_endian_c = false) else bswap32_f(ctrl.rdat); -- endianness conversion
|
ack_gated <= wb_ack_i when (ctrl.state = BUSY) else '0'; -- CPU ack gate for "async" RX
|
ack_o <= ctrl.ack;
|
rdata_gated <= wb_dat_i when (ctrl.state = BUSY) else (others => '0'); -- CPU read data gate for "async" RX
|
|
rdata <= ctrl.rdat when (wb_rx_buffer_c = true) else rdata_gated;
|
|
|
|
data_o <= rdata when (wb_big_endian_c = false) else bswap32_f(rdata); -- endianness conversion
|
|
ack_o <= ctrl.ack when (wb_rx_buffer_c = true) else ack_gated;
|
err_o <= ctrl.err;
|
err_o <= ctrl.err;
|
|
|
-- wishbone interface --
|
-- wishbone interface --
|
wb_tag_o(0) <= '1' when (ctrl.priv = priv_mode_m_c) else '0'; -- privileged access when in machine mode
|
wb_tag_o(0) <= '1' when (ctrl.priv = priv_mode_m_c) else '0'; -- privileged access when in machine mode
|
wb_tag_o(1) <= '0'; -- 0 = secure, 1 = non-secure
|
wb_tag_o(1) <= '0'; -- 0 = secure, 1 = non-secure
|
Line 256... |
Line 251... |
|
|
wb_adr_o <= ctrl.adr;
|
wb_adr_o <= ctrl.adr;
|
wb_dat_o <= ctrl.wdat;
|
wb_dat_o <= ctrl.wdat;
|
wb_we_o <= ctrl.we;
|
wb_we_o <= ctrl.we;
|
wb_sel_o <= ctrl.sel;
|
wb_sel_o <= ctrl.sel;
|
wb_stb_o <= stb_int when (WB_PIPELINED_MODE = true) else cyc_int;
|
wb_stb_o <= stb_int when (wb_pipe_mode_c = true) else cyc_int;
|
wb_cyc_o <= cyc_int;
|
wb_cyc_o <= cyc_int;
|
|
|
stb_int <= '1' when (ctrl.state = BUSY) else '0';
|
stb_int <= '1' when (ctrl.state = BUSY) else '0';
|
cyc_int <= '0' when (ctrl.state = IDLE) or (ctrl.state = RESYNC) else '1';
|
cyc_int <= '1' when (ctrl.state = BUSY) else '0';
|
|
|
|
|
end neorv32_wishbone_rtl;
|
end neorv32_wishbone_rtl;
|
|
|
No newline at end of file
|
No newline at end of file
|