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