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