1 |
45 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Processor-Internal Instruction Cache >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
47 |
zero_gravi |
-- # Direct mapped (ICACHE_NUM_SETS = 1) or 2-way set-associative (ICACHE_NUM_SETS = 2). #
|
5 |
|
|
-- # Least recently used replacement policy (if ICACHE_NUM_SETS > 1). #
|
6 |
45 |
zero_gravi |
-- # ********************************************************************************************* #
|
7 |
|
|
-- # BSD 3-Clause License #
|
8 |
|
|
-- # #
|
9 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
10 |
|
|
-- # #
|
11 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
12 |
|
|
-- # permitted provided that the following conditions are met: #
|
13 |
|
|
-- # #
|
14 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
15 |
|
|
-- # conditions and the following disclaimer. #
|
16 |
|
|
-- # #
|
17 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
18 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
19 |
|
|
-- # provided with the distribution. #
|
20 |
|
|
-- # #
|
21 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
22 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
23 |
|
|
-- # permission. #
|
24 |
|
|
-- # #
|
25 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
26 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
27 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
28 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
29 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
30 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
31 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
32 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
33 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
34 |
|
|
-- # ********************************************************************************************* #
|
35 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
36 |
|
|
-- #################################################################################################
|
37 |
|
|
|
38 |
|
|
library ieee;
|
39 |
|
|
use ieee.std_logic_1164.all;
|
40 |
|
|
use ieee.numeric_std.all;
|
41 |
|
|
|
42 |
|
|
library neorv32;
|
43 |
|
|
use neorv32.neorv32_package.all;
|
44 |
|
|
|
45 |
|
|
entity neorv32_icache is
|
46 |
|
|
generic (
|
47 |
47 |
zero_gravi |
ICACHE_NUM_BLOCKS : natural := 4; -- number of blocks (min 1), has to be a power of 2
|
48 |
|
|
ICACHE_BLOCK_SIZE : natural := 16; -- block size in bytes (min 4), has to be a power of 2
|
49 |
|
|
ICACHE_NUM_SETS : natural := 1 -- associativity / number of sets (1=direct_mapped), has to be a power of 2
|
50 |
45 |
zero_gravi |
);
|
51 |
|
|
port (
|
52 |
|
|
-- global control --
|
53 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
54 |
|
|
rstn_i : in std_ulogic; -- global reset, low-active, async
|
55 |
|
|
clear_i : in std_ulogic; -- cache clear
|
56 |
|
|
-- host controller interface --
|
57 |
|
|
host_addr_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
58 |
|
|
host_rdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
59 |
|
|
host_wdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
60 |
|
|
host_ben_i : in std_ulogic_vector(03 downto 0); -- byte enable
|
61 |
|
|
host_we_i : in std_ulogic; -- write enable
|
62 |
|
|
host_re_i : in std_ulogic; -- read enable
|
63 |
|
|
host_cancel_i : in std_ulogic; -- cancel current bus transaction
|
64 |
|
|
host_lock_i : in std_ulogic; -- locked/exclusive access
|
65 |
|
|
host_ack_o : out std_ulogic; -- bus transfer acknowledge
|
66 |
|
|
host_err_o : out std_ulogic; -- bus transfer error
|
67 |
|
|
-- peripheral bus interface --
|
68 |
|
|
bus_addr_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus access address
|
69 |
|
|
bus_rdata_i : in std_ulogic_vector(data_width_c-1 downto 0); -- bus read data
|
70 |
|
|
bus_wdata_o : out std_ulogic_vector(data_width_c-1 downto 0); -- bus write data
|
71 |
|
|
bus_ben_o : out std_ulogic_vector(03 downto 0); -- byte enable
|
72 |
|
|
bus_we_o : out std_ulogic; -- write enable
|
73 |
|
|
bus_re_o : out std_ulogic; -- read enable
|
74 |
|
|
bus_cancel_o : out std_ulogic; -- cancel current bus transaction
|
75 |
|
|
bus_lock_o : out std_ulogic; -- locked/exclusive access
|
76 |
|
|
bus_ack_i : in std_ulogic; -- bus transfer acknowledge
|
77 |
|
|
bus_err_i : in std_ulogic -- bus transfer error
|
78 |
|
|
);
|
79 |
|
|
end neorv32_icache;
|
80 |
|
|
|
81 |
|
|
architecture neorv32_icache_rtl of neorv32_icache is
|
82 |
|
|
|
83 |
|
|
-- cache layout --
|
84 |
47 |
zero_gravi |
constant cache_offset_size_c : natural := index_size_f(ICACHE_BLOCK_SIZE/4); -- offset addresses full 32-bit words
|
85 |
|
|
constant cache_index_size_c : natural := index_size_f(ICACHE_NUM_BLOCKS);
|
86 |
45 |
zero_gravi |
constant cache_tag_size_c : natural := 32 - (cache_offset_size_c + cache_index_size_c + 2); -- 2 additonal bits for byte offset
|
87 |
|
|
|
88 |
|
|
-- cache memory --
|
89 |
|
|
component neorv32_icache_memory
|
90 |
|
|
generic (
|
91 |
47 |
zero_gravi |
ICACHE_NUM_BLOCKS : natural := 4; -- number of blocks (min 1), has to be a power of 2
|
92 |
|
|
ICACHE_BLOCK_SIZE : natural := 16; -- block size in bytes (min 4), has to be a power of 2
|
93 |
|
|
ICACHE_NUM_SETS : natural := 1 -- associativity; 0=direct-mapped, 1=2-way set-associative
|
94 |
45 |
zero_gravi |
);
|
95 |
|
|
port (
|
96 |
|
|
-- global control --
|
97 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
98 |
|
|
invalidate_i : in std_ulogic; -- invalidate whole cache
|
99 |
|
|
-- host cache access (read-only) --
|
100 |
|
|
host_addr_i : in std_ulogic_vector(31 downto 0); -- access address
|
101 |
|
|
host_re_i : in std_ulogic; -- read enable
|
102 |
|
|
host_rdata_o : out std_ulogic_vector(31 downto 0); -- read data
|
103 |
|
|
-- access status (1 cycle delay to access) --
|
104 |
|
|
hit_o : out std_ulogic; -- hit access
|
105 |
|
|
-- ctrl cache access (write-only) --
|
106 |
|
|
ctrl_en_i : in std_ulogic; -- control interface enable
|
107 |
|
|
ctrl_addr_i : in std_ulogic_vector(31 downto 0); -- access address
|
108 |
|
|
ctrl_we_i : in std_ulogic; -- write enable (full-word)
|
109 |
|
|
ctrl_wdata_i : in std_ulogic_vector(31 downto 0); -- write data
|
110 |
|
|
ctrl_tag_we_i : in std_ulogic; -- write tag to selected block
|
111 |
|
|
ctrl_valid_i : in std_ulogic; -- make selected block valid
|
112 |
|
|
ctrl_invalid_i : in std_ulogic -- make selected block invalid
|
113 |
|
|
);
|
114 |
|
|
end component;
|
115 |
|
|
|
116 |
|
|
-- cache interface --
|
117 |
|
|
type cache_if_t is record
|
118 |
|
|
clear : std_ulogic; -- cache clear
|
119 |
|
|
--
|
120 |
|
|
host_addr : std_ulogic_vector(31 downto 0); -- cpu access address
|
121 |
|
|
host_rdata : std_ulogic_vector(31 downto 0); -- cpu read data
|
122 |
|
|
--
|
123 |
|
|
hit : std_ulogic; -- hit access
|
124 |
|
|
--
|
125 |
|
|
ctrl_en : std_ulogic; -- control access enable
|
126 |
|
|
ctrl_addr : std_ulogic_vector(31 downto 0); -- control access address
|
127 |
|
|
ctrl_we : std_ulogic; -- control write enable
|
128 |
|
|
ctrl_wdata : std_ulogic_vector(31 downto 0); -- control write data
|
129 |
|
|
ctrl_tag_we : std_ulogic; -- control tag write enabled
|
130 |
|
|
ctrl_valid_we : std_ulogic; -- control valid flag set
|
131 |
|
|
ctrl_invalid_we : std_ulogic; -- control valid flag clear
|
132 |
|
|
end record;
|
133 |
|
|
signal cache : cache_if_t;
|
134 |
|
|
|
135 |
|
|
-- control engine --
|
136 |
|
|
type ctrl_engine_state_t is (S_IDLE, S_CACHE_CLEAR, S_CACHE_CHECK, S_CACHE_MISS, S_BUS_DOWNLOAD_REQ, S_BUS_DOWNLOAD_GET,
|
137 |
|
|
S_CACHE_RESYNC_0, S_CACHE_RESYNC_1, S_BUS_ERROR, S_ERROR, S_HOST_CANCEL);
|
138 |
|
|
type ctrl_t is record
|
139 |
|
|
state : ctrl_engine_state_t; -- current state
|
140 |
|
|
state_nxt : ctrl_engine_state_t; -- next state
|
141 |
|
|
addr_reg : std_ulogic_vector(31 downto 0); -- address register for block download
|
142 |
|
|
addr_reg_nxt : std_ulogic_vector(31 downto 0);
|
143 |
|
|
--
|
144 |
|
|
re_buf : std_ulogic; -- read request buffer
|
145 |
|
|
re_buf_nxt : std_ulogic;
|
146 |
|
|
cancel_buf : std_ulogic; -- cancel request buffer
|
147 |
|
|
cancel_buf_nxt : std_ulogic;
|
148 |
|
|
end record;
|
149 |
|
|
signal ctrl : ctrl_t;
|
150 |
|
|
|
151 |
|
|
begin
|
152 |
|
|
|
153 |
|
|
-- Sanity Checks --------------------------------------------------------------------------
|
154 |
|
|
-- -------------------------------------------------------------------------------------------
|
155 |
|
|
-- configuration --
|
156 |
47 |
zero_gravi |
assert not (is_power_of_two_f(ICACHE_NUM_BLOCKS) = false) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache number of blocks <ICACHE_NUM_BLOCKS> has to be a power of 2." severity error;
|
157 |
|
|
assert not (is_power_of_two_f(ICACHE_BLOCK_SIZE) = false) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache block size <ICACHE_BLOCK_SIZE> has to be a power of 2." severity error;
|
158 |
|
|
assert not ((is_power_of_two_f(ICACHE_NUM_SETS) = false)) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache associativity <ICACHE_NUM_SETS> has to be a power of 2." severity error;
|
159 |
|
|
assert not (ICACHE_NUM_BLOCKS < 1) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache number of blocks <ICACHE_NUM_BLOCKS> has to be >= 1." severity error;
|
160 |
|
|
assert not (ICACHE_BLOCK_SIZE < 4) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache block size <ICACHE_BLOCK_SIZE> has to be >= 4." severity error;
|
161 |
|
|
assert not ((ICACHE_NUM_SETS = 0) or (ICACHE_NUM_SETS > 2)) report "NEORV32 PROCESSOR CONFIG ERROR! i-cache associativity <ICACHE_NUM_SETS> has to be 1 (direct-mapped) or 2 (2-way set-associative)." severity error;
|
162 |
45 |
zero_gravi |
|
163 |
|
|
|
164 |
|
|
-- Control Engine FSM Sync ----------------------------------------------------------------
|
165 |
|
|
-- -------------------------------------------------------------------------------------------
|
166 |
|
|
-- registers that REQUIRE a specific reset state --
|
167 |
|
|
ctrl_engine_fsm_sync_rst: process(rstn_i, clk_i)
|
168 |
|
|
begin
|
169 |
|
|
if (rstn_i = '0') then
|
170 |
|
|
ctrl.state <= S_CACHE_CLEAR;
|
171 |
|
|
ctrl.re_buf <= '0';
|
172 |
|
|
ctrl.cancel_buf <= '0';
|
173 |
|
|
elsif rising_edge(clk_i) then
|
174 |
|
|
ctrl.state <= ctrl.state_nxt;
|
175 |
|
|
ctrl.re_buf <= ctrl.re_buf_nxt;
|
176 |
|
|
ctrl.cancel_buf <= ctrl.cancel_buf_nxt;
|
177 |
|
|
end if;
|
178 |
|
|
end process ctrl_engine_fsm_sync_rst;
|
179 |
|
|
|
180 |
|
|
-- registers that do not require a specific reset state --
|
181 |
|
|
ctrl_engine_fsm_sync: process(clk_i)
|
182 |
|
|
begin
|
183 |
|
|
if rising_edge(clk_i) then
|
184 |
|
|
ctrl.addr_reg <= ctrl.addr_reg_nxt;
|
185 |
|
|
end if;
|
186 |
|
|
end process ctrl_engine_fsm_sync;
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
-- Control Engine FSM Comb ----------------------------------------------------------------
|
190 |
|
|
-- -------------------------------------------------------------------------------------------
|
191 |
|
|
ctrl_engine_fsm_comb: process(ctrl, cache, clear_i, host_addr_i, host_lock_i, host_re_i, host_cancel_i, bus_rdata_i, bus_ack_i, bus_err_i)
|
192 |
|
|
begin
|
193 |
|
|
-- control defaults --
|
194 |
|
|
ctrl.state_nxt <= ctrl.state;
|
195 |
|
|
ctrl.addr_reg_nxt <= ctrl.addr_reg;
|
196 |
|
|
ctrl.re_buf_nxt <= (ctrl.re_buf or host_re_i) and (not host_cancel_i);
|
197 |
|
|
ctrl.cancel_buf_nxt <= ctrl.cancel_buf or host_cancel_i;
|
198 |
|
|
|
199 |
|
|
-- cache defaults --
|
200 |
|
|
cache.clear <= '0';
|
201 |
|
|
cache.host_addr <= host_addr_i;
|
202 |
|
|
cache.ctrl_en <= '0';
|
203 |
|
|
cache.ctrl_addr <= ctrl.addr_reg;
|
204 |
|
|
cache.ctrl_we <= '0';
|
205 |
|
|
cache.ctrl_wdata <= bus_rdata_i;
|
206 |
|
|
cache.ctrl_tag_we <= '0';
|
207 |
|
|
cache.ctrl_valid_we <= '0';
|
208 |
|
|
cache.ctrl_invalid_we <= '0';
|
209 |
|
|
|
210 |
|
|
-- host interface defaults --
|
211 |
|
|
host_ack_o <= '0';
|
212 |
|
|
host_err_o <= '0';
|
213 |
|
|
host_rdata_o <= cache.host_rdata;
|
214 |
|
|
|
215 |
|
|
-- peripheral bus interface defaults --
|
216 |
|
|
bus_addr_o <= ctrl.addr_reg;
|
217 |
|
|
bus_wdata_o <= (others => '0'); -- cache is read-only
|
218 |
|
|
bus_ben_o <= (others => '0'); -- cache is read-only
|
219 |
|
|
bus_we_o <= '0'; -- cache is read-only
|
220 |
|
|
bus_re_o <= '0';
|
221 |
|
|
bus_cancel_o <= '0';
|
222 |
|
|
bus_lock_o <= host_lock_i;
|
223 |
|
|
|
224 |
|
|
-- fsm --
|
225 |
|
|
case ctrl.state is
|
226 |
|
|
|
227 |
|
|
when S_IDLE => -- wait for host access request or cache control operation
|
228 |
|
|
-- ------------------------------------------------------------
|
229 |
|
|
if (clear_i = '1') then -- cache control operation?
|
230 |
|
|
ctrl.state_nxt <= S_CACHE_CLEAR;
|
231 |
|
|
elsif (host_re_i = '1') or (ctrl.re_buf = '1') then -- cache access
|
232 |
|
|
ctrl.re_buf_nxt <= '0';
|
233 |
|
|
ctrl.cancel_buf_nxt <= '0';
|
234 |
|
|
ctrl.state_nxt <= S_CACHE_CHECK;
|
235 |
|
|
end if;
|
236 |
|
|
|
237 |
|
|
when S_CACHE_CLEAR => -- invalidate all cache entries
|
238 |
|
|
-- ------------------------------------------------------------
|
239 |
|
|
cache.clear <= '1';
|
240 |
|
|
ctrl.state_nxt <= S_IDLE;
|
241 |
|
|
|
242 |
|
|
when S_CACHE_CHECK => -- finalize host access if cache hit
|
243 |
|
|
-- ------------------------------------------------------------
|
244 |
|
|
if (cache.hit = '1') then -- cache HIT
|
245 |
|
|
host_ack_o <= not ctrl.cancel_buf; -- ACK if request has not been canceled
|
246 |
|
|
ctrl.state_nxt <= S_IDLE;
|
247 |
|
|
else -- cache MISS
|
248 |
|
|
ctrl.state_nxt <= S_CACHE_MISS;
|
249 |
|
|
end if;
|
250 |
|
|
|
251 |
|
|
when S_CACHE_MISS => --
|
252 |
|
|
-- ------------------------------------------------------------
|
253 |
|
|
-- compute block base address --
|
254 |
|
|
ctrl.addr_reg_nxt <= host_addr_i;
|
255 |
|
|
ctrl.addr_reg_nxt((2+cache_offset_size_c)-1 downto 2) <= (others => '0'); -- block-aligned
|
256 |
|
|
ctrl.addr_reg_nxt(1 downto 0) <= "00"; -- word-aligned
|
257 |
|
|
--
|
258 |
|
|
if (host_cancel_i = '1') or (ctrl.cancel_buf = '1') then -- 'early' CPU cancel (abort before bus transaction has even started)
|
259 |
|
|
ctrl.state_nxt <= S_IDLE;
|
260 |
|
|
else
|
261 |
|
|
ctrl.state_nxt <= S_BUS_DOWNLOAD_REQ;
|
262 |
|
|
end if;
|
263 |
|
|
|
264 |
|
|
when S_BUS_DOWNLOAD_REQ => -- download new cache block: request new word
|
265 |
|
|
-- ------------------------------------------------------------
|
266 |
|
|
bus_re_o <= '1'; -- request new read transfer
|
267 |
|
|
ctrl.state_nxt <= S_BUS_DOWNLOAD_GET;
|
268 |
|
|
|
269 |
|
|
when S_BUS_DOWNLOAD_GET => -- download new cache block: wait for bus response
|
270 |
|
|
-- ------------------------------------------------------------
|
271 |
|
|
cache.ctrl_en <= '1'; -- we are in cache control mode
|
272 |
|
|
--
|
273 |
|
|
if (bus_err_i = '1') then -- bus error
|
274 |
|
|
ctrl.state_nxt <= S_BUS_ERROR;
|
275 |
|
|
elsif (ctrl.cancel_buf = '1') then -- 'late' CPU cancel (timeout?)
|
276 |
|
|
ctrl.state_nxt <= S_HOST_CANCEL;
|
277 |
|
|
elsif (bus_ack_i = '1') then -- ACK = write to cache and get next word
|
278 |
|
|
cache.ctrl_we <= '1'; -- write to cache
|
279 |
|
|
if (and_all_f(ctrl.addr_reg((2+cache_offset_size_c)-1 downto 2)) = '1') then -- block complete?
|
280 |
|
|
cache.ctrl_tag_we <= '1'; -- current block is valid now
|
281 |
|
|
cache.ctrl_valid_we <= '1'; -- write tag of current address
|
282 |
|
|
ctrl.state_nxt <= S_CACHE_RESYNC_0;
|
283 |
|
|
else -- get next word
|
284 |
|
|
ctrl.addr_reg_nxt <= std_ulogic_vector(unsigned(ctrl.addr_reg) + 4);
|
285 |
|
|
ctrl.state_nxt <= S_BUS_DOWNLOAD_REQ;
|
286 |
|
|
end if;
|
287 |
|
|
end if;
|
288 |
|
|
|
289 |
|
|
when S_CACHE_RESYNC_0 => -- re-sync host/cache access: cache read-latency
|
290 |
|
|
-- ------------------------------------------------------------
|
291 |
|
|
ctrl.state_nxt <= S_CACHE_RESYNC_1;
|
292 |
|
|
|
293 |
|
|
when S_CACHE_RESYNC_1 => -- re-sync host/cache access: finalize CPU request
|
294 |
|
|
-- ------------------------------------------------------------
|
295 |
|
|
host_ack_o <= not ctrl.cancel_buf; -- ACK if request has not been canceled
|
296 |
|
|
ctrl.state_nxt <= S_IDLE;
|
297 |
|
|
|
298 |
|
|
when S_BUS_ERROR => -- bus error during download
|
299 |
|
|
-- ------------------------------------------------------------
|
300 |
|
|
host_err_o <= '1';
|
301 |
|
|
ctrl.state_nxt <= S_ERROR;
|
302 |
|
|
|
303 |
|
|
when S_ERROR => -- wait for CPU to cancel faulting transfer
|
304 |
|
|
-- ------------------------------------------------------------
|
305 |
|
|
if (host_cancel_i = '1') then
|
306 |
|
|
bus_cancel_o <= '1';
|
307 |
|
|
ctrl.state_nxt <= S_IDLE;
|
308 |
|
|
end if;
|
309 |
|
|
|
310 |
|
|
when S_HOST_CANCEL => -- host cancels transfer
|
311 |
|
|
-- ------------------------------------------------------------
|
312 |
|
|
cache.ctrl_en <= '1'; -- we are in cache control mode
|
313 |
|
|
cache.ctrl_invalid_we <= '1'; -- invalidate current cache block
|
314 |
|
|
bus_cancel_o <= '1';
|
315 |
|
|
ctrl.state_nxt <= S_IDLE;
|
316 |
|
|
|
317 |
|
|
when others => -- undefined
|
318 |
|
|
-- ------------------------------------------------------------
|
319 |
|
|
ctrl.state_nxt <= S_IDLE;
|
320 |
|
|
|
321 |
|
|
end case;
|
322 |
|
|
end process ctrl_engine_fsm_comb;
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
-- Cache Memory ---------------------------------------------------------------------------
|
326 |
|
|
-- -------------------------------------------------------------------------------------------
|
327 |
|
|
neorv32_icache_memory_inst: neorv32_icache_memory
|
328 |
|
|
generic map (
|
329 |
47 |
zero_gravi |
ICACHE_NUM_BLOCKS => ICACHE_NUM_BLOCKS, -- number of blocks (min 1), has to be a power of 2
|
330 |
|
|
ICACHE_BLOCK_SIZE => ICACHE_BLOCK_SIZE, -- block size in bytes (min 4), has to be a power of 2
|
331 |
|
|
ICACHE_NUM_SETS => ICACHE_NUM_SETS -- associativity; 0=direct-mapped, 1=2-way set-associative
|
332 |
45 |
zero_gravi |
)
|
333 |
|
|
port map (
|
334 |
|
|
-- global control --
|
335 |
|
|
clk_i => clk_i, -- global clock, rising edge
|
336 |
|
|
invalidate_i => cache.clear, -- invalidate whole cache
|
337 |
|
|
-- host cache access (read-only) --
|
338 |
|
|
host_addr_i => cache.host_addr, -- access address
|
339 |
|
|
host_re_i => host_re_i, -- read enable
|
340 |
|
|
host_rdata_o => cache.host_rdata, -- read data
|
341 |
|
|
-- access status (1 cycle delay to access) --
|
342 |
|
|
hit_o => cache.hit, -- hit access
|
343 |
|
|
-- ctrl cache access (write-only) --
|
344 |
|
|
ctrl_en_i => cache.ctrl_en, -- control interface enable
|
345 |
|
|
ctrl_addr_i => cache.ctrl_addr, -- access address
|
346 |
|
|
ctrl_we_i => cache.ctrl_we, -- write enable (full-word)
|
347 |
|
|
ctrl_wdata_i => cache.ctrl_wdata, -- write data
|
348 |
|
|
ctrl_tag_we_i => cache.ctrl_tag_we, -- write tag to selected block
|
349 |
|
|
ctrl_valid_i => cache.ctrl_valid_we, -- make selected block valid
|
350 |
|
|
ctrl_invalid_i => cache.ctrl_invalid_we -- make selected block invalid
|
351 |
|
|
);
|
352 |
|
|
|
353 |
|
|
end neorv32_icache_rtl;
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
-- ###########################################################################################################################################
|
357 |
|
|
-- ###########################################################################################################################################
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
-- #################################################################################################
|
361 |
|
|
-- # << NEORV32 - Cache Memory >> #
|
362 |
|
|
-- # ********************************************************************************************* #
|
363 |
47 |
zero_gravi |
-- # Direct mapped (ICACHE_NUM_SETS = 1) or 2-way set-associative (ICACHE_NUM_SETS = 2). #
|
364 |
|
|
-- # Least recently used replacement policy (if ICACHE_NUM_SETS > 1). #
|
365 |
45 |
zero_gravi |
-- # Read-only for host, write-only for control. All output signals have one cycle latency. #
|
366 |
|
|
-- # #
|
367 |
|
|
-- # Cache sets are mapped to individual memory components - no multi-dimensional memory arrays #
|
368 |
|
|
-- # are used as some synthesis tools have problems to map these to actual BRAM primitives. #
|
369 |
|
|
-- # ********************************************************************************************* #
|
370 |
|
|
-- # BSD 3-Clause License #
|
371 |
|
|
-- # #
|
372 |
|
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
373 |
|
|
-- # #
|
374 |
|
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
375 |
|
|
-- # permitted provided that the following conditions are met: #
|
376 |
|
|
-- # #
|
377 |
|
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
378 |
|
|
-- # conditions and the following disclaimer. #
|
379 |
|
|
-- # #
|
380 |
|
|
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
381 |
|
|
-- # conditions and the following disclaimer in the documentation and/or other materials #
|
382 |
|
|
-- # provided with the distribution. #
|
383 |
|
|
-- # #
|
384 |
|
|
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
385 |
|
|
-- # endorse or promote products derived from this software without specific prior written #
|
386 |
|
|
-- # permission. #
|
387 |
|
|
-- # #
|
388 |
|
|
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
389 |
|
|
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
390 |
|
|
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
391 |
|
|
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
392 |
|
|
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
393 |
|
|
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
394 |
|
|
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
395 |
|
|
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
396 |
|
|
-- # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
397 |
|
|
-- # ********************************************************************************************* #
|
398 |
|
|
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
399 |
|
|
-- #################################################################################################
|
400 |
|
|
|
401 |
|
|
library ieee;
|
402 |
|
|
use ieee.std_logic_1164.all;
|
403 |
|
|
use ieee.numeric_std.all;
|
404 |
|
|
|
405 |
|
|
library neorv32;
|
406 |
|
|
use neorv32.neorv32_package.all;
|
407 |
|
|
|
408 |
|
|
entity neorv32_icache_memory is
|
409 |
|
|
generic (
|
410 |
47 |
zero_gravi |
ICACHE_NUM_BLOCKS : natural := 4; -- number of blocks (min 1), has to be a power of 2
|
411 |
|
|
ICACHE_BLOCK_SIZE : natural := 16; -- block size in bytes (min 4), has to be a power of 2
|
412 |
|
|
ICACHE_NUM_SETS : natural := 1 -- associativity; 1=direct-mapped, 2=2-way set-associative
|
413 |
45 |
zero_gravi |
);
|
414 |
|
|
port (
|
415 |
|
|
-- global control --
|
416 |
|
|
clk_i : in std_ulogic; -- global clock, rising edge
|
417 |
|
|
invalidate_i : in std_ulogic; -- invalidate whole cache
|
418 |
|
|
-- host cache access (read-only) --
|
419 |
|
|
host_addr_i : in std_ulogic_vector(31 downto 0); -- access address
|
420 |
|
|
host_re_i : in std_ulogic; -- read enable
|
421 |
|
|
host_rdata_o : out std_ulogic_vector(31 downto 0); -- read data
|
422 |
|
|
-- access status (1 cycle delay to access) --
|
423 |
|
|
hit_o : out std_ulogic; -- hit access
|
424 |
|
|
-- ctrl cache access (write-only) --
|
425 |
|
|
ctrl_en_i : in std_ulogic; -- control interface enable
|
426 |
|
|
ctrl_addr_i : in std_ulogic_vector(31 downto 0); -- access address
|
427 |
|
|
ctrl_we_i : in std_ulogic; -- write enable (full-word)
|
428 |
|
|
ctrl_wdata_i : in std_ulogic_vector(31 downto 0); -- write data
|
429 |
|
|
ctrl_tag_we_i : in std_ulogic; -- write tag to selected block
|
430 |
|
|
ctrl_valid_i : in std_ulogic; -- make selected block valid
|
431 |
|
|
ctrl_invalid_i : in std_ulogic -- make selected block invalid
|
432 |
|
|
);
|
433 |
|
|
end neorv32_icache_memory;
|
434 |
|
|
|
435 |
|
|
architecture neorv32_icache_memory_rtl of neorv32_icache_memory is
|
436 |
|
|
|
437 |
|
|
-- cache layout --
|
438 |
47 |
zero_gravi |
constant cache_offset_size_c : natural := index_size_f(ICACHE_BLOCK_SIZE/4); -- offset addresses full 32-bit words
|
439 |
|
|
constant cache_index_size_c : natural := index_size_f(ICACHE_NUM_BLOCKS);
|
440 |
45 |
zero_gravi |
constant cache_tag_size_c : natural := 32 - (cache_offset_size_c + cache_index_size_c + 2); -- 2 additonal bits for byte offset
|
441 |
47 |
zero_gravi |
constant cache_entries_c : natural := ICACHE_NUM_BLOCKS * (ICACHE_BLOCK_SIZE/4); -- number of 32-bit entries (per set)
|
442 |
45 |
zero_gravi |
|
443 |
|
|
-- status flag memory --
|
444 |
47 |
zero_gravi |
signal valid_flag_s0 : std_ulogic_vector(ICACHE_NUM_BLOCKS-1 downto 0);
|
445 |
|
|
signal valid_flag_s1 : std_ulogic_vector(ICACHE_NUM_BLOCKS-1 downto 0);
|
446 |
45 |
zero_gravi |
signal valid : std_ulogic_vector(1 downto 0); -- valid flag read data
|
447 |
|
|
|
448 |
|
|
-- tag memory --
|
449 |
47 |
zero_gravi |
type tag_mem_t is array (0 to ICACHE_NUM_BLOCKS-1) of std_ulogic_vector(cache_tag_size_c-1 downto 0);
|
450 |
45 |
zero_gravi |
signal tag_mem_s0 : tag_mem_t;
|
451 |
|
|
signal tag_mem_s1 : tag_mem_t;
|
452 |
|
|
type tag_rd_t is array (0 to 1) of std_ulogic_vector(cache_tag_size_c-1 downto 0);
|
453 |
|
|
signal tag : tag_rd_t; -- tag read data
|
454 |
|
|
|
455 |
|
|
-- access status --
|
456 |
|
|
signal hit : std_ulogic_vector(1 downto 0);
|
457 |
|
|
|
458 |
|
|
-- access address decomposition --
|
459 |
|
|
type acc_addr_t is record
|
460 |
|
|
tag : std_ulogic_vector(cache_tag_size_c-1 downto 0);
|
461 |
|
|
index : std_ulogic_vector(cache_index_size_c-1 downto 0);
|
462 |
|
|
offset : std_ulogic_vector(cache_offset_size_c-1 downto 0);
|
463 |
|
|
end record;
|
464 |
|
|
signal host_acc_addr, ctrl_acc_addr : acc_addr_t;
|
465 |
|
|
|
466 |
|
|
-- cache data memory --
|
467 |
|
|
type cache_mem_t is array (0 to cache_entries_c-1) of std_ulogic_vector(31 downto 0);
|
468 |
|
|
signal cache_data_memory_s0 : cache_mem_t; -- set 0
|
469 |
|
|
signal cache_data_memory_s1 : cache_mem_t; -- set 1
|
470 |
|
|
|
471 |
|
|
-- cache data memory access --
|
472 |
|
|
type cache_rdata_t is array (0 to 1) of std_ulogic_vector(31 downto 0);
|
473 |
|
|
signal cache_rdata : cache_rdata_t;
|
474 |
|
|
signal cache_index : std_ulogic_vector(cache_index_size_c-1 downto 0);
|
475 |
|
|
signal cache_offset : std_ulogic_vector(cache_offset_size_c-1 downto 0);
|
476 |
|
|
signal cache_addr : std_ulogic_vector((cache_index_size_c+cache_offset_size_c)-1 downto 0); -- index & offset
|
477 |
|
|
signal cache_we : std_ulogic; -- write enable (full-word)
|
478 |
|
|
signal set_select : std_ulogic;
|
479 |
|
|
|
480 |
|
|
-- access history --
|
481 |
|
|
type history_t is record
|
482 |
|
|
re_ff : std_ulogic;
|
483 |
47 |
zero_gravi |
last_used_set : std_ulogic_vector(ICACHE_NUM_BLOCKS-1 downto 0);
|
484 |
45 |
zero_gravi |
to_be_replaced : std_ulogic;
|
485 |
|
|
end record;
|
486 |
|
|
signal history : history_t;
|
487 |
|
|
|
488 |
|
|
begin
|
489 |
|
|
|
490 |
|
|
-- Access Address Decomposition -----------------------------------------------------------
|
491 |
|
|
-- -------------------------------------------------------------------------------------------
|
492 |
|
|
host_acc_addr.tag <= host_addr_i(31 downto 31-(cache_tag_size_c-1));
|
493 |
|
|
host_acc_addr.index <= host_addr_i(31-cache_tag_size_c downto 2+cache_offset_size_c);
|
494 |
|
|
host_acc_addr.offset <= host_addr_i(2+(cache_offset_size_c-1) downto 2); -- discard byte offset
|
495 |
|
|
|
496 |
|
|
ctrl_acc_addr.tag <= ctrl_addr_i(31 downto 31-(cache_tag_size_c-1));
|
497 |
|
|
ctrl_acc_addr.index <= ctrl_addr_i(31-cache_tag_size_c downto 2+cache_offset_size_c);
|
498 |
|
|
ctrl_acc_addr.offset <= ctrl_addr_i(2+(cache_offset_size_c-1) downto 2); -- discard byte offset
|
499 |
|
|
|
500 |
|
|
|
501 |
|
|
-- Cache Access History -------------------------------------------------------------------
|
502 |
|
|
-- -------------------------------------------------------------------------------------------
|
503 |
|
|
access_history: process(clk_i)
|
504 |
|
|
begin
|
505 |
|
|
if rising_edge(clk_i) then
|
506 |
|
|
history.re_ff <= host_re_i;
|
507 |
|
|
if (invalidate_i = '1') then -- invalidate whole cache
|
508 |
|
|
history.last_used_set <= (others => '1');
|
509 |
|
|
elsif (history.re_ff = '1') and (or_all_f(hit) = '1') then -- store last accessed set that caused a hit
|
510 |
|
|
history.last_used_set(to_integer(unsigned(cache_index))) <= not hit(0);
|
511 |
|
|
end if;
|
512 |
|
|
history.to_be_replaced <= history.last_used_set(to_integer(unsigned(cache_index)));
|
513 |
|
|
end if;
|
514 |
|
|
end process access_history;
|
515 |
|
|
|
516 |
|
|
-- which set is going to be replaced? -> opposite of last used set = least recently used set --
|
517 |
47 |
zero_gravi |
set_select <= '0' when (ICACHE_NUM_SETS = 1) else (not history.to_be_replaced);
|
518 |
45 |
zero_gravi |
|
519 |
|
|
|
520 |
|
|
-- Status flag memory ---------------------------------------------------------------------
|
521 |
|
|
-- -------------------------------------------------------------------------------------------
|
522 |
|
|
status_memory: process(clk_i)
|
523 |
|
|
begin
|
524 |
|
|
if rising_edge(clk_i) then
|
525 |
|
|
-- write access --
|
526 |
|
|
if (invalidate_i = '1') then -- invalidate whole cache
|
527 |
|
|
valid_flag_s0 <= (others => '0');
|
528 |
|
|
valid_flag_s1 <= (others => '0');
|
529 |
|
|
elsif (ctrl_en_i = '1') then
|
530 |
|
|
if (ctrl_invalid_i = '1') then -- make current block invalid
|
531 |
|
|
if (set_select = '0') then
|
532 |
|
|
valid_flag_s0(to_integer(unsigned(cache_index))) <= '0';
|
533 |
|
|
else
|
534 |
|
|
valid_flag_s1(to_integer(unsigned(cache_index))) <= '0';
|
535 |
|
|
end if;
|
536 |
|
|
elsif (ctrl_valid_i = '1') then -- make current block valid
|
537 |
|
|
if (set_select = '0') then
|
538 |
|
|
valid_flag_s0(to_integer(unsigned(cache_index))) <= '1';
|
539 |
|
|
else
|
540 |
|
|
valid_flag_s1(to_integer(unsigned(cache_index))) <= '1';
|
541 |
|
|
end if;
|
542 |
|
|
end if;
|
543 |
|
|
end if;
|
544 |
|
|
-- read access (sync) --
|
545 |
|
|
valid(0) <= valid_flag_s0(to_integer(unsigned(cache_index)));
|
546 |
|
|
valid(1) <= valid_flag_s1(to_integer(unsigned(cache_index)));
|
547 |
|
|
end if;
|
548 |
|
|
end process status_memory;
|
549 |
|
|
|
550 |
|
|
|
551 |
|
|
-- Tag memory -----------------------------------------------------------------------------
|
552 |
|
|
-- -------------------------------------------------------------------------------------------
|
553 |
|
|
tag_memory: process(clk_i)
|
554 |
|
|
begin
|
555 |
|
|
if rising_edge(clk_i) then
|
556 |
|
|
if (ctrl_en_i = '1') and (ctrl_tag_we_i = '1') then -- write access
|
557 |
|
|
if (set_select = '0') then
|
558 |
|
|
tag_mem_s0(to_integer(unsigned(cache_index))) <= ctrl_acc_addr.tag;
|
559 |
|
|
else
|
560 |
|
|
tag_mem_s1(to_integer(unsigned(cache_index))) <= ctrl_acc_addr.tag;
|
561 |
|
|
end if;
|
562 |
|
|
else -- read access
|
563 |
|
|
tag(0) <= tag_mem_s0(to_integer(unsigned(cache_index)));
|
564 |
|
|
tag(1) <= tag_mem_s1(to_integer(unsigned(cache_index)));
|
565 |
|
|
end if;
|
566 |
|
|
end if;
|
567 |
|
|
end process tag_memory;
|
568 |
|
|
|
569 |
|
|
-- comparator --
|
570 |
|
|
comparator: process(host_acc_addr, tag, valid)
|
571 |
|
|
begin
|
572 |
|
|
hit <= (others => '0');
|
573 |
47 |
zero_gravi |
for i in 0 to ICACHE_NUM_SETS-1 loop
|
574 |
45 |
zero_gravi |
if (host_acc_addr.tag = tag(i)) and (valid(i) = '1') then
|
575 |
|
|
hit(i) <= '1';
|
576 |
|
|
end if;
|
577 |
|
|
end loop; -- i
|
578 |
|
|
end process comparator;
|
579 |
|
|
|
580 |
|
|
-- global hit --
|
581 |
|
|
hit_o <= or_all_f(hit);
|
582 |
|
|
|
583 |
|
|
|
584 |
|
|
-- Cache Data Memory ----------------------------------------------------------------------
|
585 |
|
|
-- -------------------------------------------------------------------------------------------
|
586 |
|
|
cache_mem_access: process(clk_i)
|
587 |
|
|
begin
|
588 |
|
|
if rising_edge(clk_i) then
|
589 |
|
|
if (cache_we = '1') then -- write access from control (full-word)
|
590 |
|
|
if (set_select = '0') then
|
591 |
|
|
cache_data_memory_s0(to_integer(unsigned(cache_addr))) <= ctrl_wdata_i;
|
592 |
|
|
else
|
593 |
|
|
cache_data_memory_s1(to_integer(unsigned(cache_addr))) <= ctrl_wdata_i;
|
594 |
|
|
end if;
|
595 |
|
|
else -- read access from host (full-word)
|
596 |
|
|
cache_rdata(0) <= cache_data_memory_s0(to_integer(unsigned(cache_addr)));
|
597 |
|
|
cache_rdata(1) <= cache_data_memory_s1(to_integer(unsigned(cache_addr)));
|
598 |
|
|
end if;
|
599 |
|
|
end if;
|
600 |
|
|
end process cache_mem_access;
|
601 |
|
|
|
602 |
|
|
-- data output --
|
603 |
47 |
zero_gravi |
host_rdata_o <= cache_rdata(0) when (hit(0) = '1') or (ICACHE_NUM_SETS = 1) else cache_rdata(1);
|
604 |
45 |
zero_gravi |
|
605 |
|
|
-- cache block ram access address --
|
606 |
|
|
cache_addr <= cache_index & cache_offset;
|
607 |
|
|
|
608 |
|
|
-- cache access select --
|
609 |
|
|
cache_index <= host_acc_addr.index when (ctrl_en_i = '0') else ctrl_acc_addr.index;
|
610 |
|
|
cache_offset <= host_acc_addr.offset when (ctrl_en_i = '0') else ctrl_acc_addr.offset;
|
611 |
|
|
cache_we <= '0' when (ctrl_en_i = '0') else ctrl_we_i;
|
612 |
|
|
|
613 |
|
|
|
614 |
|
|
end neorv32_icache_memory_rtl;
|