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;
386 """note: these are passed to nmigen.hdl.Memory as "attributes". don't
387 know how, just that they are.
389 # attribute ram_style of dtlb_tags : signal is "distributed";
390 # attribute ram_style of dtlb_ptes : signal is "distributed";
391 dtlb_valids
= tlb_valids_t
;
392 dtlb_tags
= tlb_tags_t
;
393 dtlb_ptes
= tlb_ptes_t
;
394 # TODO attribute ram_style of dtlb_tags : signal is "distributed";
395 # TODO attribute ram_style of dtlb_ptes : signal is "distributed";
398 # -- Record for storing permission, attribute, etc. bits from a PTE
399 # type perm_attr_t is record
400 # reference : std_ulogic;
401 # changed : std_ulogic;
402 # nocache : std_ulogic;
404 # rd_perm : std_ulogic;
405 # wr_perm : std_ulogic;
407 # Record for storing permission, attribute, etc. bits from a PTE
408 class PermAttr(RecordObject
):
411 self
.reference
= Signal()
412 self
.changed
= Signal()
413 self
.nocache
= Signal()
415 self
.rd_perm
= Signal()
416 self
.wr_perm
= Signal()
418 # function extract_perm_attr(
419 # pte : std_ulogic_vector(TLB_PTE_BITS - 1 downto 0))
420 # return perm_attr_t is
421 # variable pa : perm_attr_t;
423 # pa.reference := pte(8);
424 # pa.changed := pte(7);
425 # pa.nocache := pte(5);
427 # pa.rd_perm := pte(2);
428 # pa.wr_perm := pte(1);
431 def extract_perm_attr(pte
=Signal(TLB_PTE_BITS
)):
433 pa
.reference
= pte
[8]
441 # constant real_mode_perm_attr : perm_attr_t :=
442 # (nocache => '0', others => '1');
443 REAL_MODE_PERM_ATTR
= PermAttr()
444 REAL_MODE_PERM_ATTR
.reference
= 1
445 REAL_MODE_PERM_ATTR
.changed
= 1
446 REAL_MODE_PERM_ATTR
.priv
= 1
447 REAL_MODE_PERM_ATTR
.rd_perm
= 1
448 REAL_MODE_PERM_ATTR
.wr_perm
= 1
450 # -- Type of operation on a "valid" input
454 # OP_BAD, -- NC cache hit, TLB miss, prot/RC failure
455 # OP_STCX_FAIL, -- conditional store w/o reservation
456 # OP_LOAD_HIT, -- Cache hit on load
457 # OP_LOAD_MISS, -- Load missing cache
458 # OP_LOAD_NC, -- Non-cachable load
459 # OP_STORE_HIT, -- Store hitting cache
460 # OP_STORE_MISS -- Store missing cache
462 # Type of operation on a "valid" input
466 OP_BAD
= 1 # NC cache hit, TLB miss, prot/RC failure
467 OP_STCX_FAIL
= 2 # conditional store w/o reservation
468 OP_LOAD_HIT
= 3 # Cache hit on load
469 OP_LOAD_MISS
= 4 # Load missing cache
470 OP_LOAD_NC
= 5 # Non-cachable load
471 OP_STORE_HIT
= 6 # Store hitting cache
472 OP_STORE_MISS
= 7 # Store missing cache
474 # -- Cache state machine
477 # IDLE, -- Normal load hit processing
478 # RELOAD_WAIT_ACK, -- Cache reload wait ack
479 # STORE_WAIT_ACK, -- Store wait ack
480 # NC_LOAD_WAIT_ACK -- Non-cachable load wait ack
482 # Cache state machine
485 IDLE
= 0 # Normal load hit processing
486 RELOAD_WAIT_ACK
= 1 # Cache reload wait ack
487 STORE_WAIT_ACK
= 2 # Store wait ack
488 NC_LOAD_WAIT_ACK
= 3 # Non-cachable load wait ack
490 # -- Dcache operations:
492 # -- In order to make timing, we use the BRAMs with
493 # -- an output buffer, which means that the BRAM
494 # -- output is delayed by an extra cycle.
496 # -- Thus, the dcache has a 2-stage internal pipeline
497 # -- for cache hits with no stalls.
499 # -- All other operations are handled via stalling
500 # -- in the first stage.
502 # -- The second stage can thus complete a hit at the same
503 # -- time as the first stage emits a stall for a complex op.
505 # -- Stage 0 register, basically contains just the latched request
506 # type reg_stage_0_t is record
507 # req : Loadstore1ToDcacheType;
508 # tlbie : std_ulogic;
509 # doall : std_ulogic;
510 # tlbld : std_ulogic;
511 # mmu_req : std_ulogic; -- indicates source of request
515 # In order to make timing, we use the BRAMs with
516 # an output buffer, which means that the BRAM
517 # output is delayed by an extra cycle.
519 # Thus, the dcache has a 2-stage internal pipeline
520 # for cache hits with no stalls.
522 # All other operations are handled via stalling
523 # in the first stage.
525 # The second stage can thus complete a hit at the same
526 # time as the first stage emits a stall for a complex op.
528 # Stage 0 register, basically contains just the latched request
529 class RegStage0(RecordObject
):
532 self
.req
= LoadStore1ToDcacheType()
533 self
.tlbie
= Signal()
534 self
.doall
= Signal()
535 self
.tlbld
= Signal()
536 self
.mmu_req
= Signal() # indicates source of request
538 # signal r0 : reg_stage_0_t;
539 # signal r0_full : std_ulogic;
543 # type mem_access_request_t is record
545 # valid : std_ulogic;
547 # real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
548 # data : std_ulogic_vector(63 downto 0);
549 # byte_sel : std_ulogic_vector(7 downto 0);
551 # same_tag : std_ulogic;
552 # mmu_req : std_ulogic;
554 class MemAccessRequest(RecordObject
):
558 self
.valid
= Signal()
560 self
.real_addr
= Signal(REAL_ADDR_BITS
)
561 self
.data
= Signal(64)
562 self
.byte_sel
= Signal(8)
564 self
.same_tag
= Signal()
565 self
.mmu_req
= Signal()
567 # -- First stage register, contains state for stage 1 of load hits
568 # -- and for the state machine used by all other operations
569 # type reg_stage_1_t is record
570 # -- Info about the request
571 # full : std_ulogic; -- have uncompleted request
572 # mmu_req : std_ulogic; -- request is from MMU
573 # req : mem_access_request_t;
577 # hit_load_valid : std_ulogic;
578 # hit_index : index_t;
579 # cache_hit : std_ulogic;
582 # tlb_hit : std_ulogic;
583 # tlb_hit_way : tlb_way_t;
584 # tlb_hit_index : tlb_index_t;
586 # -- 2-stage data buffer for data forwarded from writes to reads
587 # forward_data1 : std_ulogic_vector(63 downto 0);
588 # forward_data2 : std_ulogic_vector(63 downto 0);
589 # forward_sel1 : std_ulogic_vector(7 downto 0);
590 # forward_valid1 : std_ulogic;
591 # forward_way1 : way_t;
592 # forward_row1 : row_t;
593 # use_forward1 : std_ulogic;
594 # forward_sel : std_ulogic_vector(7 downto 0);
596 # -- Cache miss state (reload state machine)
599 # write_bram : std_ulogic;
600 # write_tag : std_ulogic;
601 # slow_valid : std_ulogic;
602 # wb : wishbone_master_out;
603 # reload_tag : cache_tag_t;
606 # store_index : index_t;
607 # end_row_ix : row_in_line_t;
608 # rows_valid : row_per_line_valid_t;
609 # acks_pending : unsigned(2 downto 0);
610 # inc_acks : std_ulogic;
611 # dec_acks : std_ulogic;
613 # -- Signals to complete (possibly with error)
614 # ls_valid : std_ulogic;
615 # ls_error : std_ulogic;
616 # mmu_done : std_ulogic;
617 # mmu_error : std_ulogic;
618 # cache_paradox : std_ulogic;
620 # -- Signal to complete a failed stcx.
621 # stcx_fail : std_ulogic;
623 # First stage register, contains state for stage 1 of load hits
624 # and for the state machine used by all other operations
625 class RegStage1(RecordObject
):
628 # Info about the request
629 self
.full
= Signal() # have uncompleted request
630 self
.mmu_req
= Signal() # request is from MMU
631 self
.req
= MemAccessRequest()
635 self
.hit_load_valid
= Signal()
636 self
.hit_index
= Index()
637 self
.cache_hit
= Signal()
640 self
.tlb_hit
= Signal()
641 self
.tlb_hit_way
= TLBWay()
642 self
.tlb_hit_index
= TLBIndex()
644 # 2-stage data buffer for data forwarded from writes to reads
645 self
.forward_data1
= Signal(64)
646 self
.forward_data2
= Signal(64)
647 self
.forward_sel1
= Signal(8)
648 self
.forward_valid1
= Signal()
649 self
.forward_way1
= Way()
650 self
.forward_row1
= Row()
651 self
.use_forward1
= Signal()
652 self
.forward_sel
= Signal(8)
654 # Cache miss state (reload state machine)
657 self
.write_bram
= Signal()
658 self
.write_tag
= Signal()
659 self
.slow_valid
= Signal()
660 self
.wb
= WishboneMasterOut()
661 self
.reload_tag
= CacheTag()
662 self
.store_way
= Way()
663 self
.store_row
= Row()
664 self
.store_index
= Index()
665 self
.end_row_ix
= RowInLine()
666 self
.rows_valid
= RowPerLineValid()
667 self
.acks_pending
= Signal(3)
668 self
.inc_acks
= Signal()
669 self
.dec_acks
= Signal()
671 # Signals to complete (possibly with error)
672 self
.ls_valid
= Signal()
673 self
.ls_error
= Signal()
674 self
.mmu_done
= Signal()
675 self
.mmu_error
= Signal()
676 self
.cache_paradox
= Signal()
678 # Signal to complete a failed stcx.
679 self
.stcx_fail
= Signal()
681 # signal r1 : reg_stage_1_t;
684 # -- Reservation information
686 # type reservation_t is record
687 # valid : std_ulogic;
688 # addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
690 # Reservation information
692 class Reservation(RecordObject
):
696 addr
= Signal(63 downto LINE_OFF_BITS
) # TODO LINE_OFF_BITS is 6
698 # signal reservation : reservation_t;
700 # -- Async signals on incoming request
701 # signal req_index : index_t;
702 # signal req_row : row_t;
703 # signal req_hit_way : way_t;
704 # signal req_tag : cache_tag_t;
705 # signal req_op : op_t;
706 # signal req_data : std_ulogic_vector(63 downto 0);
707 # signal req_same_tag : std_ulogic;
708 # signal req_go : std_ulogic;
710 # signal early_req_row : row_t;
712 # signal cancel_store : std_ulogic;
713 # signal set_rsrv : std_ulogic;
714 # signal clear_rsrv : std_ulogic;
716 # signal r0_valid : std_ulogic;
717 # signal r0_stall : std_ulogic;
719 # signal use_forward1_next : std_ulogic;
720 # signal use_forward2_next : std_ulogic;
722 # -- Cache RAM interface
723 # type cache_ram_out_t is array(way_t) of cache_row_t;
724 # signal cache_out : cache_ram_out_t;
726 # -- PLRU output interface
727 # type plru_out_t is array(index_t) of
728 # std_ulogic_vector(WAY_BITS-1 downto 0);
729 # signal plru_victim : plru_out_t;
730 # signal replace_way : way_t;
732 # -- Wishbone read/write/cache write formatting signals
733 # signal bus_sel : std_ulogic_vector(7 downto 0);
736 # signal tlb_tag_way : tlb_way_tags_t;
737 # signal tlb_pte_way : tlb_way_ptes_t;
738 # signal tlb_valid_way : tlb_way_valids_t;
739 # signal tlb_req_index : tlb_index_t;
740 # signal tlb_hit : std_ulogic;
741 # signal tlb_hit_way : tlb_way_t;
742 # signal pte : tlb_pte_t;
743 # signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
744 # signal valid_ra : std_ulogic;
745 # signal perm_attr : perm_attr_t;
746 # signal rc_ok : std_ulogic;
747 # signal perm_ok : std_ulogic;
748 # signal access_ok : std_ulogic;
750 # -- TLB PLRU output interface
751 # type tlb_plru_out_t is array(tlb_index_t) of
752 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
753 # signal tlb_plru_victim : tlb_plru_out_t;
756 # -- Helper functions to decode incoming requests
759 # -- Return the cache line index (tag index) for an address
760 # function get_index(addr: std_ulogic_vector) return index_t is
763 # unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS))
767 # -- Return the cache row index (data memory) for an address
768 # function get_row(addr: std_ulogic_vector) return row_t is
771 # unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS))
775 # -- Return the index of a row within a line
776 # function get_row_of_line(row: row_t) return row_in_line_t is
777 # variable row_v : unsigned(ROW_BITS-1 downto 0);
779 # row_v := to_unsigned(row, ROW_BITS);
780 # return row_v(ROW_LINEBITS-1 downto 0);
783 # -- Returns whether this is the last row of a line
784 # function is_last_row_addr(addr: wishbone_addr_type;
785 # last: row_in_line_t) return boolean is
788 # unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
791 # -- Returns whether this is the last row of a line
792 # function is_last_row(row: row_t; last: row_in_line_t)
795 # return get_row_of_line(row) = last;
798 # -- Return the address of the next row in the current cache line
799 # function next_row_addr(addr: wishbone_addr_type)
800 # return std_ulogic_vector is
801 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
802 # variable result : wishbone_addr_type;
804 # -- Is there no simpler way in VHDL to
805 # -- generate that 3 bits adder ?
806 # row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
807 # row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
809 # result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
813 # -- Return the next row in the current cache line. We use a
814 # -- dedicated function in order to limit the size of the
815 # -- generated adder to be only the bits within a cache line
816 # -- (3 bits with default settings)
817 # function next_row(row: row_t) return row_t is
818 # variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
819 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
820 # variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
822 # row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
823 # row_idx := row_v(ROW_LINEBITS-1 downto 0);
824 # row_v(ROW_LINEBITS-1 downto 0) :=
825 # std_ulogic_vector(unsigned(row_idx) + 1);
826 # return to_integer(unsigned(row_v));
829 # -- Get the tag value from the address
830 # function get_tag(addr: std_ulogic_vector) return cache_tag_t is
832 # return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
835 # -- Read a tag from a tag memory row
836 # function read_tag(way: way_t; tagset: cache_tags_set_t)
837 # return cache_tag_t is
839 # return tagset(way * TAG_WIDTH + TAG_BITS
840 # - 1 downto way * TAG_WIDTH);
843 # -- Read a TLB tag from a TLB tag memory row
844 # function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t)
845 # return tlb_tag_t is
846 # variable j : integer;
848 # j := way * TLB_EA_TAG_BITS;
849 # return tags(j + TLB_EA_TAG_BITS - 1 downto j);
852 # -- Write a TLB tag to a TLB tag memory row
853 # procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
855 # variable j : integer;
857 # j := way * TLB_EA_TAG_BITS;
858 # tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
861 # -- Read a PTE from a TLB PTE memory row
862 # function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t)
863 # return tlb_pte_t is
864 # variable j : integer;
866 # j := way * TLB_PTE_BITS;
867 # return ptes(j + TLB_PTE_BITS - 1 downto j);
870 # procedure write_tlb_pte(way: tlb_way_t;
871 # ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
872 # variable j : integer;
874 # j := way * TLB_PTE_BITS;
875 # ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
880 # assert LINE_SIZE mod ROW_SIZE = 0
881 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
882 # assert ispow2(LINE_SIZE)
883 # report "LINE_SIZE not power of 2" severity FAILURE;
884 # assert ispow2(NUM_LINES)
885 # report "NUM_LINES not power of 2" severity FAILURE;
886 # assert ispow2(ROW_PER_LINE)
887 # report "ROW_PER_LINE not power of 2" severity FAILURE;
888 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
889 # report "geometry bits don't add up" severity FAILURE;
890 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
891 # report "geometry bits don't add up" severity FAILURE;
892 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
893 # report "geometry bits don't add up" severity FAILURE;
894 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
895 # report "geometry bits don't add up" severity FAILURE;
896 # assert (64 = wishbone_data_bits)
897 # report "Can't yet handle a wishbone width that isn't 64-bits"
899 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
900 # report "Set indexed by virtual address" severity FAILURE;
902 # -- Latch the request in r0.req as long as we're not stalling
903 # stage_0 : process(clk)
904 # variable r : reg_stage_0_t;
906 # if rising_edge(clk) then
907 # assert (d_in.valid and m_in.valid) = '0'
908 # report "request collision loadstore vs MMU";
909 # if m_in.valid = '1' then
910 # r.req.valid := '1';
911 # r.req.load := not (m_in.tlbie or m_in.tlbld);
914 # r.req.reserve := '0';
915 # r.req.virt_mode := '0';
916 # r.req.priv_mode := '1';
917 # r.req.addr := m_in.addr;
918 # r.req.data := m_in.pte;
919 # r.req.byte_sel := (others => '1');
920 # r.tlbie := m_in.tlbie;
921 # r.doall := m_in.doall;
922 # r.tlbld := m_in.tlbld;
933 # elsif r1.full = '0' or r0_full = '0' then
935 # r0_full <= r.req.valid;
940 # -- we don't yet handle collisions between loadstore1 requests
941 # -- and MMU requests
942 # m_out.stall <= '0';
944 # -- Hold off the request in r0 when r1 has an uncompleted request
945 # r0_stall <= r0_full and r1.full;
946 # r0_valid <= r0_full and not r1.full;
947 # stall_out <= r0_stall;
950 # -- Operates in the second cycle on the request latched in r0.req.
951 # -- TLB updates write the entry at the end of the second cycle.
952 # tlb_read : process(clk)
953 # variable index : tlb_index_t;
954 # variable addrbits :
955 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
957 # if rising_edge(clk) then
958 # if m_in.valid = '1' then
959 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
960 # - 1 downto TLB_LG_PGSZ);
962 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
963 # - 1 downto TLB_LG_PGSZ);
965 # index := to_integer(unsigned(addrbits));
966 # -- If we have any op and the previous op isn't finished,
967 # -- then keep the same output for next cycle.
968 # if r0_stall = '0' then
969 # tlb_valid_way <= dtlb_valids(index);
970 # tlb_tag_way <= dtlb_tags(index);
971 # tlb_pte_way <= dtlb_ptes(index);
976 # -- Generate TLB PLRUs
977 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
979 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
980 # -- TLB PLRU interface
981 # signal tlb_plru_acc :
982 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
983 # signal tlb_plru_acc_en : std_ulogic;
984 # signal tlb_plru_out :
985 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
987 # tlb_plru : entity work.plru
989 # BITS => TLB_WAY_BITS
994 # acc => tlb_plru_acc,
995 # acc_en => tlb_plru_acc_en,
996 # lru => tlb_plru_out
1002 # if r1.tlb_hit_index = i then
1003 # tlb_plru_acc_en <= r1.tlb_hit;
1005 # tlb_plru_acc_en <= '0';
1008 # std_ulogic_vector(to_unsigned(
1009 # r1.tlb_hit_way, TLB_WAY_BITS
1011 # tlb_plru_victim(i) <= tlb_plru_out;
1016 # tlb_search : process(all)
1017 # variable hitway : tlb_way_t;
1018 # variable hit : std_ulogic;
1019 # variable eatag : tlb_tag_t;
1022 # to_integer(unsigned(r0.req.addr(
1023 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
1027 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
1028 # for i in tlb_way_t loop
1029 # if tlb_valid_way(i) = '1' and
1030 # read_tlb_tag(i, tlb_tag_way) = eatag then
1035 # tlb_hit <= hit and r0_valid;
1036 # tlb_hit_way <= hitway;
1037 # if tlb_hit = '1' then
1038 # pte <= read_tlb_pte(hitway, tlb_pte_way);
1040 # pte <= (others => '0');
1042 # valid_ra <= tlb_hit or not r0.req.virt_mode;
1043 # if r0.req.virt_mode = '1' then
1044 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
1045 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
1046 # (ROW_OFF_BITS-1 downto 0 => '0');
1047 # perm_attr <= extract_perm_attr(pte);
1049 # ra <= r0.req.addr(
1050 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
1051 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
1052 # perm_attr <= real_mode_perm_attr;
1056 # tlb_update : process(clk)
1057 # variable tlbie : std_ulogic;
1058 # variable tlbwe : std_ulogic;
1059 # variable repl_way : tlb_way_t;
1060 # variable eatag : tlb_tag_t;
1061 # variable tagset : tlb_way_tags_t;
1062 # variable pteset : tlb_way_ptes_t;
1064 # if rising_edge(clk) then
1065 # tlbie := r0_valid and r0.tlbie;
1066 # tlbwe := r0_valid and r0.tlbld;
1067 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
1068 # -- clear all valid bits at once
1069 # for i in tlb_index_t loop
1070 # dtlb_valids(i) <= (others => '0');
1072 # elsif tlbie = '1' then
1073 # if tlb_hit = '1' then
1074 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
1076 # elsif tlbwe = '1' then
1077 # if tlb_hit = '1' then
1078 # repl_way := tlb_hit_way;
1080 # repl_way := to_integer(unsigned(
1081 # tlb_plru_victim(tlb_req_index)));
1083 # eatag := r0.req.addr(
1084 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
1086 # tagset := tlb_tag_way;
1087 # write_tlb_tag(repl_way, tagset, eatag);
1088 # dtlb_tags(tlb_req_index) <= tagset;
1089 # pteset := tlb_pte_way;
1090 # write_tlb_pte(repl_way, pteset, r0.req.data);
1091 # dtlb_ptes(tlb_req_index) <= pteset;
1092 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
1098 # maybe_plrus: if NUM_WAYS > 1 generate
1100 # plrus: for i in 0 to NUM_LINES-1 generate
1102 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
1103 # signal plru_acc_en : std_ulogic;
1104 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
1107 # plru : entity work.plru
1115 # acc_en => plru_acc_en,
1122 # if r1.hit_index = i then
1123 # plru_acc_en <= r1.cache_hit;
1125 # plru_acc_en <= '0';
1127 # plru_acc <= std_ulogic_vector(to_unsigned(
1128 # r1.hit_way, WAY_BITS
1130 # plru_victim(i) <= plru_out;
1135 # -- Cache tag RAM read port
1136 # cache_tag_read : process(clk)
1137 # variable index : index_t;
1139 # if rising_edge(clk) then
1140 # if r0_stall = '1' then
1141 # index := req_index;
1142 # elsif m_in.valid = '1' then
1143 # index := get_index(m_in.addr);
1145 # index := get_index(d_in.addr);
1147 # cache_tag_set <= cache_tags(index);
1151 # -- Cache request parsing and hit detection
1152 # dcache_request : process(all)
1153 # variable is_hit : std_ulogic;
1154 # variable hit_way : way_t;
1155 # variable op : op_t;
1156 # variable opsel : std_ulogic_vector(2 downto 0);
1157 # variable go : std_ulogic;
1158 # variable nc : std_ulogic;
1159 # variable s_hit : std_ulogic;
1160 # variable s_tag : cache_tag_t;
1161 # variable s_pte : tlb_pte_t;
1162 # variable s_ra : std_ulogic_vector(
1163 # REAL_ADDR_BITS - 1 downto 0
1165 # variable hit_set : std_ulogic_vector(
1166 # TLB_NUM_WAYS - 1 downto 0
1168 # variable hit_way_set : hit_way_set_t;
1169 # variable rel_matches : std_ulogic_vector(
1170 # TLB_NUM_WAYS - 1 downto 0
1172 # variable rel_match : std_ulogic;
1174 # -- Extract line, row and tag from request
1175 # req_index <= get_index(r0.req.addr);
1176 # req_row <= get_row(r0.req.addr);
1177 # req_tag <= get_tag(ra);
1179 # go := r0_valid and not (r0.tlbie or r0.tlbld)
1180 # and not r1.ls_error;
1182 # -- Test if pending request is a hit on any way
1183 # -- In order to make timing in virtual mode,
1184 # -- when we are using the TLB, we compare each
1185 # --way with each of the real addresses from each way of
1186 # -- the TLB, and then decide later which match to use.
1190 # if r0.req.virt_mode = '1' then
1191 # rel_matches := (others => '0');
1192 # for j in tlb_way_t loop
1193 # hit_way_set(j) := 0;
1195 # s_pte := read_tlb_pte(j, tlb_pte_way);
1197 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
1198 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
1199 # s_tag := get_tag(s_ra);
1200 # for i in way_t loop
1201 # if go = '1' and cache_valids(req_index)(i) = '1'
1202 # and read_tag(i, cache_tag_set) = s_tag
1203 # and tlb_valid_way(j) = '1' then
1204 # hit_way_set(j) := i;
1208 # hit_set(j) := s_hit;
1209 # if s_tag = r1.reload_tag then
1210 # rel_matches(j) := '1';
1213 # if tlb_hit = '1' then
1214 # is_hit := hit_set(tlb_hit_way);
1215 # hit_way := hit_way_set(tlb_hit_way);
1216 # rel_match := rel_matches(tlb_hit_way);
1219 # s_tag := get_tag(r0.req.addr);
1220 # for i in way_t loop
1221 # if go = '1' and cache_valids(req_index)(i) = '1' and
1222 # read_tag(i, cache_tag_set) = s_tag then
1227 # if s_tag = r1.reload_tag then
1231 # req_same_tag <= rel_match;
1233 # -- See if the request matches the line currently being reloaded
1234 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
1235 # and rel_match = '1' then
1236 # -- For a store, consider this a hit even if the row isn't
1237 # -- valid since it will be by the time we perform the store.
1238 # -- For a load, check the appropriate row valid bit.
1240 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
1241 # hit_way := replace_way;
1244 # -- Whether to use forwarded data for a load or not
1245 # use_forward1_next <= '0';
1246 # if get_row(r1.req.real_addr) = req_row
1247 # and r1.req.hit_way = hit_way then
1248 # -- Only need to consider r1.write_bram here, since if we
1249 # -- are writing refill data here, then we don't have a
1250 # -- cache hit this cycle on the line being refilled.
1251 # -- (There is the possibility that the load following the
1252 # -- load miss that started the refill could be to the old
1253 # -- contents of the victim line, since it is a couple of
1254 # -- cycles after the refill starts before we see the updated
1255 # -- cache tag. In that case we don't use the bypass.)
1256 # use_forward1_next <= r1.write_bram;
1258 # use_forward2_next <= '0';
1259 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
1260 # use_forward2_next <= r1.forward_valid1;
1263 # -- The way that matched on a hit
1264 # req_hit_way <= hit_way;
1266 # -- The way to replace on a miss
1267 # if r1.write_tag = '1' then
1268 # replace_way <= to_integer(unsigned(
1269 # plru_victim(r1.store_index)
1272 # replace_way <= r1.store_way;
1275 # -- work out whether we have permission for this access
1276 # -- NB we don't yet implement AMR, thus no KUAP
1277 # rc_ok <= perm_attr.reference and
1278 # (r0.req.load or perm_attr.changed);
1279 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
1280 # (perm_attr.wr_perm or (r0.req.load
1281 # and perm_attr.rd_perm));
1282 # access_ok <= valid_ra and perm_ok and rc_ok;
1284 # -- Combine the request and cache hit status to decide what
1285 # -- operation needs to be done
1287 # nc := r0.req.nc or perm_attr.nocache;
1290 # if access_ok = '0' then
1292 # elsif cancel_store = '1' then
1293 # op := OP_STCX_FAIL;
1295 # opsel := r0.req.load & nc & is_hit;
1297 # when "101" => op := OP_LOAD_HIT;
1298 # when "100" => op := OP_LOAD_MISS;
1299 # when "110" => op := OP_LOAD_NC;
1300 # when "001" => op := OP_STORE_HIT;
1301 # when "000" => op := OP_STORE_MISS;
1302 # when "010" => op := OP_STORE_MISS;
1303 # when "011" => op := OP_BAD;
1304 # when "111" => op := OP_BAD;
1305 # when others => op := OP_NONE;
1312 # -- Version of the row number that is valid one cycle earlier
1313 # -- in the cases where we need to read the cache data BRAM.
1314 # -- If we're stalling then we need to keep reading the last
1316 # if r0_stall = '0' then
1317 # if m_in.valid = '1' then
1318 # early_req_row <= get_row(m_in.addr);
1320 # early_req_row <= get_row(d_in.addr);
1323 # early_req_row <= req_row;
1327 # -- Wire up wishbone request latch out of stage 1
1328 # wishbone_out <= r1.wb;
1330 # -- Handle load-with-reservation and store-conditional instructions
1331 # reservation_comb: process(all)
1333 # cancel_store <= '0';
1335 # clear_rsrv <= '0';
1336 # if r0_valid = '1' and r0.req.reserve = '1' then
1337 # -- XXX generate alignment interrupt if address
1338 # -- is not aligned XXX or if r0.req.nc = '1'
1339 # if r0.req.load = '1' then
1340 # -- load with reservation
1343 # -- store conditional
1344 # clear_rsrv <= '1';
1345 # if reservation.valid = '0' or r0.req.addr(63
1346 # downto LINE_OFF_BITS) /= reservation.addr then
1347 # cancel_store <= '1';
1353 # reservation_reg: process(clk)
1355 # if rising_edge(clk) then
1357 # reservation.valid <= '0';
1358 # elsif r0_valid = '1' and access_ok = '1' then
1359 # if clear_rsrv = '1' then
1360 # reservation.valid <= '0';
1361 # elsif set_rsrv = '1' then
1362 # reservation.valid <= '1';
1363 # reservation.addr <=
1364 # r0.req.addr(63 downto LINE_OFF_BITS);
1370 # -- Return data for loads & completion control logic
1372 # writeback_control: process(all)
1373 # variable data_out : std_ulogic_vector(63 downto 0);
1374 # variable data_fwd : std_ulogic_vector(63 downto 0);
1375 # variable j : integer;
1377 # -- Use the bypass if are reading the row that was
1378 # -- written 1 or 2 cycles ago, including for the
1379 # -- slow_valid = 1 case (i.e. completing a load
1380 # -- miss or a non-cacheable load).
1381 # if r1.use_forward1 = '1' then
1382 # data_fwd := r1.forward_data1;
1384 # data_fwd := r1.forward_data2;
1386 # data_out := cache_out(r1.hit_way);
1387 # for i in 0 to 7 loop
1389 # if r1.forward_sel(i) = '1' then
1390 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1394 # d_out.valid <= r1.ls_valid;
1395 # d_out.data <= data_out;
1396 # d_out.store_done <= not r1.stcx_fail;
1397 # d_out.error <= r1.ls_error;
1398 # d_out.cache_paradox <= r1.cache_paradox;
1401 # m_out.done <= r1.mmu_done;
1402 # m_out.err <= r1.mmu_error;
1403 # m_out.data <= data_out;
1405 # -- We have a valid load or store hit or we just completed
1406 # -- a slow op such as a load miss, a NC load or a store
1408 # -- Note: the load hit is delayed by one cycle. However it
1409 # -- can still not collide with r.slow_valid (well unless I
1410 # -- miscalculated) because slow_valid can only be set on a
1411 # -- subsequent request and not on its first cycle (the state
1412 # -- machine must have advanced), which makes slow_valid
1413 # -- at least 2 cycles from the previous hit_load_valid.
1415 # -- Sanity: Only one of these must be set in any given cycle
1416 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1417 # report "unexpected slow_valid collision with stcx_fail"
1419 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1420 # /= '1' report "unexpected hit_load_delayed collision with
1421 # slow_valid" severity FAILURE;
1423 # if r1.mmu_req = '0' then
1424 # -- Request came from loadstore1...
1425 # -- Load hit case is the standard path
1426 # if r1.hit_load_valid = '1' then
1428 # "completing load hit data=" & to_hstring(data_out);
1431 # -- error cases complete without stalling
1432 # if r1.ls_error = '1' then
1433 # report "completing ld/st with error";
1436 # -- Slow ops (load miss, NC, stores)
1437 # if r1.slow_valid = '1' then
1439 # "completing store or load miss data="
1440 # & to_hstring(data_out);
1444 # -- Request came from MMU
1445 # if r1.hit_load_valid = '1' then
1446 # report "completing load hit to MMU, data="
1447 # & to_hstring(m_out.data);
1450 # -- error cases complete without stalling
1451 # if r1.mmu_error = '1' then
1452 # report "completing MMU ld with error";
1455 # -- Slow ops (i.e. load miss)
1456 # if r1.slow_valid = '1' then
1457 # report "completing MMU load miss, data="
1458 # & to_hstring(m_out.data);
1465 # -- Generate a cache RAM for each way. This handles the normal
1466 # -- reads, writes from reloads and the special store-hit update
1469 # -- Note: the BRAMs have an extra read buffer, meaning the output
1470 # -- is pipelined an extra cycle. This differs from the
1471 # -- icache. The writeback logic needs to take that into
1472 # -- account by using 1-cycle delayed signals for load hits.
1474 # rams: for i in 0 to NUM_WAYS-1 generate
1475 # signal do_read : std_ulogic;
1476 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1477 # signal do_write : std_ulogic;
1478 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1480 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1481 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1482 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1483 # signal dout : cache_row_t;
1485 # way: entity work.cache_ram
1487 # ROW_BITS => ROW_BITS,
1488 # WIDTH => wishbone_data_bits,
1494 # rd_addr => rd_addr,
1496 # wr_sel => wr_sel_m,
1497 # wr_addr => wr_addr,
1498 # wr_data => wr_data
1502 # -- Cache hit reads
1505 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1506 # cache_out(i) <= dout;
1510 # -- Defaults to wishbone read responses (cache refill)
1512 # -- For timing, the mux on wr_data/sel/addr is not
1513 # -- dependent on anything other than the current state.
1514 # wr_sel_m <= (others => '0');
1517 # if r1.write_bram = '1' then
1518 # -- Write store data to BRAM. This happens one
1519 # -- cycle after the store is in r0.
1520 # wr_data <= r1.req.data;
1521 # wr_sel <= r1.req.byte_sel;
1522 # wr_addr <= std_ulogic_vector(to_unsigned(
1523 # get_row(r1.req.real_addr), ROW_BITS
1525 # if i = r1.req.hit_way then
1529 # -- Otherwise, we might be doing a reload or a DCBZ
1530 # if r1.dcbz = '1' then
1531 # wr_data <= (others => '0');
1533 # wr_data <= wishbone_in.dat;
1535 # wr_addr <= std_ulogic_vector(to_unsigned(
1536 # r1.store_row, ROW_BITS
1538 # wr_sel <= (others => '1');
1540 # if r1.state = RELOAD_WAIT_ACK and
1541 # wishbone_in.ack = '1' and replace_way = i then
1546 # -- Mask write selects with do_write since BRAM
1547 # -- doesn't have a global write-enable
1548 # if do_write = '1' then
1549 # wr_sel_m <= wr_sel;
1555 # -- Cache hit synchronous machine for the easy case.
1556 # -- This handles load hits.
1557 # -- It also handles error cases (TLB miss, cache paradox)
1558 # dcache_fast_hit : process(clk)
1560 # if rising_edge(clk) then
1561 # if req_op /= OP_NONE then
1562 # report "op:" & op_t'image(req_op) &
1563 # " addr:" & to_hstring(r0.req.addr) &
1564 # " nc:" & std_ulogic'image(r0.req.nc) &
1565 # " idx:" & integer'image(req_index) &
1566 # " tag:" & to_hstring(req_tag) &
1567 # " way: " & integer'image(req_hit_way);
1569 # if r0_valid = '1' then
1570 # r1.mmu_req <= r0.mmu_req;
1573 # -- Fast path for load/store hits.
1574 # -- Set signals for the writeback controls.
1575 # r1.hit_way <= req_hit_way;
1576 # r1.hit_index <= req_index;
1577 # if req_op = OP_LOAD_HIT then
1578 # r1.hit_load_valid <= '1';
1580 # r1.hit_load_valid <= '0';
1582 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1583 # r1.cache_hit <= '1';
1585 # r1.cache_hit <= '0';
1588 # if req_op = OP_BAD then
1589 # report "Signalling ld/st error valid_ra=" &
1590 # std_ulogic'image(valid_ra) & " rc_ok=" &
1591 # std_ulogic'image(rc_ok) & " perm_ok=" &
1592 # std_ulogic'image(perm_ok);
1593 # r1.ls_error <= not r0.mmu_req;
1594 # r1.mmu_error <= r0.mmu_req;
1595 # r1.cache_paradox <= access_ok;
1597 # r1.ls_error <= '0';
1598 # r1.mmu_error <= '0';
1599 # r1.cache_paradox <= '0';
1602 # if req_op = OP_STCX_FAIL then
1603 # r1.stcx_fail <= '1';
1605 # r1.stcx_fail <= '0';
1608 # -- Record TLB hit information for updating TLB PLRU
1609 # r1.tlb_hit <= tlb_hit;
1610 # r1.tlb_hit_way <= tlb_hit_way;
1611 # r1.tlb_hit_index <= tlb_req_index;
1616 # -- Memory accesses are handled by this state machine:
1618 # -- * Cache load miss/reload (in conjunction with "rams")
1619 # -- * Load hits for non-cachable forms
1620 # -- * Stores (the collision case is handled in "rams")
1622 # -- All wishbone requests generation is done here.
1623 # -- This machine operates at stage 1.
1624 # dcache_slow : process(clk)
1625 # variable stbs_done : boolean;
1626 # variable req : mem_access_request_t;
1627 # variable acks : unsigned(2 downto 0);
1629 # if rising_edge(clk) then
1630 # r1.use_forward1 <= use_forward1_next;
1631 # r1.forward_sel <= (others => '0');
1632 # if use_forward1_next = '1' then
1633 # r1.forward_sel <= r1.req.byte_sel;
1634 # elsif use_forward2_next = '1' then
1635 # r1.forward_sel <= r1.forward_sel1;
1638 # r1.forward_data2 <= r1.forward_data1;
1639 # if r1.write_bram = '1' then
1640 # r1.forward_data1 <= r1.req.data;
1641 # r1.forward_sel1 <= r1.req.byte_sel;
1642 # r1.forward_way1 <= r1.req.hit_way;
1643 # r1.forward_row1 <= get_row(r1.req.real_addr);
1644 # r1.forward_valid1 <= '1';
1646 # if r1.dcbz = '1' then
1647 # r1.forward_data1 <= (others => '0');
1649 # r1.forward_data1 <= wishbone_in.dat;
1651 # r1.forward_sel1 <= (others => '1');
1652 # r1.forward_way1 <= replace_way;
1653 # r1.forward_row1 <= r1.store_row;
1654 # r1.forward_valid1 <= '0';
1657 # -- On reset, clear all valid bits to force misses
1659 # for i in index_t loop
1660 # cache_valids(i) <= (others => '0');
1664 # r1.slow_valid <= '0';
1667 # r1.ls_valid <= '0';
1668 # r1.mmu_done <= '0';
1670 # -- Not useful normally but helps avoiding
1671 # -- tons of sim warnings
1672 # r1.wb.adr <= (others => '0');
1674 # -- One cycle pulses reset
1675 # r1.slow_valid <= '0';
1676 # r1.write_bram <= '0';
1677 # r1.inc_acks <= '0';
1678 # r1.dec_acks <= '0';
1680 # r1.ls_valid <= '0';
1681 # -- complete tlbies and TLB loads in the third cycle
1682 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
1683 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
1684 # if r0.mmu_req = '0' then
1685 # r1.ls_valid <= '1';
1687 # r1.mmu_done <= '1';
1691 # if r1.write_tag = '1' then
1692 # -- Store new tag in selected way
1693 # for i in 0 to NUM_WAYS-1 loop
1694 # if i = replace_way then
1695 # cache_tags(r1.store_index)(
1696 # (i + 1) * TAG_WIDTH - 1
1697 # downto i * TAG_WIDTH
1699 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
1703 # r1.store_way <= replace_way;
1704 # r1.write_tag <= '0';
1707 # -- Take request from r1.req if there is one there,
1708 # -- else from req_op, ra, etc.
1709 # if r1.full = '1' then
1713 # req.valid := req_go;
1714 # req.mmu_req := r0.mmu_req;
1715 # req.dcbz := r0.req.dcbz;
1716 # req.real_addr := ra;
1717 # -- Force data to 0 for dcbz
1718 # if r0.req.dcbz = '0' then
1719 # req.data := r0.req.data;
1721 # req.data := (others => '0');
1723 # -- Select all bytes for dcbz
1724 # -- and for cacheable loads
1725 # if r0.req.dcbz = '1'
1726 # or (r0.req.load = '1' and r0.req.nc = '0') then
1727 # req.byte_sel := (others => '1');
1729 # req.byte_sel := r0.req.byte_sel;
1731 # req.hit_way := req_hit_way;
1732 # req.same_tag := req_same_tag;
1734 # -- Store the incoming request from r0,
1735 # -- if it is a slow request
1736 # -- Note that r1.full = 1 implies req_op = OP_NONE
1737 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
1738 # or req_op = OP_STORE_MISS
1739 # or req_op = OP_STORE_HIT then
1745 # -- Main state machine
1748 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1749 # r1.wb.sel <= req.byte_sel;
1750 # r1.wb.dat <= req.data;
1751 # r1.dcbz <= req.dcbz;
1753 # -- Keep track of our index and way
1754 # -- for subsequent stores.
1755 # r1.store_index <= get_index(req.real_addr);
1756 # r1.store_row <= get_row(req.real_addr);
1758 # get_row_of_line(get_row(req.real_addr)) - 1;
1759 # r1.reload_tag <= get_tag(req.real_addr);
1760 # r1.req.same_tag <= '1';
1762 # if req.op = OP_STORE_HIT then
1763 # r1.store_way <= req.hit_way;
1766 # -- Reset per-row valid bits,
1767 # -- ready for handling OP_LOAD_MISS
1768 # for i in 0 to ROW_PER_LINE - 1 loop
1769 # r1.rows_valid(i) <= '0';
1773 # when OP_LOAD_HIT =>
1774 # -- stay in IDLE state
1776 # when OP_LOAD_MISS =>
1777 # -- Normal load cache miss,
1778 # -- start the reload machine
1779 # report "cache miss real addr:" &
1780 # to_hstring(req.real_addr) & " idx:" &
1781 # integer'image(get_index(req.real_addr)) &
1782 # " tag:" & to_hstring(get_tag(req.real_addr));
1784 # -- Start the wishbone cycle
1789 # -- Track that we had one request sent
1790 # r1.state <= RELOAD_WAIT_ACK;
1791 # r1.write_tag <= '1';
1793 # when OP_LOAD_NC =>
1797 # r1.state <= NC_LOAD_WAIT_ACK;
1799 # when OP_STORE_HIT | OP_STORE_MISS =>
1800 # if req.dcbz = '0' then
1801 # r1.state <= STORE_WAIT_ACK;
1802 # r1.acks_pending <= to_unsigned(1, 3);
1804 # r1.slow_valid <= '1';
1805 # if req.mmu_req = '0' then
1806 # r1.ls_valid <= '1';
1808 # r1.mmu_done <= '1';
1810 # if req.op = OP_STORE_HIT then
1811 # r1.write_bram <= '1';
1814 # -- dcbz is handled much like a load
1815 # -- miss except that we are writing
1816 # -- to memory instead of reading
1817 # r1.state <= RELOAD_WAIT_ACK;
1818 # if req.op = OP_STORE_MISS then
1819 # r1.write_tag <= '1';
1826 # -- OP_NONE and OP_BAD do nothing
1827 # -- OP_BAD & OP_STCX_FAIL were handled above already
1830 # when OP_STCX_FAIL =>
1833 # when RELOAD_WAIT_ACK =>
1834 # -- Requests are all sent if stb is 0
1835 # stbs_done := r1.wb.stb = '0';
1837 # -- If we are still sending requests,
1838 # -- was one accepted?
1839 # if wishbone_in.stall = '0' and not stbs_done then
1840 # -- That was the last word ? We are done sending.
1841 # -- Clear stb and set stbs_done so we can handle
1842 # -- an eventual last ack on the same cycle.
1843 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
1845 # stbs_done := true;
1848 # -- Calculate the next row address
1849 # r1.wb.adr <= next_row_addr(r1.wb.adr);
1852 # -- Incoming acks processing
1853 # r1.forward_valid1 <= wishbone_in.ack;
1854 # if wishbone_in.ack = '1' then
1856 # r1.store_row mod ROW_PER_LINE
1858 # -- If this is the data we were looking for,
1859 # -- we can complete the request next cycle.
1860 # -- Compare the whole address in case the
1861 # -- request in r1.req is not the one that
1862 # -- started this refill.
1863 # if r1.full = '1' and r1.req.same_tag = '1'
1864 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
1865 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
1866 # and r1.store_row = get_row(r1.req.real_addr) then
1868 # r1.slow_valid <= '1';
1869 # if r1.mmu_req = '0' then
1870 # r1.ls_valid <= '1';
1872 # r1.mmu_done <= '1';
1874 # r1.forward_sel <= (others => '1');
1875 # r1.use_forward1 <= '1';
1878 # -- Check for completion
1879 # if stbs_done and is_last_row(r1.store_row,
1880 # r1.end_row_ix) then
1881 # -- Complete wishbone cycle
1884 # -- Cache line is now valid
1885 # cache_valids(r1.store_index)(
1892 # -- Increment store row counter
1893 # r1.store_row <= next_row(r1.store_row);
1896 # when STORE_WAIT_ACK =>
1897 # stbs_done := r1.wb.stb = '0';
1898 # acks := r1.acks_pending;
1899 # if r1.inc_acks /= r1.dec_acks then
1900 # if r1.inc_acks = '1' then
1906 # r1.acks_pending <= acks;
1907 # -- Clear stb when slave accepted request
1908 # if wishbone_in.stall = '0' then
1909 # -- See if there is another store waiting
1910 # -- to be done which is in the same real page.
1911 # if req.valid = '1' then
1913 # SET_SIZE_BITS - 1 downto 0
1914 # ) <= req.real_addr(
1915 # SET_SIZE_BITS - 1 downto 0
1917 # r1.wb.dat <= req.data;
1918 # r1.wb.sel <= req.byte_sel;
1920 # if acks < 7 and req.same_tag = '1'
1921 # and (req.op = OP_STORE_MISS
1922 # or req.op = OP_STORE_HIT) then
1924 # stbs_done := false;
1925 # if req.op = OP_STORE_HIT then
1926 # r1.write_bram <= '1';
1929 # r1.slow_valid <= '1';
1930 # -- Store requests never come from the MMU
1931 # r1.ls_valid <= '1';
1932 # stbs_done := false;
1933 # r1.inc_acks <= '1';
1936 # stbs_done := true;
1940 # -- Got ack ? See if complete.
1941 # if wishbone_in.ack = '1' then
1942 # if stbs_done and acks = 1 then
1947 # r1.dec_acks <= '1';
1950 # when NC_LOAD_WAIT_ACK =>
1951 # -- Clear stb when slave accepted request
1952 # if wishbone_in.stall = '0' then
1956 # -- Got ack ? complete.
1957 # if wishbone_in.ack = '1' then
1960 # r1.slow_valid <= '1';
1961 # if r1.mmu_req = '0' then
1962 # r1.ls_valid <= '1';
1964 # r1.mmu_done <= '1';
1966 # r1.forward_sel <= (others => '1');
1967 # r1.use_forward1 <= '1';
1976 # dc_log: if LOG_LENGTH > 0 generate
1977 # signal log_data : std_ulogic_vector(19 downto 0);
1979 # dcache_log: process(clk)
1981 # if rising_edge(clk) then
1982 # log_data <= r1.wb.adr(5 downto 3) &
1983 # wishbone_in.stall &
1985 # r1.wb.stb & r1.wb.cyc &
1988 # std_ulogic_vector(
1989 # to_unsigned(op_t'pos(req_op), 3)) &
1991 # std_ulogic_vector(
1992 # to_unsigned(tlb_hit_way, 3)) &
1994 # std_ulogic_vector(
1995 # to_unsigned(state_t'pos(r1.state), 3));
1998 # log_out <= log_data;