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