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