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_LINE_BITS is the number of bits to select
188 # a row within a line
189 ROW_LINE_BITS
= 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_LINE_BITS (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_LINE_BITS-1 downto 0);
236 return Signal(BRAM_ROWS
)
239 return Signal(NUM_LINES
)
242 return Signal(NUM_WAYS
)
245 return Signal(ROW_LINE_BITS
)
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 # TODO LINE_OFF_BITS is 6
697 addr
= Signal(63 downto LINE_OFF_BITS
)
699 # signal reservation : reservation_t;
700 reservation
= Reservation()
702 # -- Async signals on incoming request
703 # signal req_index : index_t;
704 # signal req_row : row_t;
705 # signal req_hit_way : way_t;
706 # signal req_tag : cache_tag_t;
707 # signal req_op : op_t;
708 # signal req_data : std_ulogic_vector(63 downto 0);
709 # signal req_same_tag : std_ulogic;
710 # signal req_go : std_ulogic;
711 # Async signals on incoming request
717 req_data
= Signal(64)
718 req_same_tag
= Signal()
721 # signal early_req_row : row_t;
723 # signal cancel_store : std_ulogic;
724 # signal set_rsrv : std_ulogic;
725 # signal clear_rsrv : std_ulogic;
727 # signal r0_valid : std_ulogic;
728 # signal r0_stall : std_ulogic;
730 # signal use_forward1_next : std_ulogic;
731 # signal use_forward2_next : std_ulogic;
732 early_req_row
= Row()
734 cancel_store
= Signal()
736 clear_rsrv
= Signal()
741 use_forward1_next
= Signal()
742 use_forward2_next
= Signal()
744 # -- Cache RAM interface
745 # type cache_ram_out_t is array(way_t) of cache_row_t;
746 # signal cache_out : cache_ram_out_t;
747 # Cache RAM interface
749 return Array(CacheRow() for x
in range(Way()))
751 cache_out
= CacheRamOut()
753 # -- PLRU output interface
754 # type plru_out_t is array(index_t) of
755 # std_ulogic_vector(WAY_BITS-1 downto 0);
756 # signal plru_victim : plru_out_t;
757 # signal replace_way : way_t;
758 # PLRU output interface
760 return Array(Signal(WAY_BITS
) for x
in range(Index()))
762 plru_victim
= PLRUOut()
765 # -- Wishbone read/write/cache write formatting signals
766 # signal bus_sel : std_ulogic_vector(7 downto 0);
767 # Wishbone read/write/cache write formatting signals
771 # signal tlb_tag_way : tlb_way_tags_t;
772 # signal tlb_pte_way : tlb_way_ptes_t;
773 # signal tlb_valid_way : tlb_way_valids_t;
774 # signal tlb_req_index : tlb_index_t;
775 # signal tlb_hit : std_ulogic;
776 # signal tlb_hit_way : tlb_way_t;
777 # signal pte : tlb_pte_t;
778 # signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
779 # signal valid_ra : std_ulogic;
780 # signal perm_attr : perm_attr_t;
781 # signal rc_ok : std_ulogic;
782 # signal perm_ok : std_ulogic;
783 # signal access_ok : std_ulogic;
785 tlb_tag_way
= TLBWayTags()
786 tlb_pte_way
= TLBWayPtes()
787 tlb_valid_way
= TLBWayValidBits()
788 tlb_req_index
= TLBIndex()
790 tlb_hit_way
= TLBWay()
792 ra
= Signal(REAL_ADDR_BITS
)
794 perm_attr
= PermAttr()
799 # -- TLB PLRU output interface
800 # type tlb_plru_out_t is array(tlb_index_t) of
801 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
802 # signal tlb_plru_victim : tlb_plru_out_t;
803 # TLB PLRU output interface
805 return Array(Signal(TLB_WAY_BITS
) for x
in range(TLBIndex()))
807 tlb_plru_victim
= TLBPLRUOut()
809 # -- Helper functions to decode incoming requests
811 # -- Return the cache line index (tag index) for an address
812 # function get_index(addr: std_ulogic_vector) return index_t is
815 # unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS))
818 # Helper functions to decode incoming requests
820 # Return the cache line index (tag index) for an address
821 def get_index(addr
=Signal()):
822 return addr
[LINE_OFF_BITS
:SET_SIZE_BITS
]
824 # -- Return the cache row index (data memory) for an address
825 # function get_row(addr: std_ulogic_vector) return row_t is
828 # unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS))
831 # Return the cache row index (data memory) for an address
832 def get_row(addr
=Signal()):
833 return addr
[ROW_OFF_BITS
:SET_SIZE_BITS
]
835 # -- Return the index of a row within a line
836 # function get_row_of_line(row: row_t) return row_in_line_t is
837 # variable row_v : unsigned(ROW_BITS-1 downto 0);
839 # row_v := to_unsigned(row, ROW_BITS);
840 # return row_v(ROW_LINEBITS-1 downto 0);
842 # Return the index of a row within a line
843 def get_row_of_line(row
=Row()):
844 row_v
= Signal(ROW_BITS
)
846 return row_v
[0:ROW_LINE_BITS
]
848 # -- Returns whether this is the last row of a line
849 # function is_last_row_addr(addr: wishbone_addr_type;
850 # last: row_in_line_t) return boolean is
853 # unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
855 # Returns whether this is the last row of a line
856 def is_last_row_addr(addr
=WBAddrType(), last
=RowInLine()):
857 return addr
[ROW_OFF_BITS
:LINE_OFF_BITS
] == last
859 # -- Returns whether this is the last row of a line
860 # function is_last_row(row: row_t; last: row_in_line_t)
863 # return get_row_of_line(row) = last;
865 # Returns whether this is the last row of a line
866 def is_last_row(row
=Row(), last
=RowInLine()):
867 return get_row_of_line(row
) == last
869 # -- Return the address of the next row in the current cache line
870 # function next_row_addr(addr: wishbone_addr_type)
871 # return std_ulogic_vector is
872 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
873 # variable result : wishbone_addr_type;
875 # -- Is there no simpler way in VHDL to
876 # -- generate that 3 bits adder ?
877 # row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
878 # row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
880 # result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
883 # Return the address of the next row in the current cache line
884 def next_row_addr(addr
=WBAddrType()):
885 row_idx
= Signal(ROW_LINE_BITS
)
886 result
= WBAddrType()
887 # Is there no simpler way in VHDL to
888 # generate that 3 bits adder ?
889 row_idx
= addr
[ROW_OFF_BITS
:LINE_OFF_BITS
]
890 row_idx
= Signal(row_idx
+ 1)
892 result
[ROW_OFF_BITS
:LINE_OFF_BITS
] = row_idx
895 # -- Return the next row in the current cache line. We use a
896 # -- dedicated function in order to limit the size of the
897 # -- generated adder to be only the bits within a cache line
898 # -- (3 bits with default settings)
899 # function next_row(row: row_t) return row_t is
900 # variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
901 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
902 # variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
904 # row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
905 # row_idx := row_v(ROW_LINEBITS-1 downto 0);
906 # row_v(ROW_LINEBITS-1 downto 0) :=
907 # std_ulogic_vector(unsigned(row_idx) + 1);
908 # return to_integer(unsigned(row_v));
910 # Return the next row in the current cache line. We use a
911 # dedicated function in order to limit the size of the
912 # generated adder to be only the bits within a cache line
913 # (3 bits with default settings)
914 def next_row(row
=Row())
915 row_v
= Signal(ROW_BITS
)
916 row_idx
= Signal(ROW_LINE_BITS
)
917 result
= Signal(ROW_BITS
)
920 row_idx
= row_v
[ROW_LINE_BITS
]
921 row_v
[0:ROW_LINE_BITS
] = Signal(row_idx
+ 1)
924 # -- Get the tag value from the address
925 # function get_tag(addr: std_ulogic_vector) return cache_tag_t is
927 # return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
929 # Get the tag value from the address
930 def get_tag(addr
=Signal()):
931 return addr
[SET_SIZE_BITS
:REAL_ADDR_BITS
]
933 # -- Read a tag from a tag memory row
934 # function read_tag(way: way_t; tagset: cache_tags_set_t)
935 # return cache_tag_t is
937 # return tagset(way * TAG_WIDTH + TAG_BITS
938 # - 1 downto way * TAG_WIDTH);
940 # Read a tag from a tag memory row
941 def read_tag(way
=Way(), tagset
=CacheTagsSet()):
942 return tagset
[way
*TAG_WIDTH
:way
* TAG_WIDTH
+ TAG_BITS
]
944 # -- Read a TLB tag from a TLB tag memory row
945 # function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t)
946 # return tlb_tag_t is
947 # variable j : integer;
949 # j := way * TLB_EA_TAG_BITS;
950 # return tags(j + TLB_EA_TAG_BITS - 1 downto j);
952 # Read a TLB tag from a TLB tag memory row
953 def read_tlb_tag(way
=TLBWay(), tags
=TLBWayTags()):
956 j
= way
* TLB_EA_TAG_BITS
957 return tags
[j
:j
+ TLB_EA_TAG_BITS
]
959 # -- Write a TLB tag to a TLB tag memory row
960 # procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
962 # variable j : integer;
964 # j := way * TLB_EA_TAG_BITS;
965 # tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
967 # Write a TLB tag to a TLB tag memory row
968 def write_tlb_tag(way
=TLBWay(), tags
=TLBWayTags()), tag
=TLBTag()):
971 j
= way
* TLB_EA_TAG_BITS
972 tags
[j
:j
+ TLB_EA_TAG_BITS
] = tag
974 # -- Read a PTE from a TLB PTE memory row
975 # function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t)
976 # return tlb_pte_t is
977 # variable j : integer;
979 # j := way * TLB_PTE_BITS;
980 # return ptes(j + TLB_PTE_BITS - 1 downto j);
982 # Read a PTE from a TLB PTE memory row
983 def read_tlb_pte(way
: TLBWay(), ptes
=TLBWayPtes()):
986 j
= way
* TLB_PTE_BITS
987 return ptes
[j
:j
+ TLB_PTE_BITS
]
989 # procedure write_tlb_pte(way: tlb_way_t;
990 # ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
991 # variable j : integer;
993 # j := way * TLB_PTE_BITS;
994 # ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
996 def write_tlb_pte(way
=TLBWay(), ptes
=TLBWayPtes(),
1001 j
= way
* TLB_PTE_BITS
1002 return ptes
[j
:j
+ TLB_PTE_BITS
] = newpte
1006 # assert LINE_SIZE mod ROW_SIZE = 0
1007 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
1008 # assert ispow2(LINE_SIZE)
1009 # report "LINE_SIZE not power of 2" severity FAILURE;
1010 # assert ispow2(NUM_LINES)
1011 # report "NUM_LINES not power of 2" severity FAILURE;
1012 # assert ispow2(ROW_PER_LINE)
1013 # report "ROW_PER_LINE not power of 2" severity FAILURE;
1014 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
1015 # report "geometry bits don't add up" severity FAILURE;
1016 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
1017 # report "geometry bits don't add up" severity FAILURE;
1018 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
1019 # report "geometry bits don't add up" severity FAILURE;
1020 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
1021 # report "geometry bits don't add up" severity FAILURE;
1022 # assert (64 = wishbone_data_bits)
1023 # report "Can't yet handle a wishbone width that isn't 64-bits"
1025 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
1026 # report "Set indexed by virtual address" severity FAILURE;
1027 assert (LINE_SIZE
% ROW_SIZE
) == 0 "LINE_SIZE not
1028 multiple of ROW_SIZE -!- severity FAILURE"
1030 assert (LINE_SIZE
% 2) == 0 "LINE_SIZE not power of
1031 2 -!- severity FAILURE"
1033 assert (NUM_LINES
% 2) == 0 "NUM_LINES not power of
1034 2 -!- severity FAILURE"
1036 assert (ROW_PER_LINE
% 2) == 0 "ROW_PER_LINE not
1037 power of 2 -!- severity FAILURE"
1039 assert ROW_BITS
== (INDEX_BITS
+ ROW_LINE_BITS
)
1040 "geometry bits don't add up -!- severity FAILURE"
1042 assert (LINE_OFF_BITS
= ROW_OFF_BITS
+ ROW_LINEBITS
)
1043 "geometry bits don't add up -!- severity FAILURE"
1045 assert REAL_ADDR_BITS
== (TAG_BITS
+ INDEX_BITS
1046 + LINE_OFF_BITS
) "geometry bits don't add up -!-
1049 assert REAL_ADDR_BITS
== (TAG_BITS
+ ROW_BITS
+ ROW_OFF_BITS
)
1050 "geometry bits don't add up -!- severity FAILURE"
1052 assert 64 == wishbone_data_bits
"Can't yet handle a
1053 wishbone width that isn't 64-bits -!- severity FAILURE"
1055 assert SET_SIZE_BITS
<= TLB_LG_PGSZ
"Set indexed by
1056 virtual address -!- severity FAILURE"
1058 # -- Latch the request in r0.req as long as we're not stalling
1059 # stage_0 : process(clk)
1060 # Latch the request in r0.req as long as we're not stalling
1061 class Stage0(Elaboratable
):
1065 def elaborate(self
, platform
):
1071 # variable r : reg_stage_0_t;
1076 # if rising_edge(clk) then
1077 # assert (d_in.valid and m_in.valid) = '0'
1078 # report "request collision loadstore vs MMU";
1079 assert ~
(d_in
.valid
& m_in
.valid
) "request collision
1082 # if m_in.valid = '1' then
1083 with m
.If(m_in
.valid
):
1084 # r.req.valid := '1';
1085 # r.req.load := not (m_in.tlbie or m_in.tlbld);
1086 # r.req.dcbz := '0';
1088 # r.req.reserve := '0';
1089 # r.req.virt_mode := '0';
1090 # r.req.priv_mode := '1';
1091 # r.req.addr := m_in.addr;
1092 # r.req.data := m_in.pte;
1093 # r.req.byte_sel := (others => '1');
1094 # r.tlbie := m_in.tlbie;
1095 # r.doall := m_in.doall;
1096 # r.tlbld := m_in.tlbld;
1098 sync
+= r
.req
.valid
.eq(1)
1099 sync
+= r
.req
.load
.eq(~
(m_in
.tlbie | m_in
.tlbld
))
1100 sync
+= r
.req
.priv_mode
.eq(1)
1101 sync
+= r
.req
.addr
.eq(m_in
.addr
)
1102 sync
+= r
.req
.data
.eq(m_in
.pte
)
1103 sync
+= r
.req
.byte_sel
.eq(1)
1104 sync
+= r
.tlbie
.eq(m_in
.tlbie
)
1105 sync
+= r
.doall
.eq(m_in
.doall
)
1106 sync
+= r
.tlbld
.eq(m_in
.tlbld
)
1107 sync
+= r
.mmu_req
.eq(1)
1115 sync
+= r
.req
.eq(d_in
)
1119 # elsif r1.full = '0' or r0_full = '0' then
1120 with m
.If(~r1
.full | ~r0_full
):
1122 # r0_full <= r.req.valid;
1124 sync
+= r0_full
.eq(r
.req
.valid
)
1129 # -- we don't yet handle collisions between loadstore1 requests
1130 # -- and MMU requests
1131 # m_out.stall <= '0';
1132 # we don't yet handle collisions between loadstore1 requests
1134 comb
+= m_out
.stall
.eq(0)
1136 # -- Hold off the request in r0 when r1 has an uncompleted request
1137 # r0_stall <= r0_full and r1.full;
1138 # r0_valid <= r0_full and not r1.full;
1139 # stall_out <= r0_stall;
1140 # Hold off the request in r0 when r1 has an uncompleted request
1141 comb
+= r0_stall
.eq(r0_full
& r1
.full
)
1142 comb
+= r0_valid
.eq(r0_full
& ~r1
.full
)
1143 comb
+= stall_out
.eq(r0_stall
)
1146 # -- Operates in the second cycle on the request latched in r0.req.
1147 # -- TLB updates write the entry at the end of the second cycle.
1148 # tlb_read : process(clk)
1150 # Operates in the second cycle on the request latched in r0.req.
1151 # TLB updates write the entry at the end of the second cycle.
1152 class TLBRead(Elaboratable
):
1156 def elaborate(self
, platform
):
1162 # variable index : tlb_index_t;
1163 # variable addrbits :
1164 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
1166 addrbits
= Signal(TLB_SET_BITS
)
1172 # if rising_edge(clk) then
1173 # if m_in.valid = '1' then
1174 with m
.If(m_in
.valid
):
1175 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
1176 # - 1 downto TLB_LG_PGSZ);
1177 sync
+= addrbits
.eq(m_in
.addr
[
1178 TLB_LG_PGSZ
:TLB_LG_PGSZ
+ TLB_SET_BITS
1182 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
1183 # - 1 downto TLB_LG_PGSZ);
1184 sync
+= addrbits
.eq(d_in
.addr
[
1185 TLB_LG_PGSZ
:TLB_LG_PGSZ
+ TLB_SET_BITS
1189 # index := to_integer(unsigned(addrbits));
1190 sync
+= index
.eq(addrbits
)
1191 # -- If we have any op and the previous op isn't finished,
1192 # -- then keep the same output for next cycle.
1193 # if r0_stall = '0' then
1194 # If we have any op and the previous op isn't finished,
1195 # then keep the same output for next cycle.
1196 with m
.If(~r0_stall
):
1197 sync
+= tlb_valid_way
.eq(dtlb_valids
[index
])
1198 sync
+= tlb_tag_way
.eq(dtlb_tags
[index
])
1199 sync
+= tlb_pte_way
.eq(dtlb_ptes
[index
])
1204 # -- Generate TLB PLRUs
1205 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
1206 # Generate TLB PLRUs
1207 class MaybeTLBPLRUs(Elaboratable
):
1211 def elaborate(self
, platform
):
1217 with m
.If(TLB_NUM_WAYS
> 1):
1219 # TODO understand how to conver generate statements
1220 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
1221 # -- TLB PLRU interface
1222 # signal tlb_plru_acc :
1223 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
1224 # signal tlb_plru_acc_en : std_ulogic;
1225 # signal tlb_plru_out :
1226 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
1228 # tlb_plru : entity work.plru
1230 # BITS => TLB_WAY_BITS
1235 # acc => tlb_plru_acc,
1236 # acc_en => tlb_plru_acc_en,
1237 # lru => tlb_plru_out
1243 # if r1.tlb_hit_index = i then
1244 # tlb_plru_acc_en <= r1.tlb_hit;
1246 # tlb_plru_acc_en <= '0';
1249 # std_ulogic_vector(to_unsigned(
1250 # r1.tlb_hit_way, TLB_WAY_BITS
1252 # tlb_plru_victim(i) <= tlb_plru_out;
1258 # tlb_search : process(all)
1259 class TLBSearch(Elaboratable
):
1263 def elborate(self
, platform
):
1269 # variable hitway : tlb_way_t;
1270 # variable hit : std_ulogic;
1271 # variable eatag : tlb_tag_t;
1282 # to_integer(unsigned(r0.req.addr(
1283 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
1287 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
1288 # for i in tlb_way_t loop
1289 # if tlb_valid_way(i) = '1' and
1290 # read_tlb_tag(i, tlb_tag_way) = eatag then
1295 # tlb_hit <= hit and r0_valid;
1296 # tlb_hit_way <= hitway;
1297 comb
+= tlb_req_index
.eq(r0
.req
.addr
[
1298 TLB_LG_PGSZ
:TLB_LG_PGSZ
+ TLB_SET_BITS
1301 comb
+= eatag
.eq(r0
.req
.addr
[
1302 TLB_LG_PGSZ
+ TLB_SET_BITS
:64
1306 with m
.If(tlb_valid_way(i
)
1307 & read_tlb_tag(i
, tlb_tag_way
) == eatag
):
1309 comb
+= hitway
.eq(i
)
1312 comb
+= tlb_hit
.eq(hit
& r0_valid
)
1313 comb
+= tlb_hit_way
.eq(hitway
)
1315 # if tlb_hit = '1' then
1317 # pte <= read_tlb_pte(hitway, tlb_pte_way);
1318 comb
+= pte
.eq(read_tlb_pte(hitway
, tlb_pte_way
))
1321 # pte <= (others => '0');
1324 # valid_ra <= tlb_hit or not r0.req.virt_mode;
1325 comb
+= valid_ra
.eq(tlb_hit | ~r0
.req
.virt_mode
)
1326 # if r0.req.virt_mode = '1' then
1327 with m
.If(r0
.req
.virt_mode
):
1328 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
1329 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
1330 # (ROW_OFF_BITS-1 downto 0 => '0');
1331 # perm_attr <= extract_perm_attr(pte);
1333 Const(ROW_OFF_BITS
, ROW_OFF_BITS
),
1334 r0
.req
.addr
[ROW_OFF_BITS
:TLB_LG_PGSZ
],
1335 pte
[TLB_LG_PGSZ
:REAL_ADDR_BITS
]
1337 comb
+= perm_attr
.eq(extract_perm_attr(pte
))
1340 # ra <= r0.req.addr(
1341 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
1342 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
1344 Const(ROW_OFF_BITS
, ROW_OFF_BITS
),
1345 r0
.rq
.addr
[ROW_OFF_BITS
:REAL_ADDR_BITS
]
1348 # perm_attr <= real_mode_perm_attr;
1349 comb
+= perm_attr
.eq(real_mode_perm_attr
)
1353 # tlb_update : process(clk)
1354 class TLBUpdate(Elaboratable
):
1358 def elaborate(self
, platform
):
1364 # variable tlbie : std_ulogic;
1365 # variable tlbwe : std_ulogic;
1366 # variable repl_way : tlb_way_t;
1367 # variable eatag : tlb_tag_t;
1368 # variable tagset : tlb_way_tags_t;
1369 # variable pteset : tlb_way_ptes_t;
1374 tagset
= TLBWayTags()
1375 pteset
= TLBWayPtes()
1385 # if rising_edge(clk) then
1386 # tlbie := r0_valid and r0.tlbie;
1387 # tlbwe := r0_valid and r0.tlbldoi;
1388 sync
+= tlbie
.eq(r0_valid
& r0
.tlbie
)
1389 sync
+= tlbwe
.eq(r0_valid
& r0
.tlbldoi
)
1391 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
1392 # with m.If (TODO understand how signal resets work in nmigen)
1393 # -- clear all valid bits at once
1394 # for i in tlb_index_t loop
1395 # dtlb_valids(i) <= (others => '0');
1397 # clear all valid bits at once
1398 for i
in range(TLBIndex()):
1399 sync
+= dtlb_valids
[i
].eq(0)
1400 # elsif tlbie = '1' then
1402 # if tlb_hit = '1' then
1404 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
1405 sync
+= dtlb_valids
[tlb_req_index
][tlb_hit_way
].eq(0)
1407 # elsif tlbwe = '1' then
1409 # if tlb_hit = '1' then
1411 # repl_way := tlb_hit_way;
1412 sync
+= repl_way
.eq(tlb_hit_way
)
1415 # repl_way := to_integer(unsigned(
1416 # tlb_plru_victim(tlb_req_index)));
1417 sync
+= repl_way
.eq(tlb_plru_victim
[tlb_req_index
])
1419 # eatag := r0.req.addr(
1420 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
1422 # tagset := tlb_tag_way;
1423 # write_tlb_tag(repl_way, tagset, eatag);
1424 # dtlb_tags(tlb_req_index) <= tagset;
1425 # pteset := tlb_pte_way;
1426 # write_tlb_pte(repl_way, pteset, r0.req.data);
1427 # dtlb_ptes(tlb_req_index) <= pteset;
1428 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
1429 sync
+= eatag
.eq(r0
.req
.addr
[TLB_LG_PGSZ
+ TLB_SET_BITS
:64])
1430 sync
+= tagset
.eq(tlb_tag_way
)
1431 sync
+= write_tlb_tag(repl_way
, tagset
, eatag
)
1432 sync
+= dtlb_tags
[tlb_req_index
].eq(tagset
)
1433 sync
+= pteset
.eq(tlb_pte_way
)
1434 sync
+= write_tlb_pte(repl_way
, pteset
, r0
.req
.data
)
1435 sync
+= dtlb_ptes
[tlb_req_index
].eq(pteset
)
1436 sync
+= dtlb_valids
[tlb_req_index
][repl_way
].eq(1)
1442 # maybe_plrus: if NUM_WAYS > 1 generate
1443 class MaybePLRUs(Elaboratable
):
1447 def elaborate(self
, platform
):
1454 # TODO learn translation of generate into nmgien @lkcl
1455 # plrus: for i in 0 to NUM_LINES-1 generate
1457 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
1458 # signal plru_acc_en : std_ulogic;
1459 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
1462 # TODO learn tranlation of entity, generic map, port map in
1464 # plru : entity work.plru
1472 # acc_en => plru_acc_en,
1479 # if r1.hit_index = i then
1480 # plru_acc_en <= r1.cache_hit;
1482 # plru_acc_en <= '0';
1484 # plru_acc <= std_ulogic_vector(to_unsigned(
1485 # r1.hit_way, WAY_BITS
1487 # plru_victim(i) <= plru_out;
1492 # -- Cache tag RAM read port
1493 # cache_tag_read : process(clk)
1494 # variable index : index_t;
1496 # if rising_edge(clk) then
1497 # if r0_stall = '1' then
1498 # index := req_index;
1499 # elsif m_in.valid = '1' then
1500 # index := get_index(m_in.addr);
1502 # index := get_index(d_in.addr);
1504 # cache_tag_set <= cache_tags(index);
1508 # -- Cache request parsing and hit detection
1509 # dcache_request : process(all)
1510 # variable is_hit : std_ulogic;
1511 # variable hit_way : way_t;
1512 # variable op : op_t;
1513 # variable opsel : std_ulogic_vector(2 downto 0);
1514 # variable go : std_ulogic;
1515 # variable nc : std_ulogic;
1516 # variable s_hit : std_ulogic;
1517 # variable s_tag : cache_tag_t;
1518 # variable s_pte : tlb_pte_t;
1519 # variable s_ra : std_ulogic_vector(
1520 # REAL_ADDR_BITS - 1 downto 0
1522 # variable hit_set : std_ulogic_vector(
1523 # TLB_NUM_WAYS - 1 downto 0
1525 # variable hit_way_set : hit_way_set_t;
1526 # variable rel_matches : std_ulogic_vector(
1527 # TLB_NUM_WAYS - 1 downto 0
1529 # variable rel_match : std_ulogic;
1531 # -- Extract line, row and tag from request
1532 # req_index <= get_index(r0.req.addr);
1533 # req_row <= get_row(r0.req.addr);
1534 # req_tag <= get_tag(ra);
1536 # go := r0_valid and not (r0.tlbie or r0.tlbld)
1537 # and not r1.ls_error;
1539 # -- Test if pending request is a hit on any way
1540 # -- In order to make timing in virtual mode,
1541 # -- when we are using the TLB, we compare each
1542 # --way with each of the real addresses from each way of
1543 # -- the TLB, and then decide later which match to use.
1547 # if r0.req.virt_mode = '1' then
1548 # rel_matches := (others => '0');
1549 # for j in tlb_way_t loop
1550 # hit_way_set(j) := 0;
1552 # s_pte := read_tlb_pte(j, tlb_pte_way);
1554 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
1555 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
1556 # s_tag := get_tag(s_ra);
1557 # for i in way_t loop
1558 # if go = '1' and cache_valids(req_index)(i) = '1'
1559 # and read_tag(i, cache_tag_set) = s_tag
1560 # and tlb_valid_way(j) = '1' then
1561 # hit_way_set(j) := i;
1565 # hit_set(j) := s_hit;
1566 # if s_tag = r1.reload_tag then
1567 # rel_matches(j) := '1';
1570 # if tlb_hit = '1' then
1571 # is_hit := hit_set(tlb_hit_way);
1572 # hit_way := hit_way_set(tlb_hit_way);
1573 # rel_match := rel_matches(tlb_hit_way);
1576 # s_tag := get_tag(r0.req.addr);
1577 # for i in way_t loop
1578 # if go = '1' and cache_valids(req_index)(i) = '1' and
1579 # read_tag(i, cache_tag_set) = s_tag then
1584 # if s_tag = r1.reload_tag then
1588 # req_same_tag <= rel_match;
1590 # -- See if the request matches the line currently being reloaded
1591 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
1592 # and rel_match = '1' then
1593 # -- For a store, consider this a hit even if the row isn't
1594 # -- valid since it will be by the time we perform the store.
1595 # -- For a load, check the appropriate row valid bit.
1597 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
1598 # hit_way := replace_way;
1601 # -- Whether to use forwarded data for a load or not
1602 # use_forward1_next <= '0';
1603 # if get_row(r1.req.real_addr) = req_row
1604 # and r1.req.hit_way = hit_way then
1605 # -- Only need to consider r1.write_bram here, since if we
1606 # -- are writing refill data here, then we don't have a
1607 # -- cache hit this cycle on the line being refilled.
1608 # -- (There is the possibility that the load following the
1609 # -- load miss that started the refill could be to the old
1610 # -- contents of the victim line, since it is a couple of
1611 # -- cycles after the refill starts before we see the updated
1612 # -- cache tag. In that case we don't use the bypass.)
1613 # use_forward1_next <= r1.write_bram;
1615 # use_forward2_next <= '0';
1616 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
1617 # use_forward2_next <= r1.forward_valid1;
1620 # -- The way that matched on a hit
1621 # req_hit_way <= hit_way;
1623 # -- The way to replace on a miss
1624 # if r1.write_tag = '1' then
1625 # replace_way <= to_integer(unsigned(
1626 # plru_victim(r1.store_index)
1629 # replace_way <= r1.store_way;
1632 # -- work out whether we have permission for this access
1633 # -- NB we don't yet implement AMR, thus no KUAP
1634 # rc_ok <= perm_attr.reference and
1635 # (r0.req.load or perm_attr.changed);
1636 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
1637 # (perm_attr.wr_perm or (r0.req.load
1638 # and perm_attr.rd_perm));
1639 # access_ok <= valid_ra and perm_ok and rc_ok;
1641 # -- Combine the request and cache hit status to decide what
1642 # -- operation needs to be done
1644 # nc := r0.req.nc or perm_attr.nocache;
1647 # if access_ok = '0' then
1649 # elsif cancel_store = '1' then
1650 # op := OP_STCX_FAIL;
1652 # opsel := r0.req.load & nc & is_hit;
1654 # when "101" => op := OP_LOAD_HIT;
1655 # when "100" => op := OP_LOAD_MISS;
1656 # when "110" => op := OP_LOAD_NC;
1657 # when "001" => op := OP_STORE_HIT;
1658 # when "000" => op := OP_STORE_MISS;
1659 # when "010" => op := OP_STORE_MISS;
1660 # when "011" => op := OP_BAD;
1661 # when "111" => op := OP_BAD;
1662 # when others => op := OP_NONE;
1669 # -- Version of the row number that is valid one cycle earlier
1670 # -- in the cases where we need to read the cache data BRAM.
1671 # -- If we're stalling then we need to keep reading the last
1673 # if r0_stall = '0' then
1674 # if m_in.valid = '1' then
1675 # early_req_row <= get_row(m_in.addr);
1677 # early_req_row <= get_row(d_in.addr);
1680 # early_req_row <= req_row;
1684 # -- Wire up wishbone request latch out of stage 1
1685 # wishbone_out <= r1.wb;
1687 # -- Handle load-with-reservation and store-conditional instructions
1688 # reservation_comb: process(all)
1690 # cancel_store <= '0';
1692 # clear_rsrv <= '0';
1693 # if r0_valid = '1' and r0.req.reserve = '1' then
1694 # -- XXX generate alignment interrupt if address
1695 # -- is not aligned XXX or if r0.req.nc = '1'
1696 # if r0.req.load = '1' then
1697 # -- load with reservation
1700 # -- store conditional
1701 # clear_rsrv <= '1';
1702 # if reservation.valid = '0' or r0.req.addr(63
1703 # downto LINE_OFF_BITS) /= reservation.addr then
1704 # cancel_store <= '1';
1710 # reservation_reg: process(clk)
1712 # if rising_edge(clk) then
1714 # reservation.valid <= '0';
1715 # elsif r0_valid = '1' and access_ok = '1' then
1716 # if clear_rsrv = '1' then
1717 # reservation.valid <= '0';
1718 # elsif set_rsrv = '1' then
1719 # reservation.valid <= '1';
1720 # reservation.addr <=
1721 # r0.req.addr(63 downto LINE_OFF_BITS);
1727 # -- Return data for loads & completion control logic
1729 # writeback_control: process(all)
1730 # variable data_out : std_ulogic_vector(63 downto 0);
1731 # variable data_fwd : std_ulogic_vector(63 downto 0);
1732 # variable j : integer;
1734 # -- Use the bypass if are reading the row that was
1735 # -- written 1 or 2 cycles ago, including for the
1736 # -- slow_valid = 1 case (i.e. completing a load
1737 # -- miss or a non-cacheable load).
1738 # if r1.use_forward1 = '1' then
1739 # data_fwd := r1.forward_data1;
1741 # data_fwd := r1.forward_data2;
1743 # data_out := cache_out(r1.hit_way);
1744 # for i in 0 to 7 loop
1746 # if r1.forward_sel(i) = '1' then
1747 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1751 # d_out.valid <= r1.ls_valid;
1752 # d_out.data <= data_out;
1753 # d_out.store_done <= not r1.stcx_fail;
1754 # d_out.error <= r1.ls_error;
1755 # d_out.cache_paradox <= r1.cache_paradox;
1758 # m_out.done <= r1.mmu_done;
1759 # m_out.err <= r1.mmu_error;
1760 # m_out.data <= data_out;
1762 # -- We have a valid load or store hit or we just completed
1763 # -- a slow op such as a load miss, a NC load or a store
1765 # -- Note: the load hit is delayed by one cycle. However it
1766 # -- can still not collide with r.slow_valid (well unless I
1767 # -- miscalculated) because slow_valid can only be set on a
1768 # -- subsequent request and not on its first cycle (the state
1769 # -- machine must have advanced), which makes slow_valid
1770 # -- at least 2 cycles from the previous hit_load_valid.
1772 # -- Sanity: Only one of these must be set in any given cycle
1773 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1774 # report "unexpected slow_valid collision with stcx_fail"
1776 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1777 # /= '1' report "unexpected hit_load_delayed collision with
1778 # slow_valid" severity FAILURE;
1780 # if r1.mmu_req = '0' then
1781 # -- Request came from loadstore1...
1782 # -- Load hit case is the standard path
1783 # if r1.hit_load_valid = '1' then
1785 # "completing load hit data=" & to_hstring(data_out);
1788 # -- error cases complete without stalling
1789 # if r1.ls_error = '1' then
1790 # report "completing ld/st with error";
1793 # -- Slow ops (load miss, NC, stores)
1794 # if r1.slow_valid = '1' then
1796 # "completing store or load miss data="
1797 # & to_hstring(data_out);
1801 # -- Request came from MMU
1802 # if r1.hit_load_valid = '1' then
1803 # report "completing load hit to MMU, data="
1804 # & to_hstring(m_out.data);
1807 # -- error cases complete without stalling
1808 # if r1.mmu_error = '1' then
1809 # report "completing MMU ld with error";
1812 # -- Slow ops (i.e. load miss)
1813 # if r1.slow_valid = '1' then
1814 # report "completing MMU load miss, data="
1815 # & to_hstring(m_out.data);
1822 # -- Generate a cache RAM for each way. This handles the normal
1823 # -- reads, writes from reloads and the special store-hit update
1826 # -- Note: the BRAMs have an extra read buffer, meaning the output
1827 # -- is pipelined an extra cycle. This differs from the
1828 # -- icache. The writeback logic needs to take that into
1829 # -- account by using 1-cycle delayed signals for load hits.
1831 # rams: for i in 0 to NUM_WAYS-1 generate
1832 # signal do_read : std_ulogic;
1833 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1834 # signal do_write : std_ulogic;
1835 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1837 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1838 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1839 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1840 # signal dout : cache_row_t;
1842 # way: entity work.cache_ram
1844 # ROW_BITS => ROW_BITS,
1845 # WIDTH => wishbone_data_bits,
1851 # rd_addr => rd_addr,
1853 # wr_sel => wr_sel_m,
1854 # wr_addr => wr_addr,
1855 # wr_data => wr_data
1859 # -- Cache hit reads
1862 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1863 # cache_out(i) <= dout;
1867 # -- Defaults to wishbone read responses (cache refill)
1869 # -- For timing, the mux on wr_data/sel/addr is not
1870 # -- dependent on anything other than the current state.
1871 # wr_sel_m <= (others => '0');
1874 # if r1.write_bram = '1' then
1875 # -- Write store data to BRAM. This happens one
1876 # -- cycle after the store is in r0.
1877 # wr_data <= r1.req.data;
1878 # wr_sel <= r1.req.byte_sel;
1879 # wr_addr <= std_ulogic_vector(to_unsigned(
1880 # get_row(r1.req.real_addr), ROW_BITS
1882 # if i = r1.req.hit_way then
1886 # -- Otherwise, we might be doing a reload or a DCBZ
1887 # if r1.dcbz = '1' then
1888 # wr_data <= (others => '0');
1890 # wr_data <= wishbone_in.dat;
1892 # wr_addr <= std_ulogic_vector(to_unsigned(
1893 # r1.store_row, ROW_BITS
1895 # wr_sel <= (others => '1');
1897 # if r1.state = RELOAD_WAIT_ACK and
1898 # wishbone_in.ack = '1' and replace_way = i then
1903 # -- Mask write selects with do_write since BRAM
1904 # -- doesn't have a global write-enable
1905 # if do_write = '1' then
1906 # wr_sel_m <= wr_sel;
1912 # -- Cache hit synchronous machine for the easy case.
1913 # -- This handles load hits.
1914 # -- It also handles error cases (TLB miss, cache paradox)
1915 # dcache_fast_hit : process(clk)
1917 # if rising_edge(clk) then
1918 # if req_op /= OP_NONE then
1919 # report "op:" & op_t'image(req_op) &
1920 # " addr:" & to_hstring(r0.req.addr) &
1921 # " nc:" & std_ulogic'image(r0.req.nc) &
1922 # " idx:" & integer'image(req_index) &
1923 # " tag:" & to_hstring(req_tag) &
1924 # " way: " & integer'image(req_hit_way);
1926 # if r0_valid = '1' then
1927 # r1.mmu_req <= r0.mmu_req;
1930 # -- Fast path for load/store hits.
1931 # -- Set signals for the writeback controls.
1932 # r1.hit_way <= req_hit_way;
1933 # r1.hit_index <= req_index;
1934 # if req_op = OP_LOAD_HIT then
1935 # r1.hit_load_valid <= '1';
1937 # r1.hit_load_valid <= '0';
1939 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1940 # r1.cache_hit <= '1';
1942 # r1.cache_hit <= '0';
1945 # if req_op = OP_BAD then
1946 # report "Signalling ld/st error valid_ra=" &
1947 # std_ulogic'image(valid_ra) & " rc_ok=" &
1948 # std_ulogic'image(rc_ok) & " perm_ok=" &
1949 # std_ulogic'image(perm_ok);
1950 # r1.ls_error <= not r0.mmu_req;
1951 # r1.mmu_error <= r0.mmu_req;
1952 # r1.cache_paradox <= access_ok;
1954 # r1.ls_error <= '0';
1955 # r1.mmu_error <= '0';
1956 # r1.cache_paradox <= '0';
1959 # if req_op = OP_STCX_FAIL then
1960 # r1.stcx_fail <= '1';
1962 # r1.stcx_fail <= '0';
1965 # -- Record TLB hit information for updating TLB PLRU
1966 # r1.tlb_hit <= tlb_hit;
1967 # r1.tlb_hit_way <= tlb_hit_way;
1968 # r1.tlb_hit_index <= tlb_req_index;
1973 # -- Memory accesses are handled by this state machine:
1975 # -- * Cache load miss/reload (in conjunction with "rams")
1976 # -- * Load hits for non-cachable forms
1977 # -- * Stores (the collision case is handled in "rams")
1979 # -- All wishbone requests generation is done here.
1980 # -- This machine operates at stage 1.
1981 # dcache_slow : process(clk)
1982 # variable stbs_done : boolean;
1983 # variable req : mem_access_request_t;
1984 # variable acks : unsigned(2 downto 0);
1986 # if rising_edge(clk) then
1987 # r1.use_forward1 <= use_forward1_next;
1988 # r1.forward_sel <= (others => '0');
1989 # if use_forward1_next = '1' then
1990 # r1.forward_sel <= r1.req.byte_sel;
1991 # elsif use_forward2_next = '1' then
1992 # r1.forward_sel <= r1.forward_sel1;
1995 # r1.forward_data2 <= r1.forward_data1;
1996 # if r1.write_bram = '1' then
1997 # r1.forward_data1 <= r1.req.data;
1998 # r1.forward_sel1 <= r1.req.byte_sel;
1999 # r1.forward_way1 <= r1.req.hit_way;
2000 # r1.forward_row1 <= get_row(r1.req.real_addr);
2001 # r1.forward_valid1 <= '1';
2003 # if r1.dcbz = '1' then
2004 # r1.forward_data1 <= (others => '0');
2006 # r1.forward_data1 <= wishbone_in.dat;
2008 # r1.forward_sel1 <= (others => '1');
2009 # r1.forward_way1 <= replace_way;
2010 # r1.forward_row1 <= r1.store_row;
2011 # r1.forward_valid1 <= '0';
2014 # -- On reset, clear all valid bits to force misses
2016 # for i in index_t loop
2017 # cache_valids(i) <= (others => '0');
2021 # r1.slow_valid <= '0';
2024 # r1.ls_valid <= '0';
2025 # r1.mmu_done <= '0';
2027 # -- Not useful normally but helps avoiding
2028 # -- tons of sim warnings
2029 # r1.wb.adr <= (others => '0');
2031 # -- One cycle pulses reset
2032 # r1.slow_valid <= '0';
2033 # r1.write_bram <= '0';
2034 # r1.inc_acks <= '0';
2035 # r1.dec_acks <= '0';
2037 # r1.ls_valid <= '0';
2038 # -- complete tlbies and TLB loads in the third cycle
2039 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
2040 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
2041 # if r0.mmu_req = '0' then
2042 # r1.ls_valid <= '1';
2044 # r1.mmu_done <= '1';
2048 # if r1.write_tag = '1' then
2049 # -- Store new tag in selected way
2050 # for i in 0 to NUM_WAYS-1 loop
2051 # if i = replace_way then
2052 # cache_tags(r1.store_index)(
2053 # (i + 1) * TAG_WIDTH - 1
2054 # downto i * TAG_WIDTH
2056 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
2060 # r1.store_way <= replace_way;
2061 # r1.write_tag <= '0';
2064 # -- Take request from r1.req if there is one there,
2065 # -- else from req_op, ra, etc.
2066 # if r1.full = '1' then
2070 # req.valid := req_go;
2071 # req.mmu_req := r0.mmu_req;
2072 # req.dcbz := r0.req.dcbz;
2073 # req.real_addr := ra;
2074 # -- Force data to 0 for dcbz
2075 # if r0.req.dcbz = '0' then
2076 # req.data := r0.req.data;
2078 # req.data := (others => '0');
2080 # -- Select all bytes for dcbz
2081 # -- and for cacheable loads
2082 # if r0.req.dcbz = '1'
2083 # or (r0.req.load = '1' and r0.req.nc = '0') then
2084 # req.byte_sel := (others => '1');
2086 # req.byte_sel := r0.req.byte_sel;
2088 # req.hit_way := req_hit_way;
2089 # req.same_tag := req_same_tag;
2091 # -- Store the incoming request from r0,
2092 # -- if it is a slow request
2093 # -- Note that r1.full = 1 implies req_op = OP_NONE
2094 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
2095 # or req_op = OP_STORE_MISS
2096 # or req_op = OP_STORE_HIT then
2102 # -- Main state machine
2105 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
2106 # r1.wb.sel <= req.byte_sel;
2107 # r1.wb.dat <= req.data;
2108 # r1.dcbz <= req.dcbz;
2110 # -- Keep track of our index and way
2111 # -- for subsequent stores.
2112 # r1.store_index <= get_index(req.real_addr);
2113 # r1.store_row <= get_row(req.real_addr);
2115 # get_row_of_line(get_row(req.real_addr)) - 1;
2116 # r1.reload_tag <= get_tag(req.real_addr);
2117 # r1.req.same_tag <= '1';
2119 # if req.op = OP_STORE_HIT then
2120 # r1.store_way <= req.hit_way;
2123 # -- Reset per-row valid bits,
2124 # -- ready for handling OP_LOAD_MISS
2125 # for i in 0 to ROW_PER_LINE - 1 loop
2126 # r1.rows_valid(i) <= '0';
2130 # when OP_LOAD_HIT =>
2131 # -- stay in IDLE state
2133 # when OP_LOAD_MISS =>
2134 # -- Normal load cache miss,
2135 # -- start the reload machine
2136 # report "cache miss real addr:" &
2137 # to_hstring(req.real_addr) & " idx:" &
2138 # integer'image(get_index(req.real_addr)) &
2139 # " tag:" & to_hstring(get_tag(req.real_addr));
2141 # -- Start the wishbone cycle
2146 # -- Track that we had one request sent
2147 # r1.state <= RELOAD_WAIT_ACK;
2148 # r1.write_tag <= '1';
2150 # when OP_LOAD_NC =>
2154 # r1.state <= NC_LOAD_WAIT_ACK;
2156 # when OP_STORE_HIT | OP_STORE_MISS =>
2157 # if req.dcbz = '0' then
2158 # r1.state <= STORE_WAIT_ACK;
2159 # r1.acks_pending <= to_unsigned(1, 3);
2161 # r1.slow_valid <= '1';
2162 # if req.mmu_req = '0' then
2163 # r1.ls_valid <= '1';
2165 # r1.mmu_done <= '1';
2167 # if req.op = OP_STORE_HIT then
2168 # r1.write_bram <= '1';
2171 # -- dcbz is handled much like a load
2172 # -- miss except that we are writing
2173 # -- to memory instead of reading
2174 # r1.state <= RELOAD_WAIT_ACK;
2175 # if req.op = OP_STORE_MISS then
2176 # r1.write_tag <= '1';
2183 # -- OP_NONE and OP_BAD do nothing
2184 # -- OP_BAD & OP_STCX_FAIL were handled above already
2187 # when OP_STCX_FAIL =>
2190 # when RELOAD_WAIT_ACK =>
2191 # -- Requests are all sent if stb is 0
2192 # stbs_done := r1.wb.stb = '0';
2194 # -- If we are still sending requests,
2195 # -- was one accepted?
2196 # if wishbone_in.stall = '0' and not stbs_done then
2197 # -- That was the last word ? We are done sending.
2198 # -- Clear stb and set stbs_done so we can handle
2199 # -- an eventual last ack on the same cycle.
2200 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
2202 # stbs_done := true;
2205 # -- Calculate the next row address
2206 # r1.wb.adr <= next_row_addr(r1.wb.adr);
2209 # -- Incoming acks processing
2210 # r1.forward_valid1 <= wishbone_in.ack;
2211 # if wishbone_in.ack = '1' then
2213 # r1.store_row mod ROW_PER_LINE
2215 # -- If this is the data we were looking for,
2216 # -- we can complete the request next cycle.
2217 # -- Compare the whole address in case the
2218 # -- request in r1.req is not the one that
2219 # -- started this refill.
2220 # if r1.full = '1' and r1.req.same_tag = '1'
2221 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
2222 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
2223 # and r1.store_row = get_row(r1.req.real_addr) then
2225 # r1.slow_valid <= '1';
2226 # if r1.mmu_req = '0' then
2227 # r1.ls_valid <= '1';
2229 # r1.mmu_done <= '1';
2231 # r1.forward_sel <= (others => '1');
2232 # r1.use_forward1 <= '1';
2235 # -- Check for completion
2236 # if stbs_done and is_last_row(r1.store_row,
2237 # r1.end_row_ix) then
2238 # -- Complete wishbone cycle
2241 # -- Cache line is now valid
2242 # cache_valids(r1.store_index)(
2249 # -- Increment store row counter
2250 # r1.store_row <= next_row(r1.store_row);
2253 # when STORE_WAIT_ACK =>
2254 # stbs_done := r1.wb.stb = '0';
2255 # acks := r1.acks_pending;
2256 # if r1.inc_acks /= r1.dec_acks then
2257 # if r1.inc_acks = '1' then
2263 # r1.acks_pending <= acks;
2264 # -- Clear stb when slave accepted request
2265 # if wishbone_in.stall = '0' then
2266 # -- See if there is another store waiting
2267 # -- to be done which is in the same real page.
2268 # if req.valid = '1' then
2270 # SET_SIZE_BITS - 1 downto 0
2271 # ) <= req.real_addr(
2272 # SET_SIZE_BITS - 1 downto 0
2274 # r1.wb.dat <= req.data;
2275 # r1.wb.sel <= req.byte_sel;
2277 # if acks < 7 and req.same_tag = '1'
2278 # and (req.op = OP_STORE_MISS
2279 # or req.op = OP_STORE_HIT) then
2281 # stbs_done := false;
2282 # if req.op = OP_STORE_HIT then
2283 # r1.write_bram <= '1';
2286 # r1.slow_valid <= '1';
2287 # -- Store requests never come from the MMU
2288 # r1.ls_valid <= '1';
2289 # stbs_done := false;
2290 # r1.inc_acks <= '1';
2293 # stbs_done := true;
2297 # -- Got ack ? See if complete.
2298 # if wishbone_in.ack = '1' then
2299 # if stbs_done and acks = 1 then
2304 # r1.dec_acks <= '1';
2307 # when NC_LOAD_WAIT_ACK =>
2308 # -- Clear stb when slave accepted request
2309 # if wishbone_in.stall = '0' then
2313 # -- Got ack ? complete.
2314 # if wishbone_in.ack = '1' then
2317 # r1.slow_valid <= '1';
2318 # if r1.mmu_req = '0' then
2319 # r1.ls_valid <= '1';
2321 # r1.mmu_done <= '1';
2323 # r1.forward_sel <= (others => '1');
2324 # r1.use_forward1 <= '1';
2333 # dc_log: if LOG_LENGTH > 0 generate
2334 # signal log_data : std_ulogic_vector(19 downto 0);
2336 # dcache_log: process(clk)
2338 # if rising_edge(clk) then
2339 # log_data <= r1.wb.adr(5 downto 3) &
2340 # wishbone_in.stall &
2342 # r1.wb.stb & r1.wb.cyc &
2345 # std_ulogic_vector(
2346 # to_unsigned(op_t'pos(req_op), 3)) &
2348 # std_ulogic_vector(
2349 # to_unsigned(tlb_hit_way, 3)) &
2351 # std_ulogic_vector(
2352 # to_unsigned(state_t'pos(r1.state), 3));
2355 # log_out <= log_data;