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 |
42 |
zero_gravi |
-- # Copyright (c) 2021, 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 |
62 |
zero_gravi |
PMP_NUM_REGIONS : natural; -- number of regions (0..64)
|
50 |
|
|
PMP_MIN_GRANULARITY : natural -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
|
51 |
2 |
zero_gravi |
);
|
52 |
|
|
port (
|
53 |
|
|
-- global control --
|
54 |
12 |
zero_gravi |
clk_i : in std_ulogic; -- global clock, rising edge
|
55 |
38 |
zero_gravi |
rstn_i : in std_ulogic := '0'; -- global reset, low-active, async
|
56 |
12 |
zero_gravi |
ctrl_i : in std_ulogic_vector(ctrl_width_c-1 downto 0); -- main control bus
|
57 |
|
|
-- cpu instruction fetch interface --
|
58 |
|
|
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 |
|
|
--
|
62 |
|
|
ma_instr_o : out std_ulogic; -- misaligned instruction address
|
63 |
|
|
be_instr_o : out std_ulogic; -- bus error on instruction access
|
64 |
|
|
-- cpu data access interface --
|
65 |
|
|
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 |
|
|
--
|
71 |
57 |
zero_gravi |
excl_state_o : out std_ulogic; -- atomic/exclusive access status
|
72 |
12 |
zero_gravi |
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 |
|
|
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 |
|
|
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 |
57 |
zero_gravi |
i_bus_lock_o : out std_ulogic; -- exclusive access request
|
87 |
12 |
zero_gravi |
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 |
|
|
-- data bus --
|
91 |
|
|
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 |
57 |
zero_gravi |
d_bus_lock_o : out std_ulogic; -- exclusive access request
|
98 |
12 |
zero_gravi |
d_bus_ack_i : in std_ulogic; -- bus transfer acknowledge
|
99 |
|
|
d_bus_err_i : in std_ulogic; -- bus transfer error
|
100 |
57 |
zero_gravi |
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 |
36 |
zero_gravi |
--constant pmp_tor_mode_c : std_ulogic_vector(1 downto 0) := "01"; -- top of range
|
109 |
|
|
--constant pmp_na4_mode_c : std_ulogic_vector(1 downto 0) := "10"; -- naturally aligned four-byte region
|
110 |
15 |
zero_gravi |
constant pmp_napot_mode_c : std_ulogic_vector(1 downto 0) := "11"; -- naturally aligned power-of-two region (>= 8 bytes)
|
111 |
|
|
|
112 |
40 |
zero_gravi |
-- PMP granularity --
|
113 |
42 |
zero_gravi |
constant pmp_g_c : natural := index_size_f(PMP_MIN_GRANULARITY);
|
114 |
40 |
zero_gravi |
|
115 |
15 |
zero_gravi |
-- PMP configuration register bits --
|
116 |
|
|
constant pmp_cfg_r_c : natural := 0; -- read permit
|
117 |
|
|
constant pmp_cfg_w_c : natural := 1; -- write permit
|
118 |
|
|
constant pmp_cfg_x_c : natural := 2; -- execute permit
|
119 |
|
|
constant pmp_cfg_al_c : natural := 3; -- mode bit low
|
120 |
|
|
constant pmp_cfg_ah_c : natural := 4; -- mode bit high
|
121 |
62 |
zero_gravi |
--
|
122 |
15 |
zero_gravi |
constant pmp_cfg_l_c : natural := 7; -- locked entry
|
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 |
42 |
zero_gravi |
type pmp_addr_t is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(data_width_c-1 downto 0);
|
151 |
15 |
zero_gravi |
type pmp_t is record
|
152 |
40 |
zero_gravi |
addr_mask : pmp_addr_t;
|
153 |
|
|
region_base : pmp_addr_t; -- region config base address
|
154 |
16 |
zero_gravi |
region_i_addr : pmp_addr_t; -- masked instruction access base address for comparator
|
155 |
|
|
region_d_addr : pmp_addr_t; -- masked data access base address for comparator
|
156 |
42 |
zero_gravi |
i_match : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for instruction interface
|
157 |
|
|
d_match : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for data interface
|
158 |
|
|
if_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for fetch operation
|
159 |
|
|
ld_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for load operation
|
160 |
|
|
st_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for store operation
|
161 |
15 |
zero_gravi |
end record;
|
162 |
|
|
signal pmp : pmp_t;
|
163 |
|
|
|
164 |
47 |
zero_gravi |
-- memory control signal buffer (when using PMP) --
|
165 |
|
|
signal d_bus_we, d_bus_we_buf : std_ulogic;
|
166 |
|
|
signal d_bus_re, d_bus_re_buf : std_ulogic;
|
167 |
|
|
signal i_bus_re, i_bus_re_buf : std_ulogic;
|
168 |
|
|
|
169 |
|
|
-- pmp faults anyone? --
|
170 |
15 |
zero_gravi |
signal if_pmp_fault : std_ulogic; -- pmp instruction access fault
|
171 |
|
|
signal ld_pmp_fault : std_ulogic; -- pmp load access fault
|
172 |
|
|
signal st_pmp_fault : std_ulogic; -- pmp store access fault
|
173 |
|
|
|
174 |
2 |
zero_gravi |
begin
|
175 |
|
|
|
176 |
47 |
zero_gravi |
-- Sanity Checks --------------------------------------------------------------------------
|
177 |
|
|
-- -------------------------------------------------------------------------------------------
|
178 |
|
|
assert not (PMP_NUM_REGIONS > pmp_num_regions_critical_c) report "NEORV32 CPU CONFIG WARNING! Number of implemented PMP regions (PMP_NUM_REGIONS = " & integer'image(PMP_NUM_REGIONS) & ") beyond critical limit (pmp_num_regions_critical_c = " & integer'image(pmp_num_regions_critical_c) & "). Inserting another register stage (that will increase memory latency by +1 cycle)." severity warning;
|
179 |
|
|
|
180 |
|
|
|
181 |
12 |
zero_gravi |
-- Data Interface: Access Address ---------------------------------------------------------
|
182 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
183 |
56 |
zero_gravi |
mem_adr_reg: process(rstn_i, clk_i)
|
184 |
2 |
zero_gravi |
begin
|
185 |
56 |
zero_gravi |
if (rstn_i = '0') then
|
186 |
|
|
mar <= (others => def_rst_val_c);
|
187 |
|
|
elsif rising_edge(clk_i) then
|
188 |
39 |
zero_gravi |
if (ctrl_i(ctrl_bus_mo_we_c) = '1') then
|
189 |
12 |
zero_gravi |
mar <= addr_i;
|
190 |
2 |
zero_gravi |
end if;
|
191 |
|
|
end if;
|
192 |
|
|
end process mem_adr_reg;
|
193 |
|
|
|
194 |
12 |
zero_gravi |
-- read-back for exception controller --
|
195 |
|
|
mar_o <= mar;
|
196 |
2 |
zero_gravi |
|
197 |
12 |
zero_gravi |
-- alignment check --
|
198 |
|
|
misaligned_d_check: process(mar, ctrl_i)
|
199 |
|
|
begin
|
200 |
|
|
-- check data access --
|
201 |
|
|
d_misaligned <= '0'; -- default
|
202 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
|
203 |
|
|
when "00" => -- byte
|
204 |
|
|
d_misaligned <= '0';
|
205 |
|
|
when "01" => -- half-word
|
206 |
|
|
if (mar(0) /= '0') then
|
207 |
|
|
d_misaligned <= '1';
|
208 |
|
|
end if;
|
209 |
|
|
when others => -- word
|
210 |
|
|
if (mar(1 downto 0) /= "00") then
|
211 |
|
|
d_misaligned <= '1';
|
212 |
|
|
end if;
|
213 |
|
|
end case;
|
214 |
|
|
end process misaligned_d_check;
|
215 |
2 |
zero_gravi |
|
216 |
|
|
|
217 |
12 |
zero_gravi |
-- Data Interface: Write Data -------------------------------------------------------------
|
218 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
219 |
56 |
zero_gravi |
mem_do_reg: process(rstn_i, clk_i)
|
220 |
2 |
zero_gravi |
begin
|
221 |
56 |
zero_gravi |
if (rstn_i = '0') then
|
222 |
|
|
mdo <= (others => def_rst_val_c);
|
223 |
|
|
elsif rising_edge(clk_i) then
|
224 |
39 |
zero_gravi |
if (ctrl_i(ctrl_bus_mo_we_c) = '1') then
|
225 |
40 |
zero_gravi |
mdo <= wdata_i; -- memory data output register (MDO)
|
226 |
2 |
zero_gravi |
end if;
|
227 |
|
|
end if;
|
228 |
|
|
end process mem_do_reg;
|
229 |
|
|
|
230 |
|
|
-- byte enable and output data alignment --
|
231 |
|
|
byte_enable: process(mar, mdo, ctrl_i)
|
232 |
|
|
begin
|
233 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is -- data size
|
234 |
|
|
when "00" => -- byte
|
235 |
12 |
zero_gravi |
d_bus_wdata(07 downto 00) <= mdo(07 downto 00);
|
236 |
|
|
d_bus_wdata(15 downto 08) <= mdo(07 downto 00);
|
237 |
|
|
d_bus_wdata(23 downto 16) <= mdo(07 downto 00);
|
238 |
|
|
d_bus_wdata(31 downto 24) <= mdo(07 downto 00);
|
239 |
36 |
zero_gravi |
case mar(1 downto 0) is
|
240 |
|
|
when "00" => d_bus_ben <= "0001";
|
241 |
|
|
when "01" => d_bus_ben <= "0010";
|
242 |
|
|
when "10" => d_bus_ben <= "0100";
|
243 |
|
|
when others => d_bus_ben <= "1000";
|
244 |
|
|
end case;
|
245 |
2 |
zero_gravi |
when "01" => -- half-word
|
246 |
12 |
zero_gravi |
d_bus_wdata(31 downto 16) <= mdo(15 downto 00);
|
247 |
|
|
d_bus_wdata(15 downto 00) <= mdo(15 downto 00);
|
248 |
2 |
zero_gravi |
if (mar(1) = '0') then
|
249 |
12 |
zero_gravi |
d_bus_ben <= "0011"; -- low half-word
|
250 |
2 |
zero_gravi |
else
|
251 |
12 |
zero_gravi |
d_bus_ben <= "1100"; -- high half-word
|
252 |
2 |
zero_gravi |
end if;
|
253 |
|
|
when others => -- word
|
254 |
12 |
zero_gravi |
d_bus_wdata <= mdo;
|
255 |
|
|
d_bus_ben <= "1111"; -- full word
|
256 |
2 |
zero_gravi |
end case;
|
257 |
|
|
end process byte_enable;
|
258 |
|
|
|
259 |
|
|
|
260 |
12 |
zero_gravi |
-- Data Interface: Read Data --------------------------------------------------------------
|
261 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
262 |
57 |
zero_gravi |
mem_di_reg: process(rstn_i, clk_i)
|
263 |
2 |
zero_gravi |
begin
|
264 |
56 |
zero_gravi |
if (rstn_i = '0') then
|
265 |
|
|
mdi <= (others => def_rst_val_c);
|
266 |
|
|
elsif rising_edge(clk_i) then
|
267 |
39 |
zero_gravi |
if (ctrl_i(ctrl_bus_mi_we_c) = '1') then
|
268 |
40 |
zero_gravi |
mdi <= d_bus_rdata; -- memory data input register (MDI)
|
269 |
2 |
zero_gravi |
end if;
|
270 |
|
|
end if;
|
271 |
57 |
zero_gravi |
end process mem_di_reg;
|
272 |
2 |
zero_gravi |
|
273 |
12 |
zero_gravi |
-- input data alignment and sign extension --
|
274 |
2 |
zero_gravi |
read_align: process(mdi, mar, ctrl_i)
|
275 |
36 |
zero_gravi |
variable byte_in_v : std_ulogic_vector(07 downto 0);
|
276 |
|
|
variable hword_in_v : std_ulogic_vector(15 downto 0);
|
277 |
2 |
zero_gravi |
begin
|
278 |
36 |
zero_gravi |
-- sub-word input --
|
279 |
|
|
case mar(1 downto 0) is
|
280 |
|
|
when "00" => byte_in_v := mdi(07 downto 00); hword_in_v := mdi(15 downto 00); -- byte 0 / half-word 0
|
281 |
|
|
when "01" => byte_in_v := mdi(15 downto 08); hword_in_v := mdi(15 downto 00); -- byte 1 / half-word 0
|
282 |
|
|
when "10" => byte_in_v := mdi(23 downto 16); hword_in_v := mdi(31 downto 16); -- byte 2 / half-word 1
|
283 |
|
|
when others => byte_in_v := mdi(31 downto 24); hword_in_v := mdi(31 downto 16); -- byte 3 / half-word 1
|
284 |
|
|
end case;
|
285 |
|
|
-- actual data size --
|
286 |
|
|
case ctrl_i(ctrl_bus_size_msb_c downto ctrl_bus_size_lsb_c) is
|
287 |
2 |
zero_gravi |
when "00" => -- byte
|
288 |
57 |
zero_gravi |
rdata_align(31 downto 08) <= (others => ((not ctrl_i(ctrl_bus_unsigned_c)) and byte_in_v(7))); -- sign extension
|
289 |
|
|
rdata_align(07 downto 00) <= byte_in_v;
|
290 |
2 |
zero_gravi |
when "01" => -- half-word
|
291 |
57 |
zero_gravi |
rdata_align(31 downto 16) <= (others => ((not ctrl_i(ctrl_bus_unsigned_c)) and hword_in_v(15))); -- sign extension
|
292 |
|
|
rdata_align(15 downto 00) <= hword_in_v; -- high half-word
|
293 |
2 |
zero_gravi |
when others => -- word
|
294 |
57 |
zero_gravi |
rdata_align <= mdi; -- full word
|
295 |
2 |
zero_gravi |
end case;
|
296 |
|
|
end process read_align;
|
297 |
|
|
|
298 |
57 |
zero_gravi |
-- insert exclusive lock status for SC operations only --
|
299 |
|
|
rdata_o <= exclusive_lock_status when (CPU_EXTENSION_RISCV_A = true) and (ctrl_i(ctrl_bus_ch_lock_c) = '1') else rdata_align;
|
300 |
2 |
zero_gravi |
|
301 |
57 |
zero_gravi |
|
302 |
39 |
zero_gravi |
-- Data Access Arbiter --------------------------------------------------------------------
|
303 |
2 |
zero_gravi |
-- -------------------------------------------------------------------------------------------
|
304 |
39 |
zero_gravi |
data_access_arbiter: process(rstn_i, clk_i)
|
305 |
2 |
zero_gravi |
begin
|
306 |
39 |
zero_gravi |
if (rstn_i = '0') then
|
307 |
|
|
d_arbiter.wr_req <= '0';
|
308 |
|
|
d_arbiter.rd_req <= '0';
|
309 |
|
|
d_arbiter.err_align <= '0';
|
310 |
|
|
d_arbiter.err_bus <= '0';
|
311 |
|
|
elsif rising_edge(clk_i) then
|
312 |
|
|
-- data access request --
|
313 |
|
|
if (d_arbiter.wr_req = '0') and (d_arbiter.rd_req = '0') then -- idle
|
314 |
|
|
d_arbiter.wr_req <= ctrl_i(ctrl_bus_wr_c);
|
315 |
|
|
d_arbiter.rd_req <= ctrl_i(ctrl_bus_rd_c);
|
316 |
|
|
d_arbiter.err_align <= d_misaligned;
|
317 |
|
|
d_arbiter.err_bus <= '0';
|
318 |
|
|
else -- in progress
|
319 |
40 |
zero_gravi |
d_arbiter.err_align <= (d_arbiter.err_align or d_misaligned) and (not ctrl_i(ctrl_bus_derr_ack_c));
|
320 |
57 |
zero_gravi |
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)) and
|
321 |
|
|
(not ctrl_i(ctrl_bus_derr_ack_c));
|
322 |
39 |
zero_gravi |
if (d_bus_ack_i = '1') or (ctrl_i(ctrl_bus_derr_ack_c) = '1') then -- wait for normal termination / CPU abort
|
323 |
|
|
d_arbiter.wr_req <= '0';
|
324 |
|
|
d_arbiter.rd_req <= '0';
|
325 |
|
|
end if;
|
326 |
|
|
end if;
|
327 |
12 |
zero_gravi |
end if;
|
328 |
39 |
zero_gravi |
end process data_access_arbiter;
|
329 |
12 |
zero_gravi |
|
330 |
39 |
zero_gravi |
-- wait for bus transaction to finish --
|
331 |
|
|
d_wait_o <= (d_arbiter.wr_req or d_arbiter.rd_req) and (not d_bus_ack_i);
|
332 |
|
|
|
333 |
|
|
-- output data access error to controller --
|
334 |
|
|
ma_load_o <= d_arbiter.rd_req and d_arbiter.err_align;
|
335 |
|
|
be_load_o <= d_arbiter.rd_req and d_arbiter.err_bus;
|
336 |
|
|
ma_store_o <= d_arbiter.wr_req and d_arbiter.err_align;
|
337 |
|
|
be_store_o <= d_arbiter.wr_req and d_arbiter.err_bus;
|
338 |
|
|
|
339 |
|
|
-- data bus (read/write)--
|
340 |
|
|
d_bus_addr_o <= mar;
|
341 |
|
|
d_bus_wdata_o <= d_bus_wdata;
|
342 |
|
|
d_bus_ben_o <= d_bus_ben;
|
343 |
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
|
344 |
|
|
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
|
345 |
|
|
d_bus_we_o <= d_bus_we_buf when (PMP_NUM_REGIONS > pmp_num_regions_critical_c) else d_bus_we;
|
346 |
|
|
d_bus_re_o <= d_bus_re_buf when (PMP_NUM_REGIONS > pmp_num_regions_critical_c) else d_bus_re;
|
347 |
39 |
zero_gravi |
d_bus_fence_o <= ctrl_i(ctrl_bus_fence_c);
|
348 |
|
|
d_bus_rdata <= d_bus_rdata_i;
|
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 |
|
|
i_bus_lock_o <= '0'; -- instruction fetches cannot be lockes
|
391 |
|
|
d_bus_lock_o <= exclusive_lock;
|
392 |
|
|
|
393 |
|
|
|
394 |
12 |
zero_gravi |
-- Instruction Fetch Arbiter --------------------------------------------------------------
|
395 |
|
|
-- -------------------------------------------------------------------------------------------
|
396 |
38 |
zero_gravi |
ifetch_arbiter: process(rstn_i, clk_i)
|
397 |
12 |
zero_gravi |
begin
|
398 |
38 |
zero_gravi |
if (rstn_i = '0') then
|
399 |
|
|
i_arbiter.rd_req <= '0';
|
400 |
|
|
i_arbiter.err_align <= '0';
|
401 |
|
|
i_arbiter.err_bus <= '0';
|
402 |
|
|
elsif rising_edge(clk_i) then
|
403 |
12 |
zero_gravi |
-- instruction fetch request --
|
404 |
|
|
if (i_arbiter.rd_req = '0') then -- idle
|
405 |
|
|
i_arbiter.rd_req <= ctrl_i(ctrl_bus_if_c);
|
406 |
|
|
i_arbiter.err_align <= i_misaligned;
|
407 |
|
|
i_arbiter.err_bus <= '0';
|
408 |
57 |
zero_gravi |
else -- in progres
|
409 |
|
|
i_arbiter.err_align <= (i_arbiter.err_align or i_misaligned) and (not ctrl_i(ctrl_bus_ierr_ack_c));
|
410 |
|
|
i_arbiter.err_bus <= (i_arbiter.err_bus or i_bus_err_i or if_pmp_fault) and (not ctrl_i(ctrl_bus_ierr_ack_c));
|
411 |
28 |
zero_gravi |
if (i_bus_ack_i = '1') or (ctrl_i(ctrl_bus_ierr_ack_c) = '1') then -- wait for normal termination / CPU abort
|
412 |
23 |
zero_gravi |
i_arbiter.rd_req <= '0';
|
413 |
2 |
zero_gravi |
end if;
|
414 |
|
|
end if;
|
415 |
|
|
end if;
|
416 |
12 |
zero_gravi |
end process ifetch_arbiter;
|
417 |
2 |
zero_gravi |
|
418 |
36 |
zero_gravi |
i_arbiter.wr_req <= '0'; -- instruction fetch is read-only
|
419 |
|
|
|
420 |
12 |
zero_gravi |
-- wait for bus transaction to finish --
|
421 |
|
|
i_wait_o <= i_arbiter.rd_req and (not i_bus_ack_i);
|
422 |
2 |
zero_gravi |
|
423 |
12 |
zero_gravi |
-- output instruction fetch error to controller --
|
424 |
|
|
ma_instr_o <= i_arbiter.err_align;
|
425 |
|
|
be_instr_o <= i_arbiter.err_bus;
|
426 |
11 |
zero_gravi |
|
427 |
12 |
zero_gravi |
-- instruction bus (read-only) --
|
428 |
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)
|
429 |
40 |
zero_gravi |
i_bus_wdata_o <= (others => '0'); -- instruction fetch is read-only
|
430 |
12 |
zero_gravi |
i_bus_ben_o <= (others => '0');
|
431 |
|
|
i_bus_we_o <= '0';
|
432 |
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
|
433 |
|
|
i_bus_re_o <= i_bus_re_buf when (PMP_NUM_REGIONS > pmp_num_regions_critical_c) else i_bus_re;
|
434 |
12 |
zero_gravi |
i_bus_fence_o <= ctrl_i(ctrl_bus_fencei_c);
|
435 |
|
|
instr_o <= i_bus_rdata_i;
|
436 |
2 |
zero_gravi |
|
437 |
39 |
zero_gravi |
-- check instruction access --
|
438 |
|
|
i_misaligned <= '0' when (CPU_EXTENSION_RISCV_C = true) else -- no alignment exceptions possible when using C-extension
|
439 |
|
|
'1' when (fetch_pc_i(1) = '1') else '0'; -- 32-bit accesses only
|
440 |
2 |
zero_gravi |
|
441 |
47 |
zero_gravi |
-- additional register stage for control signals if using PMP_NUM_REGIONS > pmp_num_regions_critical_c --
|
442 |
|
|
pmp_ibus_buffer: process(rstn_i, clk_i)
|
443 |
|
|
begin
|
444 |
|
|
if (rstn_i = '0') then
|
445 |
|
|
i_bus_re_buf <= '0';
|
446 |
|
|
elsif rising_edge(clk_i) then
|
447 |
|
|
i_bus_re_buf <= i_bus_re;
|
448 |
|
|
end if;
|
449 |
|
|
end process pmp_ibus_buffer;
|
450 |
2 |
zero_gravi |
|
451 |
47 |
zero_gravi |
|
452 |
15 |
zero_gravi |
-- Physical Memory Protection (PMP) -------------------------------------------------------
|
453 |
|
|
-- -------------------------------------------------------------------------------------------
|
454 |
40 |
zero_gravi |
-- compute address masks (ITERATIVE!!!) --
|
455 |
56 |
zero_gravi |
pmp_masks: process(rstn_i, clk_i)
|
456 |
15 |
zero_gravi |
begin
|
457 |
56 |
zero_gravi |
if (rstn_i = '0') then
|
458 |
|
|
pmp.addr_mask <= (others => (others => def_rst_val_c));
|
459 |
|
|
elsif rising_edge(clk_i) then -- address mask computation (not the actual address check!) has a latency of max +32 cycles
|
460 |
42 |
zero_gravi |
for r in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
|
461 |
40 |
zero_gravi |
pmp.addr_mask(r) <= (others => '0');
|
462 |
|
|
for i in pmp_g_c to data_width_c-1 loop
|
463 |
|
|
pmp.addr_mask(r)(i) <= pmp.addr_mask(r)(i-1) or (not pmp_addr_i(r)(i-1));
|
464 |
17 |
zero_gravi |
end loop; -- i
|
465 |
|
|
end loop; -- r
|
466 |
|
|
end if;
|
467 |
15 |
zero_gravi |
end process pmp_masks;
|
468 |
|
|
|
469 |
|
|
|
470 |
40 |
zero_gravi |
-- address access check --
|
471 |
|
|
pmp_address_check:
|
472 |
42 |
zero_gravi |
for r in 0 to PMP_NUM_REGIONS-1 generate -- iterate over all regions
|
473 |
40 |
zero_gravi |
pmp.region_i_addr(r) <= fetch_pc_i and pmp.addr_mask(r);
|
474 |
|
|
pmp.region_d_addr(r) <= mar and pmp.addr_mask(r);
|
475 |
|
|
pmp.region_base(r) <= pmp_addr_i(r)(data_width_c+1 downto 2) and pmp.addr_mask(r);
|
476 |
|
|
--
|
477 |
|
|
pmp.i_match(r) <= '1' when (pmp.region_i_addr(r)(data_width_c-1 downto pmp_g_c) = pmp.region_base(r)(data_width_c-1 downto pmp_g_c)) else '0';
|
478 |
|
|
pmp.d_match(r) <= '1' when (pmp.region_d_addr(r)(data_width_c-1 downto pmp_g_c) = pmp.region_base(r)(data_width_c-1 downto pmp_g_c)) else '0';
|
479 |
16 |
zero_gravi |
end generate; -- r
|
480 |
15 |
zero_gravi |
|
481 |
|
|
|
482 |
61 |
zero_gravi |
-- check access type and region's permissions --
|
483 |
36 |
zero_gravi |
pmp_check_permission: process(pmp, pmp_ctrl_i, ctrl_i)
|
484 |
15 |
zero_gravi |
begin
|
485 |
42 |
zero_gravi |
for r in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
|
486 |
36 |
zero_gravi |
if ((ctrl_i(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c) = priv_mode_u_c) or (pmp_ctrl_i(r)(pmp_cfg_l_c) = '1')) and -- user privilege level or locked pmp entry -> enforce permissions also for machine mode
|
487 |
59 |
zero_gravi |
(pmp_ctrl_i(r)(pmp_cfg_ah_c downto pmp_cfg_al_c) /= pmp_off_mode_c) and -- active entry
|
488 |
|
|
(ctrl_i(ctrl_debug_running_c) = '0') then -- disable PMP checks when in debug mode
|
489 |
15 |
zero_gravi |
pmp.if_fault(r) <= pmp.i_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_x_c)); -- fetch access match no execute permission
|
490 |
|
|
pmp.ld_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_r_c)); -- load access match no read permission
|
491 |
|
|
pmp.st_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_w_c)); -- store access match no write permission
|
492 |
|
|
else
|
493 |
|
|
pmp.if_fault(r) <= '0';
|
494 |
|
|
pmp.ld_fault(r) <= '0';
|
495 |
|
|
pmp.st_fault(r) <= '0';
|
496 |
|
|
end if;
|
497 |
|
|
end loop; -- r
|
498 |
|
|
end process pmp_check_permission;
|
499 |
|
|
|
500 |
|
|
|
501 |
|
|
-- final PMP access fault signals --
|
502 |
60 |
zero_gravi |
if_pmp_fault <= or_reduce_f(pmp.if_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
503 |
|
|
ld_pmp_fault <= or_reduce_f(pmp.ld_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
504 |
|
|
st_pmp_fault <= or_reduce_f(pmp.st_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
505 |
15 |
zero_gravi |
|
506 |
|
|
|
507 |
2 |
zero_gravi |
end neorv32_cpu_bus_rtl;
|