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