Minor refactor of icache to make less dependent on wishbone
[microwatt.git] / icache.vhdl
1 --
2 -- Set associative icache
3 --
4 -- TODO (in no specific order):
5 --
6 -- * Add debug interface to inspect cache content
7 -- * Add snoop/invalidate path
8 -- * Add multi-hit error detection
9 -- * Pipelined bus interface (wb or axi)
10 -- * Maybe add parity ? There's a few bits free in each BRAM row on Xilinx
11 -- * Add optimization: service hits on partially loaded lines
12 -- * Add optimization: (maybe) interrupt reload on fluch/redirect
13 -- * Check if playing with the geometry of the cache tags allow for more
14 -- efficient use of distributed RAM and less logic/muxes. Currently we
15 -- write TAG_BITS width which may not match full ram blocks and might
16 -- cause muxes to be inferred for "partial writes".
17 -- * Check if making the read size of PLRU a ROM helps utilization
18 --
19 library ieee;
20 use ieee.std_logic_1164.all;
21 use ieee.numeric_std.all;
22
23 library work;
24 use work.utils.all;
25 use work.common.all;
26 use work.wishbone_types.all;
27
28 -- 64 bit direct mapped icache. All instructions are 4B aligned.
29
30 entity icache is
31 generic (
32 SIM : boolean := false;
33 -- Line size in bytes
34 LINE_SIZE : positive := 64;
35 -- BRAM organisation: We never access more than wishbone_data_bits at
36 -- a time so to save resources we make the array only that wide, and
37 -- use consecutive indices for to make a cache "line"
38 --
39 -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
40 ROW_SIZE : positive := wishbone_data_bits / 8;
41 -- Number of lines in a set
42 NUM_LINES : positive := 32;
43 -- Number of ways
44 NUM_WAYS : positive := 4;
45 -- L1 ITLB number of entries (direct mapped)
46 TLB_SIZE : positive := 64;
47 -- L1 ITLB log_2(page_size)
48 TLB_LG_PGSZ : positive := 12;
49 -- Number of real address bits that we store
50 REAL_ADDR_BITS : positive := 56
51 );
52 port (
53 clk : in std_ulogic;
54 rst : in std_ulogic;
55
56 i_in : in Fetch1ToIcacheType;
57 i_out : out IcacheToFetch2Type;
58
59 m_in : in MmuToIcacheType;
60
61 stall_out : out std_ulogic;
62 flush_in : in std_ulogic;
63 inval_in : in std_ulogic;
64
65 wishbone_out : out wishbone_master_out;
66 wishbone_in : in wishbone_slave_out
67 );
68 end entity icache;
69
70 architecture rtl of icache is
71 constant ROW_SIZE_BITS : natural := ROW_SIZE*8;
72 -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
73 constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
74 -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
75 -- icache
76 constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
77 -- INSN_PER_ROW is the number of 32bit instructions per BRAM row
78 constant INSN_PER_ROW : natural := ROW_SIZE_BITS / 32;
79 -- Bit fields counts in the address
80
81 -- INSN_BITS is the number of bits to select an instruction in a row
82 constant INSN_BITS : natural := log2(INSN_PER_ROW);
83 -- ROW_BITS is the number of bits to select a row
84 constant ROW_BITS : natural := log2(BRAM_ROWS);
85 -- ROW_LINEBITS is the number of bits to select a row within a line
86 constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
87 -- LINE_OFF_BITS is the number of bits for the offset in a cache line
88 constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
89 -- ROW_OFF_BITS is the number of bits for the offset in a row
90 constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
91 -- INDEX_BITS is the number of bits to select a cache line
92 constant INDEX_BITS : natural := log2(NUM_LINES);
93 -- SET_SIZE_BITS is the log base 2 of the set size
94 constant SET_SIZE_BITS : natural := LINE_OFF_BITS + INDEX_BITS;
95 -- TAG_BITS is the number of bits of the tag part of the address
96 constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS;
97 -- WAY_BITS is the number of bits to select a way
98 constant WAY_BITS : natural := log2(NUM_WAYS);
99
100 -- Example of layout for 32 lines of 64 bytes:
101 --
102 -- .. tag |index| line |
103 -- .. | row | |
104 -- .. | | | |00| zero (2)
105 -- .. | | |-| | INSN_BITS (1)
106 -- .. | |---| | ROW_LINEBITS (3)
107 -- .. | |--- - --| LINE_OFF_BITS (6)
108 -- .. | |- --| ROW_OFF_BITS (3)
109 -- .. |----- ---| | ROW_BITS (8)
110 -- .. |-----| | INDEX_BITS (5)
111 -- .. --------| | TAG_BITS (53)
112
113 subtype row_t is integer range 0 to BRAM_ROWS-1;
114 subtype index_t is integer range 0 to NUM_LINES-1;
115 subtype way_t is integer range 0 to NUM_WAYS-1;
116
117 -- The cache data BRAM organized as described above for each way
118 subtype cache_row_t is std_ulogic_vector(ROW_SIZE_BITS-1 downto 0);
119
120 -- The cache tags LUTRAM has a row per set. Vivado is a pain and will
121 -- not handle a clean (commented) definition of the cache tags as a 3d
122 -- memory. For now, work around it by putting all the tags
123 subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
124 -- type cache_tags_set_t is array(way_t) of cache_tag_t;
125 -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
126 constant TAG_RAM_WIDTH : natural := TAG_BITS * NUM_WAYS;
127 subtype cache_tags_set_t is std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
128 type cache_tags_array_t is array(index_t) of cache_tags_set_t;
129
130 -- The cache valid bits
131 subtype cache_way_valids_t is std_ulogic_vector(NUM_WAYS-1 downto 0);
132 type cache_valids_t is array(index_t) of cache_way_valids_t;
133
134 -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
135 signal cache_tags : cache_tags_array_t;
136 signal cache_valids : cache_valids_t;
137
138 attribute ram_style : string;
139 attribute ram_style of cache_tags : signal is "distributed";
140
141 -- L1 ITLB.
142 constant TLB_BITS : natural := log2(TLB_SIZE);
143 constant TLB_EA_TAG_BITS : natural := 64 - (TLB_LG_PGSZ + TLB_BITS);
144 constant TLB_PTE_BITS : natural := 64;
145
146 subtype tlb_index_t is integer range 0 to TLB_SIZE - 1;
147 type tlb_valids_t is array(tlb_index_t) of std_ulogic;
148 subtype tlb_tag_t is std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
149 type tlb_tags_t is array(tlb_index_t) of tlb_tag_t;
150 subtype tlb_pte_t is std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
151 type tlb_ptes_t is array(tlb_index_t) of tlb_pte_t;
152
153 signal itlb_valids : tlb_valids_t;
154 signal itlb_tags : tlb_tags_t;
155 signal itlb_ptes : tlb_ptes_t;
156 attribute ram_style of itlb_tags : signal is "distributed";
157 attribute ram_style of itlb_ptes : signal is "distributed";
158
159 -- Privilege bit from PTE EAA field
160 signal eaa_priv : std_ulogic;
161
162 -- Cache reload state machine
163 type state_t is (IDLE, CLR_TAG, WAIT_ACK);
164
165 type reg_internal_t is record
166 -- Cache hit state (Latches for 1 cycle BRAM access)
167 hit_way : way_t;
168 hit_nia : std_ulogic_vector(63 downto 0);
169 hit_smark : std_ulogic;
170 hit_valid : std_ulogic;
171
172 -- Cache miss state (reload state machine)
173 state : state_t;
174 wb : wishbone_master_out;
175 store_way : way_t;
176 store_index : index_t;
177 store_row : row_t;
178 store_tag : cache_tag_t;
179 store_valid : std_ulogic;
180
181 -- TLB miss state
182 fetch_failed : std_ulogic;
183 end record;
184
185 signal r : reg_internal_t;
186
187 -- Async signals on incoming request
188 signal req_index : index_t;
189 signal req_row : row_t;
190 signal req_hit_way : way_t;
191 signal req_tag : cache_tag_t;
192 signal req_is_hit : std_ulogic;
193 signal req_is_miss : std_ulogic;
194 signal req_laddr : std_ulogic_vector(63 downto 0);
195
196 signal tlb_req_index : tlb_index_t;
197 signal real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
198 signal ra_valid : std_ulogic;
199 signal priv_fault : std_ulogic;
200 signal access_ok : std_ulogic;
201
202 -- Cache RAM interface
203 type cache_ram_out_t is array(way_t) of cache_row_t;
204 signal cache_out : cache_ram_out_t;
205
206 -- PLRU output interface
207 type plru_out_t is array(index_t) of std_ulogic_vector(WAY_BITS-1 downto 0);
208 signal plru_victim : plru_out_t;
209 signal replace_way : way_t;
210
211 -- Return the cache line index (tag index) for an address
212 function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
213 begin
214 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS)));
215 end;
216
217 -- Return the cache row index (data memory) for an address
218 function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
219 begin
220 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS)));
221 end;
222
223 -- Returns whether this is the last row of a line
224 function is_last_row_addr(addr: wishbone_addr_type) return boolean is
225 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
226 begin
227 return addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) = ones;
228 end;
229
230 -- Returns whether this is the last row of a line
231 function is_last_row(row: row_t) return boolean is
232 variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
233 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
234 begin
235 row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
236 return row_v(ROW_LINEBITS-1 downto 0) = ones;
237 end;
238
239 -- Return the address of the next row in the current cache line
240 function next_row_addr(addr: wishbone_addr_type)
241 return std_ulogic_vector is
242 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
243 variable result : wishbone_addr_type;
244 begin
245 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
246 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
247 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
248 result := addr;
249 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
250 return result;
251 end;
252
253 -- Return the next row in the current cache line. We use a dedicated
254 -- function in order to limit the size of the generated adder to be
255 -- only the bits within a cache line (3 bits with default settings)
256 --
257 function next_row(row: row_t) return row_t is
258 variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
259 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
260 variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
261 begin
262 row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
263 row_idx := row_v(ROW_LINEBITS-1 downto 0);
264 row_v(ROW_LINEBITS-1 downto 0) := std_ulogic_vector(unsigned(row_idx) + 1);
265 return to_integer(unsigned(row_v));
266 end;
267
268 -- Read the instruction word for the given address in the current cache row
269 function read_insn_word(addr: std_ulogic_vector(63 downto 0);
270 data: cache_row_t) return std_ulogic_vector is
271 variable word: integer range 0 to INSN_PER_ROW-1;
272 begin
273 word := to_integer(unsigned(addr(INSN_BITS+2-1 downto 2)));
274 return data(31+word*32 downto word*32);
275 end;
276
277 -- Get the tag value from the address
278 function get_tag(addr: std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0)) return cache_tag_t is
279 begin
280 return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
281 end;
282
283 -- Read a tag from a tag memory row
284 function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
285 begin
286 return tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS);
287 end;
288
289 -- Write a tag to tag memory row
290 procedure write_tag(way: in way_t; tagset: inout cache_tags_set_t;
291 tag: cache_tag_t) is
292 begin
293 tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS) := tag;
294 end;
295
296 -- Simple hash for direct-mapped TLB index
297 function hash_ea(addr: std_ulogic_vector(63 downto 0)) return tlb_index_t is
298 variable hash : std_ulogic_vector(TLB_BITS - 1 downto 0);
299 begin
300 hash := addr(TLB_LG_PGSZ + TLB_BITS - 1 downto TLB_LG_PGSZ)
301 xor addr(TLB_LG_PGSZ + 2 * TLB_BITS - 1 downto TLB_LG_PGSZ + TLB_BITS)
302 xor addr(TLB_LG_PGSZ + 3 * TLB_BITS - 1 downto TLB_LG_PGSZ + 2 * TLB_BITS);
303 return to_integer(unsigned(hash));
304 end;
305 begin
306
307 assert LINE_SIZE mod ROW_SIZE = 0;
308 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
309 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
310 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
311 assert ispow2(INSN_PER_ROW) report "INSN_PER_ROW not power of 2" severity FAILURE;
312 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
313 report "geometry bits don't add up" severity FAILURE;
314 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
315 report "geometry bits don't add up" severity FAILURE;
316 assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
317 report "geometry bits don't add up" severity FAILURE;
318 assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
319 report "geometry bits don't add up" severity FAILURE;
320
321 sim_debug: if SIM generate
322 debug: process
323 begin
324 report "ROW_SIZE = " & natural'image(ROW_SIZE);
325 report "ROW_PER_LINE = " & natural'image(ROW_PER_LINE);
326 report "BRAM_ROWS = " & natural'image(BRAM_ROWS);
327 report "INSN_PER_ROW = " & natural'image(INSN_PER_ROW);
328 report "INSN_BITS = " & natural'image(INSN_BITS);
329 report "ROW_BITS = " & natural'image(ROW_BITS);
330 report "ROW_LINEBITS = " & natural'image(ROW_LINEBITS);
331 report "LINE_OFF_BITS = " & natural'image(LINE_OFF_BITS);
332 report "ROW_OFF_BITS = " & natural'image(ROW_OFF_BITS);
333 report "INDEX_BITS = " & natural'image(INDEX_BITS);
334 report "TAG_BITS = " & natural'image(TAG_BITS);
335 report "WAY_BITS = " & natural'image(WAY_BITS);
336 wait;
337 end process;
338 end generate;
339
340 -- Generate a cache RAM for each way
341 rams: for i in 0 to NUM_WAYS-1 generate
342 signal do_read : std_ulogic;
343 signal do_write : std_ulogic;
344 signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
345 signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
346 signal dout : cache_row_t;
347 signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
348 begin
349 way: entity work.cache_ram
350 generic map (
351 ROW_BITS => ROW_BITS,
352 WIDTH => ROW_SIZE_BITS
353 )
354 port map (
355 clk => clk,
356 rd_en => do_read,
357 rd_addr => rd_addr,
358 rd_data => dout,
359 wr_sel => wr_sel,
360 wr_addr => wr_addr,
361 wr_data => wishbone_in.dat
362 );
363 process(all)
364 begin
365 do_read <= '1';
366 do_write <= '0';
367 if wishbone_in.ack = '1' and r.store_way = i then
368 do_write <= '1';
369 end if;
370 cache_out(i) <= dout;
371 rd_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
372 wr_addr <= std_ulogic_vector(to_unsigned(r.store_row, ROW_BITS));
373 for i in 0 to ROW_SIZE-1 loop
374 wr_sel(i) <= do_write;
375 end loop;
376 end process;
377 end generate;
378
379 -- Generate PLRUs
380 maybe_plrus: if NUM_WAYS > 1 generate
381 begin
382 plrus: for i in 0 to NUM_LINES-1 generate
383 -- PLRU interface
384 signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
385 signal plru_acc_en : std_ulogic;
386 signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
387
388 begin
389 plru : entity work.plru
390 generic map (
391 BITS => WAY_BITS
392 )
393 port map (
394 clk => clk,
395 rst => rst,
396 acc => plru_acc,
397 acc_en => plru_acc_en,
398 lru => plru_out
399 );
400
401 process(req_index, req_is_hit, req_hit_way, req_is_hit, plru_out)
402 begin
403 -- PLRU interface
404 if req_is_hit = '1' and req_index = i then
405 plru_acc_en <= req_is_hit;
406 else
407 plru_acc_en <= '0';
408 end if;
409 plru_acc <= std_ulogic_vector(to_unsigned(req_hit_way, WAY_BITS));
410 plru_victim(i) <= plru_out;
411 end process;
412 end generate;
413 end generate;
414
415 -- TLB hit detection and real address generation
416 itlb_lookup : process(all)
417 variable pte : tlb_pte_t;
418 variable ttag : tlb_tag_t;
419 begin
420 tlb_req_index <= hash_ea(i_in.nia);
421 pte := itlb_ptes(tlb_req_index);
422 ttag := itlb_tags(tlb_req_index);
423 if i_in.virt_mode = '1' then
424 real_addr <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
425 i_in.nia(TLB_LG_PGSZ - 1 downto 0);
426 if ttag = i_in.nia(63 downto TLB_LG_PGSZ + TLB_BITS) then
427 ra_valid <= itlb_valids(tlb_req_index);
428 else
429 ra_valid <= '0';
430 end if;
431 eaa_priv <= pte(3);
432 else
433 real_addr <= i_in.nia(REAL_ADDR_BITS - 1 downto 0);
434 ra_valid <= '1';
435 eaa_priv <= '1';
436 end if;
437
438 -- no IAMR, so no KUEP support for now
439 priv_fault <= eaa_priv and not i_in.priv_mode;
440 access_ok <= ra_valid and not priv_fault;
441 end process;
442
443 -- iTLB update
444 itlb_update: process(clk)
445 variable wr_index : tlb_index_t;
446 begin
447 if rising_edge(clk) then
448 wr_index := hash_ea(m_in.addr);
449 if rst = '1' or (m_in.tlbie = '1' and m_in.doall = '1') then
450 -- clear all valid bits
451 for i in tlb_index_t loop
452 itlb_valids(i) <= '0';
453 end loop;
454 elsif m_in.tlbie = '1' then
455 -- clear entry regardless of hit or miss
456 itlb_valids(wr_index) <= '0';
457 elsif m_in.tlbld = '1' then
458 itlb_tags(wr_index) <= m_in.addr(63 downto TLB_LG_PGSZ + TLB_BITS);
459 itlb_ptes(wr_index) <= m_in.pte;
460 itlb_valids(wr_index) <= '1';
461 end if;
462 end if;
463 end process;
464
465 -- Cache hit detection, output to fetch2 and other misc logic
466 icache_comb : process(all)
467 variable is_hit : std_ulogic;
468 variable hit_way : way_t;
469 begin
470 -- Extract line, row and tag from request
471 req_index <= get_index(i_in.nia);
472 req_row <= get_row(i_in.nia);
473 req_tag <= get_tag(real_addr);
474
475 -- Calculate address of beginning of cache line, will be
476 -- used for cache miss processing if needed
477 --
478 req_laddr <= (63 downto REAL_ADDR_BITS => '0') &
479 real_addr(REAL_ADDR_BITS - 1 downto LINE_OFF_BITS) &
480 (LINE_OFF_BITS-1 downto 0 => '0');
481
482 -- Test if pending request is a hit on any way
483 hit_way := 0;
484 is_hit := '0';
485 for i in way_t loop
486 if i_in.req = '1' and cache_valids(req_index)(i) = '1' then
487 if read_tag(i, cache_tags(req_index)) = req_tag then
488 hit_way := i;
489 is_hit := '1';
490 end if;
491 end if;
492 end loop;
493
494 -- Generate the "hit" and "miss" signals for the synchronous blocks
495 if i_in.req = '1' and access_ok = '1' and flush_in = '0' and rst = '0' then
496 req_is_hit <= is_hit;
497 req_is_miss <= not is_hit;
498 else
499 req_is_hit <= '0';
500 req_is_miss <= '0';
501 end if;
502 req_hit_way <= hit_way;
503
504 -- The way to replace on a miss
505 replace_way <= to_integer(unsigned(plru_victim(req_index)));
506
507 -- Output instruction from current cache row
508 --
509 -- Note: This is a mild violation of our design principle of having pipeline
510 -- stages output from a clean latch. In this case we output the result
511 -- of a mux. The alternative would be output an entire row which
512 -- I prefer not to do just yet as it would force fetch2 to know about
513 -- some of the cache geometry information.
514 --
515 i_out.insn <= read_insn_word(r.hit_nia, cache_out(r.hit_way));
516 i_out.valid <= r.hit_valid;
517 i_out.nia <= r.hit_nia;
518 i_out.stop_mark <= r.hit_smark;
519 i_out.fetch_failed <= r.fetch_failed;
520
521 -- Stall fetch1 if we have a miss on cache or TLB or a protection fault
522 stall_out <= not (is_hit and access_ok);
523
524 -- Wishbone requests output (from the cache miss reload machine)
525 wishbone_out <= r.wb;
526 end process;
527
528 -- Cache hit synchronous machine
529 icache_hit : process(clk)
530 begin
531 if rising_edge(clk) then
532 -- On a hit, latch the request for the next cycle, when the BRAM data
533 -- will be available on the cache_out output of the corresponding way
534 --
535 r.hit_valid <= req_is_hit;
536 -- Send stop marks and NIA down regardless of validity
537 r.hit_smark <= i_in.stop_mark;
538 r.hit_nia <= i_in.nia;
539 if req_is_hit = '1' then
540 r.hit_way <= req_hit_way;
541 r.hit_smark <= i_in.stop_mark;
542
543 report "cache hit nia:" & to_hstring(i_in.nia) &
544 " IR:" & std_ulogic'image(i_in.virt_mode) &
545 " SM:" & std_ulogic'image(i_in.stop_mark) &
546 " idx:" & integer'image(req_index) &
547 " tag:" & to_hstring(req_tag) &
548 " way:" & integer'image(req_hit_way) &
549 " RA:" & to_hstring(real_addr);
550 end if;
551 end if;
552 end process;
553
554 -- Cache miss/reload synchronous machine
555 icache_miss : process(clk)
556 variable tagset : cache_tags_set_t;
557 variable stbs_done : boolean;
558 begin
559 if rising_edge(clk) then
560 -- On reset, clear all valid bits to force misses
561 if rst = '1' then
562 for i in index_t loop
563 cache_valids(i) <= (others => '0');
564 end loop;
565 r.state <= IDLE;
566 r.wb.cyc <= '0';
567 r.wb.stb <= '0';
568
569 -- We only ever do reads on wishbone
570 r.wb.dat <= (others => '0');
571 r.wb.sel <= "11111111";
572 r.wb.we <= '0';
573
574 -- Not useful normally but helps avoiding tons of sim warnings
575 r.wb.adr <= (others => '0');
576 else
577 -- Process cache invalidations
578 if inval_in = '1' then
579 for i in index_t loop
580 cache_valids(i) <= (others => '0');
581 end loop;
582 r.store_valid <= '0';
583 end if;
584
585 -- Main state machine
586 case r.state is
587 when IDLE =>
588 -- We need to read a cache line
589 if req_is_miss = '1' then
590 report "cache miss nia:" & to_hstring(i_in.nia) &
591 " IR:" & std_ulogic'image(i_in.virt_mode) &
592 " SM:" & std_ulogic'image(i_in.stop_mark) &
593 " idx:" & integer'image(req_index) &
594 " way:" & integer'image(replace_way) &
595 " tag:" & to_hstring(req_tag) &
596 " RA:" & to_hstring(real_addr);
597
598 -- Keep track of our index and way for subsequent stores
599 r.store_index <= req_index;
600 r.store_way <= replace_way;
601 r.store_row <= get_row(req_laddr);
602 r.store_tag <= req_tag;
603 r.store_valid <= '1';
604
605 -- Prep for first wishbone read. We calculate the address of
606 -- the start of the cache line and start the WB cycle.
607 --
608 r.wb.adr <= req_laddr(r.wb.adr'left downto 0);
609 r.wb.cyc <= '1';
610 r.wb.stb <= '1';
611
612 -- Track that we had one request sent
613 r.state <= CLR_TAG;
614 end if;
615
616 when CLR_TAG | WAIT_ACK =>
617 if r.state = CLR_TAG then
618 -- Force misses on that way while reloading that line
619 cache_valids(req_index)(r.store_way) <= '0';
620
621 -- Store new tag in selected way
622 for i in 0 to NUM_WAYS-1 loop
623 if i = r.store_way then
624 tagset := cache_tags(r.store_index);
625 write_tag(i, tagset, r.store_tag);
626 cache_tags(r.store_index) <= tagset;
627 end if;
628 end loop;
629
630 r.state <= WAIT_ACK;
631 end if;
632 -- Requests are all sent if stb is 0
633 stbs_done := r.wb.stb = '0';
634
635 -- If we are still sending requests, was one accepted ?
636 if wishbone_in.stall = '0' and not stbs_done then
637 -- That was the last word ? We are done sending. Clear
638 -- stb and set stbs_done so we can handle an eventual last
639 -- ack on the same cycle.
640 --
641 if is_last_row_addr(r.wb.adr) then
642 r.wb.stb <= '0';
643 stbs_done := true;
644 end if;
645
646 -- Calculate the next row address
647 r.wb.adr <= next_row_addr(r.wb.adr);
648 end if;
649
650 -- Incoming acks processing
651 if wishbone_in.ack = '1' then
652 -- Check for completion
653 if stbs_done and is_last_row(r.store_row) then
654 -- Complete wishbone cycle
655 r.wb.cyc <= '0';
656
657 -- Cache line is now valid
658 cache_valids(r.store_index)(r.store_way) <= r.store_valid and not inval_in;
659
660 -- We are done
661 r.state <= IDLE;
662 end if;
663
664 -- Increment store row counter
665 r.store_row <= next_row(r.store_row);
666 end if;
667 end case;
668 end if;
669
670 -- TLB miss and protection fault processing
671 if rst = '1' or flush_in = '1' or m_in.tlbld = '1' then
672 r.fetch_failed <= '0';
673 elsif i_in.req = '1' and access_ok = '0' then
674 r.fetch_failed <= '1';
675 end if;
676 end if;
677 end process;
678 end;