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