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