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