1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Bus Interface Unit >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
|
|
-- # This unit connects the CPU to the memory/IO system. #
|
5 |
|
|
-- # ********************************************************************************************* #
|
6 |
|
|
-- # BSD 3-Clause License #
|
7 |
|
|
-- # #
|
8 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
9 |
|
|
-- # #
|
10 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
11 |
|
|
-- # permitted provided that the following conditions are met: #
|
12 |
|
|
-- # #
|
13 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
14 |
|
|
-- # conditions and the following disclaimer. #
|
15 |
|
|
-- # #
|
16 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
17 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
18 |
|
|
-- # provided with the distribution. #
|
19 |
|
|
-- # #
|
20 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
21 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
22 |
|
|
-- # permission. #
|
23 |
|
|
-- # #
|
24 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
25 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
26 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
27 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
28 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
29 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
30 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
31 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
32 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
33 |
|
|
-- # ********************************************************************************************* #
|
34 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
35 |
|
|
-- #################################################################################################
|
36 |
|
|
|
37 |
|
|
library ieee;
|
38 |
|
|
use ieee.std_logic_1164.all;
|
39 |
|
|
use ieee.numeric_std.all;
|
40 |
|
|
|
41 |
|
|
library neorv32;
|
42 |
|
|
use neorv32.neorv32_package.all;
|
43 |
|
|
|
44 |
|
|
entity neorv32_cpu_bus is
|
45 |
|
|
generic (
|
46 |
|
|
CPU_EXTENSION_RISCV_C : boolean := false; -- implement compressed extension?
|
47 |
|
|
MEM_EXT_TIMEOUT : natural := 15 -- cycles after which a valid bus access will timeout
|
48 |
|
|
);
|
49 |
|
|
port (
|
50 |
|
|
-- global control --
|
51 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
52 |
|
|
rstn_i : in std_ulogic; -- global reset, low-active, async
|
53 |
|
|
ctrl_i : in std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
54 |
|
|
-- data input --
|
55 |
|
|
wdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- write data
|
56 |
|
|
pc_i : in std_ulogic_vector(data_width_c-1 downto 0); -- current PC
|
57 |
|
|
alu_i : in std_ulogic_vector(data_width_c-1 downto 0); -- ALU result
|
58 |
|
|
-- data output --
|
59 |
|
|
instr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
|
60 |
|
|
rdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
|
61 |
|
|
-- status --
|
62 |
|
|
mar_o : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
|
63 |
|
|
ma_instr_o : out std_ulogic; -- misaligned instruction address
|
64 |
|
|
ma_load_o : out std_ulogic; -- misaligned load data address
|
65 |
|
|
ma_store_o : out std_ulogic; -- misaligned store data address
|
66 |
|
|
be_instr_o : out std_ulogic; -- bus error on instruction access
|
67 |
|
|
be_load_o : out std_ulogic; -- bus error on load data access
|
68 |
|
|
be_store_o : out std_ulogic; -- bus error on store data access
|
69 |
|
|
bus_wait_o : out std_ulogic; -- wait for bus operation to finish
|
70 |
|
|
exc_ack_i : in std_ulogic; -- exception controller ACK
|
71 |
|
|
-- bus system --
|
72 |
|
|
bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
73 |
|
|
bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
74 |
|
|
bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
75 |
|
|
bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
76 |
|
|
bus_we_o : out std_ulogic; -- write enable
|
77 |
|
|
bus_re_o : out std_ulogic; -- read enable
|
78 |
|
|
bus_ack_i : in std_ulogic; -- bus transfer acknowledge
|
79 |
|
|
bus_err_i : in std_ulogic -- bus transfer error
|
80 |
|
|
);
|
81 |
|
|
end neorv32_cpu_bus;
|
82 |
|
|
|
83 |
|
|
architecture neorv32_cpu_bus_rtl of neorv32_cpu_bus is
|
84 |
|
|
|
85 |
|
|
-- interface registers --
|
86 |
|
|
signal mar, mdo, mdi : std_ulogic_vector(data_width_c-1 downto 0);
|
87 |
|
|
|
88 |
|
|
-- bus request controller --
|
89 |
|
|
signal bus_busy : std_ulogic;
|
90 |
|
|
signal bus_if_req : std_ulogic;
|
91 |
|
|
signal bus_rd_req : std_ulogic;
|
92 |
|
|
signal bus_wr_req : std_ulogic;
|
93 |
|
|
signal access_err : std_ulogic;
|
94 |
|
|
signal align_err : std_ulogic;
|
95 |
|
|
signal bus_timeout : std_ulogic_vector(index_size_f(MEM_EXT_TIMEOUT)-1 downto 0);
|
96 |
|
|
|
97 |
|
|
-- misaligned access? --
|
98 |
|
|
signal misaligned_data, misaligned_instr : std_ulogic;
|
99 |
|
|
|
100 |
|
|
begin
|
101 |
|
|
|
102 |
|
|
-- Address and Control --------------------------------------------------------------------
|
103 |
|
|
-- -------------------------------------------------------------------------------------------
|
104 |
|
|
mem_adr_reg: process(rstn_i, clk_i)
|
105 |
|
|
begin
|
106 |
|
|
if (rstn_i = '0') then
|
107 |
|
|
mar <= (others => '0');
|
108 |
|
|
elsif rising_edge(clk_i) then
|
109 |
|
|
if (ctrl_i(ctrl_bus_mar_we_c) = '1') then
|
110 |
|
|
mar <= alu_i;
|
111 |
|
|
end if;
|
112 |
|
|
end if;
|
113 |
|
|
end process mem_adr_reg;
|
114 |
|
|
|
115 |
|
|
-- address output --
|
116 |
|
|
bus_addr_o <= pc_i when (ctrl_i(ctrl_bus_if_c) = '1') else mar; -- is instruction fetch?
|
117 |
|
|
mar_o <= mar;
|
118 |
|
|
|
119 |
|
|
-- write request output --
|
120 |
|
|
bus_we_o <= ctrl_i(ctrl_bus_wr_c) and (not misaligned_data);
|
121 |
|
|
|
122 |
|
|
-- read request output (also used for instruction fetch) --
|
123 |
|
|
bus_re_o <= (ctrl_i(ctrl_bus_rd_c) and (not misaligned_data)) or (ctrl_i(ctrl_bus_if_c) and (not misaligned_instr));
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
-- Write Data -----------------------------------------------------------------------------
|
127 |
|
|
-- -------------------------------------------------------------------------------------------
|
128 |
|
|
mem_do_reg: process(clk_i)
|
129 |
|
|
begin
|
130 |
|
|
if rising_edge(clk_i) then
|
131 |
|
|
if (ctrl_i(ctrl_bus_mdo_we_c) = '1') then
|
132 |
|
|
mdo <= wdata_i;
|
133 |
|
|
end if;
|
134 |
|
|
end if;
|
135 |
|
|
end process mem_do_reg;
|
136 |
|
|
|
137 |
|
|
-- byte enable and output data alignment --
|
138 |
|
|
byte_enable: process(mar, mdo, ctrl_i)
|
139 |
|
|
begin
|
140 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
|
141 |
|
|
when "00" => -- byte
|
142 |
|
|
bus_wdata_o(07 downto 00) <= mdo(07 downto 00);
|
143 |
|
|
bus_wdata_o(15 downto 08) <= mdo(07 downto 00);
|
144 |
|
|
bus_wdata_o(23 downto 16) <= mdo(07 downto 00);
|
145 |
|
|
bus_wdata_o(31 downto 24) <= mdo(07 downto 00);
|
146 |
|
|
bus_ben_o <= (others => '0');
|
147 |
|
|
bus_ben_o(to_integer(unsigned(mar(1 downto 0)))) <= '1';
|
148 |
|
|
when "01" => -- half-word
|
149 |
|
|
bus_wdata_o(31 downto 16) <= mdo(15 downto 00);
|
150 |
|
|
bus_wdata_o(15 downto 00) <= mdo(15 downto 00);
|
151 |
|
|
if (mar(1) = '0') then
|
152 |
|
|
bus_ben_o <= "0011"; -- low half-word
|
153 |
|
|
else
|
154 |
|
|
bus_ben_o <= "1100"; -- high half-word
|
155 |
|
|
end if;
|
156 |
|
|
when others => -- word
|
157 |
|
|
bus_wdata_o <= mdo;
|
158 |
|
|
bus_ben_o <= "1111"; -- full word
|
159 |
|
|
end case;
|
160 |
|
|
end process byte_enable;
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
-- Read Data ------------------------------------------------------------------------------
|
164 |
|
|
-- -------------------------------------------------------------------------------------------
|
165 |
|
|
mem_out_buf: process(clk_i)
|
166 |
|
|
begin
|
167 |
|
|
if rising_edge(clk_i) then
|
168 |
|
|
-- memory data in register (MDI) --
|
169 |
|
|
if (ctrl_i(ctrl_bus_mdi_we_c) = '1') then
|
170 |
|
|
mdi <= bus_rdata_i;
|
171 |
|
|
end if;
|
172 |
|
|
end if;
|
173 |
|
|
end process mem_out_buf;
|
174 |
|
|
|
175 |
|
|
-- instruction output --
|
176 |
|
|
instr_o <= bus_rdata_i;
|
177 |
|
|
|
178 |
|
|
-- input data align and sign extension --
|
179 |
|
|
read_align: process(mdi, mar, ctrl_i)
|
180 |
|
|
variable signed_v : std_ulogic;
|
181 |
|
|
begin
|
182 |
|
|
signed_v := not ctrl_i(ctrl_bus_unsigned_c);
|
183 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
|
184 |
|
|
when "00" => -- byte
|
185 |
|
|
case mar(1 downto 0) is
|
186 |
|
|
when "00" =>
|
187 |
|
|
rdata_o(31 downto 08) <= (others => (signed_v and mdi(07)));
|
188 |
|
|
rdata_o(07 downto 00) <= mdi(07 downto 00); -- byte 0
|
189 |
|
|
when "01" =>
|
190 |
|
|
rdata_o(31 downto 08) <= (others => (signed_v and mdi(15)));
|
191 |
|
|
rdata_o(07 downto 00) <= mdi(15 downto 08); -- byte 1
|
192 |
|
|
when "10" =>
|
193 |
|
|
rdata_o(31 downto 08) <= (others => (signed_v and mdi(23)));
|
194 |
|
|
rdata_o(07 downto 00) <= mdi(23 downto 16); -- byte 2
|
195 |
|
|
when others =>
|
196 |
|
|
rdata_o(31 downto 08) <= (others => (signed_v and mdi(31)));
|
197 |
|
|
rdata_o(07 downto 00) <= mdi(31 downto 24); -- byte 3
|
198 |
|
|
end case;
|
199 |
|
|
when "01" => -- half-word
|
200 |
|
|
if (mar(1) = '0') then
|
201 |
|
|
rdata_o(31 downto 16) <= (others => (signed_v and mdi(15)));
|
202 |
|
|
rdata_o(15 downto 00) <= mdi(15 downto 00); -- low half-word
|
203 |
|
|
else
|
204 |
|
|
rdata_o(31 downto 16) <= (others => (signed_v and mdi(31)));
|
205 |
|
|
rdata_o(15 downto 00) <= mdi(31 downto 16); -- high half-word
|
206 |
|
|
end if;
|
207 |
|
|
when others => -- word
|
208 |
|
|
rdata_o <= mdi; -- full word
|
209 |
|
|
end case;
|
210 |
|
|
end process read_align;
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
-- Bus Status Controller ------------------------------------------------------------------
|
214 |
|
|
-- -------------------------------------------------------------------------------------------
|
215 |
|
|
bus_ctrl: process(rstn_i, clk_i)
|
216 |
|
|
begin
|
217 |
|
|
if (rstn_i = '0') then
|
218 |
|
|
bus_busy <= '0';
|
219 |
|
|
bus_if_req <= '0';
|
220 |
|
|
bus_rd_req <= '0';
|
221 |
|
|
bus_wr_req <= '0';
|
222 |
|
|
access_err <= '0';
|
223 |
|
|
align_err <= '0';
|
224 |
|
|
bus_timeout <= (others => '0');
|
225 |
|
|
elsif rising_edge(clk_i) then
|
226 |
|
|
if (bus_busy = '0') then -- wait for new request
|
227 |
|
|
bus_busy <= ctrl_i(ctrl_bus_if_c) or ctrl_i(ctrl_bus_rd_c) or ctrl_i(ctrl_bus_wr_c); -- any request at all?
|
228 |
|
|
bus_if_req <= ctrl_i(ctrl_bus_if_c); -- instruction fetch
|
229 |
|
|
bus_rd_req <= ctrl_i(ctrl_bus_rd_c); -- store access
|
230 |
|
|
bus_wr_req <= ctrl_i(ctrl_bus_wr_c); -- load access
|
231 |
|
|
bus_timeout <= std_ulogic_vector(to_unsigned(MEM_EXT_TIMEOUT, index_size_f(MEM_EXT_TIMEOUT)));
|
232 |
|
|
access_err <= '0';
|
233 |
|
|
align_err <= '0';
|
234 |
|
|
else -- bus transfer in progress
|
235 |
|
|
bus_timeout <= std_ulogic_vector(unsigned(bus_timeout) - 1);
|
236 |
|
|
align_err <= (align_err or misaligned_data or misaligned_instr) and (not exc_ack_i);
|
237 |
|
|
access_err <= (access_err or (not or_all_f(bus_timeout)) or bus_err_i) and (not exc_ack_i);
|
238 |
|
|
if (align_err = '1') or (access_err = '1') then
|
239 |
|
|
if (exc_ack_i = '1') then -- wait for controller to ack exception
|
240 |
|
|
bus_if_req <= '0';
|
241 |
|
|
bus_rd_req <= '0';
|
242 |
|
|
bus_wr_req <= '0';
|
243 |
|
|
bus_busy <= '0';
|
244 |
|
|
end if;
|
245 |
|
|
elsif (bus_ack_i = '1') then -- normal termination
|
246 |
|
|
bus_if_req <= '0';
|
247 |
|
|
bus_rd_req <= '0';
|
248 |
|
|
bus_wr_req <= '0';
|
249 |
|
|
bus_busy <= '0';
|
250 |
|
|
end if;
|
251 |
|
|
end if;
|
252 |
|
|
end if;
|
253 |
|
|
end process bus_ctrl;
|
254 |
|
|
|
255 |
|
|
-- output bus access error to controller --
|
256 |
|
|
be_instr_o <= bus_if_req and access_err;
|
257 |
|
|
be_load_o <= bus_rd_req and access_err;
|
258 |
|
|
be_store_o <= bus_wr_req and access_err;
|
259 |
|
|
|
260 |
|
|
-- output alignment error to controller --
|
261 |
|
|
ma_instr_o <= bus_if_req and align_err;
|
262 |
|
|
ma_load_o <= bus_rd_req and align_err;
|
263 |
|
|
ma_store_o <= bus_wr_req and align_err;
|
264 |
|
|
|
265 |
|
|
-- wait for bus --
|
266 |
|
|
bus_wait_o <= bus_busy and (not bus_ack_i); -- FIXME: 'async' ack
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
-- Check for Misaligned Access ------------------------------------------------------------
|
270 |
|
|
-- -------------------------------------------------------------------------------------------
|
271 |
|
|
misaligned_d_check: process(mar, ctrl_i)
|
272 |
|
|
begin
|
273 |
|
|
-- check data access --
|
274 |
|
|
misaligned_data <= '0'; -- default
|
275 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
|
276 |
|
|
when "00" => -- byte
|
277 |
|
|
misaligned_data <= '0';
|
278 |
|
|
when "01" => -- half-word
|
279 |
|
|
if (mar(0) /= '0') then
|
280 |
|
|
misaligned_data <= '1';
|
281 |
|
|
end if;
|
282 |
|
|
when others => -- word
|
283 |
|
|
if (mar(1 downto 0) /= "00") then
|
284 |
|
|
misaligned_data <= '1';
|
285 |
|
|
end if;
|
286 |
|
|
end case;
|
287 |
|
|
end process misaligned_d_check;
|
288 |
|
|
|
289 |
|
|
misaligned_i_check: process(ctrl_i, pc_i)
|
290 |
|
|
begin
|
291 |
|
|
-- check instruction access --
|
292 |
|
|
misaligned_instr <= '0'; -- default
|
293 |
|
|
if (CPU_EXTENSION_RISCV_C = true) then -- 16-bit instruction access only
|
294 |
|
|
if (pc_i(0) /= '0') then
|
295 |
|
|
misaligned_instr <= '1';
|
296 |
|
|
end if;
|
297 |
|
|
else -- 32-bit instruction access only
|
298 |
|
|
if (pc_i(1 downto 0) /= "00") then
|
299 |
|
|
misaligned_instr <= '1';
|
300 |
|
|
end if;
|
301 |
|
|
end if;
|
302 |
|
|
end process misaligned_i_check;
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
end neorv32_cpu_bus_rtl;
|