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