1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - External Bus Interface (WISHBONE) >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
61 |
zero_gravi |
-- # All bus accesses from the CPU, which do not target the internal IO region / the internal #
|
5 |
|
|
-- # bootloader / the internal instruction or data memories (if implemented), are delegated via #
|
6 |
|
|
-- # this Wishbone gateway to the external bus interface. Accessed peripherals can have a response #
|
7 |
|
|
-- # latency of up to BUS_TIMEOUT - 1 cycles. #
|
8 |
41 |
zero_gravi |
-- # #
|
9 |
39 |
zero_gravi |
-- # Even when all processor-internal memories and IO devices are disabled, the EXTERNAL address #
|
10 |
|
|
-- # space ENDS at address 0xffff0000 (begin of internal BOOTROM address space). #
|
11 |
41 |
zero_gravi |
-- # #
|
12 |
61 |
zero_gravi |
-- # The interface uses registers for ALL OUTGOING AND FOR ALL INCOMING signals. Hence, an access #
|
13 |
|
|
-- # latency of (at least) 2 cycles is added. #
|
14 |
41 |
zero_gravi |
-- # #
|
15 |
61 |
zero_gravi |
-- # This interface supports classic/standard Wishbone transactions (pkg.wb_pipe_mode_c = false) #
|
16 |
|
|
-- # and also pipelined transactions (pkg.wb_pipe_mode_c = true). #
|
17 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
18 |
|
|
-- # BSD 3-Clause License #
|
19 |
|
|
-- # #
|
20 |
44 |
zero_gravi |
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
21 |
2 |
zero_gravi |
-- # #
|
22 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
23 |
|
|
-- # permitted provided that the following conditions are met: #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
26 |
|
|
-- # conditions and the following disclaimer. #
|
27 |
|
|
-- # #
|
28 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
29 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
30 |
|
|
-- # provided with the distribution. #
|
31 |
|
|
-- # #
|
32 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
33 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
34 |
|
|
-- # permission. #
|
35 |
|
|
-- # #
|
36 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
37 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
38 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
39 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
40 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
41 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
42 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
43 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
44 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
45 |
|
|
-- # ********************************************************************************************* #
|
46 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
47 |
|
|
-- #################################################################################################
|
48 |
|
|
|
49 |
|
|
library ieee;
|
50 |
|
|
use ieee.std_logic_1164.all;
|
51 |
|
|
use ieee.numeric_std.all;
|
52 |
|
|
|
53 |
|
|
library neorv32;
|
54 |
|
|
use neorv32.neorv32_package.all;
|
55 |
|
|
|
56 |
|
|
entity neorv32_wishbone is
|
57 |
|
|
generic (
|
58 |
23 |
zero_gravi |
-- Internal instruction memory --
|
59 |
44 |
zero_gravi |
MEM_INT_IMEM_EN : boolean := true; -- implement processor-internal instruction memory
|
60 |
35 |
zero_gravi |
MEM_INT_IMEM_SIZE : natural := 8*1024; -- size of processor-internal instruction memory in bytes
|
61 |
23 |
zero_gravi |
-- Internal data memory --
|
62 |
44 |
zero_gravi |
MEM_INT_DMEM_EN : boolean := true; -- implement processor-internal data memory
|
63 |
41 |
zero_gravi |
MEM_INT_DMEM_SIZE : natural := 4*1024; -- size of processor-internal data memory in bytes
|
64 |
|
|
-- Bus Timeout --
|
65 |
|
|
BUS_TIMEOUT : natural := 63 -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
|
66 |
2 |
zero_gravi |
);
|
67 |
|
|
port (
|
68 |
|
|
-- global control --
|
69 |
57 |
zero_gravi |
clk_i : in std_ulogic; -- global clock line
|
70 |
|
|
rstn_i : in std_ulogic; -- global reset line, low-active
|
71 |
2 |
zero_gravi |
-- host access --
|
72 |
57 |
zero_gravi |
src_i : in std_ulogic; -- access type (0: data, 1:instruction)
|
73 |
|
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
74 |
|
|
rden_i : in std_ulogic; -- read enable
|
75 |
|
|
wren_i : in std_ulogic; -- write enable
|
76 |
|
|
ben_i : in std_ulogic_vector(03 downto 0); -- byte write enable
|
77 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
78 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
79 |
|
|
lock_i : in std_ulogic; -- exclusive access request
|
80 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
81 |
|
|
err_o : out std_ulogic; -- transfer error
|
82 |
|
|
priv_i : in std_ulogic_vector(01 downto 0); -- current CPU privilege level
|
83 |
2 |
zero_gravi |
-- wishbone interface --
|
84 |
57 |
zero_gravi |
wb_tag_o : out std_ulogic_vector(02 downto 0); -- request tag
|
85 |
|
|
wb_adr_o : out std_ulogic_vector(31 downto 0); -- address
|
86 |
|
|
wb_dat_i : in std_ulogic_vector(31 downto 0); -- read data
|
87 |
|
|
wb_dat_o : out std_ulogic_vector(31 downto 0); -- write data
|
88 |
|
|
wb_we_o : out std_ulogic; -- read/write
|
89 |
|
|
wb_sel_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
90 |
|
|
wb_stb_o : out std_ulogic; -- strobe
|
91 |
|
|
wb_cyc_o : out std_ulogic; -- valid cycle
|
92 |
|
|
wb_lock_o : out std_ulogic; -- exclusive access request
|
93 |
|
|
wb_ack_i : in std_ulogic; -- transfer acknowledge
|
94 |
|
|
wb_err_i : in std_ulogic -- transfer error
|
95 |
2 |
zero_gravi |
);
|
96 |
|
|
end neorv32_wishbone;
|
97 |
|
|
|
98 |
|
|
architecture neorv32_wishbone_rtl of neorv32_wishbone is
|
99 |
|
|
|
100 |
57 |
zero_gravi |
-- timeout enable --
|
101 |
|
|
constant timeout_en_c : boolean := boolean(BUS_TIMEOUT /= 0); -- timeout enabled if BUS_TIMEOUT > 0
|
102 |
35 |
zero_gravi |
|
103 |
2 |
zero_gravi |
-- access control --
|
104 |
39 |
zero_gravi |
signal int_imem_acc : std_ulogic;
|
105 |
|
|
signal int_dmem_acc : std_ulogic;
|
106 |
|
|
signal int_boot_acc : std_ulogic;
|
107 |
|
|
signal xbus_access : std_ulogic;
|
108 |
2 |
zero_gravi |
|
109 |
35 |
zero_gravi |
-- bus arbiter
|
110 |
61 |
zero_gravi |
type ctrl_state_t is (IDLE, BUSY);
|
111 |
35 |
zero_gravi |
type ctrl_t is record
|
112 |
39 |
zero_gravi |
state : ctrl_state_t;
|
113 |
|
|
we : std_ulogic;
|
114 |
|
|
adr : std_ulogic_vector(31 downto 0);
|
115 |
|
|
wdat : std_ulogic_vector(31 downto 0);
|
116 |
|
|
rdat : std_ulogic_vector(31 downto 0);
|
117 |
61 |
zero_gravi |
sel : std_ulogic_vector(03 downto 0);
|
118 |
39 |
zero_gravi |
ack : std_ulogic;
|
119 |
|
|
err : std_ulogic;
|
120 |
57 |
zero_gravi |
timeout : std_ulogic_vector(index_size_f(BUS_TIMEOUT)-1 downto 0);
|
121 |
39 |
zero_gravi |
src : std_ulogic;
|
122 |
57 |
zero_gravi |
lock : std_ulogic;
|
123 |
61 |
zero_gravi |
priv : std_ulogic_vector(01 downto 0);
|
124 |
35 |
zero_gravi |
end record;
|
125 |
36 |
zero_gravi |
signal ctrl : ctrl_t;
|
126 |
|
|
signal stb_int : std_ulogic;
|
127 |
|
|
signal cyc_int : std_ulogic;
|
128 |
61 |
zero_gravi |
signal rdata : std_ulogic_vector(31 downto 0);
|
129 |
2 |
zero_gravi |
|
130 |
61 |
zero_gravi |
-- async RX mode --
|
131 |
|
|
signal ack_gated : std_ulogic;
|
132 |
|
|
signal rdata_gated : std_ulogic_vector(31 downto 0);
|
133 |
|
|
|
134 |
2 |
zero_gravi |
begin
|
135 |
|
|
|
136 |
35 |
zero_gravi |
-- Sanity Checks --------------------------------------------------------------------------
|
137 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
138 |
61 |
zero_gravi |
-- protocol --
|
139 |
|
|
assert not (wb_pipe_mode_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing STANDARD Wishbone protocol." severity note;
|
140 |
|
|
assert not (wb_pipe_mode_c = true) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing PIEPLINED Wishbone protocol." severity note;
|
141 |
|
|
|
142 |
57 |
zero_gravi |
-- bus timeout --
|
143 |
61 |
zero_gravi |
assert not (BUS_TIMEOUT /= 0) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing auto-timeout (" & integer'image(BUS_TIMEOUT) & " cycles)." severity note;
|
144 |
|
|
assert not (BUS_TIMEOUT = 0) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing no auto-timeout (can cause permanent CPU stall!)." severity note;
|
145 |
59 |
zero_gravi |
|
146 |
40 |
zero_gravi |
-- endianness --
|
147 |
61 |
zero_gravi |
assert not (wb_big_endian_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing LITTLE-endian byte order." severity note;
|
148 |
|
|
assert not (wb_big_endian_c = true) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing BIG-endian byte." severity note;
|
149 |
2 |
zero_gravi |
|
150 |
61 |
zero_gravi |
-- async RC --
|
151 |
|
|
assert not (wb_rx_buffer_c = false) report "NEORV32 PROCESSOR CONFIG NOTE: External Bus Interface - Implementing ASYNC RX path." severity note;
|
152 |
2 |
zero_gravi |
|
153 |
61 |
zero_gravi |
|
154 |
2 |
zero_gravi |
-- Access Control -------------------------------------------------------------------------
|
155 |
|
|
-- -------------------------------------------------------------------------------------------
|
156 |
39 |
zero_gravi |
-- access to processor-internal IMEM or DMEM? --
|
157 |
44 |
zero_gravi |
int_imem_acc <= '1' when (addr_i(31 downto index_size_f(MEM_INT_IMEM_SIZE)) = imem_base_c(31 downto index_size_f(MEM_INT_IMEM_SIZE))) and (MEM_INT_IMEM_EN = true) else '0';
|
158 |
|
|
int_dmem_acc <= '1' when (addr_i(31 downto index_size_f(MEM_INT_DMEM_SIZE)) = dmem_base_c(31 downto index_size_f(MEM_INT_DMEM_SIZE))) and (MEM_INT_DMEM_EN = true) else '0';
|
159 |
39 |
zero_gravi |
-- access to processor-internal BOOTROM or IO devices? --
|
160 |
|
|
int_boot_acc <= '1' when (addr_i(31 downto 16) = boot_rom_base_c(31 downto 16)) else '0'; -- hacky!
|
161 |
2 |
zero_gravi |
-- actual external bus access? --
|
162 |
39 |
zero_gravi |
xbus_access <= (not int_imem_acc) and (not int_dmem_acc) and (not int_boot_acc);
|
163 |
2 |
zero_gravi |
|
164 |
61 |
zero_gravi |
|
165 |
2 |
zero_gravi |
-- Bus Arbiter -----------------------------------------------------------------------------
|
166 |
|
|
-- -------------------------------------------------------------------------------------------
|
167 |
|
|
bus_arbiter: process(rstn_i, clk_i)
|
168 |
|
|
begin
|
169 |
|
|
if (rstn_i = '0') then
|
170 |
39 |
zero_gravi |
ctrl.state <= IDLE;
|
171 |
57 |
zero_gravi |
ctrl.we <= def_rst_val_c;
|
172 |
|
|
ctrl.adr <= (others => def_rst_val_c);
|
173 |
|
|
ctrl.wdat <= (others => def_rst_val_c);
|
174 |
|
|
ctrl.rdat <= (others => def_rst_val_c);
|
175 |
|
|
ctrl.sel <= (others => def_rst_val_c);
|
176 |
|
|
ctrl.timeout <= (others => def_rst_val_c);
|
177 |
|
|
ctrl.ack <= def_rst_val_c;
|
178 |
|
|
ctrl.err <= def_rst_val_c;
|
179 |
|
|
ctrl.src <= def_rst_val_c;
|
180 |
|
|
ctrl.lock <= def_rst_val_c;
|
181 |
|
|
ctrl.priv <= (others => def_rst_val_c);
|
182 |
2 |
zero_gravi |
elsif rising_edge(clk_i) then
|
183 |
35 |
zero_gravi |
-- defaults --
|
184 |
61 |
zero_gravi |
ctrl.rdat <= (others => '0'); -- required for internal output gating
|
185 |
39 |
zero_gravi |
ctrl.ack <= '0';
|
186 |
|
|
ctrl.err <= '0';
|
187 |
57 |
zero_gravi |
ctrl.timeout <= std_ulogic_vector(to_unsigned(BUS_TIMEOUT, index_size_f(BUS_TIMEOUT)));
|
188 |
2 |
zero_gravi |
|
189 |
35 |
zero_gravi |
-- state machine --
|
190 |
|
|
case ctrl.state is
|
191 |
2 |
zero_gravi |
|
192 |
35 |
zero_gravi |
when IDLE => -- waiting for host request
|
193 |
|
|
-- ------------------------------------------------------------
|
194 |
|
|
-- buffer all outgoing signals --
|
195 |
61 |
zero_gravi |
ctrl.we <= wren_i;
|
196 |
56 |
zero_gravi |
ctrl.adr <= addr_i;
|
197 |
61 |
zero_gravi |
if (wb_big_endian_c = true) then -- big-endian
|
198 |
60 |
zero_gravi |
ctrl.wdat <= bswap32_f(data_i);
|
199 |
|
|
ctrl.sel <= bit_rev_f(ben_i);
|
200 |
|
|
else -- little-endian
|
201 |
40 |
zero_gravi |
ctrl.wdat <= data_i;
|
202 |
|
|
ctrl.sel <= ben_i;
|
203 |
|
|
end if;
|
204 |
36 |
zero_gravi |
ctrl.src <= src_i;
|
205 |
57 |
zero_gravi |
ctrl.lock <= lock_i;
|
206 |
36 |
zero_gravi |
ctrl.priv <= priv_i;
|
207 |
39 |
zero_gravi |
-- valid new or buffered read/write request --
|
208 |
61 |
zero_gravi |
if ((xbus_access and (wren_i or rden_i)) = '1') then
|
209 |
35 |
zero_gravi |
ctrl.state <= BUSY;
|
210 |
|
|
end if;
|
211 |
2 |
zero_gravi |
|
212 |
35 |
zero_gravi |
when BUSY => -- transfer in progress
|
213 |
|
|
-- ------------------------------------------------------------
|
214 |
57 |
zero_gravi |
ctrl.rdat <= wb_dat_i;
|
215 |
61 |
zero_gravi |
if (wb_err_i = '1') or -- abnormal bus termination
|
216 |
|
|
((timeout_en_c = true) and (or_reduce_f(ctrl.timeout) = '0')) then -- valid timeout
|
217 |
35 |
zero_gravi |
ctrl.err <= '1';
|
218 |
57 |
zero_gravi |
ctrl.state <= IDLE;
|
219 |
35 |
zero_gravi |
elsif (wb_ack_i = '1') then -- normal bus termination
|
220 |
|
|
ctrl.ack <= '1';
|
221 |
|
|
ctrl.state <= IDLE;
|
222 |
|
|
end if;
|
223 |
57 |
zero_gravi |
-- timeout counter --
|
224 |
|
|
if (timeout_en_c = true) then
|
225 |
|
|
ctrl.timeout <= std_ulogic_vector(unsigned(ctrl.timeout) - 1); -- timeout counter
|
226 |
38 |
zero_gravi |
end if;
|
227 |
|
|
|
228 |
35 |
zero_gravi |
when others => -- undefined
|
229 |
|
|
-- ------------------------------------------------------------
|
230 |
|
|
ctrl.state <= IDLE;
|
231 |
2 |
zero_gravi |
|
232 |
35 |
zero_gravi |
end case;
|
233 |
|
|
end if;
|
234 |
|
|
end process bus_arbiter;
|
235 |
23 |
zero_gravi |
|
236 |
35 |
zero_gravi |
-- host access --
|
237 |
61 |
zero_gravi |
ack_gated <= wb_ack_i when (ctrl.state = BUSY) else '0'; -- CPU ack gate for "async" RX
|
238 |
|
|
rdata_gated <= wb_dat_i when (ctrl.state = BUSY) else (others => '0'); -- CPU read data gate for "async" RX
|
239 |
|
|
rdata <= ctrl.rdat when (wb_rx_buffer_c = true) else rdata_gated;
|
240 |
|
|
|
241 |
|
|
data_o <= rdata when (wb_big_endian_c = false) else bswap32_f(rdata); -- endianness conversion
|
242 |
|
|
ack_o <= ctrl.ack when (wb_rx_buffer_c = true) else ack_gated;
|
243 |
39 |
zero_gravi |
err_o <= ctrl.err;
|
244 |
2 |
zero_gravi |
|
245 |
35 |
zero_gravi |
-- wishbone interface --
|
246 |
36 |
zero_gravi |
wb_tag_o(0) <= '1' when (ctrl.priv = priv_mode_m_c) else '0'; -- privileged access when in machine mode
|
247 |
39 |
zero_gravi |
wb_tag_o(1) <= '0'; -- 0 = secure, 1 = non-secure
|
248 |
|
|
wb_tag_o(2) <= ctrl.src; -- 0 = data access, 1 = instruction access
|
249 |
36 |
zero_gravi |
|
250 |
57 |
zero_gravi |
wb_lock_o <= ctrl.lock; -- 1 = exclusive access request
|
251 |
|
|
|
252 |
61 |
zero_gravi |
wb_adr_o <= ctrl.adr;
|
253 |
|
|
wb_dat_o <= ctrl.wdat;
|
254 |
|
|
wb_we_o <= ctrl.we;
|
255 |
|
|
wb_sel_o <= ctrl.sel;
|
256 |
|
|
wb_stb_o <= stb_int when (wb_pipe_mode_c = true) else cyc_int;
|
257 |
|
|
wb_cyc_o <= cyc_int;
|
258 |
2 |
zero_gravi |
|
259 |
39 |
zero_gravi |
stb_int <= '1' when (ctrl.state = BUSY) else '0';
|
260 |
61 |
zero_gravi |
cyc_int <= '1' when (ctrl.state = BUSY) else '0';
|
261 |
2 |
zero_gravi |
|
262 |
35 |
zero_gravi |
|
263 |
2 |
zero_gravi |
end neorv32_wishbone_rtl;
|