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