3 based on Anton Blanchard microwatt dcache.vhdl
7 from enum
import Enum
, unique
9 from nmigen
import Module
, Signal
, Elaboratable
,
11 from nmigen
.cli
import main
12 from nmigen
.iocontrol
import RecordObject
13 from nmigen
.util
import log2_int
15 from experiment
.mem_types
import LoadStore1ToDcacheType
,
16 DcacheToLoadStore1Type
,
20 from experiment
.wb_types
import WB_ADDR_BITS
, WB_DATA_BITS
, WB_SEL_BITS
,
21 WBAddrType
, WBDataType
, WBSelType
,
22 WbMasterOut
, WBSlaveOut
, WBMasterOutVector
,
23 WBSlaveOutVector
, WBIOMasterOut
,
27 # -- Set associative dcache write-through
29 # -- TODO (in no specific order):
31 # -- * See list in icache.vhdl
32 # -- * Complete load misses on the cycle when WB data comes instead of
33 # -- at the end of line (this requires dealing with requests coming in
34 # -- while not idle...)
37 # use ieee.std_logic_1164.all;
38 # use ieee.numeric_std.all;
42 # use work.common.all;
43 # use work.helpers.all;
44 # use work.wishbone_types.all;
47 class Dcache(Elaboratable
):
49 # -- Line size in bytes
50 # LINE_SIZE : positive := 64;
51 # -- Number of lines in a set
52 # NUM_LINES : positive := 32;
54 # NUM_WAYS : positive := 4;
55 # -- L1 DTLB entries per set
56 # TLB_SET_SIZE : positive := 64;
57 # -- L1 DTLB number of sets
58 # TLB_NUM_WAYS : positive := 2;
59 # -- L1 DTLB log_2(page_size)
60 # TLB_LG_PGSZ : positive := 12;
61 # -- Non-zero to enable log data collection
62 # LOG_LENGTH : natural := 0
67 # Number of lines in a set
71 # L1 DTLB entries per set
72 self
.TLB_SET_SIZE
= 64
73 # L1 DTLB number of sets
75 # L1 DTLB log_2(page_size)
77 # Non-zero to enable log data collection
80 # clk : in std_ulogic;
81 # rst : in std_ulogic;
83 # d_in : in Loadstore1ToDcacheType;
84 # d_out : out DcacheToLoadstore1Type;
86 # m_in : in MmuToDcacheType;
87 # m_out : out DcacheToMmuType;
89 # stall_out : out std_ulogic;
91 # wishbone_out : out wishbone_master_out;
92 # wishbone_in : in wishbone_slave_out;
94 # log_out : out std_ulogic_vector(19 downto 0)
96 self
.d_in
= LoadStore1ToDcacheType()
97 self
.d_out
= DcacheToLoadStore1Type()
99 self
.m_in
= MmuToDcacheType()
100 self
.m_out
= DcacheToMmuType()
102 self
.stall_out
= Signal()
104 self
.wb_out
= WBMasterOut()
105 self
.wb_in
= WBSlaveOut()
107 self
.log_out
= Signal(20)
110 # architecture rtl of dcache is
111 def elaborate(self
, platform
):
112 LINE_SIZE
= self
.LINE_SIZE
113 NUM_LINES
= self
.NUM_LINES
114 NUM_WAYS
= self
.NUM_WAYS
115 TLB_SET_SIZE
= self
.TLB_SET_SIZE
116 TLB_NUM_WAYS
= self
.TLB_NUM_WAYS
117 TLB_LG_PGSZ
= self
.TLB_LG_PGSZ
118 LOG_LENGTH
= self
.LOG_LENGTH
120 # -- BRAM organisation: We never access more than
121 # -- wishbone_data_bits at a time so to save
122 # -- resources we make the array only that wide, and
123 # -- use consecutive indices for to make a cache "line"
125 # -- ROW_SIZE is the width in bytes of the BRAM
126 # -- (based on WB, so 64-bits)
127 # constant ROW_SIZE : natural := wishbone_data_bits / 8;
128 # BRAM organisation: We never access more than
129 # -- wishbone_data_bits at a time so to save
130 # -- resources we make the array only that wide, and
131 # -- use consecutive indices for to make a cache "line"
133 # -- ROW_SIZE is the width in bytes of the BRAM
134 # -- (based on WB, so 64-bits)
135 ROW_SIZE
= wishbone_data_bits
/ 8;
137 # -- ROW_PER_LINE is the number of row (wishbone
138 # -- transactions) in a line
139 # constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
140 # -- BRAM_ROWS is the number of rows in BRAM needed
141 # -- to represent the full dcache
142 # constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
143 # ROW_PER_LINE is the number of row (wishbone
144 # transactions) in a line
145 ROW_PER_LINE
= LINE_SIZE
/ ROW_SIZE
146 # BRAM_ROWS is the number of rows in BRAM needed
147 # to represent the full dcache
148 BRAM_ROWS
= NUM_LINES
* ROW_PER_LINE
150 # -- Bit fields counts in the address
152 # -- REAL_ADDR_BITS is the number of real address
153 # -- bits that we store
154 # constant REAL_ADDR_BITS : positive := 56;
155 # -- ROW_BITS is the number of bits to select a row
156 # constant ROW_BITS : natural := log2(BRAM_ROWS);
157 # -- ROW_LINEBITS is the number of bits to select
158 # -- a row within a line
159 # constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
160 # -- LINE_OFF_BITS is the number of bits for
161 # -- the offset in a cache line
162 # constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
163 # -- ROW_OFF_BITS is the number of bits for
164 # -- the offset in a row
165 # constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
166 # -- INDEX_BITS is the number if bits to
167 # -- select a cache line
168 # constant INDEX_BITS : natural := log2(NUM_LINES);
169 # -- SET_SIZE_BITS is the log base 2 of the set size
170 # constant SET_SIZE_BITS : natural := LINE_OFF_BITS
172 # -- TAG_BITS is the number of bits of
173 # -- the tag part of the address
174 # constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS;
175 # -- TAG_WIDTH is the width in bits of each way of the tag RAM
176 # constant TAG_WIDTH : natural := TAG_BITS + 7
177 # - ((TAG_BITS + 7) mod 8);
178 # -- WAY_BITS is the number of bits to select a way
179 # constant WAY_BITS : natural := log2(NUM_WAYS);
180 # Bit fields counts in the address
182 # REAL_ADDR_BITS is the number of real address
185 # ROW_BITS is the number of bits to select a row
186 ROW_BITS
= log2_int(BRAM_ROWS
)
187 # ROW_LINEBITS is the number of bits to select
188 # a row within a line
189 ROW_LINEBITS
= log2_int(ROW_PER_LINE
)
190 # LINE_OFF_BITS is the number of bits for
191 # the offset in a cache line
192 LINE_OFF_BITS
= log2_int(LINE_SIZE
)
193 # ROW_OFF_BITS is the number of bits for
194 # the offset in a row
195 ROW_OFF_BITS
= log2_int(ROW_SIZE
)
196 # INDEX_BITS is the number if bits to
197 # select a cache line
198 INDEX_BITS
= log2_int(NUM_LINES
)
199 # SET_SIZE_BITS is the log base 2 of the set size
200 SET_SIZE_BITS
= LINE_OFF_BITS
+ INDEX_BITS
201 # TAG_BITS is the number of bits of
202 # the tag part of the address
203 TAG_BITS
= REAL_ADDR_BITS
- SET_SIZE_BITS
204 # TAG_WIDTH is the width in bits of each way of the tag RAM
205 TAG_WIDTH
= TAG_BITS
+ 7 - ((TAG_BITS
+ 7) % 8)
206 # WAY_BITS is the number of bits to select a way
207 WAY_BITS
= log2_int(NUM_WAYS
)
209 # -- Example of layout for 32 lines of 64 bytes:
211 # -- .. tag |index| line |
213 # -- .. | |---| | ROW_LINEBITS (3)
214 # -- .. | |--- - --| LINE_OFF_BITS (6)
215 # -- .. | |- --| ROW_OFF_BITS (3)
216 # -- .. |----- ---| | ROW_BITS (8)
217 # -- .. |-----| | INDEX_BITS (5)
218 # -- .. --------| | TAG_BITS (45)
219 # Example of layout for 32 lines of 64 bytes:
221 # .. tag |index| line |
223 # .. | |---| | ROW_LINEBITS (3)
224 # .. | |--- - --| LINE_OFF_BITS (6)
225 # .. | |- --| ROW_OFF_BITS (3)
226 # .. |----- ---| | ROW_BITS (8)
227 # .. |-----| | INDEX_BITS (5)
228 # .. --------| | TAG_BITS (45)
231 # subtype row_t is integer range 0 to BRAM_ROWS-1;
232 # subtype index_t is integer range 0 to NUM_LINES-1;
233 # subtype way_t is integer range 0 to NUM_WAYS-1;
234 # subtype row_in_line_t is unsigned(ROW_LINEBITS-1 downto 0);
236 return Signal(BRAM_ROWS
)
239 return Signal(NUM_LINES
)
242 return Signal(NUM_WAYS
)
245 return Signal(ROW_LINEBITS
)
247 # -- The cache data BRAM organized as described above for each way
248 # subtype cache_row_t is
249 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
250 # The cache data BRAM organized as described above for each way
252 return Signal(WB_DATA_BITS
)
254 # -- The cache tags LUTRAM has a row per set.
255 # -- Vivado is a pain and will not handle a
256 # -- clean (commented) definition of the cache
257 # -- tags as a 3d memory. For now, work around
258 # -- it by putting all the tags
259 # subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
260 # The cache tags LUTRAM has a row per set.
261 # Vivado is a pain and will not handle a
262 # clean (commented) definition of the cache
263 # tags as a 3d memory. For now, work around
264 # it by putting all the tags
266 return Signal(TAG_BITS
)
268 # -- type cache_tags_set_t is array(way_t) of cache_tag_t;
269 # -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
270 # constant TAG_RAM_WIDTH : natural := TAG_WIDTH * NUM_WAYS;
271 # subtype cache_tags_set_t is
272 # std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
273 # type cache_tags_array_t is array(index_t) of cache_tags_set_t;
274 # type cache_tags_set_t is array(way_t) of cache_tag_t;
275 # type cache_tags_array_t is array(index_t) of cache_tags_set_t;
276 TAG_RAM_WIDTH
= TAG_WIDTH
* NUM_WAYS
279 return Signal(TAG_RAM_WIDTH
)
282 return Array(CacheTagSet() for x
in range(Index()))
284 # -- The cache valid bits
285 # subtype cache_way_valids_t is
286 # std_ulogic_vector(NUM_WAYS-1 downto 0);
287 # type cache_valids_t is array(index_t) of cache_way_valids_t;
288 # type row_per_line_valid_t is
289 # array(0 to ROW_PER_LINE - 1) of std_ulogic;
290 # The cache valid bits
291 def CacheWayValidBits():
292 return Signal(NUM_WAYS
)
293 def CacheValidBits():
294 return Array(CacheWayValidBits() for x
in range(Index()))
295 def RowPerLineValid():
296 return Array(Signal() for x
in range(ROW_PER_LINE
))
298 # -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
299 # signal cache_tags : cache_tags_array_t;
300 # signal cache_tag_set : cache_tags_set_t;
301 # signal cache_valids : cache_valids_t;
303 # attribute ram_style : string;
304 # attribute ram_style of cache_tags : signal is "distributed";
305 # Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
306 cache_tags
= CacheTagArray()
307 cache_tag_set
= CacheTagSet()
308 cache_valid_bits
= CacheValidBits()
310 # TODO attribute ram_style : string;
311 # TODO attribute ram_style of cache_tags : signal is "distributed";
314 # constant TLB_SET_BITS : natural := log2(TLB_SET_SIZE);
315 # constant TLB_WAY_BITS : natural := log2(TLB_NUM_WAYS);
316 # constant TLB_EA_TAG_BITS : natural :=
317 # 64 - (TLB_LG_PGSZ + TLB_SET_BITS);
318 # constant TLB_TAG_WAY_BITS : natural :=
319 # TLB_NUM_WAYS * TLB_EA_TAG_BITS;
320 # constant TLB_PTE_BITS : natural := 64;
321 # constant TLB_PTE_WAY_BITS : natural :=
322 # TLB_NUM_WAYS * TLB_PTE_BITS;
324 TLB_SET_BITS
= log2_int(TLB_SET_SIZE
)
325 TLB_WAY_BITS
= log2_int(TLB_NUM_WAYS
)
326 TLB_EA_TAG_BITS
= 64 - (TLB_LG_PGSZ
+ TLB_SET_BITS
)
327 TLB_TAG_WAY_BITS
= TLB_NUM_WAYS
* TLB_EA_TAG_BITS
329 TLB_PTE_WAY_BITS
= TLB_NUM_WAYS
* TLB_PTE_BITS
;
331 # subtype tlb_way_t is integer range 0 to TLB_NUM_WAYS - 1;
332 # subtype tlb_index_t is integer range 0 to TLB_SET_SIZE - 1;
333 # subtype tlb_way_valids_t is
334 # std_ulogic_vector(TLB_NUM_WAYS-1 downto 0);
335 # type tlb_valids_t is
336 # array(tlb_index_t) of tlb_way_valids_t;
337 # subtype tlb_tag_t is
338 # std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
339 # subtype tlb_way_tags_t is
340 # std_ulogic_vector(TLB_TAG_WAY_BITS-1 downto 0);
342 # array(tlb_index_t) of tlb_way_tags_t;
343 # subtype tlb_pte_t is
344 # std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
345 # subtype tlb_way_ptes_t is
346 # std_ulogic_vector(TLB_PTE_WAY_BITS-1 downto 0);
347 # type tlb_ptes_t is array(tlb_index_t) of tlb_way_ptes_t;
348 # type hit_way_set_t is array(tlb_way_t) of way_t;
350 return Signal(TLB_NUM_WAYS
)
353 return Signal(TLB_SET_SIZE
)
355 def TLBWayValidBits():
356 return Signal(TLB_NUM_WAYS
)
359 return Array(TLBValidBits() for x
in range(TLBIndex()))
362 return Signal(TLB_EA_TAG_BITS
)
365 return Signal(TLB_TAG_WAY_BITS
)
368 return Array(TLBWayTags() for x
in range (TLBIndex()))
371 return Signal(TLB_PTE_BITS
)
374 return Signal(TLB_PTE_WAY_BITS
)
377 return Array(TLBWayPtes() for x
in range(TLBIndex()))
380 return Array(Way() for x
in range(TLBWay()))
382 # signal dtlb_valids : tlb_valids_t;
383 # signal dtlb_tags : tlb_tags_t;
384 # signal dtlb_ptes : tlb_ptes_t;
385 # attribute ram_style of dtlb_tags : signal is "distributed";
386 # attribute ram_style of dtlb_ptes : signal is "distributed";
387 dtlb_valids
= tlb_valids_t
;
388 dtlb_tags
= tlb_tags_t
;
389 dtlb_ptes
= tlb_ptes_t
;
390 # TODO attribute ram_style of dtlb_tags : signal is "distributed";
391 # TODO attribute ram_style of dtlb_ptes : signal is "distributed";
394 # -- Record for storing permission, attribute, etc. bits from a PTE
395 # type perm_attr_t is record
396 # reference : std_ulogic;
397 # changed : std_ulogic;
398 # nocache : std_ulogic;
400 # rd_perm : std_ulogic;
401 # wr_perm : std_ulogic;
403 # Record for storing permission, attribute, etc. bits from a PTE
404 class PermAttr(RecordObject
):
407 self
.reference
= Signal()
408 self
.changed
= Signal()
409 self
.nocache
= Signal()
411 self
.rd_perm
= Signal()
412 self
.wr_perm
= Signal()
414 # function extract_perm_attr(
415 # pte : std_ulogic_vector(TLB_PTE_BITS - 1 downto 0))
416 # return perm_attr_t is
417 # variable pa : perm_attr_t;
419 # pa.reference := pte(8);
420 # pa.changed := pte(7);
421 # pa.nocache := pte(5);
423 # pa.rd_perm := pte(2);
424 # pa.wr_perm := pte(1);
427 def extract_perm_attr(pte
=Signal(TLB_PTE_BITS
)):
429 pa
.reference
= pte
[8]
437 # constant real_mode_perm_attr : perm_attr_t :=
438 # (nocache => '0', others => '1');
439 REAL_MODE_PERM_ATTR
= PermAttr()
440 REAL_MODE_PERM_ATTR
.reference
= 1
441 REAL_MODE_PERM_ATTR
.changed
= 1
442 REAL_MODE_PERM_ATTR
.priv
= 1
443 REAL_MODE_PERM_ATTR
.rd_perm
= 1
444 REAL_MODE_PERM_ATTR
.wr_perm
= 1
446 # -- Type of operation on a "valid" input
450 # OP_BAD, -- NC cache hit, TLB miss, prot/RC failure
451 # OP_STCX_FAIL, -- conditional store w/o reservation
452 # OP_LOAD_HIT, -- Cache hit on load
453 # OP_LOAD_MISS, -- Load missing cache
454 # OP_LOAD_NC, -- Non-cachable load
455 # OP_STORE_HIT, -- Store hitting cache
456 # OP_STORE_MISS -- Store missing cache
458 # Type of operation on a "valid" input
462 OP_BAD
= 1 # NC cache hit, TLB miss, prot/RC failure
463 OP_STCX_FAIL
= 2 # conditional store w/o reservation
464 OP_LOAD_HIT
= 3 # Cache hit on load
465 OP_LOAD_MISS
= 4 # Load missing cache
466 OP_LOAD_NC
= 5 # Non-cachable load
467 OP_STORE_HIT
= 6 # Store hitting cache
468 OP_STORE_MISS
= 7 # Store missing cache
470 # -- Cache state machine
473 # IDLE, -- Normal load hit processing
474 # RELOAD_WAIT_ACK, -- Cache reload wait ack
475 # STORE_WAIT_ACK, -- Store wait ack
476 # NC_LOAD_WAIT_ACK -- Non-cachable load wait ack
478 # Cache state machine
481 IDLE
= 0 # Normal load hit processing
482 RELOAD_WAIT_ACK
= 1 # Cache reload wait ack
483 STORE_WAIT_ACK
= 2 # Store wait ack
484 NC_LOAD_WAIT_ACK
= 3 # Non-cachable load wait ack
486 # -- Dcache operations:
488 # -- In order to make timing, we use the BRAMs with
489 # -- an output buffer, which means that the BRAM
490 # -- output is delayed by an extra cycle.
492 # -- Thus, the dcache has a 2-stage internal pipeline
493 # -- for cache hits with no stalls.
495 # -- All other operations are handled via stalling
496 # -- in the first stage.
498 # -- The second stage can thus complete a hit at the same
499 # -- time as the first stage emits a stall for a complex op.
501 # -- Stage 0 register, basically contains just the latched request
502 # type reg_stage_0_t is record
503 # req : Loadstore1ToDcacheType;
504 # tlbie : std_ulogic;
505 # doall : std_ulogic;
506 # tlbld : std_ulogic;
507 # mmu_req : std_ulogic; -- indicates source of request
511 # In order to make timing, we use the BRAMs with
512 # an output buffer, which means that the BRAM
513 # output is delayed by an extra cycle.
515 # Thus, the dcache has a 2-stage internal pipeline
516 # for cache hits with no stalls.
518 # All other operations are handled via stalling
519 # in the first stage.
521 # The second stage can thus complete a hit at the same
522 # time as the first stage emits a stall for a complex op.
524 # Stage 0 register, basically contains just the latched request
525 class RegStage0(RecordObject
):
528 self
.req
= LoadStore1ToDcacheType()
529 self
.tlbie
= Signal()
530 self
.doall
= Signal()
531 self
.tlbld
= Signal()
532 self
.mmu_req
= Signal() # indicates source of request
534 # signal r0 : reg_stage_0_t;
535 # signal r0_full : std_ulogic;
539 # type mem_access_request_t is record
541 # valid : std_ulogic;
543 # real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
544 # data : std_ulogic_vector(63 downto 0);
545 # byte_sel : std_ulogic_vector(7 downto 0);
547 # same_tag : std_ulogic;
548 # mmu_req : std_ulogic;
550 class MemAccessRequest(RecordObject
):
554 self
.valid
= Signal()
556 self
.real_addr
= Signal(REAL_ADDR_BITS
)
557 self
.data
= Signal(64)
558 self
.byte_sel
= Signal(8)
560 self
.same_tag
= Signal()
561 self
.mmu_req
= Signal()
563 # -- First stage register, contains state for stage 1 of load hits
564 # -- and for the state machine used by all other operations
565 # type reg_stage_1_t is record
566 # -- Info about the request
567 # full : std_ulogic; -- have uncompleted request
568 # mmu_req : std_ulogic; -- request is from MMU
569 # req : mem_access_request_t;
573 # hit_load_valid : std_ulogic;
574 # hit_index : index_t;
575 # cache_hit : std_ulogic;
578 # tlb_hit : std_ulogic;
579 # tlb_hit_way : tlb_way_t;
580 # tlb_hit_index : tlb_index_t;
582 # -- 2-stage data buffer for data forwarded from writes to reads
583 # forward_data1 : std_ulogic_vector(63 downto 0);
584 # forward_data2 : std_ulogic_vector(63 downto 0);
585 # forward_sel1 : std_ulogic_vector(7 downto 0);
586 # forward_valid1 : std_ulogic;
587 # forward_way1 : way_t;
588 # forward_row1 : row_t;
589 # use_forward1 : std_ulogic;
590 # forward_sel : std_ulogic_vector(7 downto 0);
592 # -- Cache miss state (reload state machine)
595 # write_bram : std_ulogic;
596 # write_tag : std_ulogic;
597 # slow_valid : std_ulogic;
598 # wb : wishbone_master_out;
599 # reload_tag : cache_tag_t;
602 # store_index : index_t;
603 # end_row_ix : row_in_line_t;
604 # rows_valid : row_per_line_valid_t;
605 # acks_pending : unsigned(2 downto 0);
606 # inc_acks : std_ulogic;
607 # dec_acks : std_ulogic;
609 # -- Signals to complete (possibly with error)
610 # ls_valid : std_ulogic;
611 # ls_error : std_ulogic;
612 # mmu_done : std_ulogic;
613 # mmu_error : std_ulogic;
614 # cache_paradox : std_ulogic;
616 # -- Signal to complete a failed stcx.
617 # stcx_fail : std_ulogic;
619 # First stage register, contains state for stage 1 of load hits
620 # and for the state machine used by all other operations
621 class RegStage1(RecordObject
):
624 # Info about the request
625 self
.full
= Signal() # have uncompleted request
626 self
.mmu_req
= Signal() # request is from MMU
627 self
.req
= MemAccessRequest()
631 self
.hit_load_valid
= Signal()
632 self
.hit_index
= Index()
633 self
.cache_hit
= Signal()
636 self
.tlb_hit
= Signal()
637 self
.tlb_hit_way
= TLBWay()
638 self
.tlb_hit_index
= TLBIndex()
640 # 2-stage data buffer for data forwarded from writes to reads
641 self
.forward_data1
= Signal(64)
642 self
.forward_data2
= Signal(64)
643 self
.forward_sel1
= Signal(8)
644 self
.forward_valid1
= Signal()
645 self
.forward_way1
= Way()
646 self
.forward_row1
= Row()
647 self
.use_forward1
= Signal()
648 self
.forward_sel
= Signal(8)
650 # Cache miss state (reload state machine)
653 self
.write_bram
= Signal()
654 self
.write_tag
= Signal()
655 self
.slow_valid
= Signal()
656 self
.wb
= WishboneMasterOut()
657 self
.reload_tag
= CacheTag()
658 self
.store_way
= Way()
659 self
.store_row
= Row()
660 self
.store_index
= Index()
661 self
.end_row_ix
= RowInLine()
662 self
.rows_valid
= RowPerLineValid()
663 self
.acks_pending
= Signal(3)
664 self
.inc_acks
= Signal()
665 self
.dec_acks
= Signal()
667 # Signals to complete (possibly with error)
668 self
.ls_valid
= Signal()
669 self
.ls_error
= Signal()
670 self
.mmu_done
= Signal()
671 self
.mmu_error
= Signal()
672 self
.cache_paradox
= Signal()
674 # Signal to complete a failed stcx.
675 self
.stcx_fail
= Signal()
677 # signal r1 : reg_stage_1_t;
680 # -- Reservation information
682 # type reservation_t is record
683 # valid : std_ulogic;
684 # addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
686 # Reservation information
688 class Reservation(RecordObject
):
692 addr
= Signal(63 downto LINE_OFF_BITS
) # TODO LINE_OFF_BITS is 6
694 # signal reservation : reservation_t;
696 # -- Async signals on incoming request
697 # signal req_index : index_t;
698 # signal req_row : row_t;
699 # signal req_hit_way : way_t;
700 # signal req_tag : cache_tag_t;
701 # signal req_op : op_t;
702 # signal req_data : std_ulogic_vector(63 downto 0);
703 # signal req_same_tag : std_ulogic;
704 # signal req_go : std_ulogic;
706 # signal early_req_row : row_t;
708 # signal cancel_store : std_ulogic;
709 # signal set_rsrv : std_ulogic;
710 # signal clear_rsrv : std_ulogic;
712 # signal r0_valid : std_ulogic;
713 # signal r0_stall : std_ulogic;
715 # signal use_forward1_next : std_ulogic;
716 # signal use_forward2_next : std_ulogic;
718 # -- Cache RAM interface
719 # type cache_ram_out_t is array(way_t) of cache_row_t;
720 # signal cache_out : cache_ram_out_t;
722 # -- PLRU output interface
723 # type plru_out_t is array(index_t) of
724 # std_ulogic_vector(WAY_BITS-1 downto 0);
725 # signal plru_victim : plru_out_t;
726 # signal replace_way : way_t;
728 # -- Wishbone read/write/cache write formatting signals
729 # signal bus_sel : std_ulogic_vector(7 downto 0);
732 # signal tlb_tag_way : tlb_way_tags_t;
733 # signal tlb_pte_way : tlb_way_ptes_t;
734 # signal tlb_valid_way : tlb_way_valids_t;
735 # signal tlb_req_index : tlb_index_t;
736 # signal tlb_hit : std_ulogic;
737 # signal tlb_hit_way : tlb_way_t;
738 # signal pte : tlb_pte_t;
739 # signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
740 # signal valid_ra : std_ulogic;
741 # signal perm_attr : perm_attr_t;
742 # signal rc_ok : std_ulogic;
743 # signal perm_ok : std_ulogic;
744 # signal access_ok : std_ulogic;
746 # -- TLB PLRU output interface
747 # type tlb_plru_out_t is array(tlb_index_t) of
748 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
749 # signal tlb_plru_victim : tlb_plru_out_t;
752 # -- Helper functions to decode incoming requests
755 # -- Return the cache line index (tag index) for an address
756 # function get_index(addr: std_ulogic_vector) return index_t is
759 # unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS))
763 # -- Return the cache row index (data memory) for an address
764 # function get_row(addr: std_ulogic_vector) return row_t is
767 # unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS))
771 # -- Return the index of a row within a line
772 # function get_row_of_line(row: row_t) return row_in_line_t is
773 # variable row_v : unsigned(ROW_BITS-1 downto 0);
775 # row_v := to_unsigned(row, ROW_BITS);
776 # return row_v(ROW_LINEBITS-1 downto 0);
779 # -- Returns whether this is the last row of a line
780 # function is_last_row_addr(addr: wishbone_addr_type;
781 # last: row_in_line_t) return boolean is
784 # unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
787 # -- Returns whether this is the last row of a line
788 # function is_last_row(row: row_t; last: row_in_line_t)
791 # return get_row_of_line(row) = last;
794 # -- Return the address of the next row in the current cache line
795 # function next_row_addr(addr: wishbone_addr_type)
796 # return std_ulogic_vector is
797 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
798 # variable result : wishbone_addr_type;
800 # -- Is there no simpler way in VHDL to
801 # -- generate that 3 bits adder ?
802 # row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
803 # row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
805 # result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
809 # -- Return the next row in the current cache line. We use a
810 # -- dedicated function in order to limit the size of the
811 # -- generated adder to be only the bits within a cache line
812 # -- (3 bits with default settings)
813 # function next_row(row: row_t) return row_t is
814 # variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
815 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
816 # variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
818 # row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
819 # row_idx := row_v(ROW_LINEBITS-1 downto 0);
820 # row_v(ROW_LINEBITS-1 downto 0) :=
821 # std_ulogic_vector(unsigned(row_idx) + 1);
822 # return to_integer(unsigned(row_v));
825 # -- Get the tag value from the address
826 # function get_tag(addr: std_ulogic_vector) return cache_tag_t is
828 # return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
831 # -- Read a tag from a tag memory row
832 # function read_tag(way: way_t; tagset: cache_tags_set_t)
833 # return cache_tag_t is
835 # return tagset(way * TAG_WIDTH + TAG_BITS
836 # - 1 downto way * TAG_WIDTH);
839 # -- Read a TLB tag from a TLB tag memory row
840 # function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t)
841 # return tlb_tag_t is
842 # variable j : integer;
844 # j := way * TLB_EA_TAG_BITS;
845 # return tags(j + TLB_EA_TAG_BITS - 1 downto j);
848 # -- Write a TLB tag to a TLB tag memory row
849 # procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
851 # variable j : integer;
853 # j := way * TLB_EA_TAG_BITS;
854 # tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
857 # -- Read a PTE from a TLB PTE memory row
858 # function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t)
859 # return tlb_pte_t is
860 # variable j : integer;
862 # j := way * TLB_PTE_BITS;
863 # return ptes(j + TLB_PTE_BITS - 1 downto j);
866 # procedure write_tlb_pte(way: tlb_way_t;
867 # ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
868 # variable j : integer;
870 # j := way * TLB_PTE_BITS;
871 # ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
876 # assert LINE_SIZE mod ROW_SIZE = 0
877 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
878 # assert ispow2(LINE_SIZE)
879 # report "LINE_SIZE not power of 2" severity FAILURE;
880 # assert ispow2(NUM_LINES)
881 # report "NUM_LINES not power of 2" severity FAILURE;
882 # assert ispow2(ROW_PER_LINE)
883 # report "ROW_PER_LINE not power of 2" severity FAILURE;
884 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
885 # report "geometry bits don't add up" severity FAILURE;
886 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
887 # report "geometry bits don't add up" severity FAILURE;
888 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
889 # report "geometry bits don't add up" severity FAILURE;
890 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
891 # report "geometry bits don't add up" severity FAILURE;
892 # assert (64 = wishbone_data_bits)
893 # report "Can't yet handle a wishbone width that isn't 64-bits"
895 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
896 # report "Set indexed by virtual address" severity FAILURE;
898 # -- Latch the request in r0.req as long as we're not stalling
899 # stage_0 : process(clk)
900 # variable r : reg_stage_0_t;
902 # if rising_edge(clk) then
903 # assert (d_in.valid and m_in.valid) = '0'
904 # report "request collision loadstore vs MMU";
905 # if m_in.valid = '1' then
906 # r.req.valid := '1';
907 # r.req.load := not (m_in.tlbie or m_in.tlbld);
910 # r.req.reserve := '0';
911 # r.req.virt_mode := '0';
912 # r.req.priv_mode := '1';
913 # r.req.addr := m_in.addr;
914 # r.req.data := m_in.pte;
915 # r.req.byte_sel := (others => '1');
916 # r.tlbie := m_in.tlbie;
917 # r.doall := m_in.doall;
918 # r.tlbld := m_in.tlbld;
929 # elsif r1.full = '0' or r0_full = '0' then
931 # r0_full <= r.req.valid;
936 # -- we don't yet handle collisions between loadstore1 requests
937 # -- and MMU requests
938 # m_out.stall <= '0';
940 # -- Hold off the request in r0 when r1 has an uncompleted request
941 # r0_stall <= r0_full and r1.full;
942 # r0_valid <= r0_full and not r1.full;
943 # stall_out <= r0_stall;
946 # -- Operates in the second cycle on the request latched in r0.req.
947 # -- TLB updates write the entry at the end of the second cycle.
948 # tlb_read : process(clk)
949 # variable index : tlb_index_t;
950 # variable addrbits :
951 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
953 # if rising_edge(clk) then
954 # if m_in.valid = '1' then
955 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
956 # - 1 downto TLB_LG_PGSZ);
958 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
959 # - 1 downto TLB_LG_PGSZ);
961 # index := to_integer(unsigned(addrbits));
962 # -- If we have any op and the previous op isn't finished,
963 # -- then keep the same output for next cycle.
964 # if r0_stall = '0' then
965 # tlb_valid_way <= dtlb_valids(index);
966 # tlb_tag_way <= dtlb_tags(index);
967 # tlb_pte_way <= dtlb_ptes(index);
972 # -- Generate TLB PLRUs
973 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
975 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
976 # -- TLB PLRU interface
977 # signal tlb_plru_acc :
978 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
979 # signal tlb_plru_acc_en : std_ulogic;
980 # signal tlb_plru_out :
981 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
983 # tlb_plru : entity work.plru
985 # BITS => TLB_WAY_BITS
990 # acc => tlb_plru_acc,
991 # acc_en => tlb_plru_acc_en,
992 # lru => tlb_plru_out
998 # if r1.tlb_hit_index = i then
999 # tlb_plru_acc_en <= r1.tlb_hit;
1001 # tlb_plru_acc_en <= '0';
1004 # std_ulogic_vector(to_unsigned(
1005 # r1.tlb_hit_way, TLB_WAY_BITS
1007 # tlb_plru_victim(i) <= tlb_plru_out;
1012 # tlb_search : process(all)
1013 # variable hitway : tlb_way_t;
1014 # variable hit : std_ulogic;
1015 # variable eatag : tlb_tag_t;
1018 # to_integer(unsigned(r0.req.addr(
1019 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
1023 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
1024 # for i in tlb_way_t loop
1025 # if tlb_valid_way(i) = '1' and
1026 # read_tlb_tag(i, tlb_tag_way) = eatag then
1031 # tlb_hit <= hit and r0_valid;
1032 # tlb_hit_way <= hitway;
1033 # if tlb_hit = '1' then
1034 # pte <= read_tlb_pte(hitway, tlb_pte_way);
1036 # pte <= (others => '0');
1038 # valid_ra <= tlb_hit or not r0.req.virt_mode;
1039 # if r0.req.virt_mode = '1' then
1040 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
1041 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
1042 # (ROW_OFF_BITS-1 downto 0 => '0');
1043 # perm_attr <= extract_perm_attr(pte);
1045 # ra <= r0.req.addr(
1046 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
1047 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
1048 # perm_attr <= real_mode_perm_attr;
1052 # tlb_update : process(clk)
1053 # variable tlbie : std_ulogic;
1054 # variable tlbwe : std_ulogic;
1055 # variable repl_way : tlb_way_t;
1056 # variable eatag : tlb_tag_t;
1057 # variable tagset : tlb_way_tags_t;
1058 # variable pteset : tlb_way_ptes_t;
1060 # if rising_edge(clk) then
1061 # tlbie := r0_valid and r0.tlbie;
1062 # tlbwe := r0_valid and r0.tlbld;
1063 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
1064 # -- clear all valid bits at once
1065 # for i in tlb_index_t loop
1066 # dtlb_valids(i) <= (others => '0');
1068 # elsif tlbie = '1' then
1069 # if tlb_hit = '1' then
1070 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
1072 # elsif tlbwe = '1' then
1073 # if tlb_hit = '1' then
1074 # repl_way := tlb_hit_way;
1076 # repl_way := to_integer(unsigned(
1077 # tlb_plru_victim(tlb_req_index)));
1079 # eatag := r0.req.addr(
1080 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
1082 # tagset := tlb_tag_way;
1083 # write_tlb_tag(repl_way, tagset, eatag);
1084 # dtlb_tags(tlb_req_index) <= tagset;
1085 # pteset := tlb_pte_way;
1086 # write_tlb_pte(repl_way, pteset, r0.req.data);
1087 # dtlb_ptes(tlb_req_index) <= pteset;
1088 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
1094 # maybe_plrus: if NUM_WAYS > 1 generate
1096 # plrus: for i in 0 to NUM_LINES-1 generate
1098 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
1099 # signal plru_acc_en : std_ulogic;
1100 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
1103 # plru : entity work.plru
1111 # acc_en => plru_acc_en,
1118 # if r1.hit_index = i then
1119 # plru_acc_en <= r1.cache_hit;
1121 # plru_acc_en <= '0';
1123 # plru_acc <= std_ulogic_vector(to_unsigned(
1124 # r1.hit_way, WAY_BITS
1126 # plru_victim(i) <= plru_out;
1131 # -- Cache tag RAM read port
1132 # cache_tag_read : process(clk)
1133 # variable index : index_t;
1135 # if rising_edge(clk) then
1136 # if r0_stall = '1' then
1137 # index := req_index;
1138 # elsif m_in.valid = '1' then
1139 # index := get_index(m_in.addr);
1141 # index := get_index(d_in.addr);
1143 # cache_tag_set <= cache_tags(index);
1147 # -- Cache request parsing and hit detection
1148 # dcache_request : process(all)
1149 # variable is_hit : std_ulogic;
1150 # variable hit_way : way_t;
1151 # variable op : op_t;
1152 # variable opsel : std_ulogic_vector(2 downto 0);
1153 # variable go : std_ulogic;
1154 # variable nc : std_ulogic;
1155 # variable s_hit : std_ulogic;
1156 # variable s_tag : cache_tag_t;
1157 # variable s_pte : tlb_pte_t;
1158 # variable s_ra : std_ulogic_vector(
1159 # REAL_ADDR_BITS - 1 downto 0
1161 # variable hit_set : std_ulogic_vector(
1162 # TLB_NUM_WAYS - 1 downto 0
1164 # variable hit_way_set : hit_way_set_t;
1165 # variable rel_matches : std_ulogic_vector(
1166 # TLB_NUM_WAYS - 1 downto 0
1168 # variable rel_match : std_ulogic;
1170 # -- Extract line, row and tag from request
1171 # req_index <= get_index(r0.req.addr);
1172 # req_row <= get_row(r0.req.addr);
1173 # req_tag <= get_tag(ra);
1175 # go := r0_valid and not (r0.tlbie or r0.tlbld)
1176 # and not r1.ls_error;
1178 # -- Test if pending request is a hit on any way
1179 # -- In order to make timing in virtual mode,
1180 # -- when we are using the TLB, we compare each
1181 # --way with each of the real addresses from each way of
1182 # -- the TLB, and then decide later which match to use.
1186 # if r0.req.virt_mode = '1' then
1187 # rel_matches := (others => '0');
1188 # for j in tlb_way_t loop
1189 # hit_way_set(j) := 0;
1191 # s_pte := read_tlb_pte(j, tlb_pte_way);
1193 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
1194 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
1195 # s_tag := get_tag(s_ra);
1196 # for i in way_t loop
1197 # if go = '1' and cache_valids(req_index)(i) = '1'
1198 # and read_tag(i, cache_tag_set) = s_tag
1199 # and tlb_valid_way(j) = '1' then
1200 # hit_way_set(j) := i;
1204 # hit_set(j) := s_hit;
1205 # if s_tag = r1.reload_tag then
1206 # rel_matches(j) := '1';
1209 # if tlb_hit = '1' then
1210 # is_hit := hit_set(tlb_hit_way);
1211 # hit_way := hit_way_set(tlb_hit_way);
1212 # rel_match := rel_matches(tlb_hit_way);
1215 # s_tag := get_tag(r0.req.addr);
1216 # for i in way_t loop
1217 # if go = '1' and cache_valids(req_index)(i) = '1' and
1218 # read_tag(i, cache_tag_set) = s_tag then
1223 # if s_tag = r1.reload_tag then
1227 # req_same_tag <= rel_match;
1229 # -- See if the request matches the line currently being reloaded
1230 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
1231 # and rel_match = '1' then
1232 # -- For a store, consider this a hit even if the row isn't
1233 # -- valid since it will be by the time we perform the store.
1234 # -- For a load, check the appropriate row valid bit.
1236 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
1237 # hit_way := replace_way;
1240 # -- Whether to use forwarded data for a load or not
1241 # use_forward1_next <= '0';
1242 # if get_row(r1.req.real_addr) = req_row
1243 # and r1.req.hit_way = hit_way then
1244 # -- Only need to consider r1.write_bram here, since if we
1245 # -- are writing refill data here, then we don't have a
1246 # -- cache hit this cycle on the line being refilled.
1247 # -- (There is the possibility that the load following the
1248 # -- load miss that started the refill could be to the old
1249 # -- contents of the victim line, since it is a couple of
1250 # -- cycles after the refill starts before we see the updated
1251 # -- cache tag. In that case we don't use the bypass.)
1252 # use_forward1_next <= r1.write_bram;
1254 # use_forward2_next <= '0';
1255 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
1256 # use_forward2_next <= r1.forward_valid1;
1259 # -- The way that matched on a hit
1260 # req_hit_way <= hit_way;
1262 # -- The way to replace on a miss
1263 # if r1.write_tag = '1' then
1264 # replace_way <= to_integer(unsigned(
1265 # plru_victim(r1.store_index)
1268 # replace_way <= r1.store_way;
1271 # -- work out whether we have permission for this access
1272 # -- NB we don't yet implement AMR, thus no KUAP
1273 # rc_ok <= perm_attr.reference and
1274 # (r0.req.load or perm_attr.changed);
1275 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
1276 # (perm_attr.wr_perm or (r0.req.load
1277 # and perm_attr.rd_perm));
1278 # access_ok <= valid_ra and perm_ok and rc_ok;
1280 # -- Combine the request and cache hit status to decide what
1281 # -- operation needs to be done
1283 # nc := r0.req.nc or perm_attr.nocache;
1286 # if access_ok = '0' then
1288 # elsif cancel_store = '1' then
1289 # op := OP_STCX_FAIL;
1291 # opsel := r0.req.load & nc & is_hit;
1293 # when "101" => op := OP_LOAD_HIT;
1294 # when "100" => op := OP_LOAD_MISS;
1295 # when "110" => op := OP_LOAD_NC;
1296 # when "001" => op := OP_STORE_HIT;
1297 # when "000" => op := OP_STORE_MISS;
1298 # when "010" => op := OP_STORE_MISS;
1299 # when "011" => op := OP_BAD;
1300 # when "111" => op := OP_BAD;
1301 # when others => op := OP_NONE;
1308 # -- Version of the row number that is valid one cycle earlier
1309 # -- in the cases where we need to read the cache data BRAM.
1310 # -- If we're stalling then we need to keep reading the last
1312 # if r0_stall = '0' then
1313 # if m_in.valid = '1' then
1314 # early_req_row <= get_row(m_in.addr);
1316 # early_req_row <= get_row(d_in.addr);
1319 # early_req_row <= req_row;
1323 # -- Wire up wishbone request latch out of stage 1
1324 # wishbone_out <= r1.wb;
1326 # -- Handle load-with-reservation and store-conditional instructions
1327 # reservation_comb: process(all)
1329 # cancel_store <= '0';
1331 # clear_rsrv <= '0';
1332 # if r0_valid = '1' and r0.req.reserve = '1' then
1333 # -- XXX generate alignment interrupt if address
1334 # -- is not aligned XXX or if r0.req.nc = '1'
1335 # if r0.req.load = '1' then
1336 # -- load with reservation
1339 # -- store conditional
1340 # clear_rsrv <= '1';
1341 # if reservation.valid = '0' or r0.req.addr(63
1342 # downto LINE_OFF_BITS) /= reservation.addr then
1343 # cancel_store <= '1';
1349 # reservation_reg: process(clk)
1351 # if rising_edge(clk) then
1353 # reservation.valid <= '0';
1354 # elsif r0_valid = '1' and access_ok = '1' then
1355 # if clear_rsrv = '1' then
1356 # reservation.valid <= '0';
1357 # elsif set_rsrv = '1' then
1358 # reservation.valid <= '1';
1359 # reservation.addr <=
1360 # r0.req.addr(63 downto LINE_OFF_BITS);
1366 # -- Return data for loads & completion control logic
1368 # writeback_control: process(all)
1369 # variable data_out : std_ulogic_vector(63 downto 0);
1370 # variable data_fwd : std_ulogic_vector(63 downto 0);
1371 # variable j : integer;
1373 # -- Use the bypass if are reading the row that was
1374 # -- written 1 or 2 cycles ago, including for the
1375 # -- slow_valid = 1 case (i.e. completing a load
1376 # -- miss or a non-cacheable load).
1377 # if r1.use_forward1 = '1' then
1378 # data_fwd := r1.forward_data1;
1380 # data_fwd := r1.forward_data2;
1382 # data_out := cache_out(r1.hit_way);
1383 # for i in 0 to 7 loop
1385 # if r1.forward_sel(i) = '1' then
1386 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1390 # d_out.valid <= r1.ls_valid;
1391 # d_out.data <= data_out;
1392 # d_out.store_done <= not r1.stcx_fail;
1393 # d_out.error <= r1.ls_error;
1394 # d_out.cache_paradox <= r1.cache_paradox;
1397 # m_out.done <= r1.mmu_done;
1398 # m_out.err <= r1.mmu_error;
1399 # m_out.data <= data_out;
1401 # -- We have a valid load or store hit or we just completed
1402 # -- a slow op such as a load miss, a NC load or a store
1404 # -- Note: the load hit is delayed by one cycle. However it
1405 # -- can still not collide with r.slow_valid (well unless I
1406 # -- miscalculated) because slow_valid can only be set on a
1407 # -- subsequent request and not on its first cycle (the state
1408 # -- machine must have advanced), which makes slow_valid
1409 # -- at least 2 cycles from the previous hit_load_valid.
1411 # -- Sanity: Only one of these must be set in any given cycle
1412 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1413 # report "unexpected slow_valid collision with stcx_fail"
1415 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1416 # /= '1' report "unexpected hit_load_delayed collision with
1417 # slow_valid" severity FAILURE;
1419 # if r1.mmu_req = '0' then
1420 # -- Request came from loadstore1...
1421 # -- Load hit case is the standard path
1422 # if r1.hit_load_valid = '1' then
1424 # "completing load hit data=" & to_hstring(data_out);
1427 # -- error cases complete without stalling
1428 # if r1.ls_error = '1' then
1429 # report "completing ld/st with error";
1432 # -- Slow ops (load miss, NC, stores)
1433 # if r1.slow_valid = '1' then
1435 # "completing store or load miss data="
1436 # & to_hstring(data_out);
1440 # -- Request came from MMU
1441 # if r1.hit_load_valid = '1' then
1442 # report "completing load hit to MMU, data="
1443 # & to_hstring(m_out.data);
1446 # -- error cases complete without stalling
1447 # if r1.mmu_error = '1' then
1448 # report "completing MMU ld with error";
1451 # -- Slow ops (i.e. load miss)
1452 # if r1.slow_valid = '1' then
1453 # report "completing MMU load miss, data="
1454 # & to_hstring(m_out.data);
1461 # -- Generate a cache RAM for each way. This handles the normal
1462 # -- reads, writes from reloads and the special store-hit update
1465 # -- Note: the BRAMs have an extra read buffer, meaning the output
1466 # -- is pipelined an extra cycle. This differs from the
1467 # -- icache. The writeback logic needs to take that into
1468 # -- account by using 1-cycle delayed signals for load hits.
1470 # rams: for i in 0 to NUM_WAYS-1 generate
1471 # signal do_read : std_ulogic;
1472 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1473 # signal do_write : std_ulogic;
1474 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1476 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1477 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1478 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1479 # signal dout : cache_row_t;
1481 # way: entity work.cache_ram
1483 # ROW_BITS => ROW_BITS,
1484 # WIDTH => wishbone_data_bits,
1490 # rd_addr => rd_addr,
1492 # wr_sel => wr_sel_m,
1493 # wr_addr => wr_addr,
1494 # wr_data => wr_data
1498 # -- Cache hit reads
1501 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1502 # cache_out(i) <= dout;
1506 # -- Defaults to wishbone read responses (cache refill)
1508 # -- For timing, the mux on wr_data/sel/addr is not
1509 # -- dependent on anything other than the current state.
1510 # wr_sel_m <= (others => '0');
1513 # if r1.write_bram = '1' then
1514 # -- Write store data to BRAM. This happens one
1515 # -- cycle after the store is in r0.
1516 # wr_data <= r1.req.data;
1517 # wr_sel <= r1.req.byte_sel;
1518 # wr_addr <= std_ulogic_vector(to_unsigned(
1519 # get_row(r1.req.real_addr), ROW_BITS
1521 # if i = r1.req.hit_way then
1525 # -- Otherwise, we might be doing a reload or a DCBZ
1526 # if r1.dcbz = '1' then
1527 # wr_data <= (others => '0');
1529 # wr_data <= wishbone_in.dat;
1531 # wr_addr <= std_ulogic_vector(to_unsigned(
1532 # r1.store_row, ROW_BITS
1534 # wr_sel <= (others => '1');
1536 # if r1.state = RELOAD_WAIT_ACK and
1537 # wishbone_in.ack = '1' and replace_way = i then
1542 # -- Mask write selects with do_write since BRAM
1543 # -- doesn't have a global write-enable
1544 # if do_write = '1' then
1545 # wr_sel_m <= wr_sel;
1551 # -- Cache hit synchronous machine for the easy case.
1552 # -- This handles load hits.
1553 # -- It also handles error cases (TLB miss, cache paradox)
1554 # dcache_fast_hit : process(clk)
1556 # if rising_edge(clk) then
1557 # if req_op /= OP_NONE then
1558 # report "op:" & op_t'image(req_op) &
1559 # " addr:" & to_hstring(r0.req.addr) &
1560 # " nc:" & std_ulogic'image(r0.req.nc) &
1561 # " idx:" & integer'image(req_index) &
1562 # " tag:" & to_hstring(req_tag) &
1563 # " way: " & integer'image(req_hit_way);
1565 # if r0_valid = '1' then
1566 # r1.mmu_req <= r0.mmu_req;
1569 # -- Fast path for load/store hits.
1570 # -- Set signals for the writeback controls.
1571 # r1.hit_way <= req_hit_way;
1572 # r1.hit_index <= req_index;
1573 # if req_op = OP_LOAD_HIT then
1574 # r1.hit_load_valid <= '1';
1576 # r1.hit_load_valid <= '0';
1578 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1579 # r1.cache_hit <= '1';
1581 # r1.cache_hit <= '0';
1584 # if req_op = OP_BAD then
1585 # report "Signalling ld/st error valid_ra=" &
1586 # std_ulogic'image(valid_ra) & " rc_ok=" &
1587 # std_ulogic'image(rc_ok) & " perm_ok=" &
1588 # std_ulogic'image(perm_ok);
1589 # r1.ls_error <= not r0.mmu_req;
1590 # r1.mmu_error <= r0.mmu_req;
1591 # r1.cache_paradox <= access_ok;
1593 # r1.ls_error <= '0';
1594 # r1.mmu_error <= '0';
1595 # r1.cache_paradox <= '0';
1598 # if req_op = OP_STCX_FAIL then
1599 # r1.stcx_fail <= '1';
1601 # r1.stcx_fail <= '0';
1604 # -- Record TLB hit information for updating TLB PLRU
1605 # r1.tlb_hit <= tlb_hit;
1606 # r1.tlb_hit_way <= tlb_hit_way;
1607 # r1.tlb_hit_index <= tlb_req_index;
1612 # -- Memory accesses are handled by this state machine:
1614 # -- * Cache load miss/reload (in conjunction with "rams")
1615 # -- * Load hits for non-cachable forms
1616 # -- * Stores (the collision case is handled in "rams")
1618 # -- All wishbone requests generation is done here.
1619 # -- This machine operates at stage 1.
1620 # dcache_slow : process(clk)
1621 # variable stbs_done : boolean;
1622 # variable req : mem_access_request_t;
1623 # variable acks : unsigned(2 downto 0);
1625 # if rising_edge(clk) then
1626 # r1.use_forward1 <= use_forward1_next;
1627 # r1.forward_sel <= (others => '0');
1628 # if use_forward1_next = '1' then
1629 # r1.forward_sel <= r1.req.byte_sel;
1630 # elsif use_forward2_next = '1' then
1631 # r1.forward_sel <= r1.forward_sel1;
1634 # r1.forward_data2 <= r1.forward_data1;
1635 # if r1.write_bram = '1' then
1636 # r1.forward_data1 <= r1.req.data;
1637 # r1.forward_sel1 <= r1.req.byte_sel;
1638 # r1.forward_way1 <= r1.req.hit_way;
1639 # r1.forward_row1 <= get_row(r1.req.real_addr);
1640 # r1.forward_valid1 <= '1';
1642 # if r1.dcbz = '1' then
1643 # r1.forward_data1 <= (others => '0');
1645 # r1.forward_data1 <= wishbone_in.dat;
1647 # r1.forward_sel1 <= (others => '1');
1648 # r1.forward_way1 <= replace_way;
1649 # r1.forward_row1 <= r1.store_row;
1650 # r1.forward_valid1 <= '0';
1653 # -- On reset, clear all valid bits to force misses
1655 # for i in index_t loop
1656 # cache_valids(i) <= (others => '0');
1660 # r1.slow_valid <= '0';
1663 # r1.ls_valid <= '0';
1664 # r1.mmu_done <= '0';
1666 # -- Not useful normally but helps avoiding
1667 # -- tons of sim warnings
1668 # r1.wb.adr <= (others => '0');
1670 # -- One cycle pulses reset
1671 # r1.slow_valid <= '0';
1672 # r1.write_bram <= '0';
1673 # r1.inc_acks <= '0';
1674 # r1.dec_acks <= '0';
1676 # r1.ls_valid <= '0';
1677 # -- complete tlbies and TLB loads in the third cycle
1678 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
1679 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
1680 # if r0.mmu_req = '0' then
1681 # r1.ls_valid <= '1';
1683 # r1.mmu_done <= '1';
1687 # if r1.write_tag = '1' then
1688 # -- Store new tag in selected way
1689 # for i in 0 to NUM_WAYS-1 loop
1690 # if i = replace_way then
1691 # cache_tags(r1.store_index)(
1692 # (i + 1) * TAG_WIDTH - 1
1693 # downto i * TAG_WIDTH
1695 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
1699 # r1.store_way <= replace_way;
1700 # r1.write_tag <= '0';
1703 # -- Take request from r1.req if there is one there,
1704 # -- else from req_op, ra, etc.
1705 # if r1.full = '1' then
1709 # req.valid := req_go;
1710 # req.mmu_req := r0.mmu_req;
1711 # req.dcbz := r0.req.dcbz;
1712 # req.real_addr := ra;
1713 # -- Force data to 0 for dcbz
1714 # if r0.req.dcbz = '0' then
1715 # req.data := r0.req.data;
1717 # req.data := (others => '0');
1719 # -- Select all bytes for dcbz
1720 # -- and for cacheable loads
1721 # if r0.req.dcbz = '1'
1722 # or (r0.req.load = '1' and r0.req.nc = '0') then
1723 # req.byte_sel := (others => '1');
1725 # req.byte_sel := r0.req.byte_sel;
1727 # req.hit_way := req_hit_way;
1728 # req.same_tag := req_same_tag;
1730 # -- Store the incoming request from r0,
1731 # -- if it is a slow request
1732 # -- Note that r1.full = 1 implies req_op = OP_NONE
1733 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
1734 # or req_op = OP_STORE_MISS
1735 # or req_op = OP_STORE_HIT then
1741 # -- Main state machine
1744 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1745 # r1.wb.sel <= req.byte_sel;
1746 # r1.wb.dat <= req.data;
1747 # r1.dcbz <= req.dcbz;
1749 # -- Keep track of our index and way
1750 # -- for subsequent stores.
1751 # r1.store_index <= get_index(req.real_addr);
1752 # r1.store_row <= get_row(req.real_addr);
1754 # get_row_of_line(get_row(req.real_addr)) - 1;
1755 # r1.reload_tag <= get_tag(req.real_addr);
1756 # r1.req.same_tag <= '1';
1758 # if req.op = OP_STORE_HIT then
1759 # r1.store_way <= req.hit_way;
1762 # -- Reset per-row valid bits,
1763 # -- ready for handling OP_LOAD_MISS
1764 # for i in 0 to ROW_PER_LINE - 1 loop
1765 # r1.rows_valid(i) <= '0';
1769 # when OP_LOAD_HIT =>
1770 # -- stay in IDLE state
1772 # when OP_LOAD_MISS =>
1773 # -- Normal load cache miss,
1774 # -- start the reload machine
1775 # report "cache miss real addr:" &
1776 # to_hstring(req.real_addr) & " idx:" &
1777 # integer'image(get_index(req.real_addr)) &
1778 # " tag:" & to_hstring(get_tag(req.real_addr));
1780 # -- Start the wishbone cycle
1785 # -- Track that we had one request sent
1786 # r1.state <= RELOAD_WAIT_ACK;
1787 # r1.write_tag <= '1';
1789 # when OP_LOAD_NC =>
1793 # r1.state <= NC_LOAD_WAIT_ACK;
1795 # when OP_STORE_HIT | OP_STORE_MISS =>
1796 # if req.dcbz = '0' then
1797 # r1.state <= STORE_WAIT_ACK;
1798 # r1.acks_pending <= to_unsigned(1, 3);
1800 # r1.slow_valid <= '1';
1801 # if req.mmu_req = '0' then
1802 # r1.ls_valid <= '1';
1804 # r1.mmu_done <= '1';
1806 # if req.op = OP_STORE_HIT then
1807 # r1.write_bram <= '1';
1810 # -- dcbz is handled much like a load
1811 # -- miss except that we are writing
1812 # -- to memory instead of reading
1813 # r1.state <= RELOAD_WAIT_ACK;
1814 # if req.op = OP_STORE_MISS then
1815 # r1.write_tag <= '1';
1822 # -- OP_NONE and OP_BAD do nothing
1823 # -- OP_BAD & OP_STCX_FAIL were handled above already
1826 # when OP_STCX_FAIL =>
1829 # when RELOAD_WAIT_ACK =>
1830 # -- Requests are all sent if stb is 0
1831 # stbs_done := r1.wb.stb = '0';
1833 # -- If we are still sending requests,
1834 # -- was one accepted?
1835 # if wishbone_in.stall = '0' and not stbs_done then
1836 # -- That was the last word ? We are done sending.
1837 # -- Clear stb and set stbs_done so we can handle
1838 # -- an eventual last ack on the same cycle.
1839 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
1841 # stbs_done := true;
1844 # -- Calculate the next row address
1845 # r1.wb.adr <= next_row_addr(r1.wb.adr);
1848 # -- Incoming acks processing
1849 # r1.forward_valid1 <= wishbone_in.ack;
1850 # if wishbone_in.ack = '1' then
1852 # r1.store_row mod ROW_PER_LINE
1854 # -- If this is the data we were looking for,
1855 # -- we can complete the request next cycle.
1856 # -- Compare the whole address in case the
1857 # -- request in r1.req is not the one that
1858 # -- started this refill.
1859 # if r1.full = '1' and r1.req.same_tag = '1'
1860 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
1861 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
1862 # and r1.store_row = get_row(r1.req.real_addr) then
1864 # r1.slow_valid <= '1';
1865 # if r1.mmu_req = '0' then
1866 # r1.ls_valid <= '1';
1868 # r1.mmu_done <= '1';
1870 # r1.forward_sel <= (others => '1');
1871 # r1.use_forward1 <= '1';
1874 # -- Check for completion
1875 # if stbs_done and is_last_row(r1.store_row,
1876 # r1.end_row_ix) then
1877 # -- Complete wishbone cycle
1880 # -- Cache line is now valid
1881 # cache_valids(r1.store_index)(
1888 # -- Increment store row counter
1889 # r1.store_row <= next_row(r1.store_row);
1892 # when STORE_WAIT_ACK =>
1893 # stbs_done := r1.wb.stb = '0';
1894 # acks := r1.acks_pending;
1895 # if r1.inc_acks /= r1.dec_acks then
1896 # if r1.inc_acks = '1' then
1902 # r1.acks_pending <= acks;
1903 # -- Clear stb when slave accepted request
1904 # if wishbone_in.stall = '0' then
1905 # -- See if there is another store waiting
1906 # -- to be done which is in the same real page.
1907 # if req.valid = '1' then
1909 # SET_SIZE_BITS - 1 downto 0
1910 # ) <= req.real_addr(
1911 # SET_SIZE_BITS - 1 downto 0
1913 # r1.wb.dat <= req.data;
1914 # r1.wb.sel <= req.byte_sel;
1916 # if acks < 7 and req.same_tag = '1'
1917 # and (req.op = OP_STORE_MISS
1918 # or req.op = OP_STORE_HIT) then
1920 # stbs_done := false;
1921 # if req.op = OP_STORE_HIT then
1922 # r1.write_bram <= '1';
1925 # r1.slow_valid <= '1';
1926 # -- Store requests never come from the MMU
1927 # r1.ls_valid <= '1';
1928 # stbs_done := false;
1929 # r1.inc_acks <= '1';
1932 # stbs_done := true;
1936 # -- Got ack ? See if complete.
1937 # if wishbone_in.ack = '1' then
1938 # if stbs_done and acks = 1 then
1943 # r1.dec_acks <= '1';
1946 # when NC_LOAD_WAIT_ACK =>
1947 # -- Clear stb when slave accepted request
1948 # if wishbone_in.stall = '0' then
1952 # -- Got ack ? complete.
1953 # if wishbone_in.ack = '1' then
1956 # r1.slow_valid <= '1';
1957 # if r1.mmu_req = '0' then
1958 # r1.ls_valid <= '1';
1960 # r1.mmu_done <= '1';
1962 # r1.forward_sel <= (others => '1');
1963 # r1.use_forward1 <= '1';
1972 # dc_log: if LOG_LENGTH > 0 generate
1973 # signal log_data : std_ulogic_vector(19 downto 0);
1975 # dcache_log: process(clk)
1977 # if rising_edge(clk) then
1978 # log_data <= r1.wb.adr(5 downto 3) &
1979 # wishbone_in.stall &
1981 # r1.wb.stb & r1.wb.cyc &
1984 # std_ulogic_vector(
1985 # to_unsigned(op_t'pos(req_op), 3)) &
1987 # std_ulogic_vector(
1988 # to_unsigned(tlb_hit_way, 3)) &
1990 # std_ulogic_vector(
1991 # to_unsigned(state_t'pos(r1.state), 3));
1994 # log_out <= log_data;