1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Bus Interface Unit >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
18 |
zero_gravi |
-- # Instruction and data bus interfaces and physical memory protection (PMP). #
|
5 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
6 |
|
|
-- # BSD 3-Clause License #
|
7 |
|
|
-- # #
|
8 |
70 |
zero_gravi |
-- # Copyright (c) 2022, Stephan Nolting. All rights reserved. #
|
9 |
2 |
zero_gravi |
-- # #
|
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 |
62 |
zero_gravi |
CPU_EXTENSION_RISCV_A : boolean; -- implement atomic extension?
|
47 |
|
|
CPU_EXTENSION_RISCV_C : boolean; -- implement compressed extension?
|
48 |
15 |
zero_gravi |
-- Physical memory protection (PMP) --
|
49 |
73 |
zero_gravi |
PMP_NUM_REGIONS : natural; -- number of regions (0..16)
|
50 |
|
|
PMP_MIN_GRANULARITY : natural -- minimal region granularity in bytes, has to be a power of 2, min 4 bytes
|
51 |
2 |
zero_gravi |
);
|
52 |
|
|
port (
|
53 |
|
|
-- global control --
|
54 |
70 |
zero_gravi |
clk_i : in std_ulogic; -- global clock, rising edge
|
55 |
|
|
rstn_i : in std_ulogic := '0'; -- global reset, low-active, async
|
56 |
|
|
ctrl_i : in std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
57 |
12 |
zero_gravi |
-- cpu instruction fetch interface --
|
58 |
70 |
zero_gravi |
fetch_pc_i : in std_ulogic_vector(data_width_c-1 downto 0); -- PC for instruction fetch
|
59 |
|
|
instr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- instruction
|
60 |
|
|
i_wait_o : out std_ulogic; -- wait for fetch to complete
|
61 |
12 |
zero_gravi |
--
|
62 |
70 |
zero_gravi |
ma_instr_o : out std_ulogic; -- misaligned instruction address
|
63 |
|
|
be_instr_o : out std_ulogic; -- bus error on instruction access
|
64 |
12 |
zero_gravi |
-- cpu data access interface --
|
65 |
70 |
zero_gravi |
addr_i : in std_ulogic_vector(data_width_c-1 downto 0); -- ALU result -> access address
|
66 |
|
|
wdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- write data
|
67 |
|
|
rdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- read data
|
68 |
|
|
mar_o : out std_ulogic_vector(data_width_c-1 downto 0); -- current memory address register
|
69 |
|
|
d_wait_o : out std_ulogic; -- wait for access to complete
|
70 |
12 |
zero_gravi |
--
|
71 |
70 |
zero_gravi |
excl_state_o : out std_ulogic; -- atomic/exclusive access status
|
72 |
|
|
ma_load_o : out std_ulogic; -- misaligned load data address
|
73 |
|
|
ma_store_o : out std_ulogic; -- misaligned store data address
|
74 |
|
|
be_load_o : out std_ulogic; -- bus error on load data access
|
75 |
|
|
be_store_o : out std_ulogic; -- bus error on store data access
|
76 |
15 |
zero_gravi |
-- physical memory protection --
|
77 |
70 |
zero_gravi |
pmp_addr_i : in pmp_addr_if_t; -- addresses
|
78 |
|
|
pmp_ctrl_i : in pmp_ctrl_if_t; -- configs
|
79 |
12 |
zero_gravi |
-- instruction bus --
|
80 |
70 |
zero_gravi |
i_bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
81 |
|
|
i_bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
82 |
|
|
i_bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
83 |
|
|
i_bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
84 |
|
|
i_bus_we_o : out std_ulogic; -- write enable
|
85 |
|
|
i_bus_re_o : out std_ulogic; -- read enable
|
86 |
|
|
i_bus_lock_o : out std_ulogic; -- exclusive access request
|
87 |
|
|
i_bus_ack_i : in std_ulogic; -- bus transfer acknowledge
|
88 |
|
|
i_bus_err_i : in std_ulogic; -- bus transfer error
|
89 |
|
|
i_bus_fence_o : out std_ulogic; -- fence operation
|
90 |
12 |
zero_gravi |
-- data bus --
|
91 |
70 |
zero_gravi |
d_bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
92 |
|
|
d_bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
93 |
|
|
d_bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
94 |
|
|
d_bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
95 |
|
|
d_bus_we_o : out std_ulogic; -- write enable
|
96 |
|
|
d_bus_re_o : out std_ulogic; -- read enable
|
97 |
|
|
d_bus_lock_o : out std_ulogic; -- exclusive access request
|
98 |
|
|
d_bus_ack_i : in std_ulogic; -- bus transfer acknowledge
|
99 |
|
|
d_bus_err_i : in std_ulogic; -- bus transfer error
|
100 |
|
|
d_bus_fence_o : out std_ulogic -- fence operation
|
101 |
2 |
zero_gravi |
);
|
102 |
|
|
end neorv32_cpu_bus;
|
103 |
|
|
|
104 |
|
|
architecture neorv32_cpu_bus_rtl of neorv32_cpu_bus is
|
105 |
|
|
|
106 |
15 |
zero_gravi |
-- PMP modes --
|
107 |
|
|
constant pmp_off_mode_c : std_ulogic_vector(1 downto 0) := "00"; -- null region (disabled)
|
108 |
73 |
zero_gravi |
constant pmp_tor_mode_c : std_ulogic_vector(1 downto 0) := "01"; -- top of range
|
109 |
36 |
zero_gravi |
--constant pmp_na4_mode_c : std_ulogic_vector(1 downto 0) := "10"; -- naturally aligned four-byte region
|
110 |
73 |
zero_gravi |
--constant pmp_napot_mode_c : std_ulogic_vector(1 downto 0) := "11"; -- naturally aligned power-of-two region (>= 8 bytes)
|
111 |
15 |
zero_gravi |
|
112 |
|
|
-- PMP configuration register bits --
|
113 |
|
|
constant pmp_cfg_r_c : natural := 0; -- read permit
|
114 |
|
|
constant pmp_cfg_w_c : natural := 1; -- write permit
|
115 |
|
|
constant pmp_cfg_x_c : natural := 2; -- execute permit
|
116 |
|
|
constant pmp_cfg_al_c : natural := 3; -- mode bit low
|
117 |
|
|
constant pmp_cfg_ah_c : natural := 4; -- mode bit high
|
118 |
62 |
zero_gravi |
--
|
119 |
15 |
zero_gravi |
constant pmp_cfg_l_c : natural := 7; -- locked entry
|
120 |
|
|
|
121 |
73 |
zero_gravi |
-- PMP minimal granularity --
|
122 |
|
|
constant pmp_lsb_c : natural := index_size_f(PMP_MIN_GRANULARITY);
|
123 |
|
|
|
124 |
12 |
zero_gravi |
-- data interface registers --
|
125 |
2 |
zero_gravi |
signal mar, mdo, mdi : std_ulogic_vector(data_width_c-1 downto 0);
|
126 |
|
|
|
127 |
12 |
zero_gravi |
-- data access --
|
128 |
|
|
signal d_bus_wdata : std_ulogic_vector(data_width_c-1 downto 0); -- write data
|
129 |
|
|
signal d_bus_rdata : std_ulogic_vector(data_width_c-1 downto 0); -- read data
|
130 |
57 |
zero_gravi |
signal rdata_align : std_ulogic_vector(data_width_c-1 downto 0); -- read-data alignment
|
131 |
12 |
zero_gravi |
signal d_bus_ben : std_ulogic_vector(3 downto 0); -- write data byte enable
|
132 |
2 |
zero_gravi |
|
133 |
|
|
-- misaligned access? --
|
134 |
12 |
zero_gravi |
signal d_misaligned, i_misaligned : std_ulogic;
|
135 |
2 |
zero_gravi |
|
136 |
12 |
zero_gravi |
-- bus arbiter --
|
137 |
|
|
type bus_arbiter_t is record
|
138 |
|
|
rd_req : std_ulogic; -- read access in progress
|
139 |
|
|
wr_req : std_ulogic; -- write access in progress
|
140 |
|
|
err_align : std_ulogic; -- alignment error
|
141 |
|
|
err_bus : std_ulogic; -- bus access error
|
142 |
|
|
end record;
|
143 |
|
|
signal i_arbiter, d_arbiter : bus_arbiter_t;
|
144 |
|
|
|
145 |
57 |
zero_gravi |
-- atomic/exclusive access - reservation controller --
|
146 |
|
|
signal exclusive_lock : std_ulogic;
|
147 |
|
|
signal exclusive_lock_status : std_ulogic_vector(data_width_c-1 downto 0); -- read data
|
148 |
|
|
|
149 |
15 |
zero_gravi |
-- physical memory protection --
|
150 |
|
|
type pmp_t is record
|
151 |
73 |
zero_gravi |
i_match : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for instruction interface
|
152 |
|
|
d_match : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for data interface
|
153 |
|
|
if_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for fetch operation
|
154 |
|
|
ld_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for load operation
|
155 |
|
|
st_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for store operation
|
156 |
15 |
zero_gravi |
end record;
|
157 |
|
|
signal pmp : pmp_t;
|
158 |
|
|
|
159 |
47 |
zero_gravi |
-- memory control signal buffer (when using PMP) --
|
160 |
|
|
signal d_bus_we, d_bus_we_buf : std_ulogic;
|
161 |
|
|
signal d_bus_re, d_bus_re_buf : std_ulogic;
|
162 |
|
|
signal i_bus_re, i_bus_re_buf : std_ulogic;
|
163 |
|
|
|
164 |
|
|
-- pmp faults anyone? --
|
165 |
15 |
zero_gravi |
signal if_pmp_fault : std_ulogic; -- pmp instruction access fault
|
166 |
|
|
signal ld_pmp_fault : std_ulogic; -- pmp load access fault
|
167 |
|
|
signal st_pmp_fault : std_ulogic; -- pmp store access fault
|
168 |
|
|
|
169 |
2 |
zero_gravi |
begin
|
170 |
|
|
|
171 |
47 |
zero_gravi |
-- Sanity Checks --------------------------------------------------------------------------
|
172 |
|
|
-- -------------------------------------------------------------------------------------------
|
173 |
64 |
zero_gravi |
assert not (PMP_NUM_REGIONS > pmp_num_regions_critical_c) report "NEORV32 CPU CONFIG WARNING! Number of implemented PMP regions (PMP_NUM_REGIONS = " &
|
174 |
|
|
integer'image(PMP_NUM_REGIONS) & ") beyond critical limit (pmp_num_regions_critical_c = " & integer'image(pmp_num_regions_critical_c) &
|
175 |
|
|
"). Inserting another register stage (that will increase memory latency by +1 cycle)." severity warning;
|
176 |
47 |
zero_gravi |
|
177 |
|
|
|
178 |
12 |
zero_gravi |
-- Data Interface: Access Address ---------------------------------------------------------
|
179 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
180 |
56 |
zero_gravi |
mem_adr_reg: process(rstn_i, clk_i)
|
181 |
2 |
zero_gravi |
begin
|
182 |
56 |
zero_gravi |
if (rstn_i = '0') then
|
183 |
|
|
mar <= (others => def_rst_val_c);
|
184 |
|
|
elsif rising_edge(clk_i) then
|
185 |
39 |
zero_gravi |
if (ctrl_i(ctrl_bus_mo_we_c) = '1') then
|
186 |
12 |
zero_gravi |
mar <= addr_i;
|
187 |
2 |
zero_gravi |
end if;
|
188 |
|
|
end if;
|
189 |
|
|
end process mem_adr_reg;
|
190 |
|
|
|
191 |
71 |
zero_gravi |
-- address read-back for exception controller --
|
192 |
12 |
zero_gravi |
mar_o <= mar;
|
193 |
2 |
zero_gravi |
|
194 |
|
|
|
195 |
12 |
zero_gravi |
-- Data Interface: Write Data -------------------------------------------------------------
|
196 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
197 |
56 |
zero_gravi |
mem_do_reg: process(rstn_i, clk_i)
|
198 |
2 |
zero_gravi |
begin
|
199 |
56 |
zero_gravi |
if (rstn_i = '0') then
|
200 |
|
|
mdo <= (others => def_rst_val_c);
|
201 |
|
|
elsif rising_edge(clk_i) then
|
202 |
39 |
zero_gravi |
if (ctrl_i(ctrl_bus_mo_we_c) = '1') then
|
203 |
40 |
zero_gravi |
mdo <= wdata_i; -- memory data output register (MDO)
|
204 |
2 |
zero_gravi |
end if;
|
205 |
|
|
end if;
|
206 |
|
|
end process mem_do_reg;
|
207 |
|
|
|
208 |
|
|
-- byte enable and output data alignment --
|
209 |
71 |
zero_gravi |
write_align: process(mar, mdo, ctrl_i)
|
210 |
2 |
zero_gravi |
begin
|
211 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
|
212 |
|
|
when "00" => -- byte
|
213 |
71 |
zero_gravi |
d_bus_wdata(07 downto 00) <= mdo(7 downto 0);
|
214 |
|
|
d_bus_wdata(15 downto 08) <= mdo(7 downto 0);
|
215 |
|
|
d_bus_wdata(23 downto 16) <= mdo(7 downto 0);
|
216 |
|
|
d_bus_wdata(31 downto 24) <= mdo(7 downto 0);
|
217 |
36 |
zero_gravi |
case mar(1 downto 0) is
|
218 |
|
|
when "00" => d_bus_ben <= "0001";
|
219 |
|
|
when "01" => d_bus_ben <= "0010";
|
220 |
|
|
when "10" => d_bus_ben <= "0100";
|
221 |
|
|
when others => d_bus_ben <= "1000";
|
222 |
|
|
end case;
|
223 |
2 |
zero_gravi |
when "01" => -- half-word
|
224 |
71 |
zero_gravi |
d_bus_wdata(31 downto 16) <= mdo(15 downto 0);
|
225 |
|
|
d_bus_wdata(15 downto 00) <= mdo(15 downto 0);
|
226 |
2 |
zero_gravi |
if (mar(1) = '0') then
|
227 |
12 |
zero_gravi |
d_bus_ben <= "0011"; -- low half-word
|
228 |
2 |
zero_gravi |
else
|
229 |
12 |
zero_gravi |
d_bus_ben <= "1100"; -- high half-word
|
230 |
2 |
zero_gravi |
end if;
|
231 |
|
|
when others => -- word
|
232 |
12 |
zero_gravi |
d_bus_wdata <= mdo;
|
233 |
|
|
d_bus_ben <= "1111"; -- full word
|
234 |
2 |
zero_gravi |
end case;
|
235 |
71 |
zero_gravi |
end process write_align;
|
236 |
2 |
zero_gravi |
|
237 |
|
|
|
238 |
12 |
zero_gravi |
-- Data Interface: Read Data --------------------------------------------------------------
|
239 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
240 |
57 |
zero_gravi |
mem_di_reg: process(rstn_i, clk_i)
|
241 |
2 |
zero_gravi |
begin
|
242 |
56 |
zero_gravi |
if (rstn_i = '0') then
|
243 |
|
|
mdi <= (others => def_rst_val_c);
|
244 |
|
|
elsif rising_edge(clk_i) then
|
245 |
39 |
zero_gravi |
if (ctrl_i(ctrl_bus_mi_we_c) = '1') then
|
246 |
40 |
zero_gravi |
mdi <= d_bus_rdata; -- memory data input register (MDI)
|
247 |
2 |
zero_gravi |
end if;
|
248 |
|
|
end if;
|
249 |
57 |
zero_gravi |
end process mem_di_reg;
|
250 |
2 |
zero_gravi |
|
251 |
12 |
zero_gravi |
-- input data alignment and sign extension --
|
252 |
2 |
zero_gravi |
read_align: process(mdi, mar, ctrl_i)
|
253 |
71 |
zero_gravi |
variable shifted_data_v : std_ulogic_vector(31 downto 0);
|
254 |
2 |
zero_gravi |
begin
|
255 |
71 |
zero_gravi |
-- align input word --
|
256 |
36 |
zero_gravi |
case mar(1 downto 0) is
|
257 |
71 |
zero_gravi |
when "00" => shifted_data_v := mdi(31 downto 00);
|
258 |
|
|
when "01" => shifted_data_v := x"00" & mdi(31 downto 08);
|
259 |
|
|
when "10" => shifted_data_v := x"0000" & mdi(31 downto 16);
|
260 |
|
|
when others => shifted_data_v := x"000000" & mdi(31 downto 24);
|
261 |
36 |
zero_gravi |
end case;
|
262 |
71 |
zero_gravi |
-- actual data size and sign-extension --
|
263 |
36 |
zero_gravi |
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is
|
264 |
2 |
zero_gravi |
when "00" => -- byte
|
265 |
71 |
zero_gravi |
rdata_align(31 downto 08) <= (others => ((not ctrl_i(ctrl_bus_unsigned_c)) and shifted_data_v(7))); -- sign extension
|
266 |
|
|
rdata_align(07 downto 00) <= shifted_data_v(07 downto 00);
|
267 |
2 |
zero_gravi |
when "01" => -- half-word
|
268 |
71 |
zero_gravi |
rdata_align(31 downto 16) <= (others => ((not ctrl_i(ctrl_bus_unsigned_c)) and shifted_data_v(15))); -- sign extension
|
269 |
|
|
rdata_align(15 downto 00) <= shifted_data_v(15 downto 00); -- high half-word
|
270 |
2 |
zero_gravi |
when others => -- word
|
271 |
71 |
zero_gravi |
rdata_align <= shifted_data_v; -- full word
|
272 |
2 |
zero_gravi |
end case;
|
273 |
|
|
end process read_align;
|
274 |
|
|
|
275 |
57 |
zero_gravi |
-- insert exclusive lock status for SC operations only --
|
276 |
|
|
rdata_o <= exclusive_lock_status when (CPU_EXTENSION_RISCV_A = true) and (ctrl_i(ctrl_bus_ch_lock_c) = '1') else rdata_align;
|
277 |
2 |
zero_gravi |
|
278 |
57 |
zero_gravi |
|
279 |
39 |
zero_gravi |
-- Data Access Arbiter --------------------------------------------------------------------
|
280 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
281 |
73 |
zero_gravi |
-- controlled by pipeline BACK-end --
|
282 |
39 |
zero_gravi |
data_access_arbiter: process(rstn_i, clk_i)
|
283 |
2 |
zero_gravi |
begin
|
284 |
39 |
zero_gravi |
if (rstn_i = '0') then
|
285 |
|
|
d_arbiter.wr_req <= '0';
|
286 |
|
|
d_arbiter.rd_req <= '0';
|
287 |
|
|
d_arbiter.err_align <= '0';
|
288 |
|
|
d_arbiter.err_bus <= '0';
|
289 |
|
|
elsif rising_edge(clk_i) then
|
290 |
|
|
-- data access request --
|
291 |
|
|
if (d_arbiter.wr_req = '0') and (d_arbiter.rd_req = '0') then -- idle
|
292 |
|
|
d_arbiter.wr_req <= ctrl_i(ctrl_bus_wr_c);
|
293 |
|
|
d_arbiter.rd_req <= ctrl_i(ctrl_bus_rd_c);
|
294 |
73 |
zero_gravi |
d_arbiter.err_align <= '0';
|
295 |
39 |
zero_gravi |
d_arbiter.err_bus <= '0';
|
296 |
73 |
zero_gravi |
else -- in progress, accumulate error
|
297 |
|
|
d_arbiter.err_align <= d_arbiter.err_align or d_misaligned;
|
298 |
|
|
d_arbiter.err_bus <= d_arbiter.err_bus or d_bus_err_i or (st_pmp_fault and d_arbiter.wr_req) or (ld_pmp_fault and d_arbiter.rd_req);
|
299 |
|
|
if (d_bus_ack_i = '1') or (ctrl_i(ctrl_trap_c) = '1') then -- wait for ACK or TRAP
|
300 |
|
|
-- > do not abort directly when an error has been detected - wait until the trap environment
|
301 |
|
|
-- > has started (ctrl_i(ctrl_trap_c)) to make sure the error signals are evaluated BEFORE d_wait_o clears
|
302 |
39 |
zero_gravi |
d_arbiter.wr_req <= '0';
|
303 |
|
|
d_arbiter.rd_req <= '0';
|
304 |
|
|
end if;
|
305 |
|
|
end if;
|
306 |
12 |
zero_gravi |
end if;
|
307 |
39 |
zero_gravi |
end process data_access_arbiter;
|
308 |
12 |
zero_gravi |
|
309 |
39 |
zero_gravi |
-- wait for bus transaction to finish --
|
310 |
|
|
d_wait_o <= (d_arbiter.wr_req or d_arbiter.rd_req) and (not d_bus_ack_i);
|
311 |
|
|
|
312 |
|
|
-- output data access error to controller --
|
313 |
|
|
ma_load_o <= d_arbiter.rd_req and d_arbiter.err_align;
|
314 |
|
|
be_load_o <= d_arbiter.rd_req and d_arbiter.err_bus;
|
315 |
|
|
ma_store_o <= d_arbiter.wr_req and d_arbiter.err_align;
|
316 |
|
|
be_store_o <= d_arbiter.wr_req and d_arbiter.err_bus;
|
317 |
|
|
|
318 |
|
|
-- data bus (read/write)--
|
319 |
|
|
d_bus_addr_o <= mar;
|
320 |
|
|
d_bus_wdata_o <= d_bus_wdata;
|
321 |
|
|
d_bus_ben_o <= d_bus_ben;
|
322 |
47 |
zero_gravi |
d_bus_we <= ctrl_i(ctrl_bus_wr_c) and (not d_misaligned) and (not st_pmp_fault); -- no actual write when misaligned or PMP fault
|
323 |
|
|
d_bus_re <= ctrl_i(ctrl_bus_rd_c) and (not d_misaligned) and (not ld_pmp_fault); -- no actual read when misaligned or PMP fault
|
324 |
|
|
d_bus_we_o <= d_bus_we_buf when (PMP_NUM_REGIONS > pmp_num_regions_critical_c) else d_bus_we;
|
325 |
|
|
d_bus_re_o <= d_bus_re_buf when (PMP_NUM_REGIONS > pmp_num_regions_critical_c) else d_bus_re;
|
326 |
39 |
zero_gravi |
d_bus_fence_o <= ctrl_i(ctrl_bus_fence_c);
|
327 |
|
|
d_bus_rdata <= d_bus_rdata_i;
|
328 |
|
|
|
329 |
73 |
zero_gravi |
-- check data access address alignment --
|
330 |
|
|
misaligned_d_check: process(mar, ctrl_i)
|
331 |
|
|
begin
|
332 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
|
333 |
|
|
when "00" => -- byte
|
334 |
|
|
d_misaligned <= '0';
|
335 |
|
|
when "01" => -- half-word
|
336 |
|
|
if (mar(0) /= '0') then
|
337 |
|
|
d_misaligned <= '1';
|
338 |
|
|
else
|
339 |
|
|
d_misaligned <= '0';
|
340 |
|
|
end if;
|
341 |
|
|
when others => -- word
|
342 |
|
|
if (mar(1 downto 0) /= "00") then
|
343 |
|
|
d_misaligned <= '1';
|
344 |
|
|
else
|
345 |
|
|
d_misaligned <= '0';
|
346 |
|
|
end if;
|
347 |
|
|
end case;
|
348 |
|
|
end process misaligned_d_check;
|
349 |
|
|
|
350 |
47 |
zero_gravi |
-- additional register stage for control signals if using PMP_NUM_REGIONS > pmp_num_regions_critical_c --
|
351 |
|
|
pmp_dbus_buffer: process(rstn_i, clk_i)
|
352 |
|
|
begin
|
353 |
|
|
if (rstn_i = '0') then
|
354 |
|
|
d_bus_we_buf <= '0';
|
355 |
|
|
d_bus_re_buf <= '0';
|
356 |
|
|
elsif rising_edge(clk_i) then
|
357 |
|
|
d_bus_we_buf <= d_bus_we;
|
358 |
|
|
d_bus_re_buf <= d_bus_re;
|
359 |
|
|
end if;
|
360 |
|
|
end process pmp_dbus_buffer;
|
361 |
39 |
zero_gravi |
|
362 |
57 |
zero_gravi |
|
363 |
|
|
-- Reservation Controller (LR/SC [A extension]) -------------------------------------------
|
364 |
|
|
-- -------------------------------------------------------------------------------------------
|
365 |
|
|
exclusive_access_controller: process(rstn_i, clk_i)
|
366 |
53 |
zero_gravi |
begin
|
367 |
|
|
if (rstn_i = '0') then
|
368 |
57 |
zero_gravi |
exclusive_lock <= '0';
|
369 |
53 |
zero_gravi |
elsif rising_edge(clk_i) then
|
370 |
|
|
if (CPU_EXTENSION_RISCV_A = true) then
|
371 |
57 |
zero_gravi |
if (ctrl_i(ctrl_trap_c) = '1') or (ctrl_i(ctrl_bus_de_lock_c) = '1') then -- remove lock if entering a trap or executing a non-load-reservate memory access
|
372 |
|
|
exclusive_lock <= '0';
|
373 |
|
|
elsif (ctrl_i(ctrl_bus_lock_c) = '1') then -- set new lock
|
374 |
|
|
exclusive_lock <= '1';
|
375 |
53 |
zero_gravi |
end if;
|
376 |
|
|
else
|
377 |
57 |
zero_gravi |
exclusive_lock <= '0';
|
378 |
53 |
zero_gravi |
end if;
|
379 |
|
|
end if;
|
380 |
57 |
zero_gravi |
end process exclusive_access_controller;
|
381 |
47 |
zero_gravi |
|
382 |
57 |
zero_gravi |
-- lock status for SC operation --
|
383 |
|
|
exclusive_lock_status(data_width_c-1 downto 1) <= (others => '0');
|
384 |
|
|
exclusive_lock_status(0) <= not exclusive_lock;
|
385 |
53 |
zero_gravi |
|
386 |
57 |
zero_gravi |
-- output reservation status to control unit (to check if SC should write at all) --
|
387 |
|
|
excl_state_o <= exclusive_lock;
|
388 |
|
|
|
389 |
|
|
-- output to memory system --
|
390 |
64 |
zero_gravi |
i_bus_lock_o <= '0'; -- instruction fetches cannot be locked
|
391 |
57 |
zero_gravi |
d_bus_lock_o <= exclusive_lock;
|
392 |
|
|
|
393 |
|
|
|
394 |
12 |
zero_gravi |
-- Instruction Fetch Arbiter --------------------------------------------------------------
|
395 |
|
|
-- -------------------------------------------------------------------------------------------
|
396 |
73 |
zero_gravi |
-- controlled by pipeline FRONT-end --
|
397 |
38 |
zero_gravi |
ifetch_arbiter: process(rstn_i, clk_i)
|
398 |
12 |
zero_gravi |
begin
|
399 |
38 |
zero_gravi |
if (rstn_i = '0') then
|
400 |
|
|
i_arbiter.rd_req <= '0';
|
401 |
|
|
i_arbiter.err_align <= '0';
|
402 |
|
|
i_arbiter.err_bus <= '0';
|
403 |
|
|
elsif rising_edge(clk_i) then
|
404 |
12 |
zero_gravi |
-- instruction fetch request --
|
405 |
|
|
if (i_arbiter.rd_req = '0') then -- idle
|
406 |
|
|
i_arbiter.rd_req <= ctrl_i(ctrl_bus_if_c);
|
407 |
73 |
zero_gravi |
i_arbiter.err_align <= '0';
|
408 |
12 |
zero_gravi |
i_arbiter.err_bus <= '0';
|
409 |
73 |
zero_gravi |
else -- in progress, accumulate errors
|
410 |
|
|
i_arbiter.err_align <= i_arbiter.err_align or i_misaligned;
|
411 |
|
|
i_arbiter.err_bus <= i_arbiter.err_bus or i_bus_err_i or if_pmp_fault;
|
412 |
|
|
if (i_bus_ack_i = '1') or (i_arbiter.err_align = '1') or (i_arbiter.err_bus = '1') then -- wait for ACK or ERROR
|
413 |
23 |
zero_gravi |
i_arbiter.rd_req <= '0';
|
414 |
2 |
zero_gravi |
end if;
|
415 |
|
|
end if;
|
416 |
|
|
end if;
|
417 |
12 |
zero_gravi |
end process ifetch_arbiter;
|
418 |
2 |
zero_gravi |
|
419 |
36 |
zero_gravi |
i_arbiter.wr_req <= '0'; -- instruction fetch is read-only
|
420 |
|
|
|
421 |
12 |
zero_gravi |
-- wait for bus transaction to finish --
|
422 |
|
|
i_wait_o <= i_arbiter.rd_req and (not i_bus_ack_i);
|
423 |
2 |
zero_gravi |
|
424 |
12 |
zero_gravi |
-- output instruction fetch error to controller --
|
425 |
|
|
ma_instr_o <= i_arbiter.err_align;
|
426 |
|
|
be_instr_o <= i_arbiter.err_bus;
|
427 |
11 |
zero_gravi |
|
428 |
12 |
zero_gravi |
-- instruction bus (read-only) --
|
429 |
31 |
zero_gravi |
i_bus_addr_o <= fetch_pc_i(data_width_c-1 downto 2) & "00"; -- instruction access is always 4-byte aligned (even for compressed instructions)
|
430 |
40 |
zero_gravi |
i_bus_wdata_o <= (others => '0'); -- instruction fetch is read-only
|
431 |
12 |
zero_gravi |
i_bus_ben_o <= (others => '0');
|
432 |
|
|
i_bus_we_o <= '0';
|
433 |
47 |
zero_gravi |
i_bus_re <= ctrl_i(ctrl_bus_if_c) and (not i_misaligned) and (not if_pmp_fault); -- no actual read when misaligned or PMP fault
|
434 |
|
|
i_bus_re_o <= i_bus_re_buf when (PMP_NUM_REGIONS > pmp_num_regions_critical_c) else i_bus_re;
|
435 |
12 |
zero_gravi |
i_bus_fence_o <= ctrl_i(ctrl_bus_fencei_c);
|
436 |
|
|
instr_o <= i_bus_rdata_i;
|
437 |
2 |
zero_gravi |
|
438 |
64 |
zero_gravi |
-- check instruction access address alignment --
|
439 |
39 |
zero_gravi |
i_misaligned <= '0' when (CPU_EXTENSION_RISCV_C = true) else -- no alignment exceptions possible when using C-extension
|
440 |
|
|
'1' when (fetch_pc_i(1) = '1') else '0'; -- 32-bit accesses only
|
441 |
2 |
zero_gravi |
|
442 |
47 |
zero_gravi |
-- additional register stage for control signals if using PMP_NUM_REGIONS > pmp_num_regions_critical_c --
|
443 |
|
|
pmp_ibus_buffer: process(rstn_i, clk_i)
|
444 |
|
|
begin
|
445 |
|
|
if (rstn_i = '0') then
|
446 |
|
|
i_bus_re_buf <= '0';
|
447 |
|
|
elsif rising_edge(clk_i) then
|
448 |
|
|
i_bus_re_buf <= i_bus_re;
|
449 |
|
|
end if;
|
450 |
|
|
end process pmp_ibus_buffer;
|
451 |
2 |
zero_gravi |
|
452 |
47 |
zero_gravi |
|
453 |
15 |
zero_gravi |
-- Physical Memory Protection (PMP) -------------------------------------------------------
|
454 |
|
|
-- -------------------------------------------------------------------------------------------
|
455 |
73 |
zero_gravi |
-- check access address region --
|
456 |
|
|
pmp_check_address: process(pmp_addr_i, fetch_pc_i, mar)
|
457 |
15 |
zero_gravi |
begin
|
458 |
73 |
zero_gravi |
for i in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
|
459 |
|
|
if (i = 0) then -- use ZERO as bottom boundary and current entry as top boundary for first entry
|
460 |
|
|
pmp.i_match(i) <= bool_to_ulogic_f(unsigned(fetch_pc_i(data_width_c-1 downto pmp_lsb_c)) < unsigned(pmp_addr_i(0)(data_width_c-1 downto pmp_lsb_c)));
|
461 |
|
|
pmp.d_match(i) <= bool_to_ulogic_f(unsigned(mar(data_width_c-1 downto pmp_lsb_c)) < unsigned(pmp_addr_i(0)(data_width_c-1 downto pmp_lsb_c)));
|
462 |
|
|
else -- use previous entry as bottom boundary and current entry as top boundary
|
463 |
|
|
pmp.i_match(i) <= bool_to_ulogic_f((unsigned(pmp_addr_i(i-1)(data_width_c-1 downto pmp_lsb_c)) <= unsigned(fetch_pc_i(data_width_c-1 downto pmp_lsb_c))) and
|
464 |
|
|
(unsigned(fetch_pc_i(data_width_c-1 downto pmp_lsb_c)) < unsigned(pmp_addr_i(i)(data_width_c-1 downto pmp_lsb_c))));
|
465 |
|
|
pmp.d_match(i) <= bool_to_ulogic_f((unsigned(pmp_addr_i(i-1)(data_width_c-1 downto pmp_lsb_c)) <= unsigned(mar(data_width_c-1 downto pmp_lsb_c))) and
|
466 |
|
|
(unsigned(mar(data_width_c-1 downto pmp_lsb_c)) < unsigned(pmp_addr_i(i)(data_width_c-1 downto pmp_lsb_c))));
|
467 |
|
|
end if;
|
468 |
|
|
end loop; -- i
|
469 |
|
|
end process pmp_check_address;
|
470 |
15 |
zero_gravi |
|
471 |
73 |
zero_gravi |
-- check access type and permissions --
|
472 |
36 |
zero_gravi |
pmp_check_permission: process(pmp, pmp_ctrl_i, ctrl_i)
|
473 |
15 |
zero_gravi |
begin
|
474 |
73 |
zero_gravi |
pmp.if_fault <= (others => '0');
|
475 |
|
|
pmp.ld_fault <= (others => '0');
|
476 |
|
|
pmp.st_fault <= (others => '0');
|
477 |
|
|
for i in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
|
478 |
|
|
if ((ctrl_i(ctrl_priv_mode_c) = priv_mode_u_c) or (pmp_ctrl_i(i)(pmp_cfg_l_c) = '1')) and -- enforce if USER-mode or LOCKED
|
479 |
|
|
(pmp_ctrl_i(i)(pmp_cfg_ah_c downto pmp_cfg_al_c) = pmp_tor_mode_c) and -- active entry
|
480 |
59 |
zero_gravi |
(ctrl_i(ctrl_debug_running_c) = '0') then -- disable PMP checks when in debug mode
|
481 |
73 |
zero_gravi |
pmp.if_fault(i) <= pmp.i_match(i) and (not pmp_ctrl_i(i)(pmp_cfg_x_c)); -- fetch access match no execute permission
|
482 |
|
|
pmp.ld_fault(i) <= pmp.d_match(i) and (not pmp_ctrl_i(i)(pmp_cfg_r_c)); -- load access match no read permission
|
483 |
|
|
pmp.st_fault(i) <= pmp.d_match(i) and (not pmp_ctrl_i(i)(pmp_cfg_w_c)); -- store access match no write permission
|
484 |
15 |
zero_gravi |
end if;
|
485 |
73 |
zero_gravi |
end loop; -- i
|
486 |
15 |
zero_gravi |
end process pmp_check_permission;
|
487 |
|
|
|
488 |
|
|
-- final PMP access fault signals --
|
489 |
60 |
zero_gravi |
if_pmp_fault <= or_reduce_f(pmp.if_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
490 |
|
|
ld_pmp_fault <= or_reduce_f(pmp.ld_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
491 |
|
|
st_pmp_fault <= or_reduce_f(pmp.st_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
492 |
15 |
zero_gravi |
|
493 |
|
|
|
494 |
2 |
zero_gravi |
end neorv32_cpu_bus_rtl;
|