dcache: Introduce an extra cycle latency to make timing
[microwatt.git] / dcache.vhdl
1 --
2 -- Set associative dcache write-through
3 --
4 -- TODO (in no specific order):
5 --
6 -- * See list in icache.vhdl
7 -- * Complete load misses on the cycle when WB data comes instead of
8 -- at the end of line (this requires dealing with requests coming in
9 -- while not idle...)
10 -- * Load with update could use one less non-pipelined cycle by moving
11 -- the register update to the pipeline bubble that exists when going
12 -- back to the IDLE state.
13 --
14 library ieee;
15 use ieee.std_logic_1164.all;
16 use ieee.numeric_std.all;
17
18 library work;
19 use work.common.all;
20 use work.helpers.all;
21 use work.wishbone_types.all;
22
23 entity dcache is
24 generic (
25 -- Line size in bytes
26 LINE_SIZE : positive := 64;
27 -- Number of lines in a set
28 NUM_LINES : positive := 32;
29 -- Number of ways
30 NUM_WAYS : positive := 4
31 );
32 port (
33 clk : in std_ulogic;
34 rst : in std_ulogic;
35
36 d_in : in Loadstore1ToDcacheType;
37 d_out : out DcacheToWritebackType;
38
39 wishbone_out : out wishbone_master_out;
40 wishbone_in : in wishbone_slave_out
41 );
42 end entity dcache;
43
44 architecture rtl of dcache is
45 function log2(i : natural) return integer is
46 variable tmp : integer := i;
47 variable ret : integer := 0;
48 begin
49 while tmp > 1 loop
50 ret := ret + 1;
51 tmp := tmp / 2;
52 end loop;
53 return ret;
54 end function;
55
56 function ispow2(i : integer) return boolean is
57 begin
58 if to_integer(to_unsigned(i, 32) and to_unsigned(i - 1, 32)) = 0 then
59 return true;
60 else
61 return false;
62 end if;
63 end function;
64
65 -- BRAM organisation: We never access more than wishbone_data_bits at
66 -- a time so to save resources we make the array only that wide, and
67 -- use consecutive indices for to make a cache "line"
68 --
69 -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
70 constant ROW_SIZE : natural := wishbone_data_bits / 8;
71 -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
72 constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
73 -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
74 -- dcache
75 constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
76
77 -- Bit fields counts in the address
78
79 -- ROW_BITS is the number of bits to select a row
80 constant ROW_BITS : natural := log2(BRAM_ROWS);
81 -- ROW_LINEBITS is the number of bits to select a row within a line
82 constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
83 -- LINE_OFF_BITS is the number of bits for the offset in a cache line
84 constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
85 -- ROW_OFF_BITS is the number of bits for the offset in a row
86 constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
87 -- INDEX_BITS is the number if bits to select a cache line
88 constant INDEX_BITS : natural := log2(NUM_LINES);
89 -- TAG_BITS is the number of bits of the tag part of the address
90 constant TAG_BITS : natural := 64 - LINE_OFF_BITS - INDEX_BITS;
91 -- WAY_BITS is the number of bits to select a way
92 constant WAY_BITS : natural := log2(NUM_WAYS);
93
94 -- Example of layout for 32 lines of 64 bytes:
95 --
96 -- .. tag |index| line |
97 -- .. | row | |
98 -- .. | |---| | ROW_LINEBITS (3)
99 -- .. | |--- - --| LINE_OFF_BITS (6)
100 -- .. | |- --| ROW_OFF_BITS (3)
101 -- .. |----- ---| | ROW_BITS (8)
102 -- .. |-----| | INDEX_BITS (5)
103 -- .. --------| | TAG_BITS (53)
104
105 subtype row_t is integer range 0 to BRAM_ROWS-1;
106 subtype index_t is integer range 0 to NUM_LINES-1;
107 subtype way_t is integer range 0 to NUM_WAYS-1;
108
109 -- The cache data BRAM organized as described above for each way
110 subtype cache_row_t is std_ulogic_vector(wishbone_data_bits-1 downto 0);
111
112 -- The cache tags LUTRAM has a row per set. Vivado is a pain and will
113 -- not handle a clean (commented) definition of the cache tags as a 3d
114 -- memory. For now, work around it by putting all the tags
115 subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
116 -- type cache_tags_set_t is array(way_t) of cache_tag_t;
117 -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
118 constant TAG_RAM_WIDTH : natural := TAG_BITS * NUM_WAYS;
119 subtype cache_tags_set_t is std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
120 type cache_tags_array_t is array(index_t) of cache_tags_set_t;
121
122 -- The cache valid bits
123 subtype cache_way_valids_t is std_ulogic_vector(NUM_WAYS-1 downto 0);
124 type cache_valids_t is array(index_t) of cache_way_valids_t;
125
126 -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
127 signal cache_tags : cache_tags_array_t;
128 signal cache_valids : cache_valids_t;
129
130 attribute ram_style : string;
131 attribute ram_style of cache_tags : signal is "distributed";
132
133 -- Type of operation on a "valid" input
134 type op_t is (OP_NONE,
135 OP_LOAD_HIT, -- Cache hit on load
136 OP_LOAD_MISS, -- Load missing cache
137 OP_LOAD_NC, -- Non-cachable load
138 OP_BAD, -- BAD: Cache hit on NC load/store
139 OP_STORE_HIT, -- Store hitting cache
140 OP_STORE_MISS); -- Store missing cache
141
142 -- Cache state machine
143 type state_t is (IDLE, -- Normal load hit processing
144 LOAD_UPDATE, -- Load with update extra cycle
145 LOAD_UPDATE2, -- Load with update extra cycle
146 RELOAD_WAIT_ACK, -- Cache reload wait ack
147 STORE_WAIT_ACK, -- Store wait ack
148 NC_LOAD_WAIT_ACK);-- Non-cachable load wait ack
149
150 type reg_internal_t is record
151 req_latch : Loadstore1ToDcacheType;
152
153 -- Cache hit state (Latches for 1 cycle BRAM access)
154 hit_way : way_t;
155 hit_load_valid : std_ulogic;
156
157 -- 1-cycle delayed signals to account for the BRAM extra
158 -- buffer that seems necessary to make timing on load hits
159 --
160 hit_way_delayed : way_t;
161 hit_load_delayed : std_ulogic;
162 hit_load_upd_delayed : std_ulogic;
163 hit_load_reg_delayed : std_ulogic_vector(4 downto 0);
164 hit_data_shift_delayed : std_ulogic_vector(2 downto 0);
165 hit_dlength_delayed : std_ulogic_vector(3 downto 0);
166 hit_sign_ext_delayed : std_ulogic;
167 hit_byte_rev_delayed : std_ulogic;
168
169 -- Register update (load/store with update)
170 update_valid : std_ulogic;
171
172 -- Data buffer for "slow" read ops (load miss and NC loads).
173 slow_data : std_ulogic_vector(63 downto 0);
174 slow_valid : std_ulogic;
175
176 -- Cache miss state (reload state machine)
177 state : state_t;
178 wb : wishbone_master_out;
179 store_way : way_t;
180 store_index : index_t;
181 end record;
182
183 signal r : reg_internal_t;
184
185 -- Async signals on incoming request
186 signal req_index : index_t;
187 signal req_row : row_t;
188 signal req_hit_way : way_t;
189 signal req_tag : cache_tag_t;
190 signal req_op : op_t;
191
192 -- Cache RAM interface
193 type cache_ram_out_t is array(way_t) of cache_row_t;
194 signal cache_out : cache_ram_out_t;
195
196 -- PLRU output interface
197 type plru_out_t is array(index_t) of std_ulogic_vector(WAY_BITS-1 downto 0);
198 signal plru_victim : plru_out_t;
199
200 -- Wishbone read/write/cache write formatting signals
201 signal bus_sel : wishbone_sel_type;
202 signal store_data : wishbone_data_type;
203
204 -- Return the cache line index (tag index) for an address
205 function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
206 begin
207 return to_integer(unsigned(addr(63-TAG_BITS downto LINE_OFF_BITS)));
208 end;
209
210 -- Return the cache row index (data memory) for an address
211 function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
212 begin
213 return to_integer(unsigned(addr(63-TAG_BITS downto ROW_OFF_BITS)));
214 end;
215
216 -- Returns whether this is the last row of a line
217 function is_last_row(addr: std_ulogic_vector(63 downto 0)) return boolean is
218 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
219 begin
220 return addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) = ones;
221 end;
222
223 -- Return the address of the next row in the current cache line
224 function next_row_addr(addr: std_ulogic_vector(63 downto 0)) return std_ulogic_vector is
225 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
226 variable result : std_ulogic_vector(63 downto 0);
227 begin
228 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
229 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
230 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
231 result := addr;
232 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
233 return result;
234 end;
235
236 -- Get the tag value from the address
237 function get_tag(addr: std_ulogic_vector(63 downto 0)) return cache_tag_t is
238 begin
239 return addr(63 downto 64-TAG_BITS);
240 end;
241
242 -- Read a tag from a tag memory row
243 function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
244 begin
245 return tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS);
246 end;
247
248 -- Write a tag to tag memory row
249 procedure write_tag(way: in way_t; tagset: inout cache_tags_set_t;
250 tag: cache_tag_t) is
251 begin
252 tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS) := tag;
253 end;
254
255 -- Generate byte enables from sizes
256 function length_to_sel(length : in std_logic_vector(3 downto 0)) return std_ulogic_vector is
257 begin
258 case length is
259 when "0001" =>
260 return "00000001";
261 when "0010" =>
262 return "00000011";
263 when "0100" =>
264 return "00001111";
265 when "1000" =>
266 return "11111111";
267 when others =>
268 return "00000000";
269 end case;
270 end function length_to_sel;
271
272 -- Calculate shift and byte enables for wishbone
273 function wishbone_data_shift(address : in std_ulogic_vector(63 downto 0)) return natural is
274 begin
275 return to_integer(unsigned(address(2 downto 0))) * 8;
276 end function wishbone_data_shift;
277
278 function wishbone_data_sel(size : in std_logic_vector(3 downto 0);
279 address : in std_logic_vector(63 downto 0))
280 return std_ulogic_vector is
281 begin
282 return std_ulogic_vector(shift_left(unsigned(length_to_sel(size)),
283 to_integer(unsigned(address(2 downto 0)))));
284 end function wishbone_data_sel;
285
286 begin
287
288 assert LINE_SIZE mod ROW_SIZE = 0 report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
289 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
290 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
291 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
292 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
293 report "geometry bits don't add up" severity FAILURE;
294 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
295 report "geometry bits don't add up" severity FAILURE;
296 assert (64 = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
297 report "geometry bits don't add up" severity FAILURE;
298 assert (64 = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
299 report "geometry bits don't add up" severity FAILURE;
300 assert (64 = wishbone_data_bits)
301 report "Can't yet handle a wishbone width that isn't 64-bits" severity FAILURE;
302
303 -- Generate PLRUs
304 maybe_plrus: if NUM_WAYS > 1 generate
305 begin
306 plrus: for i in 0 to NUM_LINES-1 generate
307 -- PLRU interface
308 signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
309 signal plru_acc_en : std_ulogic;
310 signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
311
312 begin
313 plru : entity work.plru
314 generic map (
315 BITS => WAY_BITS
316 )
317 port map (
318 clk => clk,
319 rst => rst,
320 acc => plru_acc,
321 acc_en => plru_acc_en,
322 lru => plru_out
323 );
324
325 process(req_index, req_op, req_hit_way, plru_out)
326 begin
327 -- PLRU interface
328 if (req_op = OP_LOAD_HIT or
329 req_op = OP_STORE_HIT) and req_index = i then
330 plru_acc_en <= '1';
331 else
332 plru_acc_en <= '0';
333 end if;
334 plru_acc <= std_ulogic_vector(to_unsigned(req_hit_way, WAY_BITS));
335 plru_victim(i) <= plru_out;
336 end process;
337 end generate;
338 end generate;
339
340 -- Cache request parsing and hit detection
341 dcache_request : process(all)
342 variable is_hit : std_ulogic;
343 variable hit_way : way_t;
344 variable op : op_t;
345 variable tmp : std_ulogic_vector(63 downto 0);
346 variable data : std_ulogic_vector(63 downto 0);
347 variable opsel : std_ulogic_vector(3 downto 0);
348 begin
349 -- Extract line, row and tag from request
350 req_index <= get_index(d_in.addr);
351 req_row <= get_row(d_in.addr);
352 req_tag <= get_tag(d_in.addr);
353
354 -- Test if pending request is a hit on any way
355 hit_way := 0;
356 is_hit := '0';
357 for i in way_t loop
358 if d_in.valid = '1' and cache_valids(req_index)(i) = '1' then
359 if read_tag(i, cache_tags(req_index)) = req_tag then
360 hit_way := i;
361 is_hit := '1';
362 end if;
363 end if;
364 end loop;
365
366 -- The way that matched on a hit
367 req_hit_way <= hit_way;
368
369 -- Combine the request and cache his status to decide what
370 -- operation needs to be done
371 --
372 opsel := d_in.valid & d_in.load & d_in.nc & is_hit;
373 case opsel is
374 when "1101" => op := OP_LOAD_HIT;
375 when "1100" => op := OP_LOAD_MISS;
376 when "1110" => op := OP_LOAD_NC;
377 when "1001" => op := OP_STORE_HIT;
378 when "1000" => op := OP_STORE_MISS;
379 when "1010" => op := OP_STORE_MISS;
380 when "1011" => op := OP_BAD;
381 when "1111" => op := OP_BAD;
382 when others => op := OP_NONE;
383 end case;
384
385 req_op <= op;
386
387 -- XXX GENERATE ERRORS
388 -- err_nc_collision <= '1' when op = OP_BAD else '0';
389
390 -- XXX Generate stalls
391 -- stall_out <= r.state /= IDLE ?
392
393 end process;
394
395 -- Wire up wishbone request latch
396 wishbone_out <= r.wb;
397
398 -- Writeback (loads and reg updates) & completion control logic
399 --
400 writeback_control: process(all)
401 begin
402
403 -- The mux on d_out.write reg defaults to the normal load hit case.
404 d_out.write_enable <= '0';
405 d_out.valid <= '0';
406 d_out.write_reg <= r.hit_load_reg_delayed;
407 d_out.write_data <= cache_out(r.hit_way_delayed);
408 d_out.write_len <= r.hit_dlength_delayed;
409 d_out.write_shift <= r.hit_data_shift_delayed;
410 d_out.sign_extend <= r.hit_sign_ext_delayed;
411 d_out.byte_reverse <= r.hit_byte_rev_delayed;
412 d_out.second_word <= '0';
413
414 -- We have a valid load or store hit or we just completed a slow
415 -- op such as a load miss, a NC load or a store
416 --
417 -- Note: the load hit is delayed by one cycle. However it can still
418 -- not collide with r.slow_valid (well unless I miscalculated) because
419 -- slow_valid can only be set on a subsequent request and not on its
420 -- first cycle (the state machine must have advanced), which makes
421 -- slow_valid at least 2 cycles from the previous hit_load_valid.
422 --
423
424 -- Sanity: Only one of these must be set in any given cycle
425 assert (r.update_valid and r.hit_load_delayed) /= '1' report
426 "unexpected hit_load_delayed collision with update_valid"
427 severity FAILURE;
428 assert (r.slow_valid and r.hit_load_delayed) /= '1' report
429 "unexpected hit_load_delayed collision with slow_valid"
430 severity FAILURE;
431 assert (r.slow_valid and r.update_valid) /= '1' report
432 "unexpected update_valid collision with slow_valid"
433 severity FAILURE;
434
435 -- Delayed load hit case is the standard path
436 if r.hit_load_delayed = '1' then
437 d_out.write_enable <= '1';
438
439 -- If it's not a load with update, complete it now
440 if r.hit_load_upd_delayed = '0' then
441 d_out.valid <= '1';
442 end if;
443 end if;
444
445 -- Slow ops (load miss, NC, stores)
446 if r.slow_valid = '1' then
447 -- If it's a load, enable register writeback and switch
448 -- mux accordingly
449 --
450 if r.req_latch.load then
451 d_out.write_reg <= r.req_latch.write_reg;
452 d_out.write_enable <= '1';
453
454 -- Read data comes from the slow data latch, formatter
455 -- from the latched request.
456 --
457 d_out.write_data <= r.slow_data;
458 d_out.write_shift <= r.req_latch.addr(2 downto 0);
459 d_out.sign_extend <= r.req_latch.sign_extend;
460 d_out.byte_reverse <= r.req_latch.byte_reverse;
461 d_out.write_len <= r.req_latch.length;
462 end if;
463
464 -- If it's a store or a non-update load form, complete now
465 if r.req_latch.load = '0' or r.req_latch.update = '0' then
466 d_out.valid <= '1';
467 end if;
468 end if;
469
470 -- We have a register update to do.
471 if r.update_valid = '1' then
472 d_out.write_enable <= '1';
473 d_out.write_reg <= r.req_latch.update_reg;
474
475 -- Change the read data mux to the address that's going into
476 -- the register and the formatter does nothing.
477 --
478 d_out.write_data <= r.req_latch.addr;
479 d_out.write_shift <= "000";
480 d_out.write_len <= "1000";
481 d_out.sign_extend <= '0';
482 d_out.byte_reverse <= '0';
483
484 -- If it was a load, this completes the operation (load with
485 -- update case).
486 --
487 if r.req_latch.load = '1' then
488 d_out.valid <= '1';
489 end if;
490 end if;
491
492 end process;
493
494 -- Misc data & sel signals
495 misc: process(d_in)
496 begin
497 -- Wishbone & BRAM write data formatting for stores (most of it already
498 -- happens in loadstore1, this is the remaining sel generation and shifting)
499 --
500 store_data <= std_logic_vector(shift_left(unsigned(d_in.data),
501 wishbone_data_shift(d_in.addr)));
502
503 -- Wishbone read and write and BRAM write sel bits generation
504 bus_sel <= wishbone_data_sel(d_in.length, d_in.addr);
505 end process;
506
507 -- Generate a cache RAM for each way. This handles the normal
508 -- reads, writes from reloads and the special store-hit update
509 -- path as well.
510 --
511 -- Note: the BRAMs have an extra read buffer, meaning the output
512 -- is pipelined an extra cycle. This differs from the
513 -- icache. The writeback logic needs to take that into
514 -- account by using 1-cycle delayed signals for load hits.
515 --
516 rams: for i in 0 to NUM_WAYS-1 generate
517 signal do_read : std_ulogic;
518 signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
519 signal do_write : std_ulogic;
520 signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
521 signal wr_data : std_ulogic_vector(wishbone_data_bits-1 downto 0);
522 signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
523 signal dout : cache_row_t;
524 begin
525 way: entity work.cache_ram
526 generic map (
527 ROW_BITS => ROW_BITS,
528 WIDTH => wishbone_data_bits,
529 ADD_BUF => true
530 )
531 port map (
532 clk => clk,
533 rd_en => do_read,
534 rd_addr => rd_addr,
535 rd_data => dout,
536 wr_en => do_write,
537 wr_sel => wr_sel,
538 wr_addr => wr_addr,
539 wr_data => wr_data
540 );
541 process(all)
542 begin
543 -- Cache hit reads
544 do_read <= '1';
545 rd_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
546 cache_out(i) <= dout;
547
548 -- Write mux:
549 --
550 -- Defaults to wishbone read responses (cache refill),
551 --
552 -- For timing, the mux on wr_data/sel/addr is not dependent on anything
553 -- other than the current state. Only the do_write signal is.
554 --
555 if r.state = IDLE then
556 -- When IDLE, the only write path is the store-hit update case
557 wr_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
558 wr_data <= store_data;
559 wr_sel <= bus_sel;
560 else
561 -- Otherwise, we might be doing a reload
562 wr_data <= wishbone_in.dat;
563 wr_sel <= (others => '1');
564 wr_addr <= std_ulogic_vector(to_unsigned(get_row(r.wb.adr), ROW_BITS));
565 end if;
566
567 -- The two actual write cases here
568 do_write <= '0';
569 if r.state = RELOAD_WAIT_ACK and wishbone_in.ack = '1' and r.store_way = i then
570 do_write <= '1';
571 end if;
572 if req_op = OP_STORE_HIT and req_hit_way = i then
573 assert r.state /= RELOAD_WAIT_ACK report "Store hit while in state:" &
574 state_t'image(r.state)
575 severity FAILURE;
576 do_write <= '1';
577 end if;
578 end process;
579 end generate;
580
581 -- Cache hit synchronous machine for the easy case. This handles
582 -- non-update form load hits.
583 --
584 dcache_fast_hit : process(clk)
585 begin
586 if rising_edge(clk) then
587 -- 1-cycle delayed signals for load hit response
588 r.hit_load_delayed <= r.hit_load_valid;
589 r.hit_way_delayed <= r.hit_way;
590 r.hit_load_upd_delayed <= r.req_latch.update;
591 r.hit_load_reg_delayed <= r.req_latch.write_reg;
592 r.hit_data_shift_delayed <= r.req_latch.addr(2 downto 0);
593 r.hit_sign_ext_delayed <= r.req_latch.sign_extend;
594 r.hit_byte_rev_delayed <= r.req_latch.byte_reverse;
595 r.hit_dlength_delayed <= r.req_latch.length;
596
597 -- On-cycle pulse values get reset on every cycle
598 r.hit_load_valid <= '0';
599
600 -- If we have a request incoming, we have to latch it as d_in.valid
601 -- is only set for a single cycle. It's up to the control logic to
602 -- ensure we don't override an uncompleted request (for now we are
603 -- single issue on load/stores so we are fine, later, we can generate
604 -- a stall output if necessary).
605
606 if d_in.valid = '1' then
607 r.req_latch <= d_in;
608
609 report "op:" & op_t'image(req_op) &
610 " addr:" & to_hstring(d_in.addr) &
611 " upd:" & std_ulogic'image(d_in.update) &
612 " nc:" & std_ulogic'image(d_in.nc) &
613 " reg:" & to_hstring(d_in.write_reg) &
614 " idx:" & integer'image(req_index) &
615 " tag:" & to_hstring(req_tag) &
616 " way: " & integer'image(req_hit_way);
617 end if;
618
619 -- Fast path for load/store hits. Set signals for the writeback controls.
620 if req_op = OP_LOAD_HIT then
621 r.hit_way <= req_hit_way;
622 r.hit_load_valid <= '1';
623 end if;
624 end if;
625 end process;
626
627 -- Every other case is handled by this stage machine:
628 --
629 -- * Cache load miss/reload (in conjunction with "rams")
630 -- * Load hits for update forms
631 -- * Load hits for non-cachable forms
632 -- * Stores (the collision case is handled in "rams")
633 --
634 -- All wishbone requests generation is done here
635 --
636 dcache_slow : process(clk)
637 variable way : integer range 0 to NUM_WAYS-1;
638 variable tagset : cache_tags_set_t;
639 begin
640 if rising_edge(clk) then
641 -- On reset, clear all valid bits to force misses
642 if rst = '1' then
643 for i in index_t loop
644 cache_valids(i) <= (others => '0');
645 end loop;
646 r.state <= IDLE;
647 r.slow_valid <= '0';
648 r.update_valid <= '0';
649 r.wb.cyc <= '0';
650 r.wb.stb <= '0';
651
652 -- Not useful normally but helps avoiding tons of sim warnings
653 r.wb.adr <= (others => '0');
654 else
655 -- One cycle pulses reset
656 r.slow_valid <= '0';
657 r.update_valid <= '0';
658
659 -- We cannot currently process a new request when not idle
660 assert req_op = OP_NONE or r.state = IDLE report "request " &
661 op_t'image(req_op) & " while in state " & state_t'image(r.state)
662 severity FAILURE;
663
664 -- Main state machine
665 case r.state is
666 when IDLE =>
667 case req_op is
668 when OP_LOAD_HIT =>
669 -- We have a load with update hit, we need the delayed update cycle
670 if d_in.update = '1' then
671 r.state <= LOAD_UPDATE;
672 end if;
673
674 when OP_LOAD_MISS =>
675 -- Normal load cache miss, start the reload machine
676 --
677 -- First find a victim way from the PLRU
678 --
679 way := to_integer(unsigned(plru_victim(req_index)));
680
681 report "cache miss addr:" & to_hstring(d_in.addr) &
682 " idx:" & integer'image(req_index) &
683 " way:" & integer'image(way) &
684 " tag:" & to_hstring(req_tag);
685
686 -- Force misses on that way while reloading that line
687 cache_valids(req_index)(way) <= '0';
688
689 -- Store new tag in selected way
690 for i in 0 to NUM_WAYS-1 loop
691 if i = way then
692 tagset := cache_tags(req_index);
693 write_tag(i, tagset, req_tag);
694 cache_tags(req_index) <= tagset;
695 end if;
696 end loop;
697
698 -- Keep track of our index and way for subsequent stores.
699 r.store_index <= req_index;
700 r.store_way <= way;
701
702 -- Prep for first wishbone read. We calculate the address of
703 -- the start of the cache line
704 --
705 r.wb.adr <= d_in.addr(63 downto LINE_OFF_BITS) &
706 (LINE_OFF_BITS-1 downto 0 => '0');
707 r.wb.sel <= (others => '1');
708 r.wb.we <= '0';
709 r.wb.cyc <= '1';
710 r.wb.stb <= '1';
711 r.state <= RELOAD_WAIT_ACK;
712
713 when OP_LOAD_NC =>
714 r.wb.sel <= bus_sel;
715 r.wb.adr <= d_in.addr(63 downto 3) & "000";
716 r.wb.cyc <= '1';
717 r.wb.stb <= '1';
718 r.wb.we <= '0';
719 r.state <= NC_LOAD_WAIT_ACK;
720
721 when OP_STORE_HIT | OP_STORE_MISS =>
722 -- For store-with-update do the register update
723 if d_in.update = '1' then
724 r.update_valid <= '1';
725 end if;
726 r.wb.sel <= bus_sel;
727 r.wb.adr <= d_in.addr(63 downto 3) & "000";
728 r.wb.dat <= store_data;
729 r.wb.cyc <= '1';
730 r.wb.stb <= '1';
731 r.wb.we <= '1';
732 r.state <= STORE_WAIT_ACK;
733
734 -- OP_NONE and OP_BAD do nothing
735 when OP_NONE =>
736 when OP_BAD =>
737 end case;
738
739 when RELOAD_WAIT_ACK =>
740 if wishbone_in.ack = '1' then
741 -- Is this the data we were looking for ? Latch it so
742 -- we can respond later. We don't currently complete the
743 -- pending miss request immediately, we wait for the
744 -- whole line to be loaded. The reason is that if we
745 -- did, we would potentially get new requests in while
746 -- not idle, which we don't currently know how to deal
747 -- with.
748 --
749 if r.wb.adr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) =
750 r.req_latch.addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) then
751 r.slow_data <= wishbone_in.dat;
752 end if;
753
754 -- That was the last word ? We are done
755 if is_last_row(r.wb.adr) then
756 cache_valids(r.store_index)(way) <= '1';
757 r.wb.cyc <= '0';
758 r.wb.stb <= '0';
759
760 -- Complete the load that missed. For load with update
761 -- we also need to do the deferred update cycle.
762 --
763 r.slow_valid <= '1';
764 if r.req_latch.load = '1' and r.req_latch.update = '1' then
765 r.state <= LOAD_UPDATE;
766 report "completing miss with load-update !";
767 else
768 r.state <= IDLE;
769 report "completing miss !";
770 end if;
771 else
772 -- Otherwise, calculate the next row address
773 r.wb.adr <= next_row_addr(r.wb.adr);
774 end if;
775 end if;
776
777 when LOAD_UPDATE =>
778 -- We need the extra cycle to complete a load with update
779 r.state <= LOAD_UPDATE2;
780 when LOAD_UPDATE2 =>
781 -- We need the extra cycle to complete a load with update
782 r.update_valid <= '1';
783 r.state <= IDLE;
784
785 when STORE_WAIT_ACK | NC_LOAD_WAIT_ACK =>
786 if wishbone_in.ack = '1' then
787 if r.state = NC_LOAD_WAIT_ACK then
788 r.slow_data <= wishbone_in.dat;
789 end if;
790 r.slow_valid <= '1';
791 r.wb.cyc <= '0';
792 r.wb.stb <= '0';
793 r.state <= IDLE;
794 end if;
795 end case;
796 end if;
797 end if;
798 end process;
799 end;