comments in dcache
[soc.git] / src / soc / experiment / dcache.py
1 """Dcache
2
3 based on Anton Blanchard microwatt dcache.vhdl
4
5 """
6
7 from enum import Enum, unique
8
9 from nmigen import Module, Signal, Elaboratable,
10 Cat, Repl
11 from nmigen.cli import main
12 from nmigen.iocontrol import RecordObject
13 from nmigen.util import log2_int
14
15 from experiment.mem_types import LoadStore1ToDcacheType,
16 DcacheToLoadStore1Type,
17 MmuToDcacheType,
18 DcacheToMmuType
19
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,
24 WBIOSlaveOut
25
26 # --
27 # -- Set associative dcache write-through
28 # --
29 # -- TODO (in no specific order):
30 # --
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...)
35 # --
36 # library ieee;
37 # use ieee.std_logic_1164.all;
38 # use ieee.numeric_std.all;
39 #
40 # library work;
41 # use work.utils.all;
42 # use work.common.all;
43 # use work.helpers.all;
44 # use work.wishbone_types.all;
45 #
46 # entity dcache is
47 class Dcache(Elaboratable):
48 # generic (
49 # -- Line size in bytes
50 # LINE_SIZE : positive := 64;
51 # -- Number of lines in a set
52 # NUM_LINES : positive := 32;
53 # -- Number of ways
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
63 # );
64 def __init__(self):
65 # Line size in bytes
66 self.LINE_SIZE = 64
67 # Number of lines in a set
68 self.NUM_LINES = 32
69 # Number of ways
70 self.NUM_WAYS = 4
71 # L1 DTLB entries per set
72 self.TLB_SET_SIZE = 64
73 # L1 DTLB number of sets
74 self.TLB_NUM_WAYS = 2
75 # L1 DTLB log_2(page_size)
76 self.TLB_LG_PGSZ = 12
77 # Non-zero to enable log data collection
78 self.LOG_LENGTH = 0
79 # port (
80 # clk : in std_ulogic;
81 # rst : in std_ulogic;
82 #
83 # d_in : in Loadstore1ToDcacheType;
84 # d_out : out DcacheToLoadstore1Type;
85 #
86 # m_in : in MmuToDcacheType;
87 # m_out : out DcacheToMmuType;
88 #
89 # stall_out : out std_ulogic;
90 #
91 # wishbone_out : out wishbone_master_out;
92 # wishbone_in : in wishbone_slave_out;
93 #
94 # log_out : out std_ulogic_vector(19 downto 0)
95 # );
96 self.d_in = LoadStore1ToDcacheType()
97 self.d_out = DcacheToLoadStore1Type()
98
99 self.m_in = MmuToDcacheType()
100 self.m_out = DcacheToMmuType()
101
102 self.stall_out = Signal()
103
104 self.wb_out = WBMasterOut()
105 self.wb_in = WBSlaveOut()
106
107 self.log_out = Signal(20)
108 # end entity dcache;
109
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
119
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"
124 # --
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"
132 # --
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;
136
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
149
150 # -- Bit fields counts in the address
151 #
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
171 # + INDEX_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
181
182 # REAL_ADDR_BITS is the number of real address
183 # bits that we store
184 REAL_ADDR_BITS = 56
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)
208
209 # -- Example of layout for 32 lines of 64 bytes:
210 # --
211 # -- .. tag |index| line |
212 # -- .. | row | |
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:
220 #
221 # .. tag |index| line |
222 # .. | row | |
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)
229
230
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);
235 def Row():
236 return Signal(BRAM_ROWS)
237
238 def Index():
239 return Signal(NUM_LINES)
240
241 def Way():
242 return Signal(NUM_WAYS)
243
244 def RowInLine():
245 return Signal(ROW_LINEBITS)
246
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
251 def CacheRow():
252 return Signal(WB_DATA_BITS)
253
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
265 def CacheTag():
266 return Signal(TAG_BITS)
267
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
277
278 def CacheTagSet():
279 return Signal(TAG_RAM_WIDTH)
280
281 def CacheTagArray():
282 return Array(CacheTagSet() for x in range(Index()))
283
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))
297
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;
302 #
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()
309
310 # TODO attribute ram_style : string;
311 # TODO attribute ram_style of cache_tags : signal is "distributed";
312
313 # -- L1 TLB.
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;
323 # L1 TLB
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
328 TLB_PTE_BITS = 64
329 TLB_PTE_WAY_BITS = TLB_NUM_WAYS * TLB_PTE_BITS;
330
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);
341 # type tlb_tags_t is
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;
349 def TLBWay():
350 return Signal(TLB_NUM_WAYS)
351
352 def TLBIndex():
353 return Signal(TLB_SET_SIZE)
354
355 def TLBWayValidBits():
356 return Signal(TLB_NUM_WAYS)
357
358 def TLBValidBits():
359 return Array(TLBValidBits() for x in range(TLBIndex()))
360
361 def TLBTag():
362 return Signal(TLB_EA_TAG_BITS)
363
364 def TLBWayTags():
365 return Signal(TLB_TAG_WAY_BITS)
366
367 def TLBTags():
368 return Array(TLBWayTags() for x in range (TLBIndex()))
369
370 def TLBPte():
371 return Signal(TLB_PTE_BITS)
372
373 def TLBWayPtes():
374 return Signal(TLB_PTE_WAY_BITS)
375
376 def TLBPtes():
377 return Array(TLBWayPtes() for x in range(TLBIndex()))
378
379 def HitWaySet():
380 return Array(Way() for x in range(TLBWay()))
381
382 # signal dtlb_valids : tlb_valids_t;
383 # signal dtlb_tags : tlb_tags_t;
384 # signal dtlb_ptes : tlb_ptes_t;
385
386 """note: these are passed to nmigen.hdl.Memory as "attributes". don't
387 know how, just that they are.
388 """
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";
396
397
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;
403 # priv : std_ulogic;
404 # rd_perm : std_ulogic;
405 # wr_perm : std_ulogic;
406 # end record;
407 # Record for storing permission, attribute, etc. bits from a PTE
408 class PermAttr(RecordObject):
409 def __init__(self):
410 super().__init__()
411 self.reference = Signal()
412 self.changed = Signal()
413 self.nocache = Signal()
414 self.priv = Signal()
415 self.rd_perm = Signal()
416 self.wr_perm = Signal()
417
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;
422 # begin
423 # pa.reference := pte(8);
424 # pa.changed := pte(7);
425 # pa.nocache := pte(5);
426 # pa.priv := pte(3);
427 # pa.rd_perm := pte(2);
428 # pa.wr_perm := pte(1);
429 # return pa;
430 # end;
431 def extract_perm_attr(pte=Signal(TLB_PTE_BITS)):
432 pa = PermAttr()
433 pa.reference = pte[8]
434 pa.changed = pte[7]
435 pa.nocache = pte[5]
436 pa.priv = pte[3]
437 pa.rd_perm = pte[2]
438 pa.wr_perm = pte[1]
439 return pa;
440
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
449
450 # -- Type of operation on a "valid" input
451 # type op_t is
452 # (
453 # OP_NONE,
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
461 # );
462 # Type of operation on a "valid" input
463 @unique
464 class OP(Enum):
465 OP_NONE = 0
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
473
474 # -- Cache state machine
475 # type state_t is
476 # (
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
481 # );
482 # Cache state machine
483 @unique
484 class State(Enum):
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
489
490 # -- Dcache operations:
491 # --
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.
495 # --
496 # -- Thus, the dcache has a 2-stage internal pipeline
497 # -- for cache hits with no stalls.
498 # --
499 # -- All other operations are handled via stalling
500 # -- in the first stage.
501 # --
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.
504 #
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
512 # end record;
513 # Dcache operations:
514 #
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.
518 #
519 # Thus, the dcache has a 2-stage internal pipeline
520 # for cache hits with no stalls.
521 #
522 # All other operations are handled via stalling
523 # in the first stage.
524 #
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.
527 #
528 # Stage 0 register, basically contains just the latched request
529 class RegStage0(RecordObject):
530 def __init__(self):
531 super().__init__()
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
537
538 # signal r0 : reg_stage_0_t;
539 # signal r0_full : std_ulogic;
540 r0 = RegStage0()
541 r0_full = Signal()
542
543 # type mem_access_request_t is record
544 # op : op_t;
545 # valid : std_ulogic;
546 # dcbz : 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);
550 # hit_way : way_t;
551 # same_tag : std_ulogic;
552 # mmu_req : std_ulogic;
553 # end record;
554 class MemAccessRequest(RecordObject):
555 def __init__(self):
556 super().__init__()
557 self.op = Op()
558 self.valid = Signal()
559 self.dcbz = Signal()
560 self.real_addr = Signal(REAL_ADDR_BITS)
561 self.data = Signal(64)
562 self.byte_sel = Signal(8)
563 self.hit_way = Way()
564 self.same_tag = Signal()
565 self.mmu_req = Signal()
566
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;
574 #
575 # -- Cache hit state
576 # hit_way : way_t;
577 # hit_load_valid : std_ulogic;
578 # hit_index : index_t;
579 # cache_hit : std_ulogic;
580 #
581 # -- TLB hit state
582 # tlb_hit : std_ulogic;
583 # tlb_hit_way : tlb_way_t;
584 # tlb_hit_index : tlb_index_t;
585 #
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);
595 #
596 # -- Cache miss state (reload state machine)
597 # state : state_t;
598 # dcbz : std_ulogic;
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;
604 # store_way : way_t;
605 # store_row : row_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;
612 #
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;
619 #
620 # -- Signal to complete a failed stcx.
621 # stcx_fail : std_ulogic;
622 # end record;
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):
626 def __init__(self):
627 super().__init__()
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()
632
633 # Cache hit state
634 self.hit_way = Way()
635 self.hit_load_valid = Signal()
636 self.hit_index = Index()
637 self.cache_hit = Signal()
638
639 # TLB hit state
640 self.tlb_hit = Signal()
641 self.tlb_hit_way = TLBWay()
642 self.tlb_hit_index = TLBIndex()
643 self.
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)
653
654 # Cache miss state (reload state machine)
655 self.state = State()
656 self.dcbz = Signal()
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()
670
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()
677
678 # Signal to complete a failed stcx.
679 self.stcx_fail = Signal()
680
681 # signal r1 : reg_stage_1_t;
682 r1 = RegStage1()
683
684 # -- Reservation information
685 # --
686 # type reservation_t is record
687 # valid : std_ulogic;
688 # addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
689 # end record;
690 # Reservation information
691
692 class Reservation(RecordObject):
693 def __init__(self):
694 super().__init__()
695 valid = Signal()
696 addr = Signal(63 downto LINE_OFF_BITS) # TODO LINE_OFF_BITS is 6
697
698 # signal reservation : reservation_t;
699 #
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;
709 #
710 # signal early_req_row : row_t;
711 #
712 # signal cancel_store : std_ulogic;
713 # signal set_rsrv : std_ulogic;
714 # signal clear_rsrv : std_ulogic;
715 #
716 # signal r0_valid : std_ulogic;
717 # signal r0_stall : std_ulogic;
718 #
719 # signal use_forward1_next : std_ulogic;
720 # signal use_forward2_next : std_ulogic;
721 #
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;
725 #
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;
731 #
732 # -- Wishbone read/write/cache write formatting signals
733 # signal bus_sel : std_ulogic_vector(7 downto 0);
734 #
735 # -- TLB signals
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;
749 #
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;
754 #
755 # --
756 # -- Helper functions to decode incoming requests
757 # --
758 #
759 # -- Return the cache line index (tag index) for an address
760 # function get_index(addr: std_ulogic_vector) return index_t is
761 # begin
762 # return to_integer(
763 # unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS))
764 # );
765 # end;
766 #
767 # -- Return the cache row index (data memory) for an address
768 # function get_row(addr: std_ulogic_vector) return row_t is
769 # begin
770 # return to_integer(
771 # unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS))
772 # );
773 # end;
774 #
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);
778 # begin
779 # row_v := to_unsigned(row, ROW_BITS);
780 # return row_v(ROW_LINEBITS-1 downto 0);
781 # end;
782 #
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
786 # begin
787 # return
788 # unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
789 # end;
790 #
791 # -- Returns whether this is the last row of a line
792 # function is_last_row(row: row_t; last: row_in_line_t)
793 # return boolean is
794 # begin
795 # return get_row_of_line(row) = last;
796 # end;
797 #
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;
803 # begin
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);
808 # result := addr;
809 # result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
810 # return result;
811 # end;
812 #
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);
821 # begin
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));
827 # end;
828 #
829 # -- Get the tag value from the address
830 # function get_tag(addr: std_ulogic_vector) return cache_tag_t is
831 # begin
832 # return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
833 # end;
834 #
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
838 # begin
839 # return tagset(way * TAG_WIDTH + TAG_BITS
840 # - 1 downto way * TAG_WIDTH);
841 # end;
842 #
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;
847 # begin
848 # j := way * TLB_EA_TAG_BITS;
849 # return tags(j + TLB_EA_TAG_BITS - 1 downto j);
850 # end;
851 #
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;
854 # tag: tlb_tag_t) is
855 # variable j : integer;
856 # begin
857 # j := way * TLB_EA_TAG_BITS;
858 # tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
859 # end;
860 #
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;
865 # begin
866 # j := way * TLB_PTE_BITS;
867 # return ptes(j + TLB_PTE_BITS - 1 downto j);
868 # end;
869 #
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;
873 # begin
874 # j := way * TLB_PTE_BITS;
875 # ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
876 # end;
877 #
878 # begin
879 #
880 """these, because they are constants, can actually be done *as*
881 python asserts:
882 assert LINE_SIZE % ROWSIZE == 0, "line size not ...."
883 """
884 # assert LINE_SIZE mod ROW_SIZE = 0
885 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
886 # assert ispow2(LINE_SIZE)
887 # report "LINE_SIZE not power of 2" severity FAILURE;
888 # assert ispow2(NUM_LINES)
889 # report "NUM_LINES not power of 2" severity FAILURE;
890 # assert ispow2(ROW_PER_LINE)
891 # report "ROW_PER_LINE not power of 2" severity FAILURE;
892 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
893 # report "geometry bits don't add up" severity FAILURE;
894 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
895 # report "geometry bits don't add up" severity FAILURE;
896 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
897 # report "geometry bits don't add up" severity FAILURE;
898 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
899 # report "geometry bits don't add up" severity FAILURE;
900 # assert (64 = wishbone_data_bits)
901 # report "Can't yet handle a wishbone width that isn't 64-bits"
902 # severity FAILURE;
903 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
904 # report "Set indexed by virtual address" severity FAILURE;
905 #
906 # -- Latch the request in r0.req as long as we're not stalling
907 # stage_0 : process(clk)
908 # variable r : reg_stage_0_t;
909 # begin
910 # if rising_edge(clk) then
911 # assert (d_in.valid and m_in.valid) = '0'
912 # report "request collision loadstore vs MMU";
913 # if m_in.valid = '1' then
914 # r.req.valid := '1';
915 # r.req.load := not (m_in.tlbie or m_in.tlbld);
916 # r.req.dcbz := '0';
917 # r.req.nc := '0';
918 # r.req.reserve := '0';
919 # r.req.virt_mode := '0';
920 # r.req.priv_mode := '1';
921 # r.req.addr := m_in.addr;
922 # r.req.data := m_in.pte;
923 # r.req.byte_sel := (others => '1');
924 # r.tlbie := m_in.tlbie;
925 # r.doall := m_in.doall;
926 # r.tlbld := m_in.tlbld;
927 # r.mmu_req := '1';
928 # else
929 # r.req := d_in;
930 # r.tlbie := '0';
931 # r.doall := '0';
932 # r.tlbld := '0';
933 # r.mmu_req := '0';
934 # end if;
935 # if rst = '1' then
936 # r0_full <= '0';
937 # elsif r1.full = '0' or r0_full = '0' then
938 # r0 <= r;
939 # r0_full <= r.req.valid;
940 # end if;
941 # end if;
942 # end process;
943 #
944 # -- we don't yet handle collisions between loadstore1 requests
945 # -- and MMU requests
946 # m_out.stall <= '0';
947 #
948 # -- Hold off the request in r0 when r1 has an uncompleted request
949 # r0_stall <= r0_full and r1.full;
950 # r0_valid <= r0_full and not r1.full;
951 # stall_out <= r0_stall;
952 #
953 # -- TLB
954 # -- Operates in the second cycle on the request latched in r0.req.
955 # -- TLB updates write the entry at the end of the second cycle.
956 # tlb_read : process(clk)
957 # variable index : tlb_index_t;
958 # variable addrbits :
959 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
960 # begin
961 # if rising_edge(clk) then
962 # if m_in.valid = '1' then
963 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
964 # - 1 downto TLB_LG_PGSZ);
965 # else
966 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
967 # - 1 downto TLB_LG_PGSZ);
968 # end if;
969 # index := to_integer(unsigned(addrbits));
970 # -- If we have any op and the previous op isn't finished,
971 # -- then keep the same output for next cycle.
972 # if r0_stall = '0' then
973 # tlb_valid_way <= dtlb_valids(index);
974 # tlb_tag_way <= dtlb_tags(index);
975 # tlb_pte_way <= dtlb_ptes(index);
976 # end if;
977 # end if;
978 # end process;
979 #
980 # -- Generate TLB PLRUs
981 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
982 # begin
983 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
984 # -- TLB PLRU interface
985 # signal tlb_plru_acc :
986 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
987 # signal tlb_plru_acc_en : std_ulogic;
988 # signal tlb_plru_out :
989 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
990 # begin
991 # tlb_plru : entity work.plru
992 # generic map (
993 # BITS => TLB_WAY_BITS
994 # )
995 # port map (
996 # clk => clk,
997 # rst => rst,
998 # acc => tlb_plru_acc,
999 # acc_en => tlb_plru_acc_en,
1000 # lru => tlb_plru_out
1001 # );
1002 #
1003 # process(all)
1004 # begin
1005 # -- PLRU interface
1006 # if r1.tlb_hit_index = i then
1007 # tlb_plru_acc_en <= r1.tlb_hit;
1008 # else
1009 # tlb_plru_acc_en <= '0';
1010 # end if;
1011 # tlb_plru_acc <=
1012 # std_ulogic_vector(to_unsigned(
1013 # r1.tlb_hit_way, TLB_WAY_BITS
1014 # ));
1015 # tlb_plru_victim(i) <= tlb_plru_out;
1016 # end process;
1017 # end generate;
1018 # end generate;
1019 #
1020 # tlb_search : process(all)
1021 # variable hitway : tlb_way_t;
1022 # variable hit : std_ulogic;
1023 # variable eatag : tlb_tag_t;
1024 # begin
1025 # tlb_req_index <=
1026 # to_integer(unsigned(r0.req.addr(
1027 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
1028 # )));
1029 # hitway := 0;
1030 # hit := '0';
1031 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
1032 # for i in tlb_way_t loop
1033 # if tlb_valid_way(i) = '1' and
1034 # read_tlb_tag(i, tlb_tag_way) = eatag then
1035 # hitway := i;
1036 # hit := '1';
1037 # end if;
1038 # end loop;
1039 # tlb_hit <= hit and r0_valid;
1040 # tlb_hit_way <= hitway;
1041 # if tlb_hit = '1' then
1042 # pte <= read_tlb_pte(hitway, tlb_pte_way);
1043 # else
1044 # pte <= (others => '0');
1045 # end if;
1046 # valid_ra <= tlb_hit or not r0.req.virt_mode;
1047 # if r0.req.virt_mode = '1' then
1048 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
1049 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
1050 # (ROW_OFF_BITS-1 downto 0 => '0');
1051 # perm_attr <= extract_perm_attr(pte);
1052 # else
1053 # ra <= r0.req.addr(
1054 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
1055 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
1056 # perm_attr <= real_mode_perm_attr;
1057 # end if;
1058 # end process;
1059 #
1060 # tlb_update : process(clk)
1061 # variable tlbie : std_ulogic;
1062 # variable tlbwe : std_ulogic;
1063 # variable repl_way : tlb_way_t;
1064 # variable eatag : tlb_tag_t;
1065 # variable tagset : tlb_way_tags_t;
1066 # variable pteset : tlb_way_ptes_t;
1067 # begin
1068 # if rising_edge(clk) then
1069 # tlbie := r0_valid and r0.tlbie;
1070 # tlbwe := r0_valid and r0.tlbld;
1071 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
1072 # -- clear all valid bits at once
1073 # for i in tlb_index_t loop
1074 # dtlb_valids(i) <= (others => '0');
1075 # end loop;
1076 # elsif tlbie = '1' then
1077 # if tlb_hit = '1' then
1078 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
1079 # end if;
1080 # elsif tlbwe = '1' then
1081 # if tlb_hit = '1' then
1082 # repl_way := tlb_hit_way;
1083 # else
1084 # repl_way := to_integer(unsigned(
1085 # tlb_plru_victim(tlb_req_index)));
1086 # end if;
1087 # eatag := r0.req.addr(
1088 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
1089 # );
1090 # tagset := tlb_tag_way;
1091 # write_tlb_tag(repl_way, tagset, eatag);
1092 # dtlb_tags(tlb_req_index) <= tagset;
1093 # pteset := tlb_pte_way;
1094 # write_tlb_pte(repl_way, pteset, r0.req.data);
1095 # dtlb_ptes(tlb_req_index) <= pteset;
1096 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
1097 # end if;
1098 # end if;
1099 # end process;
1100 #
1101 # -- Generate PLRUs
1102 # maybe_plrus: if NUM_WAYS > 1 generate
1103 # begin
1104 # plrus: for i in 0 to NUM_LINES-1 generate
1105 # -- PLRU interface
1106 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
1107 # signal plru_acc_en : std_ulogic;
1108 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
1109 #
1110 # begin
1111 # plru : entity work.plru
1112 # generic map (
1113 # BITS => WAY_BITS
1114 # )
1115 # port map (
1116 # clk => clk,
1117 # rst => rst,
1118 # acc => plru_acc,
1119 # acc_en => plru_acc_en,
1120 # lru => plru_out
1121 # );
1122 #
1123 # process(all)
1124 # begin
1125 # -- PLRU interface
1126 # if r1.hit_index = i then
1127 # plru_acc_en <= r1.cache_hit;
1128 # else
1129 # plru_acc_en <= '0';
1130 # end if;
1131 # plru_acc <= std_ulogic_vector(to_unsigned(
1132 # r1.hit_way, WAY_BITS
1133 # ));
1134 # plru_victim(i) <= plru_out;
1135 # end process;
1136 # end generate;
1137 # end generate;
1138 #
1139 # -- Cache tag RAM read port
1140 # cache_tag_read : process(clk)
1141 # variable index : index_t;
1142 # begin
1143 # if rising_edge(clk) then
1144 # if r0_stall = '1' then
1145 # index := req_index;
1146 # elsif m_in.valid = '1' then
1147 # index := get_index(m_in.addr);
1148 # else
1149 # index := get_index(d_in.addr);
1150 # end if;
1151 # cache_tag_set <= cache_tags(index);
1152 # end if;
1153 # end process;
1154 #
1155 # -- Cache request parsing and hit detection
1156 # dcache_request : process(all)
1157 # variable is_hit : std_ulogic;
1158 # variable hit_way : way_t;
1159 # variable op : op_t;
1160 # variable opsel : std_ulogic_vector(2 downto 0);
1161 # variable go : std_ulogic;
1162 # variable nc : std_ulogic;
1163 # variable s_hit : std_ulogic;
1164 # variable s_tag : cache_tag_t;
1165 # variable s_pte : tlb_pte_t;
1166 # variable s_ra : std_ulogic_vector(
1167 # REAL_ADDR_BITS - 1 downto 0
1168 # );
1169 # variable hit_set : std_ulogic_vector(
1170 # TLB_NUM_WAYS - 1 downto 0
1171 # );
1172 # variable hit_way_set : hit_way_set_t;
1173 # variable rel_matches : std_ulogic_vector(
1174 # TLB_NUM_WAYS - 1 downto 0
1175 # );
1176 # variable rel_match : std_ulogic;
1177 # begin
1178 # -- Extract line, row and tag from request
1179 # req_index <= get_index(r0.req.addr);
1180 # req_row <= get_row(r0.req.addr);
1181 # req_tag <= get_tag(ra);
1182 #
1183 # go := r0_valid and not (r0.tlbie or r0.tlbld)
1184 # and not r1.ls_error;
1185 #
1186 # -- Test if pending request is a hit on any way
1187 # -- In order to make timing in virtual mode,
1188 # -- when we are using the TLB, we compare each
1189 # --way with each of the real addresses from each way of
1190 # -- the TLB, and then decide later which match to use.
1191 # hit_way := 0;
1192 # is_hit := '0';
1193 # rel_match := '0';
1194 # if r0.req.virt_mode = '1' then
1195 # rel_matches := (others => '0');
1196 # for j in tlb_way_t loop
1197 # hit_way_set(j) := 0;
1198 # s_hit := '0';
1199 # s_pte := read_tlb_pte(j, tlb_pte_way);
1200 # s_ra :=
1201 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
1202 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
1203 # s_tag := get_tag(s_ra);
1204 # for i in way_t loop
1205 # if go = '1' and cache_valids(req_index)(i) = '1'
1206 # and read_tag(i, cache_tag_set) = s_tag
1207 # and tlb_valid_way(j) = '1' then
1208 # hit_way_set(j) := i;
1209 # s_hit := '1';
1210 # end if;
1211 # end loop;
1212 # hit_set(j) := s_hit;
1213 # if s_tag = r1.reload_tag then
1214 # rel_matches(j) := '1';
1215 # end if;
1216 # end loop;
1217 # if tlb_hit = '1' then
1218 # is_hit := hit_set(tlb_hit_way);
1219 # hit_way := hit_way_set(tlb_hit_way);
1220 # rel_match := rel_matches(tlb_hit_way);
1221 # end if;
1222 # else
1223 # s_tag := get_tag(r0.req.addr);
1224 # for i in way_t loop
1225 # if go = '1' and cache_valids(req_index)(i) = '1' and
1226 # read_tag(i, cache_tag_set) = s_tag then
1227 # hit_way := i;
1228 # is_hit := '1';
1229 # end if;
1230 # end loop;
1231 # if s_tag = r1.reload_tag then
1232 # rel_match := '1';
1233 # end if;
1234 # end if;
1235 # req_same_tag <= rel_match;
1236 #
1237 # -- See if the request matches the line currently being reloaded
1238 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
1239 # and rel_match = '1' then
1240 # -- For a store, consider this a hit even if the row isn't
1241 # -- valid since it will be by the time we perform the store.
1242 # -- For a load, check the appropriate row valid bit.
1243 # is_hit :=
1244 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
1245 # hit_way := replace_way;
1246 # end if;
1247 #
1248 # -- Whether to use forwarded data for a load or not
1249 # use_forward1_next <= '0';
1250 # if get_row(r1.req.real_addr) = req_row
1251 # and r1.req.hit_way = hit_way then
1252 # -- Only need to consider r1.write_bram here, since if we
1253 # -- are writing refill data here, then we don't have a
1254 # -- cache hit this cycle on the line being refilled.
1255 # -- (There is the possibility that the load following the
1256 # -- load miss that started the refill could be to the old
1257 # -- contents of the victim line, since it is a couple of
1258 # -- cycles after the refill starts before we see the updated
1259 # -- cache tag. In that case we don't use the bypass.)
1260 # use_forward1_next <= r1.write_bram;
1261 # end if;
1262 # use_forward2_next <= '0';
1263 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
1264 # use_forward2_next <= r1.forward_valid1;
1265 # end if;
1266 #
1267 # -- The way that matched on a hit
1268 # req_hit_way <= hit_way;
1269 #
1270 # -- The way to replace on a miss
1271 # if r1.write_tag = '1' then
1272 # replace_way <= to_integer(unsigned(
1273 # plru_victim(r1.store_index)
1274 # ));
1275 # else
1276 # replace_way <= r1.store_way;
1277 # end if;
1278 #
1279 # -- work out whether we have permission for this access
1280 # -- NB we don't yet implement AMR, thus no KUAP
1281 # rc_ok <= perm_attr.reference and
1282 # (r0.req.load or perm_attr.changed);
1283 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
1284 # (perm_attr.wr_perm or (r0.req.load
1285 # and perm_attr.rd_perm));
1286 # access_ok <= valid_ra and perm_ok and rc_ok;
1287 #
1288 # -- Combine the request and cache hit status to decide what
1289 # -- operation needs to be done
1290 # --
1291 # nc := r0.req.nc or perm_attr.nocache;
1292 # op := OP_NONE;
1293 # if go = '1' then
1294 # if access_ok = '0' then
1295 # op := OP_BAD;
1296 # elsif cancel_store = '1' then
1297 # op := OP_STCX_FAIL;
1298 # else
1299 # opsel := r0.req.load & nc & is_hit;
1300 # case opsel is
1301 # when "101" => op := OP_LOAD_HIT;
1302 # when "100" => op := OP_LOAD_MISS;
1303 # when "110" => op := OP_LOAD_NC;
1304 # when "001" => op := OP_STORE_HIT;
1305 # when "000" => op := OP_STORE_MISS;
1306 # when "010" => op := OP_STORE_MISS;
1307 # when "011" => op := OP_BAD;
1308 # when "111" => op := OP_BAD;
1309 # when others => op := OP_NONE;
1310 # end case;
1311 # end if;
1312 # end if;
1313 # req_op <= op;
1314 # req_go <= go;
1315 #
1316 # -- Version of the row number that is valid one cycle earlier
1317 # -- in the cases where we need to read the cache data BRAM.
1318 # -- If we're stalling then we need to keep reading the last
1319 # -- row requested.
1320 # if r0_stall = '0' then
1321 # if m_in.valid = '1' then
1322 # early_req_row <= get_row(m_in.addr);
1323 # else
1324 # early_req_row <= get_row(d_in.addr);
1325 # end if;
1326 # else
1327 # early_req_row <= req_row;
1328 # end if;
1329 # end process;
1330 #
1331 # -- Wire up wishbone request latch out of stage 1
1332 # wishbone_out <= r1.wb;
1333 #
1334 # -- Handle load-with-reservation and store-conditional instructions
1335 # reservation_comb: process(all)
1336 # begin
1337 # cancel_store <= '0';
1338 # set_rsrv <= '0';
1339 # clear_rsrv <= '0';
1340 # if r0_valid = '1' and r0.req.reserve = '1' then
1341 # -- XXX generate alignment interrupt if address
1342 # -- is not aligned XXX or if r0.req.nc = '1'
1343 # if r0.req.load = '1' then
1344 # -- load with reservation
1345 # set_rsrv <= '1';
1346 # else
1347 # -- store conditional
1348 # clear_rsrv <= '1';
1349 # if reservation.valid = '0' or r0.req.addr(63
1350 # downto LINE_OFF_BITS) /= reservation.addr then
1351 # cancel_store <= '1';
1352 # end if;
1353 # end if;
1354 # end if;
1355 # end process;
1356 #
1357 # reservation_reg: process(clk)
1358 # begin
1359 # if rising_edge(clk) then
1360 # if rst = '1' then
1361 # reservation.valid <= '0';
1362 # elsif r0_valid = '1' and access_ok = '1' then
1363 # if clear_rsrv = '1' then
1364 # reservation.valid <= '0';
1365 # elsif set_rsrv = '1' then
1366 # reservation.valid <= '1';
1367 # reservation.addr <=
1368 # r0.req.addr(63 downto LINE_OFF_BITS);
1369 # end if;
1370 # end if;
1371 # end if;
1372 # end process;
1373 #
1374 # -- Return data for loads & completion control logic
1375 # --
1376 # writeback_control: process(all)
1377 # variable data_out : std_ulogic_vector(63 downto 0);
1378 # variable data_fwd : std_ulogic_vector(63 downto 0);
1379 # variable j : integer;
1380 # begin
1381 # -- Use the bypass if are reading the row that was
1382 # -- written 1 or 2 cycles ago, including for the
1383 # -- slow_valid = 1 case (i.e. completing a load
1384 # -- miss or a non-cacheable load).
1385 # if r1.use_forward1 = '1' then
1386 # data_fwd := r1.forward_data1;
1387 # else
1388 # data_fwd := r1.forward_data2;
1389 # end if;
1390 # data_out := cache_out(r1.hit_way);
1391 # for i in 0 to 7 loop
1392 # j := i * 8;
1393 # if r1.forward_sel(i) = '1' then
1394 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1395 # end if;
1396 # end loop;
1397 #
1398 # d_out.valid <= r1.ls_valid;
1399 # d_out.data <= data_out;
1400 # d_out.store_done <= not r1.stcx_fail;
1401 # d_out.error <= r1.ls_error;
1402 # d_out.cache_paradox <= r1.cache_paradox;
1403 #
1404 # -- Outputs to MMU
1405 # m_out.done <= r1.mmu_done;
1406 # m_out.err <= r1.mmu_error;
1407 # m_out.data <= data_out;
1408 #
1409 # -- We have a valid load or store hit or we just completed
1410 # -- a slow op such as a load miss, a NC load or a store
1411 # --
1412 # -- Note: the load hit is delayed by one cycle. However it
1413 # -- can still not collide with r.slow_valid (well unless I
1414 # -- miscalculated) because slow_valid can only be set on a
1415 # -- subsequent request and not on its first cycle (the state
1416 # -- machine must have advanced), which makes slow_valid
1417 # -- at least 2 cycles from the previous hit_load_valid.
1418 #
1419 # -- Sanity: Only one of these must be set in any given cycle
1420 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1421 # report "unexpected slow_valid collision with stcx_fail"
1422 # severity FAILURE;
1423 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1424 # /= '1' report "unexpected hit_load_delayed collision with
1425 # slow_valid" severity FAILURE;
1426 #
1427 # if r1.mmu_req = '0' then
1428 # -- Request came from loadstore1...
1429 # -- Load hit case is the standard path
1430 # if r1.hit_load_valid = '1' then
1431 # report
1432 # "completing load hit data=" & to_hstring(data_out);
1433 # end if;
1434 #
1435 # -- error cases complete without stalling
1436 # if r1.ls_error = '1' then
1437 # report "completing ld/st with error";
1438 # end if;
1439 #
1440 # -- Slow ops (load miss, NC, stores)
1441 # if r1.slow_valid = '1' then
1442 # report
1443 # "completing store or load miss data="
1444 # & to_hstring(data_out);
1445 # end if;
1446 #
1447 # else
1448 # -- Request came from MMU
1449 # if r1.hit_load_valid = '1' then
1450 # report "completing load hit to MMU, data="
1451 # & to_hstring(m_out.data);
1452 # end if;
1453 #
1454 # -- error cases complete without stalling
1455 # if r1.mmu_error = '1' then
1456 # report "completing MMU ld with error";
1457 # end if;
1458 #
1459 # -- Slow ops (i.e. load miss)
1460 # if r1.slow_valid = '1' then
1461 # report "completing MMU load miss, data="
1462 # & to_hstring(m_out.data);
1463 # end if;
1464 # end if;
1465 #
1466 # end process;
1467 #
1468 #
1469 # -- Generate a cache RAM for each way. This handles the normal
1470 # -- reads, writes from reloads and the special store-hit update
1471 # -- path as well.
1472 # --
1473 # -- Note: the BRAMs have an extra read buffer, meaning the output
1474 # -- is pipelined an extra cycle. This differs from the
1475 # -- icache. The writeback logic needs to take that into
1476 # -- account by using 1-cycle delayed signals for load hits.
1477 # --
1478 # rams: for i in 0 to NUM_WAYS-1 generate
1479 # signal do_read : std_ulogic;
1480 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1481 # signal do_write : std_ulogic;
1482 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1483 # signal wr_data :
1484 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1485 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1486 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1487 # signal dout : cache_row_t;
1488 # begin
1489 # way: entity work.cache_ram
1490 # generic map (
1491 # ROW_BITS => ROW_BITS,
1492 # WIDTH => wishbone_data_bits,
1493 # ADD_BUF => true
1494 # )
1495 # port map (
1496 # clk => clk,
1497 # rd_en => do_read,
1498 # rd_addr => rd_addr,
1499 # rd_data => dout,
1500 # wr_sel => wr_sel_m,
1501 # wr_addr => wr_addr,
1502 # wr_data => wr_data
1503 # );
1504 # process(all)
1505 # begin
1506 # -- Cache hit reads
1507 # do_read <= '1';
1508 # rd_addr <=
1509 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1510 # cache_out(i) <= dout;
1511 #
1512 # -- Write mux:
1513 # --
1514 # -- Defaults to wishbone read responses (cache refill)
1515 # --
1516 # -- For timing, the mux on wr_data/sel/addr is not
1517 # -- dependent on anything other than the current state.
1518 # wr_sel_m <= (others => '0');
1519 #
1520 # do_write <= '0';
1521 # if r1.write_bram = '1' then
1522 # -- Write store data to BRAM. This happens one
1523 # -- cycle after the store is in r0.
1524 # wr_data <= r1.req.data;
1525 # wr_sel <= r1.req.byte_sel;
1526 # wr_addr <= std_ulogic_vector(to_unsigned(
1527 # get_row(r1.req.real_addr), ROW_BITS
1528 # ));
1529 # if i = r1.req.hit_way then
1530 # do_write <= '1';
1531 # end if;
1532 # else
1533 # -- Otherwise, we might be doing a reload or a DCBZ
1534 # if r1.dcbz = '1' then
1535 # wr_data <= (others => '0');
1536 # else
1537 # wr_data <= wishbone_in.dat;
1538 # end if;
1539 # wr_addr <= std_ulogic_vector(to_unsigned(
1540 # r1.store_row, ROW_BITS
1541 # ));
1542 # wr_sel <= (others => '1');
1543 #
1544 # if r1.state = RELOAD_WAIT_ACK and
1545 # wishbone_in.ack = '1' and replace_way = i then
1546 # do_write <= '1';
1547 # end if;
1548 # end if;
1549 #
1550 # -- Mask write selects with do_write since BRAM
1551 # -- doesn't have a global write-enable
1552 # if do_write = '1' then
1553 # wr_sel_m <= wr_sel;
1554 # end if;
1555 #
1556 # end process;
1557 # end generate;
1558 #
1559 # -- Cache hit synchronous machine for the easy case.
1560 # -- This handles load hits.
1561 # -- It also handles error cases (TLB miss, cache paradox)
1562 # dcache_fast_hit : process(clk)
1563 # begin
1564 # if rising_edge(clk) then
1565 # if req_op /= OP_NONE then
1566 # report "op:" & op_t'image(req_op) &
1567 # " addr:" & to_hstring(r0.req.addr) &
1568 # " nc:" & std_ulogic'image(r0.req.nc) &
1569 # " idx:" & integer'image(req_index) &
1570 # " tag:" & to_hstring(req_tag) &
1571 # " way: " & integer'image(req_hit_way);
1572 # end if;
1573 # if r0_valid = '1' then
1574 # r1.mmu_req <= r0.mmu_req;
1575 # end if;
1576 #
1577 # -- Fast path for load/store hits.
1578 # -- Set signals for the writeback controls.
1579 # r1.hit_way <= req_hit_way;
1580 # r1.hit_index <= req_index;
1581 # if req_op = OP_LOAD_HIT then
1582 # r1.hit_load_valid <= '1';
1583 # else
1584 # r1.hit_load_valid <= '0';
1585 # end if;
1586 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1587 # r1.cache_hit <= '1';
1588 # else
1589 # r1.cache_hit <= '0';
1590 # end if;
1591 #
1592 # if req_op = OP_BAD then
1593 # report "Signalling ld/st error valid_ra=" &
1594 # std_ulogic'image(valid_ra) & " rc_ok=" &
1595 # std_ulogic'image(rc_ok) & " perm_ok=" &
1596 # std_ulogic'image(perm_ok);
1597 # r1.ls_error <= not r0.mmu_req;
1598 # r1.mmu_error <= r0.mmu_req;
1599 # r1.cache_paradox <= access_ok;
1600 # else
1601 # r1.ls_error <= '0';
1602 # r1.mmu_error <= '0';
1603 # r1.cache_paradox <= '0';
1604 # end if;
1605 #
1606 # if req_op = OP_STCX_FAIL then
1607 # r1.stcx_fail <= '1';
1608 # else
1609 # r1.stcx_fail <= '0';
1610 # end if;
1611 #
1612 # -- Record TLB hit information for updating TLB PLRU
1613 # r1.tlb_hit <= tlb_hit;
1614 # r1.tlb_hit_way <= tlb_hit_way;
1615 # r1.tlb_hit_index <= tlb_req_index;
1616 #
1617 # end if;
1618 # end process;
1619 #
1620 # -- Memory accesses are handled by this state machine:
1621 # --
1622 # -- * Cache load miss/reload (in conjunction with "rams")
1623 # -- * Load hits for non-cachable forms
1624 # -- * Stores (the collision case is handled in "rams")
1625 # --
1626 # -- All wishbone requests generation is done here.
1627 # -- This machine operates at stage 1.
1628 # dcache_slow : process(clk)
1629 # variable stbs_done : boolean;
1630 # variable req : mem_access_request_t;
1631 # variable acks : unsigned(2 downto 0);
1632 # begin
1633 # if rising_edge(clk) then
1634 # r1.use_forward1 <= use_forward1_next;
1635 # r1.forward_sel <= (others => '0');
1636 # if use_forward1_next = '1' then
1637 # r1.forward_sel <= r1.req.byte_sel;
1638 # elsif use_forward2_next = '1' then
1639 # r1.forward_sel <= r1.forward_sel1;
1640 # end if;
1641 #
1642 # r1.forward_data2 <= r1.forward_data1;
1643 # if r1.write_bram = '1' then
1644 # r1.forward_data1 <= r1.req.data;
1645 # r1.forward_sel1 <= r1.req.byte_sel;
1646 # r1.forward_way1 <= r1.req.hit_way;
1647 # r1.forward_row1 <= get_row(r1.req.real_addr);
1648 # r1.forward_valid1 <= '1';
1649 # else
1650 # if r1.dcbz = '1' then
1651 # r1.forward_data1 <= (others => '0');
1652 # else
1653 # r1.forward_data1 <= wishbone_in.dat;
1654 # end if;
1655 # r1.forward_sel1 <= (others => '1');
1656 # r1.forward_way1 <= replace_way;
1657 # r1.forward_row1 <= r1.store_row;
1658 # r1.forward_valid1 <= '0';
1659 # end if;
1660 #
1661 # -- On reset, clear all valid bits to force misses
1662 # if rst = '1' then
1663 # for i in index_t loop
1664 # cache_valids(i) <= (others => '0');
1665 # end loop;
1666 # r1.state <= IDLE;
1667 # r1.full <= '0';
1668 # r1.slow_valid <= '0';
1669 # r1.wb.cyc <= '0';
1670 # r1.wb.stb <= '0';
1671 # r1.ls_valid <= '0';
1672 # r1.mmu_done <= '0';
1673 #
1674 # -- Not useful normally but helps avoiding
1675 # -- tons of sim warnings
1676 # r1.wb.adr <= (others => '0');
1677 # else
1678 # -- One cycle pulses reset
1679 # r1.slow_valid <= '0';
1680 # r1.write_bram <= '0';
1681 # r1.inc_acks <= '0';
1682 # r1.dec_acks <= '0';
1683 #
1684 # r1.ls_valid <= '0';
1685 # -- complete tlbies and TLB loads in the third cycle
1686 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
1687 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
1688 # if r0.mmu_req = '0' then
1689 # r1.ls_valid <= '1';
1690 # else
1691 # r1.mmu_done <= '1';
1692 # end if;
1693 # end if;
1694 #
1695 # if r1.write_tag = '1' then
1696 # -- Store new tag in selected way
1697 # for i in 0 to NUM_WAYS-1 loop
1698 # if i = replace_way then
1699 # cache_tags(r1.store_index)(
1700 # (i + 1) * TAG_WIDTH - 1
1701 # downto i * TAG_WIDTH
1702 # ) <=
1703 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
1704 # & r1.reload_tag;
1705 # end if;
1706 # end loop;
1707 # r1.store_way <= replace_way;
1708 # r1.write_tag <= '0';
1709 # end if;
1710 #
1711 # -- Take request from r1.req if there is one there,
1712 # -- else from req_op, ra, etc.
1713 # if r1.full = '1' then
1714 # req := r1.req;
1715 # else
1716 # req.op := req_op;
1717 # req.valid := req_go;
1718 # req.mmu_req := r0.mmu_req;
1719 # req.dcbz := r0.req.dcbz;
1720 # req.real_addr := ra;
1721 # -- Force data to 0 for dcbz
1722 # if r0.req.dcbz = '0' then
1723 # req.data := r0.req.data;
1724 # else
1725 # req.data := (others => '0');
1726 # end if;
1727 # -- Select all bytes for dcbz
1728 # -- and for cacheable loads
1729 # if r0.req.dcbz = '1'
1730 # or (r0.req.load = '1' and r0.req.nc = '0') then
1731 # req.byte_sel := (others => '1');
1732 # else
1733 # req.byte_sel := r0.req.byte_sel;
1734 # end if;
1735 # req.hit_way := req_hit_way;
1736 # req.same_tag := req_same_tag;
1737 #
1738 # -- Store the incoming request from r0,
1739 # -- if it is a slow request
1740 # -- Note that r1.full = 1 implies req_op = OP_NONE
1741 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
1742 # or req_op = OP_STORE_MISS
1743 # or req_op = OP_STORE_HIT then
1744 # r1.req <= req;
1745 # r1.full <= '1';
1746 # end if;
1747 # end if;
1748 #
1749 # -- Main state machine
1750 # case r1.state is
1751 # when IDLE =>
1752 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1753 # r1.wb.sel <= req.byte_sel;
1754 # r1.wb.dat <= req.data;
1755 # r1.dcbz <= req.dcbz;
1756 #
1757 # -- Keep track of our index and way
1758 # -- for subsequent stores.
1759 # r1.store_index <= get_index(req.real_addr);
1760 # r1.store_row <= get_row(req.real_addr);
1761 # r1.end_row_ix <=
1762 # get_row_of_line(get_row(req.real_addr)) - 1;
1763 # r1.reload_tag <= get_tag(req.real_addr);
1764 # r1.req.same_tag <= '1';
1765 #
1766 # if req.op = OP_STORE_HIT then
1767 # r1.store_way <= req.hit_way;
1768 # end if;
1769 #
1770 # -- Reset per-row valid bits,
1771 # -- ready for handling OP_LOAD_MISS
1772 # for i in 0 to ROW_PER_LINE - 1 loop
1773 # r1.rows_valid(i) <= '0';
1774 # end loop;
1775 #
1776 # case req.op is
1777 # when OP_LOAD_HIT =>
1778 # -- stay in IDLE state
1779 #
1780 # when OP_LOAD_MISS =>
1781 # -- Normal load cache miss,
1782 # -- start the reload machine
1783 # report "cache miss real addr:" &
1784 # to_hstring(req.real_addr) & " idx:" &
1785 # integer'image(get_index(req.real_addr)) &
1786 # " tag:" & to_hstring(get_tag(req.real_addr));
1787 #
1788 # -- Start the wishbone cycle
1789 # r1.wb.we <= '0';
1790 # r1.wb.cyc <= '1';
1791 # r1.wb.stb <= '1';
1792 #
1793 # -- Track that we had one request sent
1794 # r1.state <= RELOAD_WAIT_ACK;
1795 # r1.write_tag <= '1';
1796 #
1797 # when OP_LOAD_NC =>
1798 # r1.wb.cyc <= '1';
1799 # r1.wb.stb <= '1';
1800 # r1.wb.we <= '0';
1801 # r1.state <= NC_LOAD_WAIT_ACK;
1802 #
1803 # when OP_STORE_HIT | OP_STORE_MISS =>
1804 # if req.dcbz = '0' then
1805 # r1.state <= STORE_WAIT_ACK;
1806 # r1.acks_pending <= to_unsigned(1, 3);
1807 # r1.full <= '0';
1808 # r1.slow_valid <= '1';
1809 # if req.mmu_req = '0' then
1810 # r1.ls_valid <= '1';
1811 # else
1812 # r1.mmu_done <= '1';
1813 # end if;
1814 # if req.op = OP_STORE_HIT then
1815 # r1.write_bram <= '1';
1816 # end if;
1817 # else
1818 # -- dcbz is handled much like a load
1819 # -- miss except that we are writing
1820 # -- to memory instead of reading
1821 # r1.state <= RELOAD_WAIT_ACK;
1822 # if req.op = OP_STORE_MISS then
1823 # r1.write_tag <= '1';
1824 # end if;
1825 # end if;
1826 # r1.wb.we <= '1';
1827 # r1.wb.cyc <= '1';
1828 # r1.wb.stb <= '1';
1829 #
1830 # -- OP_NONE and OP_BAD do nothing
1831 # -- OP_BAD & OP_STCX_FAIL were handled above already
1832 # when OP_NONE =>
1833 # when OP_BAD =>
1834 # when OP_STCX_FAIL =>
1835 # end case;
1836 #
1837 # when RELOAD_WAIT_ACK =>
1838 # -- Requests are all sent if stb is 0
1839 # stbs_done := r1.wb.stb = '0';
1840 #
1841 # -- If we are still sending requests,
1842 # -- was one accepted?
1843 # if wishbone_in.stall = '0' and not stbs_done then
1844 # -- That was the last word ? We are done sending.
1845 # -- Clear stb and set stbs_done so we can handle
1846 # -- an eventual last ack on the same cycle.
1847 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
1848 # r1.wb.stb <= '0';
1849 # stbs_done := true;
1850 # end if;
1851 #
1852 # -- Calculate the next row address
1853 # r1.wb.adr <= next_row_addr(r1.wb.adr);
1854 # end if;
1855 #
1856 # -- Incoming acks processing
1857 # r1.forward_valid1 <= wishbone_in.ack;
1858 # if wishbone_in.ack = '1' then
1859 # r1.rows_valid(
1860 # r1.store_row mod ROW_PER_LINE
1861 # ) <= '1';
1862 # -- If this is the data we were looking for,
1863 # -- we can complete the request next cycle.
1864 # -- Compare the whole address in case the
1865 # -- request in r1.req is not the one that
1866 # -- started this refill.
1867 # if r1.full = '1' and r1.req.same_tag = '1'
1868 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
1869 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
1870 # and r1.store_row = get_row(r1.req.real_addr) then
1871 # r1.full <= '0';
1872 # r1.slow_valid <= '1';
1873 # if r1.mmu_req = '0' then
1874 # r1.ls_valid <= '1';
1875 # else
1876 # r1.mmu_done <= '1';
1877 # end if;
1878 # r1.forward_sel <= (others => '1');
1879 # r1.use_forward1 <= '1';
1880 # end if;
1881 #
1882 # -- Check for completion
1883 # if stbs_done and is_last_row(r1.store_row,
1884 # r1.end_row_ix) then
1885 # -- Complete wishbone cycle
1886 # r1.wb.cyc <= '0';
1887 #
1888 # -- Cache line is now valid
1889 # cache_valids(r1.store_index)(
1890 # r1.store_way
1891 # ) <= '1';
1892 #
1893 # r1.state <= IDLE;
1894 # end if;
1895 #
1896 # -- Increment store row counter
1897 # r1.store_row <= next_row(r1.store_row);
1898 # end if;
1899 #
1900 # when STORE_WAIT_ACK =>
1901 # stbs_done := r1.wb.stb = '0';
1902 # acks := r1.acks_pending;
1903 # if r1.inc_acks /= r1.dec_acks then
1904 # if r1.inc_acks = '1' then
1905 # acks := acks + 1;
1906 # else
1907 # acks := acks - 1;
1908 # end if;
1909 # end if;
1910 # r1.acks_pending <= acks;
1911 # -- Clear stb when slave accepted request
1912 # if wishbone_in.stall = '0' then
1913 # -- See if there is another store waiting
1914 # -- to be done which is in the same real page.
1915 # if req.valid = '1' then
1916 # r1.wb.adr(
1917 # SET_SIZE_BITS - 1 downto 0
1918 # ) <= req.real_addr(
1919 # SET_SIZE_BITS - 1 downto 0
1920 # );
1921 # r1.wb.dat <= req.data;
1922 # r1.wb.sel <= req.byte_sel;
1923 # end if;
1924 # if acks < 7 and req.same_tag = '1'
1925 # and (req.op = OP_STORE_MISS
1926 # or req.op = OP_STORE_HIT) then
1927 # r1.wb.stb <= '1';
1928 # stbs_done := false;
1929 # if req.op = OP_STORE_HIT then
1930 # r1.write_bram <= '1';
1931 # end if;
1932 # r1.full <= '0';
1933 # r1.slow_valid <= '1';
1934 # -- Store requests never come from the MMU
1935 # r1.ls_valid <= '1';
1936 # stbs_done := false;
1937 # r1.inc_acks <= '1';
1938 # else
1939 # r1.wb.stb <= '0';
1940 # stbs_done := true;
1941 # end if;
1942 # end if;
1943 #
1944 # -- Got ack ? See if complete.
1945 # if wishbone_in.ack = '1' then
1946 # if stbs_done and acks = 1 then
1947 # r1.state <= IDLE;
1948 # r1.wb.cyc <= '0';
1949 # r1.wb.stb <= '0';
1950 # end if;
1951 # r1.dec_acks <= '1';
1952 # end if;
1953 #
1954 # when NC_LOAD_WAIT_ACK =>
1955 # -- Clear stb when slave accepted request
1956 # if wishbone_in.stall = '0' then
1957 # r1.wb.stb <= '0';
1958 # end if;
1959 #
1960 # -- Got ack ? complete.
1961 # if wishbone_in.ack = '1' then
1962 # r1.state <= IDLE;
1963 # r1.full <= '0';
1964 # r1.slow_valid <= '1';
1965 # if r1.mmu_req = '0' then
1966 # r1.ls_valid <= '1';
1967 # else
1968 # r1.mmu_done <= '1';
1969 # end if;
1970 # r1.forward_sel <= (others => '1');
1971 # r1.use_forward1 <= '1';
1972 # r1.wb.cyc <= '0';
1973 # r1.wb.stb <= '0';
1974 # end if;
1975 # end case;
1976 # end if;
1977 # end if;
1978 # end process;
1979 #
1980 # dc_log: if LOG_LENGTH > 0 generate
1981 # signal log_data : std_ulogic_vector(19 downto 0);
1982 # begin
1983 # dcache_log: process(clk)
1984 # begin
1985 # if rising_edge(clk) then
1986 # log_data <= r1.wb.adr(5 downto 3) &
1987 # wishbone_in.stall &
1988 # wishbone_in.ack &
1989 # r1.wb.stb & r1.wb.cyc &
1990 # d_out.error &
1991 # d_out.valid &
1992 # std_ulogic_vector(
1993 # to_unsigned(op_t'pos(req_op), 3)) &
1994 # stall_out &
1995 # std_ulogic_vector(
1996 # to_unsigned(tlb_hit_way, 3)) &
1997 # valid_ra &
1998 # std_ulogic_vector(
1999 # to_unsigned(state_t'pos(r1.state), 3));
2000 # end if;
2001 # end process;
2002 # log_out <= log_data;
2003 # end generate;
2004 # end;