1 |
12 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Bus Switch >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
|
|
-- # Allows to access a single peripheral bus ("p_bus") by two controller busses. Controller port #
|
5 |
|
|
-- # A ("ca_bus") has priority over controller port B ("cb_bus"). #
|
6 |
|
|
-- # ********************************************************************************************* #
|
7 |
|
|
-- # BSD 3-Clause License #
|
8 |
|
|
-- # #
|
9 |
42 |
zero_gravi |
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
10 |
12 |
zero_gravi |
-- # #
|
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 |
|
|
|
42 |
|
|
library neorv32;
|
43 |
|
|
use neorv32.neorv32_package.all;
|
44 |
|
|
|
45 |
|
|
entity neorv32_busswitch is
|
46 |
|
|
generic (
|
47 |
|
|
PORT_CA_READ_ONLY : boolean := false; -- set if controller port A is read-only
|
48 |
|
|
PORT_CB_READ_ONLY : boolean := false -- set if controller port B is read-only
|
49 |
|
|
);
|
50 |
|
|
port (
|
51 |
|
|
-- global control --
|
52 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
53 |
|
|
rstn_i : in std_ulogic; -- global reset, low-active, async
|
54 |
|
|
-- controller interface a --
|
55 |
|
|
ca_bus_addr_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
56 |
|
|
ca_bus_rdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
57 |
|
|
ca_bus_wdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
58 |
|
|
ca_bus_ben_i : in std_ulogic_vector(03 downto 0); -- byte enable
|
59 |
|
|
ca_bus_we_i : in std_ulogic; -- write enable
|
60 |
|
|
ca_bus_re_i : in std_ulogic; -- read enable
|
61 |
|
|
ca_bus_cancel_i : in std_ulogic; -- cancel current bus transaction
|
62 |
39 |
zero_gravi |
ca_bus_lock_i : in std_ulogic; -- locked/exclusive access
|
63 |
12 |
zero_gravi |
ca_bus_ack_o : out std_ulogic; -- bus transfer acknowledge
|
64 |
|
|
ca_bus_err_o : out std_ulogic; -- bus transfer error
|
65 |
|
|
-- controller interface b --
|
66 |
|
|
cb_bus_addr_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
67 |
|
|
cb_bus_rdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
68 |
|
|
cb_bus_wdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
69 |
|
|
cb_bus_ben_i : in std_ulogic_vector(03 downto 0); -- byte enable
|
70 |
|
|
cb_bus_we_i : in std_ulogic; -- write enable
|
71 |
|
|
cb_bus_re_i : in std_ulogic; -- read enable
|
72 |
|
|
cb_bus_cancel_i : in std_ulogic; -- cancel current bus transaction
|
73 |
39 |
zero_gravi |
cb_bus_lock_i : in std_ulogic; -- locked/exclusive access
|
74 |
12 |
zero_gravi |
cb_bus_ack_o : out std_ulogic; -- bus transfer acknowledge
|
75 |
|
|
cb_bus_err_o : out std_ulogic; -- bus transfer error
|
76 |
|
|
-- peripheral bus --
|
77 |
36 |
zero_gravi |
p_bus_src_o : out std_ulogic; -- access source: 0 = A, 1 = B
|
78 |
12 |
zero_gravi |
p_bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
79 |
|
|
p_bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
80 |
|
|
p_bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
81 |
|
|
p_bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
82 |
|
|
p_bus_we_o : out std_ulogic; -- write enable
|
83 |
|
|
p_bus_re_o : out std_ulogic; -- read enable
|
84 |
|
|
p_bus_cancel_o : out std_ulogic; -- cancel current bus transaction
|
85 |
39 |
zero_gravi |
p_bus_lock_o : out std_ulogic; -- locked/exclusive access
|
86 |
12 |
zero_gravi |
p_bus_ack_i : in std_ulogic; -- bus transfer acknowledge
|
87 |
|
|
p_bus_err_i : in std_ulogic -- bus transfer error
|
88 |
|
|
);
|
89 |
|
|
end neorv32_busswitch;
|
90 |
|
|
|
91 |
|
|
architecture neorv32_busswitch_rtl of neorv32_busswitch is
|
92 |
|
|
|
93 |
|
|
-- access requests --
|
94 |
42 |
zero_gravi |
signal ca_rd_req_buf, ca_wr_req_buf : std_ulogic;
|
95 |
|
|
signal cb_rd_req_buf, cb_wr_req_buf : std_ulogic;
|
96 |
|
|
signal ca_req_current, ca_req_buffered : std_ulogic;
|
97 |
|
|
signal cb_req_current, cb_req_buffered : std_ulogic;
|
98 |
12 |
zero_gravi |
|
99 |
|
|
-- internal bus lines --
|
100 |
42 |
zero_gravi |
signal ca_bus_ack, cb_bus_ack : std_ulogic;
|
101 |
|
|
signal ca_bus_err, cb_bus_err : std_ulogic;
|
102 |
|
|
signal p_bus_we, p_bus_re : std_ulogic;
|
103 |
12 |
zero_gravi |
|
104 |
|
|
-- access arbiter --
|
105 |
|
|
type arbiter_state_t is (IDLE, BUSY, RETIRE, BUSY_SWITCHED, RETIRE_SWITCHED);
|
106 |
|
|
type arbiter_t is record
|
107 |
|
|
state : arbiter_state_t;
|
108 |
|
|
state_nxt : arbiter_state_t;
|
109 |
|
|
bus_sel : std_ulogic;
|
110 |
|
|
re_trig : std_ulogic;
|
111 |
|
|
we_trig : std_ulogic;
|
112 |
|
|
end record;
|
113 |
|
|
signal arbiter : arbiter_t;
|
114 |
|
|
|
115 |
|
|
begin
|
116 |
|
|
|
117 |
|
|
-- Access Buffer --------------------------------------------------------------------------
|
118 |
|
|
-- -------------------------------------------------------------------------------------------
|
119 |
|
|
access_buffer: process(rstn_i, clk_i)
|
120 |
|
|
begin
|
121 |
|
|
if (rstn_i = '0') then
|
122 |
|
|
ca_rd_req_buf <= '0';
|
123 |
|
|
ca_wr_req_buf <= '0';
|
124 |
|
|
cb_rd_req_buf <= '0';
|
125 |
|
|
cb_wr_req_buf <= '0';
|
126 |
|
|
elsif rising_edge(clk_i) then
|
127 |
|
|
|
128 |
|
|
-- controller A requests --
|
129 |
|
|
if (ca_rd_req_buf = '0') and (ca_wr_req_buf = '0') then -- idle
|
130 |
|
|
ca_rd_req_buf <= ca_bus_re_i;
|
131 |
|
|
ca_wr_req_buf <= ca_bus_we_i;
|
132 |
|
|
elsif (ca_bus_cancel_i = '1') or -- controller cancels access
|
133 |
|
|
(ca_bus_err = '1') or -- peripheral cancels access
|
134 |
|
|
(ca_bus_ack = '1') then -- normal termination
|
135 |
|
|
ca_rd_req_buf <= '0';
|
136 |
|
|
ca_wr_req_buf <= '0';
|
137 |
|
|
end if;
|
138 |
|
|
|
139 |
|
|
-- controller B requests --
|
140 |
|
|
if (cb_rd_req_buf = '0') and (cb_wr_req_buf = '0') then
|
141 |
|
|
cb_rd_req_buf <= cb_bus_re_i;
|
142 |
|
|
cb_wr_req_buf <= cb_bus_we_i;
|
143 |
|
|
elsif (cb_bus_cancel_i = '1') or -- controller cancels access
|
144 |
|
|
(cb_bus_err = '1') or -- peripheral cancels access
|
145 |
|
|
(cb_bus_ack = '1') then -- normal termination
|
146 |
|
|
cb_rd_req_buf <= '0';
|
147 |
|
|
cb_wr_req_buf <= '0';
|
148 |
|
|
end if;
|
149 |
|
|
|
150 |
|
|
end if;
|
151 |
|
|
end process access_buffer;
|
152 |
|
|
|
153 |
|
|
-- any current requests? --
|
154 |
|
|
ca_req_current <= (ca_bus_re_i or ca_bus_we_i) when (PORT_CA_READ_ONLY = false) else ca_bus_re_i;
|
155 |
|
|
cb_req_current <= (cb_bus_re_i or cb_bus_we_i) when (PORT_CB_READ_ONLY = false) else cb_bus_re_i;
|
156 |
|
|
|
157 |
|
|
-- any buffered requests? --
|
158 |
|
|
ca_req_buffered <= (ca_rd_req_buf or ca_wr_req_buf) when (PORT_CA_READ_ONLY = false) else ca_rd_req_buf;
|
159 |
|
|
cb_req_buffered <= (cb_rd_req_buf or cb_wr_req_buf) when (PORT_CB_READ_ONLY = false) else cb_rd_req_buf;
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
-- Access Arbiter Sync --------------------------------------------------------------------
|
163 |
|
|
-- -------------------------------------------------------------------------------------------
|
164 |
|
|
-- for registers that require a specific reset state --
|
165 |
|
|
arbiter_sync: process(rstn_i, clk_i)
|
166 |
|
|
begin
|
167 |
|
|
if (rstn_i = '0') then
|
168 |
|
|
arbiter.state <= IDLE;
|
169 |
|
|
elsif rising_edge(clk_i) then
|
170 |
|
|
arbiter.state <= arbiter.state_nxt;
|
171 |
|
|
end if;
|
172 |
|
|
end process arbiter_sync;
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
-- Peripheral Bus Arbiter -----------------------------------------------------------------
|
176 |
|
|
-- -------------------------------------------------------------------------------------------
|
177 |
|
|
arbiter_comb: process(arbiter, ca_req_current, cb_req_current, ca_req_buffered, cb_req_buffered,
|
178 |
|
|
ca_rd_req_buf, ca_wr_req_buf, cb_rd_req_buf, cb_wr_req_buf,
|
179 |
|
|
ca_bus_cancel_i, cb_bus_cancel_i, p_bus_ack_i, p_bus_err_i)
|
180 |
|
|
begin
|
181 |
|
|
-- arbiter defaults --
|
182 |
|
|
arbiter.state_nxt <= arbiter.state;
|
183 |
|
|
arbiter.bus_sel <= '0';
|
184 |
|
|
arbiter.we_trig <= '0';
|
185 |
|
|
arbiter.re_trig <= '0';
|
186 |
36 |
zero_gravi |
--
|
187 |
|
|
p_bus_src_o <= '0';
|
188 |
12 |
zero_gravi |
|
189 |
|
|
-- state machine --
|
190 |
|
|
case arbiter.state is
|
191 |
|
|
|
192 |
|
|
when IDLE => -- Controller a has full bus access
|
193 |
|
|
-- ------------------------------------------------------------
|
194 |
36 |
zero_gravi |
p_bus_src_o <= '0'; -- access from port A
|
195 |
12 |
zero_gravi |
if (ca_req_current = '1') then -- current request?
|
196 |
|
|
arbiter.bus_sel <= '0';
|
197 |
|
|
arbiter.state_nxt <= BUSY;
|
198 |
|
|
elsif (ca_req_buffered = '1') then -- buffered request?
|
199 |
|
|
arbiter.bus_sel <= '0';
|
200 |
|
|
arbiter.state_nxt <= RETIRE;
|
201 |
|
|
elsif (cb_req_current = '1') then -- current request from controller b?
|
202 |
|
|
arbiter.bus_sel <= '1';
|
203 |
|
|
arbiter.state_nxt <= BUSY_SWITCHED;
|
204 |
|
|
elsif (cb_req_buffered = '1') then -- buffered request from controller b?
|
205 |
|
|
arbiter.bus_sel <= '1';
|
206 |
|
|
arbiter.state_nxt <= RETIRE_SWITCHED;
|
207 |
|
|
end if;
|
208 |
|
|
|
209 |
|
|
when BUSY => -- transaction in progress
|
210 |
|
|
-- ------------------------------------------------------------
|
211 |
36 |
zero_gravi |
p_bus_src_o <= '0'; -- access from port A
|
212 |
12 |
zero_gravi |
arbiter.bus_sel <= '0';
|
213 |
|
|
if (ca_bus_cancel_i = '1') or -- controller cancels access
|
214 |
|
|
(p_bus_err_i = '1') or -- peripheral cancels access
|
215 |
|
|
(p_bus_ack_i = '1') then -- normal termination
|
216 |
|
|
arbiter.state_nxt <= IDLE;
|
217 |
|
|
end if;
|
218 |
|
|
|
219 |
|
|
when RETIRE => -- retire pending access
|
220 |
|
|
-- ------------------------------------------------------------
|
221 |
36 |
zero_gravi |
p_bus_src_o <= '0'; -- access from port A
|
222 |
12 |
zero_gravi |
arbiter.bus_sel <= '0';
|
223 |
|
|
if (PORT_CA_READ_ONLY = false) then
|
224 |
|
|
arbiter.we_trig <= ca_wr_req_buf;
|
225 |
|
|
end if;
|
226 |
|
|
arbiter.re_trig <= ca_rd_req_buf;
|
227 |
|
|
arbiter.state_nxt <= BUSY;
|
228 |
|
|
|
229 |
|
|
when BUSY_SWITCHED => -- switched transaction in progress
|
230 |
|
|
-- ------------------------------------------------------------
|
231 |
36 |
zero_gravi |
p_bus_src_o <= '1'; -- access from port B
|
232 |
12 |
zero_gravi |
arbiter.bus_sel <= '1';
|
233 |
|
|
if (cb_bus_cancel_i = '1') or -- controller cancels access
|
234 |
|
|
(p_bus_err_i = '1') or -- peripheral cancels access
|
235 |
|
|
(p_bus_ack_i = '1') then -- normal termination
|
236 |
42 |
zero_gravi |
if (ca_req_buffered = '1') or (ca_req_current = '1') then -- any request from A?
|
237 |
|
|
arbiter.state_nxt <= RETIRE;
|
238 |
|
|
else
|
239 |
|
|
arbiter.state_nxt <= IDLE;
|
240 |
|
|
end if;
|
241 |
12 |
zero_gravi |
end if;
|
242 |
|
|
|
243 |
|
|
when RETIRE_SWITCHED => -- retire pending switched access
|
244 |
|
|
-- ------------------------------------------------------------
|
245 |
36 |
zero_gravi |
p_bus_src_o <= '1'; -- access from port B
|
246 |
12 |
zero_gravi |
arbiter.bus_sel <= '1';
|
247 |
|
|
if (PORT_CB_READ_ONLY = false) then
|
248 |
|
|
arbiter.we_trig <= cb_wr_req_buf;
|
249 |
|
|
end if;
|
250 |
|
|
arbiter.re_trig <= cb_rd_req_buf;
|
251 |
|
|
arbiter.state_nxt <= BUSY_SWITCHED;
|
252 |
|
|
|
253 |
|
|
end case;
|
254 |
|
|
end process arbiter_comb;
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
-- Peripheral Bus Switch ------------------------------------------------------------------
|
258 |
|
|
-- -------------------------------------------------------------------------------------------
|
259 |
36 |
zero_gravi |
p_bus_addr_o <= ca_bus_addr_i when (arbiter.bus_sel = '0') else cb_bus_addr_i;
|
260 |
|
|
p_bus_wdata_o <= cb_bus_wdata_i when (PORT_CA_READ_ONLY = true) else ca_bus_wdata_i when (PORT_CB_READ_ONLY = true) else
|
261 |
|
|
ca_bus_wdata_i when (arbiter.bus_sel = '0') else cb_bus_wdata_i;
|
262 |
|
|
p_bus_ben_o <= cb_bus_ben_i when (PORT_CA_READ_ONLY = true) else ca_bus_ben_i when (PORT_CB_READ_ONLY = true) else
|
263 |
|
|
ca_bus_ben_i when (arbiter.bus_sel = '0') else cb_bus_ben_i;
|
264 |
|
|
p_bus_we <= ca_bus_we_i when (arbiter.bus_sel = '0') else cb_bus_we_i;
|
265 |
|
|
p_bus_re <= ca_bus_re_i when (arbiter.bus_sel = '0') else cb_bus_re_i;
|
266 |
|
|
p_bus_cancel_o <= ca_bus_cancel_i when (arbiter.bus_sel = '0') else cb_bus_cancel_i;
|
267 |
12 |
zero_gravi |
p_bus_we_o <= (p_bus_we or arbiter.we_trig);
|
268 |
|
|
p_bus_re_o <= (p_bus_re or arbiter.re_trig);
|
269 |
39 |
zero_gravi |
p_bus_lock_o <= ca_bus_lock_i or cb_bus_lock_i;
|
270 |
12 |
zero_gravi |
|
271 |
|
|
ca_bus_rdata_o <= p_bus_rdata_i;
|
272 |
|
|
cb_bus_rdata_o <= p_bus_rdata_i;
|
273 |
|
|
|
274 |
|
|
ca_bus_ack <= p_bus_ack_i and (not arbiter.bus_sel);
|
275 |
|
|
cb_bus_ack <= p_bus_ack_i and ( arbiter.bus_sel);
|
276 |
|
|
ca_bus_ack_o <= ca_bus_ack;
|
277 |
|
|
cb_bus_ack_o <= cb_bus_ack;
|
278 |
|
|
|
279 |
|
|
ca_bus_err <= p_bus_err_i and (not arbiter.bus_sel);
|
280 |
|
|
cb_bus_err <= p_bus_err_i and ( arbiter.bus_sel);
|
281 |
|
|
ca_bus_err_o <= ca_bus_err;
|
282 |
|
|
cb_bus_err_o <= cb_bus_err;
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
end neorv32_busswitch_rtl;
|