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