dcache.py commit today's progress on translating dcache.vhdl
[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 # attribute ram_style of dtlb_tags : signal is "distributed";
386 # attribute ram_style of dtlb_ptes : signal is "distributed";
387 dtlb_valids = tlb_valids_t;
388 dtlb_tags = tlb_tags_t;
389 dtlb_ptes = tlb_ptes_t;
390 # TODO attribute ram_style of dtlb_tags : signal is "distributed";
391 # TODO attribute ram_style of dtlb_ptes : signal is "distributed";
392
393
394 # -- Record for storing permission, attribute, etc. bits from a PTE
395 # type perm_attr_t is record
396 # reference : std_ulogic;
397 # changed : std_ulogic;
398 # nocache : std_ulogic;
399 # priv : std_ulogic;
400 # rd_perm : std_ulogic;
401 # wr_perm : std_ulogic;
402 # end record;
403 # Record for storing permission, attribute, etc. bits from a PTE
404 class PermAttr(RecordObject):
405 def __init__(self):
406 super().__init__()
407 self.reference = Signal()
408 self.changed = Signal()
409 self.nocache = Signal()
410 self.priv = Signal()
411 self.rd_perm = Signal()
412 self.wr_perm = Signal()
413
414 # function extract_perm_attr(
415 # pte : std_ulogic_vector(TLB_PTE_BITS - 1 downto 0))
416 # return perm_attr_t is
417 # variable pa : perm_attr_t;
418 # begin
419 # pa.reference := pte(8);
420 # pa.changed := pte(7);
421 # pa.nocache := pte(5);
422 # pa.priv := pte(3);
423 # pa.rd_perm := pte(2);
424 # pa.wr_perm := pte(1);
425 # return pa;
426 # end;
427 def extract_perm_attr(pte=Signal(TLB_PTE_BITS)):
428 pa = PermAttr()
429 pa.reference = pte[8]
430 pa.changed = pte[7]
431 pa.nocache = pte[5]
432 pa.priv = pte[3]
433 pa.rd_perm = pte[2]
434 pa.wr_perm = pte[1]
435 return pa;
436
437 # constant real_mode_perm_attr : perm_attr_t :=
438 # (nocache => '0', others => '1');
439 REAL_MODE_PERM_ATTR = PermAttr()
440 REAL_MODE_PERM_ATTR.reference = 1
441 REAL_MODE_PERM_ATTR.changed = 1
442 REAL_MODE_PERM_ATTR.priv = 1
443 REAL_MODE_PERM_ATTR.rd_perm = 1
444 REAL_MODE_PERM_ATTR.wr_perm = 1
445
446 # -- Type of operation on a "valid" input
447 # type op_t is
448 # (
449 # OP_NONE,
450 # OP_BAD, -- NC cache hit, TLB miss, prot/RC failure
451 # OP_STCX_FAIL, -- conditional store w/o reservation
452 # OP_LOAD_HIT, -- Cache hit on load
453 # OP_LOAD_MISS, -- Load missing cache
454 # OP_LOAD_NC, -- Non-cachable load
455 # OP_STORE_HIT, -- Store hitting cache
456 # OP_STORE_MISS -- Store missing cache
457 # );
458 # Type of operation on a "valid" input
459 @unique
460 class OP(Enum):
461 OP_NONE = 0
462 OP_BAD = 1 # NC cache hit, TLB miss, prot/RC failure
463 OP_STCX_FAIL = 2 # conditional store w/o reservation
464 OP_LOAD_HIT = 3 # Cache hit on load
465 OP_LOAD_MISS = 4 # Load missing cache
466 OP_LOAD_NC = 5 # Non-cachable load
467 OP_STORE_HIT = 6 # Store hitting cache
468 OP_STORE_MISS = 7 # Store missing cache
469
470 # -- Cache state machine
471 # type state_t is
472 # (
473 # IDLE, -- Normal load hit processing
474 # RELOAD_WAIT_ACK, -- Cache reload wait ack
475 # STORE_WAIT_ACK, -- Store wait ack
476 # NC_LOAD_WAIT_ACK -- Non-cachable load wait ack
477 # );
478 # Cache state machine
479 @unique
480 class State(Enum):
481 IDLE = 0 # Normal load hit processing
482 RELOAD_WAIT_ACK = 1 # Cache reload wait ack
483 STORE_WAIT_ACK = 2 # Store wait ack
484 NC_LOAD_WAIT_ACK = 3 # Non-cachable load wait ack
485
486 # -- Dcache operations:
487 # --
488 # -- In order to make timing, we use the BRAMs with
489 # -- an output buffer, which means that the BRAM
490 # -- output is delayed by an extra cycle.
491 # --
492 # -- Thus, the dcache has a 2-stage internal pipeline
493 # -- for cache hits with no stalls.
494 # --
495 # -- All other operations are handled via stalling
496 # -- in the first stage.
497 # --
498 # -- The second stage can thus complete a hit at the same
499 # -- time as the first stage emits a stall for a complex op.
500 #
501 # -- Stage 0 register, basically contains just the latched request
502 # type reg_stage_0_t is record
503 # req : Loadstore1ToDcacheType;
504 # tlbie : std_ulogic;
505 # doall : std_ulogic;
506 # tlbld : std_ulogic;
507 # mmu_req : std_ulogic; -- indicates source of request
508 # end record;
509 # Dcache operations:
510 #
511 # In order to make timing, we use the BRAMs with
512 # an output buffer, which means that the BRAM
513 # output is delayed by an extra cycle.
514 #
515 # Thus, the dcache has a 2-stage internal pipeline
516 # for cache hits with no stalls.
517 #
518 # All other operations are handled via stalling
519 # in the first stage.
520 #
521 # The second stage can thus complete a hit at the same
522 # time as the first stage emits a stall for a complex op.
523 #
524 # Stage 0 register, basically contains just the latched request
525 class RegStage0(RecordObject):
526 def __init__(self):
527 super().__init__()
528 self.req = LoadStore1ToDcacheType()
529 self.tlbie = Signal()
530 self.doall = Signal()
531 self.tlbld = Signal()
532 self.mmu_req = Signal() # indicates source of request
533
534 # signal r0 : reg_stage_0_t;
535 # signal r0_full : std_ulogic;
536 r0 = RegStage0()
537 r0_full = Signal()
538
539 # type mem_access_request_t is record
540 # op : op_t;
541 # valid : std_ulogic;
542 # dcbz : std_ulogic;
543 # real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
544 # data : std_ulogic_vector(63 downto 0);
545 # byte_sel : std_ulogic_vector(7 downto 0);
546 # hit_way : way_t;
547 # same_tag : std_ulogic;
548 # mmu_req : std_ulogic;
549 # end record;
550 class MemAccessRequest(RecordObject):
551 def __init__(self):
552 super().__init__()
553 self.op = Op()
554 self.valid = Signal()
555 self.dcbz = Signal()
556 self.real_addr = Signal(REAL_ADDR_BITS)
557 self.data = Signal(64)
558 self.byte_sel = Signal(8)
559 self.hit_way = Way()
560 self.same_tag = Signal()
561 self.mmu_req = Signal()
562
563 # -- First stage register, contains state for stage 1 of load hits
564 # -- and for the state machine used by all other operations
565 # type reg_stage_1_t is record
566 # -- Info about the request
567 # full : std_ulogic; -- have uncompleted request
568 # mmu_req : std_ulogic; -- request is from MMU
569 # req : mem_access_request_t;
570 #
571 # -- Cache hit state
572 # hit_way : way_t;
573 # hit_load_valid : std_ulogic;
574 # hit_index : index_t;
575 # cache_hit : std_ulogic;
576 #
577 # -- TLB hit state
578 # tlb_hit : std_ulogic;
579 # tlb_hit_way : tlb_way_t;
580 # tlb_hit_index : tlb_index_t;
581 #
582 # -- 2-stage data buffer for data forwarded from writes to reads
583 # forward_data1 : std_ulogic_vector(63 downto 0);
584 # forward_data2 : std_ulogic_vector(63 downto 0);
585 # forward_sel1 : std_ulogic_vector(7 downto 0);
586 # forward_valid1 : std_ulogic;
587 # forward_way1 : way_t;
588 # forward_row1 : row_t;
589 # use_forward1 : std_ulogic;
590 # forward_sel : std_ulogic_vector(7 downto 0);
591 #
592 # -- Cache miss state (reload state machine)
593 # state : state_t;
594 # dcbz : std_ulogic;
595 # write_bram : std_ulogic;
596 # write_tag : std_ulogic;
597 # slow_valid : std_ulogic;
598 # wb : wishbone_master_out;
599 # reload_tag : cache_tag_t;
600 # store_way : way_t;
601 # store_row : row_t;
602 # store_index : index_t;
603 # end_row_ix : row_in_line_t;
604 # rows_valid : row_per_line_valid_t;
605 # acks_pending : unsigned(2 downto 0);
606 # inc_acks : std_ulogic;
607 # dec_acks : std_ulogic;
608 #
609 # -- Signals to complete (possibly with error)
610 # ls_valid : std_ulogic;
611 # ls_error : std_ulogic;
612 # mmu_done : std_ulogic;
613 # mmu_error : std_ulogic;
614 # cache_paradox : std_ulogic;
615 #
616 # -- Signal to complete a failed stcx.
617 # stcx_fail : std_ulogic;
618 # end record;
619 # First stage register, contains state for stage 1 of load hits
620 # and for the state machine used by all other operations
621 class RegStage1(RecordObject):
622 def __init__(self):
623 super().__init__()
624 # Info about the request
625 self.full = Signal() # have uncompleted request
626 self.mmu_req = Signal() # request is from MMU
627 self.req = MemAccessRequest()
628
629 # Cache hit state
630 self.hit_way = Way()
631 self.hit_load_valid = Signal()
632 self.hit_index = Index()
633 self.cache_hit = Signal()
634
635 # TLB hit state
636 self.tlb_hit = Signal()
637 self.tlb_hit_way = TLBWay()
638 self.tlb_hit_index = TLBIndex()
639 self.
640 # 2-stage data buffer for data forwarded from writes to reads
641 self.forward_data1 = Signal(64)
642 self.forward_data2 = Signal(64)
643 self.forward_sel1 = Signal(8)
644 self.forward_valid1 = Signal()
645 self.forward_way1 = Way()
646 self.forward_row1 = Row()
647 self.use_forward1 = Signal()
648 self.forward_sel = Signal(8)
649
650 # Cache miss state (reload state machine)
651 self.state = State()
652 self.dcbz = Signal()
653 self.write_bram = Signal()
654 self.write_tag = Signal()
655 self.slow_valid = Signal()
656 self.wb = WishboneMasterOut()
657 self.reload_tag = CacheTag()
658 self.store_way = Way()
659 self.store_row = Row()
660 self.store_index = Index()
661 self.end_row_ix = RowInLine()
662 self.rows_valid = RowPerLineValid()
663 self.acks_pending = Signal(3)
664 self.inc_acks = Signal()
665 self.dec_acks = Signal()
666
667 # Signals to complete (possibly with error)
668 self.ls_valid = Signal()
669 self.ls_error = Signal()
670 self.mmu_done = Signal()
671 self.mmu_error = Signal()
672 self.cache_paradox = Signal()
673
674 # Signal to complete a failed stcx.
675 self.stcx_fail = Signal()
676
677 # signal r1 : reg_stage_1_t;
678 r1 = RegStage1()
679
680 # -- Reservation information
681 # --
682 # type reservation_t is record
683 # valid : std_ulogic;
684 # addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
685 # end record;
686 # Reservation information
687
688 class Reservation(RecordObject):
689 def __init__(self):
690 super().__init__()
691 valid = Signal()
692 addr = Signal(63 downto LINE_OFF_BITS) # TODO LINE_OFF_BITS is 6
693
694 # signal reservation : reservation_t;
695 #
696 # -- Async signals on incoming request
697 # signal req_index : index_t;
698 # signal req_row : row_t;
699 # signal req_hit_way : way_t;
700 # signal req_tag : cache_tag_t;
701 # signal req_op : op_t;
702 # signal req_data : std_ulogic_vector(63 downto 0);
703 # signal req_same_tag : std_ulogic;
704 # signal req_go : std_ulogic;
705 #
706 # signal early_req_row : row_t;
707 #
708 # signal cancel_store : std_ulogic;
709 # signal set_rsrv : std_ulogic;
710 # signal clear_rsrv : std_ulogic;
711 #
712 # signal r0_valid : std_ulogic;
713 # signal r0_stall : std_ulogic;
714 #
715 # signal use_forward1_next : std_ulogic;
716 # signal use_forward2_next : std_ulogic;
717 #
718 # -- Cache RAM interface
719 # type cache_ram_out_t is array(way_t) of cache_row_t;
720 # signal cache_out : cache_ram_out_t;
721 #
722 # -- PLRU output interface
723 # type plru_out_t is array(index_t) of
724 # std_ulogic_vector(WAY_BITS-1 downto 0);
725 # signal plru_victim : plru_out_t;
726 # signal replace_way : way_t;
727 #
728 # -- Wishbone read/write/cache write formatting signals
729 # signal bus_sel : std_ulogic_vector(7 downto 0);
730 #
731 # -- TLB signals
732 # signal tlb_tag_way : tlb_way_tags_t;
733 # signal tlb_pte_way : tlb_way_ptes_t;
734 # signal tlb_valid_way : tlb_way_valids_t;
735 # signal tlb_req_index : tlb_index_t;
736 # signal tlb_hit : std_ulogic;
737 # signal tlb_hit_way : tlb_way_t;
738 # signal pte : tlb_pte_t;
739 # signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
740 # signal valid_ra : std_ulogic;
741 # signal perm_attr : perm_attr_t;
742 # signal rc_ok : std_ulogic;
743 # signal perm_ok : std_ulogic;
744 # signal access_ok : std_ulogic;
745 #
746 # -- TLB PLRU output interface
747 # type tlb_plru_out_t is array(tlb_index_t) of
748 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
749 # signal tlb_plru_victim : tlb_plru_out_t;
750 #
751 # --
752 # -- Helper functions to decode incoming requests
753 # --
754 #
755 # -- Return the cache line index (tag index) for an address
756 # function get_index(addr: std_ulogic_vector) return index_t is
757 # begin
758 # return to_integer(
759 # unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS))
760 # );
761 # end;
762 #
763 # -- Return the cache row index (data memory) for an address
764 # function get_row(addr: std_ulogic_vector) return row_t is
765 # begin
766 # return to_integer(
767 # unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS))
768 # );
769 # end;
770 #
771 # -- Return the index of a row within a line
772 # function get_row_of_line(row: row_t) return row_in_line_t is
773 # variable row_v : unsigned(ROW_BITS-1 downto 0);
774 # begin
775 # row_v := to_unsigned(row, ROW_BITS);
776 # return row_v(ROW_LINEBITS-1 downto 0);
777 # end;
778 #
779 # -- Returns whether this is the last row of a line
780 # function is_last_row_addr(addr: wishbone_addr_type;
781 # last: row_in_line_t) return boolean is
782 # begin
783 # return
784 # unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
785 # end;
786 #
787 # -- Returns whether this is the last row of a line
788 # function is_last_row(row: row_t; last: row_in_line_t)
789 # return boolean is
790 # begin
791 # return get_row_of_line(row) = last;
792 # end;
793 #
794 # -- Return the address of the next row in the current cache line
795 # function next_row_addr(addr: wishbone_addr_type)
796 # return std_ulogic_vector is
797 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
798 # variable result : wishbone_addr_type;
799 # begin
800 # -- Is there no simpler way in VHDL to
801 # -- generate that 3 bits adder ?
802 # row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
803 # row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
804 # result := addr;
805 # result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
806 # return result;
807 # end;
808 #
809 # -- Return the next row in the current cache line. We use a
810 # -- dedicated function in order to limit the size of the
811 # -- generated adder to be only the bits within a cache line
812 # -- (3 bits with default settings)
813 # function next_row(row: row_t) return row_t is
814 # variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
815 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
816 # variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
817 # begin
818 # row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
819 # row_idx := row_v(ROW_LINEBITS-1 downto 0);
820 # row_v(ROW_LINEBITS-1 downto 0) :=
821 # std_ulogic_vector(unsigned(row_idx) + 1);
822 # return to_integer(unsigned(row_v));
823 # end;
824 #
825 # -- Get the tag value from the address
826 # function get_tag(addr: std_ulogic_vector) return cache_tag_t is
827 # begin
828 # return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
829 # end;
830 #
831 # -- Read a tag from a tag memory row
832 # function read_tag(way: way_t; tagset: cache_tags_set_t)
833 # return cache_tag_t is
834 # begin
835 # return tagset(way * TAG_WIDTH + TAG_BITS
836 # - 1 downto way * TAG_WIDTH);
837 # end;
838 #
839 # -- Read a TLB tag from a TLB tag memory row
840 # function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t)
841 # return tlb_tag_t is
842 # variable j : integer;
843 # begin
844 # j := way * TLB_EA_TAG_BITS;
845 # return tags(j + TLB_EA_TAG_BITS - 1 downto j);
846 # end;
847 #
848 # -- Write a TLB tag to a TLB tag memory row
849 # procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
850 # tag: tlb_tag_t) is
851 # variable j : integer;
852 # begin
853 # j := way * TLB_EA_TAG_BITS;
854 # tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
855 # end;
856 #
857 # -- Read a PTE from a TLB PTE memory row
858 # function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t)
859 # return tlb_pte_t is
860 # variable j : integer;
861 # begin
862 # j := way * TLB_PTE_BITS;
863 # return ptes(j + TLB_PTE_BITS - 1 downto j);
864 # end;
865 #
866 # procedure write_tlb_pte(way: tlb_way_t;
867 # ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
868 # variable j : integer;
869 # begin
870 # j := way * TLB_PTE_BITS;
871 # ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
872 # end;
873 #
874 # begin
875 #
876 # assert LINE_SIZE mod ROW_SIZE = 0
877 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
878 # assert ispow2(LINE_SIZE)
879 # report "LINE_SIZE not power of 2" severity FAILURE;
880 # assert ispow2(NUM_LINES)
881 # report "NUM_LINES not power of 2" severity FAILURE;
882 # assert ispow2(ROW_PER_LINE)
883 # report "ROW_PER_LINE not power of 2" severity FAILURE;
884 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
885 # report "geometry bits don't add up" severity FAILURE;
886 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
887 # report "geometry bits don't add up" severity FAILURE;
888 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
889 # report "geometry bits don't add up" severity FAILURE;
890 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
891 # report "geometry bits don't add up" severity FAILURE;
892 # assert (64 = wishbone_data_bits)
893 # report "Can't yet handle a wishbone width that isn't 64-bits"
894 # severity FAILURE;
895 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
896 # report "Set indexed by virtual address" severity FAILURE;
897 #
898 # -- Latch the request in r0.req as long as we're not stalling
899 # stage_0 : process(clk)
900 # variable r : reg_stage_0_t;
901 # begin
902 # if rising_edge(clk) then
903 # assert (d_in.valid and m_in.valid) = '0'
904 # report "request collision loadstore vs MMU";
905 # if m_in.valid = '1' then
906 # r.req.valid := '1';
907 # r.req.load := not (m_in.tlbie or m_in.tlbld);
908 # r.req.dcbz := '0';
909 # r.req.nc := '0';
910 # r.req.reserve := '0';
911 # r.req.virt_mode := '0';
912 # r.req.priv_mode := '1';
913 # r.req.addr := m_in.addr;
914 # r.req.data := m_in.pte;
915 # r.req.byte_sel := (others => '1');
916 # r.tlbie := m_in.tlbie;
917 # r.doall := m_in.doall;
918 # r.tlbld := m_in.tlbld;
919 # r.mmu_req := '1';
920 # else
921 # r.req := d_in;
922 # r.tlbie := '0';
923 # r.doall := '0';
924 # r.tlbld := '0';
925 # r.mmu_req := '0';
926 # end if;
927 # if rst = '1' then
928 # r0_full <= '0';
929 # elsif r1.full = '0' or r0_full = '0' then
930 # r0 <= r;
931 # r0_full <= r.req.valid;
932 # end if;
933 # end if;
934 # end process;
935 #
936 # -- we don't yet handle collisions between loadstore1 requests
937 # -- and MMU requests
938 # m_out.stall <= '0';
939 #
940 # -- Hold off the request in r0 when r1 has an uncompleted request
941 # r0_stall <= r0_full and r1.full;
942 # r0_valid <= r0_full and not r1.full;
943 # stall_out <= r0_stall;
944 #
945 # -- TLB
946 # -- Operates in the second cycle on the request latched in r0.req.
947 # -- TLB updates write the entry at the end of the second cycle.
948 # tlb_read : process(clk)
949 # variable index : tlb_index_t;
950 # variable addrbits :
951 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
952 # begin
953 # if rising_edge(clk) then
954 # if m_in.valid = '1' then
955 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
956 # - 1 downto TLB_LG_PGSZ);
957 # else
958 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
959 # - 1 downto TLB_LG_PGSZ);
960 # end if;
961 # index := to_integer(unsigned(addrbits));
962 # -- If we have any op and the previous op isn't finished,
963 # -- then keep the same output for next cycle.
964 # if r0_stall = '0' then
965 # tlb_valid_way <= dtlb_valids(index);
966 # tlb_tag_way <= dtlb_tags(index);
967 # tlb_pte_way <= dtlb_ptes(index);
968 # end if;
969 # end if;
970 # end process;
971 #
972 # -- Generate TLB PLRUs
973 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
974 # begin
975 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
976 # -- TLB PLRU interface
977 # signal tlb_plru_acc :
978 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
979 # signal tlb_plru_acc_en : std_ulogic;
980 # signal tlb_plru_out :
981 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
982 # begin
983 # tlb_plru : entity work.plru
984 # generic map (
985 # BITS => TLB_WAY_BITS
986 # )
987 # port map (
988 # clk => clk,
989 # rst => rst,
990 # acc => tlb_plru_acc,
991 # acc_en => tlb_plru_acc_en,
992 # lru => tlb_plru_out
993 # );
994 #
995 # process(all)
996 # begin
997 # -- PLRU interface
998 # if r1.tlb_hit_index = i then
999 # tlb_plru_acc_en <= r1.tlb_hit;
1000 # else
1001 # tlb_plru_acc_en <= '0';
1002 # end if;
1003 # tlb_plru_acc <=
1004 # std_ulogic_vector(to_unsigned(
1005 # r1.tlb_hit_way, TLB_WAY_BITS
1006 # ));
1007 # tlb_plru_victim(i) <= tlb_plru_out;
1008 # end process;
1009 # end generate;
1010 # end generate;
1011 #
1012 # tlb_search : process(all)
1013 # variable hitway : tlb_way_t;
1014 # variable hit : std_ulogic;
1015 # variable eatag : tlb_tag_t;
1016 # begin
1017 # tlb_req_index <=
1018 # to_integer(unsigned(r0.req.addr(
1019 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
1020 # )));
1021 # hitway := 0;
1022 # hit := '0';
1023 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
1024 # for i in tlb_way_t loop
1025 # if tlb_valid_way(i) = '1' and
1026 # read_tlb_tag(i, tlb_tag_way) = eatag then
1027 # hitway := i;
1028 # hit := '1';
1029 # end if;
1030 # end loop;
1031 # tlb_hit <= hit and r0_valid;
1032 # tlb_hit_way <= hitway;
1033 # if tlb_hit = '1' then
1034 # pte <= read_tlb_pte(hitway, tlb_pte_way);
1035 # else
1036 # pte <= (others => '0');
1037 # end if;
1038 # valid_ra <= tlb_hit or not r0.req.virt_mode;
1039 # if r0.req.virt_mode = '1' then
1040 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
1041 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
1042 # (ROW_OFF_BITS-1 downto 0 => '0');
1043 # perm_attr <= extract_perm_attr(pte);
1044 # else
1045 # ra <= r0.req.addr(
1046 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
1047 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
1048 # perm_attr <= real_mode_perm_attr;
1049 # end if;
1050 # end process;
1051 #
1052 # tlb_update : process(clk)
1053 # variable tlbie : std_ulogic;
1054 # variable tlbwe : std_ulogic;
1055 # variable repl_way : tlb_way_t;
1056 # variable eatag : tlb_tag_t;
1057 # variable tagset : tlb_way_tags_t;
1058 # variable pteset : tlb_way_ptes_t;
1059 # begin
1060 # if rising_edge(clk) then
1061 # tlbie := r0_valid and r0.tlbie;
1062 # tlbwe := r0_valid and r0.tlbld;
1063 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
1064 # -- clear all valid bits at once
1065 # for i in tlb_index_t loop
1066 # dtlb_valids(i) <= (others => '0');
1067 # end loop;
1068 # elsif tlbie = '1' then
1069 # if tlb_hit = '1' then
1070 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
1071 # end if;
1072 # elsif tlbwe = '1' then
1073 # if tlb_hit = '1' then
1074 # repl_way := tlb_hit_way;
1075 # else
1076 # repl_way := to_integer(unsigned(
1077 # tlb_plru_victim(tlb_req_index)));
1078 # end if;
1079 # eatag := r0.req.addr(
1080 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
1081 # );
1082 # tagset := tlb_tag_way;
1083 # write_tlb_tag(repl_way, tagset, eatag);
1084 # dtlb_tags(tlb_req_index) <= tagset;
1085 # pteset := tlb_pte_way;
1086 # write_tlb_pte(repl_way, pteset, r0.req.data);
1087 # dtlb_ptes(tlb_req_index) <= pteset;
1088 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
1089 # end if;
1090 # end if;
1091 # end process;
1092 #
1093 # -- Generate PLRUs
1094 # maybe_plrus: if NUM_WAYS > 1 generate
1095 # begin
1096 # plrus: for i in 0 to NUM_LINES-1 generate
1097 # -- PLRU interface
1098 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
1099 # signal plru_acc_en : std_ulogic;
1100 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
1101 #
1102 # begin
1103 # plru : entity work.plru
1104 # generic map (
1105 # BITS => WAY_BITS
1106 # )
1107 # port map (
1108 # clk => clk,
1109 # rst => rst,
1110 # acc => plru_acc,
1111 # acc_en => plru_acc_en,
1112 # lru => plru_out
1113 # );
1114 #
1115 # process(all)
1116 # begin
1117 # -- PLRU interface
1118 # if r1.hit_index = i then
1119 # plru_acc_en <= r1.cache_hit;
1120 # else
1121 # plru_acc_en <= '0';
1122 # end if;
1123 # plru_acc <= std_ulogic_vector(to_unsigned(
1124 # r1.hit_way, WAY_BITS
1125 # ));
1126 # plru_victim(i) <= plru_out;
1127 # end process;
1128 # end generate;
1129 # end generate;
1130 #
1131 # -- Cache tag RAM read port
1132 # cache_tag_read : process(clk)
1133 # variable index : index_t;
1134 # begin
1135 # if rising_edge(clk) then
1136 # if r0_stall = '1' then
1137 # index := req_index;
1138 # elsif m_in.valid = '1' then
1139 # index := get_index(m_in.addr);
1140 # else
1141 # index := get_index(d_in.addr);
1142 # end if;
1143 # cache_tag_set <= cache_tags(index);
1144 # end if;
1145 # end process;
1146 #
1147 # -- Cache request parsing and hit detection
1148 # dcache_request : process(all)
1149 # variable is_hit : std_ulogic;
1150 # variable hit_way : way_t;
1151 # variable op : op_t;
1152 # variable opsel : std_ulogic_vector(2 downto 0);
1153 # variable go : std_ulogic;
1154 # variable nc : std_ulogic;
1155 # variable s_hit : std_ulogic;
1156 # variable s_tag : cache_tag_t;
1157 # variable s_pte : tlb_pte_t;
1158 # variable s_ra : std_ulogic_vector(
1159 # REAL_ADDR_BITS - 1 downto 0
1160 # );
1161 # variable hit_set : std_ulogic_vector(
1162 # TLB_NUM_WAYS - 1 downto 0
1163 # );
1164 # variable hit_way_set : hit_way_set_t;
1165 # variable rel_matches : std_ulogic_vector(
1166 # TLB_NUM_WAYS - 1 downto 0
1167 # );
1168 # variable rel_match : std_ulogic;
1169 # begin
1170 # -- Extract line, row and tag from request
1171 # req_index <= get_index(r0.req.addr);
1172 # req_row <= get_row(r0.req.addr);
1173 # req_tag <= get_tag(ra);
1174 #
1175 # go := r0_valid and not (r0.tlbie or r0.tlbld)
1176 # and not r1.ls_error;
1177 #
1178 # -- Test if pending request is a hit on any way
1179 # -- In order to make timing in virtual mode,
1180 # -- when we are using the TLB, we compare each
1181 # --way with each of the real addresses from each way of
1182 # -- the TLB, and then decide later which match to use.
1183 # hit_way := 0;
1184 # is_hit := '0';
1185 # rel_match := '0';
1186 # if r0.req.virt_mode = '1' then
1187 # rel_matches := (others => '0');
1188 # for j in tlb_way_t loop
1189 # hit_way_set(j) := 0;
1190 # s_hit := '0';
1191 # s_pte := read_tlb_pte(j, tlb_pte_way);
1192 # s_ra :=
1193 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
1194 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
1195 # s_tag := get_tag(s_ra);
1196 # for i in way_t loop
1197 # if go = '1' and cache_valids(req_index)(i) = '1'
1198 # and read_tag(i, cache_tag_set) = s_tag
1199 # and tlb_valid_way(j) = '1' then
1200 # hit_way_set(j) := i;
1201 # s_hit := '1';
1202 # end if;
1203 # end loop;
1204 # hit_set(j) := s_hit;
1205 # if s_tag = r1.reload_tag then
1206 # rel_matches(j) := '1';
1207 # end if;
1208 # end loop;
1209 # if tlb_hit = '1' then
1210 # is_hit := hit_set(tlb_hit_way);
1211 # hit_way := hit_way_set(tlb_hit_way);
1212 # rel_match := rel_matches(tlb_hit_way);
1213 # end if;
1214 # else
1215 # s_tag := get_tag(r0.req.addr);
1216 # for i in way_t loop
1217 # if go = '1' and cache_valids(req_index)(i) = '1' and
1218 # read_tag(i, cache_tag_set) = s_tag then
1219 # hit_way := i;
1220 # is_hit := '1';
1221 # end if;
1222 # end loop;
1223 # if s_tag = r1.reload_tag then
1224 # rel_match := '1';
1225 # end if;
1226 # end if;
1227 # req_same_tag <= rel_match;
1228 #
1229 # -- See if the request matches the line currently being reloaded
1230 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
1231 # and rel_match = '1' then
1232 # -- For a store, consider this a hit even if the row isn't
1233 # -- valid since it will be by the time we perform the store.
1234 # -- For a load, check the appropriate row valid bit.
1235 # is_hit :=
1236 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
1237 # hit_way := replace_way;
1238 # end if;
1239 #
1240 # -- Whether to use forwarded data for a load or not
1241 # use_forward1_next <= '0';
1242 # if get_row(r1.req.real_addr) = req_row
1243 # and r1.req.hit_way = hit_way then
1244 # -- Only need to consider r1.write_bram here, since if we
1245 # -- are writing refill data here, then we don't have a
1246 # -- cache hit this cycle on the line being refilled.
1247 # -- (There is the possibility that the load following the
1248 # -- load miss that started the refill could be to the old
1249 # -- contents of the victim line, since it is a couple of
1250 # -- cycles after the refill starts before we see the updated
1251 # -- cache tag. In that case we don't use the bypass.)
1252 # use_forward1_next <= r1.write_bram;
1253 # end if;
1254 # use_forward2_next <= '0';
1255 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
1256 # use_forward2_next <= r1.forward_valid1;
1257 # end if;
1258 #
1259 # -- The way that matched on a hit
1260 # req_hit_way <= hit_way;
1261 #
1262 # -- The way to replace on a miss
1263 # if r1.write_tag = '1' then
1264 # replace_way <= to_integer(unsigned(
1265 # plru_victim(r1.store_index)
1266 # ));
1267 # else
1268 # replace_way <= r1.store_way;
1269 # end if;
1270 #
1271 # -- work out whether we have permission for this access
1272 # -- NB we don't yet implement AMR, thus no KUAP
1273 # rc_ok <= perm_attr.reference and
1274 # (r0.req.load or perm_attr.changed);
1275 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
1276 # (perm_attr.wr_perm or (r0.req.load
1277 # and perm_attr.rd_perm));
1278 # access_ok <= valid_ra and perm_ok and rc_ok;
1279 #
1280 # -- Combine the request and cache hit status to decide what
1281 # -- operation needs to be done
1282 # --
1283 # nc := r0.req.nc or perm_attr.nocache;
1284 # op := OP_NONE;
1285 # if go = '1' then
1286 # if access_ok = '0' then
1287 # op := OP_BAD;
1288 # elsif cancel_store = '1' then
1289 # op := OP_STCX_FAIL;
1290 # else
1291 # opsel := r0.req.load & nc & is_hit;
1292 # case opsel is
1293 # when "101" => op := OP_LOAD_HIT;
1294 # when "100" => op := OP_LOAD_MISS;
1295 # when "110" => op := OP_LOAD_NC;
1296 # when "001" => op := OP_STORE_HIT;
1297 # when "000" => op := OP_STORE_MISS;
1298 # when "010" => op := OP_STORE_MISS;
1299 # when "011" => op := OP_BAD;
1300 # when "111" => op := OP_BAD;
1301 # when others => op := OP_NONE;
1302 # end case;
1303 # end if;
1304 # end if;
1305 # req_op <= op;
1306 # req_go <= go;
1307 #
1308 # -- Version of the row number that is valid one cycle earlier
1309 # -- in the cases where we need to read the cache data BRAM.
1310 # -- If we're stalling then we need to keep reading the last
1311 # -- row requested.
1312 # if r0_stall = '0' then
1313 # if m_in.valid = '1' then
1314 # early_req_row <= get_row(m_in.addr);
1315 # else
1316 # early_req_row <= get_row(d_in.addr);
1317 # end if;
1318 # else
1319 # early_req_row <= req_row;
1320 # end if;
1321 # end process;
1322 #
1323 # -- Wire up wishbone request latch out of stage 1
1324 # wishbone_out <= r1.wb;
1325 #
1326 # -- Handle load-with-reservation and store-conditional instructions
1327 # reservation_comb: process(all)
1328 # begin
1329 # cancel_store <= '0';
1330 # set_rsrv <= '0';
1331 # clear_rsrv <= '0';
1332 # if r0_valid = '1' and r0.req.reserve = '1' then
1333 # -- XXX generate alignment interrupt if address
1334 # -- is not aligned XXX or if r0.req.nc = '1'
1335 # if r0.req.load = '1' then
1336 # -- load with reservation
1337 # set_rsrv <= '1';
1338 # else
1339 # -- store conditional
1340 # clear_rsrv <= '1';
1341 # if reservation.valid = '0' or r0.req.addr(63
1342 # downto LINE_OFF_BITS) /= reservation.addr then
1343 # cancel_store <= '1';
1344 # end if;
1345 # end if;
1346 # end if;
1347 # end process;
1348 #
1349 # reservation_reg: process(clk)
1350 # begin
1351 # if rising_edge(clk) then
1352 # if rst = '1' then
1353 # reservation.valid <= '0';
1354 # elsif r0_valid = '1' and access_ok = '1' then
1355 # if clear_rsrv = '1' then
1356 # reservation.valid <= '0';
1357 # elsif set_rsrv = '1' then
1358 # reservation.valid <= '1';
1359 # reservation.addr <=
1360 # r0.req.addr(63 downto LINE_OFF_BITS);
1361 # end if;
1362 # end if;
1363 # end if;
1364 # end process;
1365 #
1366 # -- Return data for loads & completion control logic
1367 # --
1368 # writeback_control: process(all)
1369 # variable data_out : std_ulogic_vector(63 downto 0);
1370 # variable data_fwd : std_ulogic_vector(63 downto 0);
1371 # variable j : integer;
1372 # begin
1373 # -- Use the bypass if are reading the row that was
1374 # -- written 1 or 2 cycles ago, including for the
1375 # -- slow_valid = 1 case (i.e. completing a load
1376 # -- miss or a non-cacheable load).
1377 # if r1.use_forward1 = '1' then
1378 # data_fwd := r1.forward_data1;
1379 # else
1380 # data_fwd := r1.forward_data2;
1381 # end if;
1382 # data_out := cache_out(r1.hit_way);
1383 # for i in 0 to 7 loop
1384 # j := i * 8;
1385 # if r1.forward_sel(i) = '1' then
1386 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1387 # end if;
1388 # end loop;
1389 #
1390 # d_out.valid <= r1.ls_valid;
1391 # d_out.data <= data_out;
1392 # d_out.store_done <= not r1.stcx_fail;
1393 # d_out.error <= r1.ls_error;
1394 # d_out.cache_paradox <= r1.cache_paradox;
1395 #
1396 # -- Outputs to MMU
1397 # m_out.done <= r1.mmu_done;
1398 # m_out.err <= r1.mmu_error;
1399 # m_out.data <= data_out;
1400 #
1401 # -- We have a valid load or store hit or we just completed
1402 # -- a slow op such as a load miss, a NC load or a store
1403 # --
1404 # -- Note: the load hit is delayed by one cycle. However it
1405 # -- can still not collide with r.slow_valid (well unless I
1406 # -- miscalculated) because slow_valid can only be set on a
1407 # -- subsequent request and not on its first cycle (the state
1408 # -- machine must have advanced), which makes slow_valid
1409 # -- at least 2 cycles from the previous hit_load_valid.
1410 #
1411 # -- Sanity: Only one of these must be set in any given cycle
1412 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1413 # report "unexpected slow_valid collision with stcx_fail"
1414 # severity FAILURE;
1415 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1416 # /= '1' report "unexpected hit_load_delayed collision with
1417 # slow_valid" severity FAILURE;
1418 #
1419 # if r1.mmu_req = '0' then
1420 # -- Request came from loadstore1...
1421 # -- Load hit case is the standard path
1422 # if r1.hit_load_valid = '1' then
1423 # report
1424 # "completing load hit data=" & to_hstring(data_out);
1425 # end if;
1426 #
1427 # -- error cases complete without stalling
1428 # if r1.ls_error = '1' then
1429 # report "completing ld/st with error";
1430 # end if;
1431 #
1432 # -- Slow ops (load miss, NC, stores)
1433 # if r1.slow_valid = '1' then
1434 # report
1435 # "completing store or load miss data="
1436 # & to_hstring(data_out);
1437 # end if;
1438 #
1439 # else
1440 # -- Request came from MMU
1441 # if r1.hit_load_valid = '1' then
1442 # report "completing load hit to MMU, data="
1443 # & to_hstring(m_out.data);
1444 # end if;
1445 #
1446 # -- error cases complete without stalling
1447 # if r1.mmu_error = '1' then
1448 # report "completing MMU ld with error";
1449 # end if;
1450 #
1451 # -- Slow ops (i.e. load miss)
1452 # if r1.slow_valid = '1' then
1453 # report "completing MMU load miss, data="
1454 # & to_hstring(m_out.data);
1455 # end if;
1456 # end if;
1457 #
1458 # end process;
1459 #
1460 #
1461 # -- Generate a cache RAM for each way. This handles the normal
1462 # -- reads, writes from reloads and the special store-hit update
1463 # -- path as well.
1464 # --
1465 # -- Note: the BRAMs have an extra read buffer, meaning the output
1466 # -- is pipelined an extra cycle. This differs from the
1467 # -- icache. The writeback logic needs to take that into
1468 # -- account by using 1-cycle delayed signals for load hits.
1469 # --
1470 # rams: for i in 0 to NUM_WAYS-1 generate
1471 # signal do_read : std_ulogic;
1472 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1473 # signal do_write : std_ulogic;
1474 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1475 # signal wr_data :
1476 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1477 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1478 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1479 # signal dout : cache_row_t;
1480 # begin
1481 # way: entity work.cache_ram
1482 # generic map (
1483 # ROW_BITS => ROW_BITS,
1484 # WIDTH => wishbone_data_bits,
1485 # ADD_BUF => true
1486 # )
1487 # port map (
1488 # clk => clk,
1489 # rd_en => do_read,
1490 # rd_addr => rd_addr,
1491 # rd_data => dout,
1492 # wr_sel => wr_sel_m,
1493 # wr_addr => wr_addr,
1494 # wr_data => wr_data
1495 # );
1496 # process(all)
1497 # begin
1498 # -- Cache hit reads
1499 # do_read <= '1';
1500 # rd_addr <=
1501 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1502 # cache_out(i) <= dout;
1503 #
1504 # -- Write mux:
1505 # --
1506 # -- Defaults to wishbone read responses (cache refill)
1507 # --
1508 # -- For timing, the mux on wr_data/sel/addr is not
1509 # -- dependent on anything other than the current state.
1510 # wr_sel_m <= (others => '0');
1511 #
1512 # do_write <= '0';
1513 # if r1.write_bram = '1' then
1514 # -- Write store data to BRAM. This happens one
1515 # -- cycle after the store is in r0.
1516 # wr_data <= r1.req.data;
1517 # wr_sel <= r1.req.byte_sel;
1518 # wr_addr <= std_ulogic_vector(to_unsigned(
1519 # get_row(r1.req.real_addr), ROW_BITS
1520 # ));
1521 # if i = r1.req.hit_way then
1522 # do_write <= '1';
1523 # end if;
1524 # else
1525 # -- Otherwise, we might be doing a reload or a DCBZ
1526 # if r1.dcbz = '1' then
1527 # wr_data <= (others => '0');
1528 # else
1529 # wr_data <= wishbone_in.dat;
1530 # end if;
1531 # wr_addr <= std_ulogic_vector(to_unsigned(
1532 # r1.store_row, ROW_BITS
1533 # ));
1534 # wr_sel <= (others => '1');
1535 #
1536 # if r1.state = RELOAD_WAIT_ACK and
1537 # wishbone_in.ack = '1' and replace_way = i then
1538 # do_write <= '1';
1539 # end if;
1540 # end if;
1541 #
1542 # -- Mask write selects with do_write since BRAM
1543 # -- doesn't have a global write-enable
1544 # if do_write = '1' then
1545 # wr_sel_m <= wr_sel;
1546 # end if;
1547 #
1548 # end process;
1549 # end generate;
1550 #
1551 # -- Cache hit synchronous machine for the easy case.
1552 # -- This handles load hits.
1553 # -- It also handles error cases (TLB miss, cache paradox)
1554 # dcache_fast_hit : process(clk)
1555 # begin
1556 # if rising_edge(clk) then
1557 # if req_op /= OP_NONE then
1558 # report "op:" & op_t'image(req_op) &
1559 # " addr:" & to_hstring(r0.req.addr) &
1560 # " nc:" & std_ulogic'image(r0.req.nc) &
1561 # " idx:" & integer'image(req_index) &
1562 # " tag:" & to_hstring(req_tag) &
1563 # " way: " & integer'image(req_hit_way);
1564 # end if;
1565 # if r0_valid = '1' then
1566 # r1.mmu_req <= r0.mmu_req;
1567 # end if;
1568 #
1569 # -- Fast path for load/store hits.
1570 # -- Set signals for the writeback controls.
1571 # r1.hit_way <= req_hit_way;
1572 # r1.hit_index <= req_index;
1573 # if req_op = OP_LOAD_HIT then
1574 # r1.hit_load_valid <= '1';
1575 # else
1576 # r1.hit_load_valid <= '0';
1577 # end if;
1578 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1579 # r1.cache_hit <= '1';
1580 # else
1581 # r1.cache_hit <= '0';
1582 # end if;
1583 #
1584 # if req_op = OP_BAD then
1585 # report "Signalling ld/st error valid_ra=" &
1586 # std_ulogic'image(valid_ra) & " rc_ok=" &
1587 # std_ulogic'image(rc_ok) & " perm_ok=" &
1588 # std_ulogic'image(perm_ok);
1589 # r1.ls_error <= not r0.mmu_req;
1590 # r1.mmu_error <= r0.mmu_req;
1591 # r1.cache_paradox <= access_ok;
1592 # else
1593 # r1.ls_error <= '0';
1594 # r1.mmu_error <= '0';
1595 # r1.cache_paradox <= '0';
1596 # end if;
1597 #
1598 # if req_op = OP_STCX_FAIL then
1599 # r1.stcx_fail <= '1';
1600 # else
1601 # r1.stcx_fail <= '0';
1602 # end if;
1603 #
1604 # -- Record TLB hit information for updating TLB PLRU
1605 # r1.tlb_hit <= tlb_hit;
1606 # r1.tlb_hit_way <= tlb_hit_way;
1607 # r1.tlb_hit_index <= tlb_req_index;
1608 #
1609 # end if;
1610 # end process;
1611 #
1612 # -- Memory accesses are handled by this state machine:
1613 # --
1614 # -- * Cache load miss/reload (in conjunction with "rams")
1615 # -- * Load hits for non-cachable forms
1616 # -- * Stores (the collision case is handled in "rams")
1617 # --
1618 # -- All wishbone requests generation is done here.
1619 # -- This machine operates at stage 1.
1620 # dcache_slow : process(clk)
1621 # variable stbs_done : boolean;
1622 # variable req : mem_access_request_t;
1623 # variable acks : unsigned(2 downto 0);
1624 # begin
1625 # if rising_edge(clk) then
1626 # r1.use_forward1 <= use_forward1_next;
1627 # r1.forward_sel <= (others => '0');
1628 # if use_forward1_next = '1' then
1629 # r1.forward_sel <= r1.req.byte_sel;
1630 # elsif use_forward2_next = '1' then
1631 # r1.forward_sel <= r1.forward_sel1;
1632 # end if;
1633 #
1634 # r1.forward_data2 <= r1.forward_data1;
1635 # if r1.write_bram = '1' then
1636 # r1.forward_data1 <= r1.req.data;
1637 # r1.forward_sel1 <= r1.req.byte_sel;
1638 # r1.forward_way1 <= r1.req.hit_way;
1639 # r1.forward_row1 <= get_row(r1.req.real_addr);
1640 # r1.forward_valid1 <= '1';
1641 # else
1642 # if r1.dcbz = '1' then
1643 # r1.forward_data1 <= (others => '0');
1644 # else
1645 # r1.forward_data1 <= wishbone_in.dat;
1646 # end if;
1647 # r1.forward_sel1 <= (others => '1');
1648 # r1.forward_way1 <= replace_way;
1649 # r1.forward_row1 <= r1.store_row;
1650 # r1.forward_valid1 <= '0';
1651 # end if;
1652 #
1653 # -- On reset, clear all valid bits to force misses
1654 # if rst = '1' then
1655 # for i in index_t loop
1656 # cache_valids(i) <= (others => '0');
1657 # end loop;
1658 # r1.state <= IDLE;
1659 # r1.full <= '0';
1660 # r1.slow_valid <= '0';
1661 # r1.wb.cyc <= '0';
1662 # r1.wb.stb <= '0';
1663 # r1.ls_valid <= '0';
1664 # r1.mmu_done <= '0';
1665 #
1666 # -- Not useful normally but helps avoiding
1667 # -- tons of sim warnings
1668 # r1.wb.adr <= (others => '0');
1669 # else
1670 # -- One cycle pulses reset
1671 # r1.slow_valid <= '0';
1672 # r1.write_bram <= '0';
1673 # r1.inc_acks <= '0';
1674 # r1.dec_acks <= '0';
1675 #
1676 # r1.ls_valid <= '0';
1677 # -- complete tlbies and TLB loads in the third cycle
1678 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
1679 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
1680 # if r0.mmu_req = '0' then
1681 # r1.ls_valid <= '1';
1682 # else
1683 # r1.mmu_done <= '1';
1684 # end if;
1685 # end if;
1686 #
1687 # if r1.write_tag = '1' then
1688 # -- Store new tag in selected way
1689 # for i in 0 to NUM_WAYS-1 loop
1690 # if i = replace_way then
1691 # cache_tags(r1.store_index)(
1692 # (i + 1) * TAG_WIDTH - 1
1693 # downto i * TAG_WIDTH
1694 # ) <=
1695 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
1696 # & r1.reload_tag;
1697 # end if;
1698 # end loop;
1699 # r1.store_way <= replace_way;
1700 # r1.write_tag <= '0';
1701 # end if;
1702 #
1703 # -- Take request from r1.req if there is one there,
1704 # -- else from req_op, ra, etc.
1705 # if r1.full = '1' then
1706 # req := r1.req;
1707 # else
1708 # req.op := req_op;
1709 # req.valid := req_go;
1710 # req.mmu_req := r0.mmu_req;
1711 # req.dcbz := r0.req.dcbz;
1712 # req.real_addr := ra;
1713 # -- Force data to 0 for dcbz
1714 # if r0.req.dcbz = '0' then
1715 # req.data := r0.req.data;
1716 # else
1717 # req.data := (others => '0');
1718 # end if;
1719 # -- Select all bytes for dcbz
1720 # -- and for cacheable loads
1721 # if r0.req.dcbz = '1'
1722 # or (r0.req.load = '1' and r0.req.nc = '0') then
1723 # req.byte_sel := (others => '1');
1724 # else
1725 # req.byte_sel := r0.req.byte_sel;
1726 # end if;
1727 # req.hit_way := req_hit_way;
1728 # req.same_tag := req_same_tag;
1729 #
1730 # -- Store the incoming request from r0,
1731 # -- if it is a slow request
1732 # -- Note that r1.full = 1 implies req_op = OP_NONE
1733 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
1734 # or req_op = OP_STORE_MISS
1735 # or req_op = OP_STORE_HIT then
1736 # r1.req <= req;
1737 # r1.full <= '1';
1738 # end if;
1739 # end if;
1740 #
1741 # -- Main state machine
1742 # case r1.state is
1743 # when IDLE =>
1744 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1745 # r1.wb.sel <= req.byte_sel;
1746 # r1.wb.dat <= req.data;
1747 # r1.dcbz <= req.dcbz;
1748 #
1749 # -- Keep track of our index and way
1750 # -- for subsequent stores.
1751 # r1.store_index <= get_index(req.real_addr);
1752 # r1.store_row <= get_row(req.real_addr);
1753 # r1.end_row_ix <=
1754 # get_row_of_line(get_row(req.real_addr)) - 1;
1755 # r1.reload_tag <= get_tag(req.real_addr);
1756 # r1.req.same_tag <= '1';
1757 #
1758 # if req.op = OP_STORE_HIT then
1759 # r1.store_way <= req.hit_way;
1760 # end if;
1761 #
1762 # -- Reset per-row valid bits,
1763 # -- ready for handling OP_LOAD_MISS
1764 # for i in 0 to ROW_PER_LINE - 1 loop
1765 # r1.rows_valid(i) <= '0';
1766 # end loop;
1767 #
1768 # case req.op is
1769 # when OP_LOAD_HIT =>
1770 # -- stay in IDLE state
1771 #
1772 # when OP_LOAD_MISS =>
1773 # -- Normal load cache miss,
1774 # -- start the reload machine
1775 # report "cache miss real addr:" &
1776 # to_hstring(req.real_addr) & " idx:" &
1777 # integer'image(get_index(req.real_addr)) &
1778 # " tag:" & to_hstring(get_tag(req.real_addr));
1779 #
1780 # -- Start the wishbone cycle
1781 # r1.wb.we <= '0';
1782 # r1.wb.cyc <= '1';
1783 # r1.wb.stb <= '1';
1784 #
1785 # -- Track that we had one request sent
1786 # r1.state <= RELOAD_WAIT_ACK;
1787 # r1.write_tag <= '1';
1788 #
1789 # when OP_LOAD_NC =>
1790 # r1.wb.cyc <= '1';
1791 # r1.wb.stb <= '1';
1792 # r1.wb.we <= '0';
1793 # r1.state <= NC_LOAD_WAIT_ACK;
1794 #
1795 # when OP_STORE_HIT | OP_STORE_MISS =>
1796 # if req.dcbz = '0' then
1797 # r1.state <= STORE_WAIT_ACK;
1798 # r1.acks_pending <= to_unsigned(1, 3);
1799 # r1.full <= '0';
1800 # r1.slow_valid <= '1';
1801 # if req.mmu_req = '0' then
1802 # r1.ls_valid <= '1';
1803 # else
1804 # r1.mmu_done <= '1';
1805 # end if;
1806 # if req.op = OP_STORE_HIT then
1807 # r1.write_bram <= '1';
1808 # end if;
1809 # else
1810 # -- dcbz is handled much like a load
1811 # -- miss except that we are writing
1812 # -- to memory instead of reading
1813 # r1.state <= RELOAD_WAIT_ACK;
1814 # if req.op = OP_STORE_MISS then
1815 # r1.write_tag <= '1';
1816 # end if;
1817 # end if;
1818 # r1.wb.we <= '1';
1819 # r1.wb.cyc <= '1';
1820 # r1.wb.stb <= '1';
1821 #
1822 # -- OP_NONE and OP_BAD do nothing
1823 # -- OP_BAD & OP_STCX_FAIL were handled above already
1824 # when OP_NONE =>
1825 # when OP_BAD =>
1826 # when OP_STCX_FAIL =>
1827 # end case;
1828 #
1829 # when RELOAD_WAIT_ACK =>
1830 # -- Requests are all sent if stb is 0
1831 # stbs_done := r1.wb.stb = '0';
1832 #
1833 # -- If we are still sending requests,
1834 # -- was one accepted?
1835 # if wishbone_in.stall = '0' and not stbs_done then
1836 # -- That was the last word ? We are done sending.
1837 # -- Clear stb and set stbs_done so we can handle
1838 # -- an eventual last ack on the same cycle.
1839 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
1840 # r1.wb.stb <= '0';
1841 # stbs_done := true;
1842 # end if;
1843 #
1844 # -- Calculate the next row address
1845 # r1.wb.adr <= next_row_addr(r1.wb.adr);
1846 # end if;
1847 #
1848 # -- Incoming acks processing
1849 # r1.forward_valid1 <= wishbone_in.ack;
1850 # if wishbone_in.ack = '1' then
1851 # r1.rows_valid(
1852 # r1.store_row mod ROW_PER_LINE
1853 # ) <= '1';
1854 # -- If this is the data we were looking for,
1855 # -- we can complete the request next cycle.
1856 # -- Compare the whole address in case the
1857 # -- request in r1.req is not the one that
1858 # -- started this refill.
1859 # if r1.full = '1' and r1.req.same_tag = '1'
1860 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
1861 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
1862 # and r1.store_row = get_row(r1.req.real_addr) then
1863 # r1.full <= '0';
1864 # r1.slow_valid <= '1';
1865 # if r1.mmu_req = '0' then
1866 # r1.ls_valid <= '1';
1867 # else
1868 # r1.mmu_done <= '1';
1869 # end if;
1870 # r1.forward_sel <= (others => '1');
1871 # r1.use_forward1 <= '1';
1872 # end if;
1873 #
1874 # -- Check for completion
1875 # if stbs_done and is_last_row(r1.store_row,
1876 # r1.end_row_ix) then
1877 # -- Complete wishbone cycle
1878 # r1.wb.cyc <= '0';
1879 #
1880 # -- Cache line is now valid
1881 # cache_valids(r1.store_index)(
1882 # r1.store_way
1883 # ) <= '1';
1884 #
1885 # r1.state <= IDLE;
1886 # end if;
1887 #
1888 # -- Increment store row counter
1889 # r1.store_row <= next_row(r1.store_row);
1890 # end if;
1891 #
1892 # when STORE_WAIT_ACK =>
1893 # stbs_done := r1.wb.stb = '0';
1894 # acks := r1.acks_pending;
1895 # if r1.inc_acks /= r1.dec_acks then
1896 # if r1.inc_acks = '1' then
1897 # acks := acks + 1;
1898 # else
1899 # acks := acks - 1;
1900 # end if;
1901 # end if;
1902 # r1.acks_pending <= acks;
1903 # -- Clear stb when slave accepted request
1904 # if wishbone_in.stall = '0' then
1905 # -- See if there is another store waiting
1906 # -- to be done which is in the same real page.
1907 # if req.valid = '1' then
1908 # r1.wb.adr(
1909 # SET_SIZE_BITS - 1 downto 0
1910 # ) <= req.real_addr(
1911 # SET_SIZE_BITS - 1 downto 0
1912 # );
1913 # r1.wb.dat <= req.data;
1914 # r1.wb.sel <= req.byte_sel;
1915 # end if;
1916 # if acks < 7 and req.same_tag = '1'
1917 # and (req.op = OP_STORE_MISS
1918 # or req.op = OP_STORE_HIT) then
1919 # r1.wb.stb <= '1';
1920 # stbs_done := false;
1921 # if req.op = OP_STORE_HIT then
1922 # r1.write_bram <= '1';
1923 # end if;
1924 # r1.full <= '0';
1925 # r1.slow_valid <= '1';
1926 # -- Store requests never come from the MMU
1927 # r1.ls_valid <= '1';
1928 # stbs_done := false;
1929 # r1.inc_acks <= '1';
1930 # else
1931 # r1.wb.stb <= '0';
1932 # stbs_done := true;
1933 # end if;
1934 # end if;
1935 #
1936 # -- Got ack ? See if complete.
1937 # if wishbone_in.ack = '1' then
1938 # if stbs_done and acks = 1 then
1939 # r1.state <= IDLE;
1940 # r1.wb.cyc <= '0';
1941 # r1.wb.stb <= '0';
1942 # end if;
1943 # r1.dec_acks <= '1';
1944 # end if;
1945 #
1946 # when NC_LOAD_WAIT_ACK =>
1947 # -- Clear stb when slave accepted request
1948 # if wishbone_in.stall = '0' then
1949 # r1.wb.stb <= '0';
1950 # end if;
1951 #
1952 # -- Got ack ? complete.
1953 # if wishbone_in.ack = '1' then
1954 # r1.state <= IDLE;
1955 # r1.full <= '0';
1956 # r1.slow_valid <= '1';
1957 # if r1.mmu_req = '0' then
1958 # r1.ls_valid <= '1';
1959 # else
1960 # r1.mmu_done <= '1';
1961 # end if;
1962 # r1.forward_sel <= (others => '1');
1963 # r1.use_forward1 <= '1';
1964 # r1.wb.cyc <= '0';
1965 # r1.wb.stb <= '0';
1966 # end if;
1967 # end case;
1968 # end if;
1969 # end if;
1970 # end process;
1971 #
1972 # dc_log: if LOG_LENGTH > 0 generate
1973 # signal log_data : std_ulogic_vector(19 downto 0);
1974 # begin
1975 # dcache_log: process(clk)
1976 # begin
1977 # if rising_edge(clk) then
1978 # log_data <= r1.wb.adr(5 downto 3) &
1979 # wishbone_in.stall &
1980 # wishbone_in.ack &
1981 # r1.wb.stb & r1.wb.cyc &
1982 # d_out.error &
1983 # d_out.valid &
1984 # std_ulogic_vector(
1985 # to_unsigned(op_t'pos(req_op), 3)) &
1986 # stall_out &
1987 # std_ulogic_vector(
1988 # to_unsigned(tlb_hit_way, 3)) &
1989 # valid_ra &
1990 # std_ulogic_vector(
1991 # to_unsigned(state_t'pos(r1.state), 3));
1992 # end if;
1993 # end process;
1994 # log_out <= log_data;
1995 # end generate;
1996 # end;