add comment in dcache.py
[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 # assert LINE_SIZE mod ROW_SIZE = 0
881 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
882 # assert ispow2(LINE_SIZE)
883 # report "LINE_SIZE not power of 2" severity FAILURE;
884 # assert ispow2(NUM_LINES)
885 # report "NUM_LINES not power of 2" severity FAILURE;
886 # assert ispow2(ROW_PER_LINE)
887 # report "ROW_PER_LINE not power of 2" severity FAILURE;
888 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
889 # report "geometry bits don't add up" severity FAILURE;
890 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
891 # report "geometry bits don't add up" severity FAILURE;
892 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
893 # report "geometry bits don't add up" severity FAILURE;
894 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
895 # report "geometry bits don't add up" severity FAILURE;
896 # assert (64 = wishbone_data_bits)
897 # report "Can't yet handle a wishbone width that isn't 64-bits"
898 # severity FAILURE;
899 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
900 # report "Set indexed by virtual address" severity FAILURE;
901 #
902 # -- Latch the request in r0.req as long as we're not stalling
903 # stage_0 : process(clk)
904 # variable r : reg_stage_0_t;
905 # begin
906 # if rising_edge(clk) then
907 # assert (d_in.valid and m_in.valid) = '0'
908 # report "request collision loadstore vs MMU";
909 # if m_in.valid = '1' then
910 # r.req.valid := '1';
911 # r.req.load := not (m_in.tlbie or m_in.tlbld);
912 # r.req.dcbz := '0';
913 # r.req.nc := '0';
914 # r.req.reserve := '0';
915 # r.req.virt_mode := '0';
916 # r.req.priv_mode := '1';
917 # r.req.addr := m_in.addr;
918 # r.req.data := m_in.pte;
919 # r.req.byte_sel := (others => '1');
920 # r.tlbie := m_in.tlbie;
921 # r.doall := m_in.doall;
922 # r.tlbld := m_in.tlbld;
923 # r.mmu_req := '1';
924 # else
925 # r.req := d_in;
926 # r.tlbie := '0';
927 # r.doall := '0';
928 # r.tlbld := '0';
929 # r.mmu_req := '0';
930 # end if;
931 # if rst = '1' then
932 # r0_full <= '0';
933 # elsif r1.full = '0' or r0_full = '0' then
934 # r0 <= r;
935 # r0_full <= r.req.valid;
936 # end if;
937 # end if;
938 # end process;
939 #
940 # -- we don't yet handle collisions between loadstore1 requests
941 # -- and MMU requests
942 # m_out.stall <= '0';
943 #
944 # -- Hold off the request in r0 when r1 has an uncompleted request
945 # r0_stall <= r0_full and r1.full;
946 # r0_valid <= r0_full and not r1.full;
947 # stall_out <= r0_stall;
948 #
949 # -- TLB
950 # -- Operates in the second cycle on the request latched in r0.req.
951 # -- TLB updates write the entry at the end of the second cycle.
952 # tlb_read : process(clk)
953 # variable index : tlb_index_t;
954 # variable addrbits :
955 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
956 # begin
957 # if rising_edge(clk) then
958 # if m_in.valid = '1' then
959 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
960 # - 1 downto TLB_LG_PGSZ);
961 # else
962 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
963 # - 1 downto TLB_LG_PGSZ);
964 # end if;
965 # index := to_integer(unsigned(addrbits));
966 # -- If we have any op and the previous op isn't finished,
967 # -- then keep the same output for next cycle.
968 # if r0_stall = '0' then
969 # tlb_valid_way <= dtlb_valids(index);
970 # tlb_tag_way <= dtlb_tags(index);
971 # tlb_pte_way <= dtlb_ptes(index);
972 # end if;
973 # end if;
974 # end process;
975 #
976 # -- Generate TLB PLRUs
977 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
978 # begin
979 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
980 # -- TLB PLRU interface
981 # signal tlb_plru_acc :
982 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
983 # signal tlb_plru_acc_en : std_ulogic;
984 # signal tlb_plru_out :
985 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
986 # begin
987 # tlb_plru : entity work.plru
988 # generic map (
989 # BITS => TLB_WAY_BITS
990 # )
991 # port map (
992 # clk => clk,
993 # rst => rst,
994 # acc => tlb_plru_acc,
995 # acc_en => tlb_plru_acc_en,
996 # lru => tlb_plru_out
997 # );
998 #
999 # process(all)
1000 # begin
1001 # -- PLRU interface
1002 # if r1.tlb_hit_index = i then
1003 # tlb_plru_acc_en <= r1.tlb_hit;
1004 # else
1005 # tlb_plru_acc_en <= '0';
1006 # end if;
1007 # tlb_plru_acc <=
1008 # std_ulogic_vector(to_unsigned(
1009 # r1.tlb_hit_way, TLB_WAY_BITS
1010 # ));
1011 # tlb_plru_victim(i) <= tlb_plru_out;
1012 # end process;
1013 # end generate;
1014 # end generate;
1015 #
1016 # tlb_search : process(all)
1017 # variable hitway : tlb_way_t;
1018 # variable hit : std_ulogic;
1019 # variable eatag : tlb_tag_t;
1020 # begin
1021 # tlb_req_index <=
1022 # to_integer(unsigned(r0.req.addr(
1023 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
1024 # )));
1025 # hitway := 0;
1026 # hit := '0';
1027 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
1028 # for i in tlb_way_t loop
1029 # if tlb_valid_way(i) = '1' and
1030 # read_tlb_tag(i, tlb_tag_way) = eatag then
1031 # hitway := i;
1032 # hit := '1';
1033 # end if;
1034 # end loop;
1035 # tlb_hit <= hit and r0_valid;
1036 # tlb_hit_way <= hitway;
1037 # if tlb_hit = '1' then
1038 # pte <= read_tlb_pte(hitway, tlb_pte_way);
1039 # else
1040 # pte <= (others => '0');
1041 # end if;
1042 # valid_ra <= tlb_hit or not r0.req.virt_mode;
1043 # if r0.req.virt_mode = '1' then
1044 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
1045 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
1046 # (ROW_OFF_BITS-1 downto 0 => '0');
1047 # perm_attr <= extract_perm_attr(pte);
1048 # else
1049 # ra <= r0.req.addr(
1050 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
1051 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
1052 # perm_attr <= real_mode_perm_attr;
1053 # end if;
1054 # end process;
1055 #
1056 # tlb_update : process(clk)
1057 # variable tlbie : std_ulogic;
1058 # variable tlbwe : std_ulogic;
1059 # variable repl_way : tlb_way_t;
1060 # variable eatag : tlb_tag_t;
1061 # variable tagset : tlb_way_tags_t;
1062 # variable pteset : tlb_way_ptes_t;
1063 # begin
1064 # if rising_edge(clk) then
1065 # tlbie := r0_valid and r0.tlbie;
1066 # tlbwe := r0_valid and r0.tlbld;
1067 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
1068 # -- clear all valid bits at once
1069 # for i in tlb_index_t loop
1070 # dtlb_valids(i) <= (others => '0');
1071 # end loop;
1072 # elsif tlbie = '1' then
1073 # if tlb_hit = '1' then
1074 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
1075 # end if;
1076 # elsif tlbwe = '1' then
1077 # if tlb_hit = '1' then
1078 # repl_way := tlb_hit_way;
1079 # else
1080 # repl_way := to_integer(unsigned(
1081 # tlb_plru_victim(tlb_req_index)));
1082 # end if;
1083 # eatag := r0.req.addr(
1084 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
1085 # );
1086 # tagset := tlb_tag_way;
1087 # write_tlb_tag(repl_way, tagset, eatag);
1088 # dtlb_tags(tlb_req_index) <= tagset;
1089 # pteset := tlb_pte_way;
1090 # write_tlb_pte(repl_way, pteset, r0.req.data);
1091 # dtlb_ptes(tlb_req_index) <= pteset;
1092 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
1093 # end if;
1094 # end if;
1095 # end process;
1096 #
1097 # -- Generate PLRUs
1098 # maybe_plrus: if NUM_WAYS > 1 generate
1099 # begin
1100 # plrus: for i in 0 to NUM_LINES-1 generate
1101 # -- PLRU interface
1102 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
1103 # signal plru_acc_en : std_ulogic;
1104 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
1105 #
1106 # begin
1107 # plru : entity work.plru
1108 # generic map (
1109 # BITS => WAY_BITS
1110 # )
1111 # port map (
1112 # clk => clk,
1113 # rst => rst,
1114 # acc => plru_acc,
1115 # acc_en => plru_acc_en,
1116 # lru => plru_out
1117 # );
1118 #
1119 # process(all)
1120 # begin
1121 # -- PLRU interface
1122 # if r1.hit_index = i then
1123 # plru_acc_en <= r1.cache_hit;
1124 # else
1125 # plru_acc_en <= '0';
1126 # end if;
1127 # plru_acc <= std_ulogic_vector(to_unsigned(
1128 # r1.hit_way, WAY_BITS
1129 # ));
1130 # plru_victim(i) <= plru_out;
1131 # end process;
1132 # end generate;
1133 # end generate;
1134 #
1135 # -- Cache tag RAM read port
1136 # cache_tag_read : process(clk)
1137 # variable index : index_t;
1138 # begin
1139 # if rising_edge(clk) then
1140 # if r0_stall = '1' then
1141 # index := req_index;
1142 # elsif m_in.valid = '1' then
1143 # index := get_index(m_in.addr);
1144 # else
1145 # index := get_index(d_in.addr);
1146 # end if;
1147 # cache_tag_set <= cache_tags(index);
1148 # end if;
1149 # end process;
1150 #
1151 # -- Cache request parsing and hit detection
1152 # dcache_request : process(all)
1153 # variable is_hit : std_ulogic;
1154 # variable hit_way : way_t;
1155 # variable op : op_t;
1156 # variable opsel : std_ulogic_vector(2 downto 0);
1157 # variable go : std_ulogic;
1158 # variable nc : std_ulogic;
1159 # variable s_hit : std_ulogic;
1160 # variable s_tag : cache_tag_t;
1161 # variable s_pte : tlb_pte_t;
1162 # variable s_ra : std_ulogic_vector(
1163 # REAL_ADDR_BITS - 1 downto 0
1164 # );
1165 # variable hit_set : std_ulogic_vector(
1166 # TLB_NUM_WAYS - 1 downto 0
1167 # );
1168 # variable hit_way_set : hit_way_set_t;
1169 # variable rel_matches : std_ulogic_vector(
1170 # TLB_NUM_WAYS - 1 downto 0
1171 # );
1172 # variable rel_match : std_ulogic;
1173 # begin
1174 # -- Extract line, row and tag from request
1175 # req_index <= get_index(r0.req.addr);
1176 # req_row <= get_row(r0.req.addr);
1177 # req_tag <= get_tag(ra);
1178 #
1179 # go := r0_valid and not (r0.tlbie or r0.tlbld)
1180 # and not r1.ls_error;
1181 #
1182 # -- Test if pending request is a hit on any way
1183 # -- In order to make timing in virtual mode,
1184 # -- when we are using the TLB, we compare each
1185 # --way with each of the real addresses from each way of
1186 # -- the TLB, and then decide later which match to use.
1187 # hit_way := 0;
1188 # is_hit := '0';
1189 # rel_match := '0';
1190 # if r0.req.virt_mode = '1' then
1191 # rel_matches := (others => '0');
1192 # for j in tlb_way_t loop
1193 # hit_way_set(j) := 0;
1194 # s_hit := '0';
1195 # s_pte := read_tlb_pte(j, tlb_pte_way);
1196 # s_ra :=
1197 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
1198 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
1199 # s_tag := get_tag(s_ra);
1200 # for i in way_t loop
1201 # if go = '1' and cache_valids(req_index)(i) = '1'
1202 # and read_tag(i, cache_tag_set) = s_tag
1203 # and tlb_valid_way(j) = '1' then
1204 # hit_way_set(j) := i;
1205 # s_hit := '1';
1206 # end if;
1207 # end loop;
1208 # hit_set(j) := s_hit;
1209 # if s_tag = r1.reload_tag then
1210 # rel_matches(j) := '1';
1211 # end if;
1212 # end loop;
1213 # if tlb_hit = '1' then
1214 # is_hit := hit_set(tlb_hit_way);
1215 # hit_way := hit_way_set(tlb_hit_way);
1216 # rel_match := rel_matches(tlb_hit_way);
1217 # end if;
1218 # else
1219 # s_tag := get_tag(r0.req.addr);
1220 # for i in way_t loop
1221 # if go = '1' and cache_valids(req_index)(i) = '1' and
1222 # read_tag(i, cache_tag_set) = s_tag then
1223 # hit_way := i;
1224 # is_hit := '1';
1225 # end if;
1226 # end loop;
1227 # if s_tag = r1.reload_tag then
1228 # rel_match := '1';
1229 # end if;
1230 # end if;
1231 # req_same_tag <= rel_match;
1232 #
1233 # -- See if the request matches the line currently being reloaded
1234 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
1235 # and rel_match = '1' then
1236 # -- For a store, consider this a hit even if the row isn't
1237 # -- valid since it will be by the time we perform the store.
1238 # -- For a load, check the appropriate row valid bit.
1239 # is_hit :=
1240 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
1241 # hit_way := replace_way;
1242 # end if;
1243 #
1244 # -- Whether to use forwarded data for a load or not
1245 # use_forward1_next <= '0';
1246 # if get_row(r1.req.real_addr) = req_row
1247 # and r1.req.hit_way = hit_way then
1248 # -- Only need to consider r1.write_bram here, since if we
1249 # -- are writing refill data here, then we don't have a
1250 # -- cache hit this cycle on the line being refilled.
1251 # -- (There is the possibility that the load following the
1252 # -- load miss that started the refill could be to the old
1253 # -- contents of the victim line, since it is a couple of
1254 # -- cycles after the refill starts before we see the updated
1255 # -- cache tag. In that case we don't use the bypass.)
1256 # use_forward1_next <= r1.write_bram;
1257 # end if;
1258 # use_forward2_next <= '0';
1259 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
1260 # use_forward2_next <= r1.forward_valid1;
1261 # end if;
1262 #
1263 # -- The way that matched on a hit
1264 # req_hit_way <= hit_way;
1265 #
1266 # -- The way to replace on a miss
1267 # if r1.write_tag = '1' then
1268 # replace_way <= to_integer(unsigned(
1269 # plru_victim(r1.store_index)
1270 # ));
1271 # else
1272 # replace_way <= r1.store_way;
1273 # end if;
1274 #
1275 # -- work out whether we have permission for this access
1276 # -- NB we don't yet implement AMR, thus no KUAP
1277 # rc_ok <= perm_attr.reference and
1278 # (r0.req.load or perm_attr.changed);
1279 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
1280 # (perm_attr.wr_perm or (r0.req.load
1281 # and perm_attr.rd_perm));
1282 # access_ok <= valid_ra and perm_ok and rc_ok;
1283 #
1284 # -- Combine the request and cache hit status to decide what
1285 # -- operation needs to be done
1286 # --
1287 # nc := r0.req.nc or perm_attr.nocache;
1288 # op := OP_NONE;
1289 # if go = '1' then
1290 # if access_ok = '0' then
1291 # op := OP_BAD;
1292 # elsif cancel_store = '1' then
1293 # op := OP_STCX_FAIL;
1294 # else
1295 # opsel := r0.req.load & nc & is_hit;
1296 # case opsel is
1297 # when "101" => op := OP_LOAD_HIT;
1298 # when "100" => op := OP_LOAD_MISS;
1299 # when "110" => op := OP_LOAD_NC;
1300 # when "001" => op := OP_STORE_HIT;
1301 # when "000" => op := OP_STORE_MISS;
1302 # when "010" => op := OP_STORE_MISS;
1303 # when "011" => op := OP_BAD;
1304 # when "111" => op := OP_BAD;
1305 # when others => op := OP_NONE;
1306 # end case;
1307 # end if;
1308 # end if;
1309 # req_op <= op;
1310 # req_go <= go;
1311 #
1312 # -- Version of the row number that is valid one cycle earlier
1313 # -- in the cases where we need to read the cache data BRAM.
1314 # -- If we're stalling then we need to keep reading the last
1315 # -- row requested.
1316 # if r0_stall = '0' then
1317 # if m_in.valid = '1' then
1318 # early_req_row <= get_row(m_in.addr);
1319 # else
1320 # early_req_row <= get_row(d_in.addr);
1321 # end if;
1322 # else
1323 # early_req_row <= req_row;
1324 # end if;
1325 # end process;
1326 #
1327 # -- Wire up wishbone request latch out of stage 1
1328 # wishbone_out <= r1.wb;
1329 #
1330 # -- Handle load-with-reservation and store-conditional instructions
1331 # reservation_comb: process(all)
1332 # begin
1333 # cancel_store <= '0';
1334 # set_rsrv <= '0';
1335 # clear_rsrv <= '0';
1336 # if r0_valid = '1' and r0.req.reserve = '1' then
1337 # -- XXX generate alignment interrupt if address
1338 # -- is not aligned XXX or if r0.req.nc = '1'
1339 # if r0.req.load = '1' then
1340 # -- load with reservation
1341 # set_rsrv <= '1';
1342 # else
1343 # -- store conditional
1344 # clear_rsrv <= '1';
1345 # if reservation.valid = '0' or r0.req.addr(63
1346 # downto LINE_OFF_BITS) /= reservation.addr then
1347 # cancel_store <= '1';
1348 # end if;
1349 # end if;
1350 # end if;
1351 # end process;
1352 #
1353 # reservation_reg: process(clk)
1354 # begin
1355 # if rising_edge(clk) then
1356 # if rst = '1' then
1357 # reservation.valid <= '0';
1358 # elsif r0_valid = '1' and access_ok = '1' then
1359 # if clear_rsrv = '1' then
1360 # reservation.valid <= '0';
1361 # elsif set_rsrv = '1' then
1362 # reservation.valid <= '1';
1363 # reservation.addr <=
1364 # r0.req.addr(63 downto LINE_OFF_BITS);
1365 # end if;
1366 # end if;
1367 # end if;
1368 # end process;
1369 #
1370 # -- Return data for loads & completion control logic
1371 # --
1372 # writeback_control: process(all)
1373 # variable data_out : std_ulogic_vector(63 downto 0);
1374 # variable data_fwd : std_ulogic_vector(63 downto 0);
1375 # variable j : integer;
1376 # begin
1377 # -- Use the bypass if are reading the row that was
1378 # -- written 1 or 2 cycles ago, including for the
1379 # -- slow_valid = 1 case (i.e. completing a load
1380 # -- miss or a non-cacheable load).
1381 # if r1.use_forward1 = '1' then
1382 # data_fwd := r1.forward_data1;
1383 # else
1384 # data_fwd := r1.forward_data2;
1385 # end if;
1386 # data_out := cache_out(r1.hit_way);
1387 # for i in 0 to 7 loop
1388 # j := i * 8;
1389 # if r1.forward_sel(i) = '1' then
1390 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1391 # end if;
1392 # end loop;
1393 #
1394 # d_out.valid <= r1.ls_valid;
1395 # d_out.data <= data_out;
1396 # d_out.store_done <= not r1.stcx_fail;
1397 # d_out.error <= r1.ls_error;
1398 # d_out.cache_paradox <= r1.cache_paradox;
1399 #
1400 # -- Outputs to MMU
1401 # m_out.done <= r1.mmu_done;
1402 # m_out.err <= r1.mmu_error;
1403 # m_out.data <= data_out;
1404 #
1405 # -- We have a valid load or store hit or we just completed
1406 # -- a slow op such as a load miss, a NC load or a store
1407 # --
1408 # -- Note: the load hit is delayed by one cycle. However it
1409 # -- can still not collide with r.slow_valid (well unless I
1410 # -- miscalculated) because slow_valid can only be set on a
1411 # -- subsequent request and not on its first cycle (the state
1412 # -- machine must have advanced), which makes slow_valid
1413 # -- at least 2 cycles from the previous hit_load_valid.
1414 #
1415 # -- Sanity: Only one of these must be set in any given cycle
1416 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1417 # report "unexpected slow_valid collision with stcx_fail"
1418 # severity FAILURE;
1419 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1420 # /= '1' report "unexpected hit_load_delayed collision with
1421 # slow_valid" severity FAILURE;
1422 #
1423 # if r1.mmu_req = '0' then
1424 # -- Request came from loadstore1...
1425 # -- Load hit case is the standard path
1426 # if r1.hit_load_valid = '1' then
1427 # report
1428 # "completing load hit data=" & to_hstring(data_out);
1429 # end if;
1430 #
1431 # -- error cases complete without stalling
1432 # if r1.ls_error = '1' then
1433 # report "completing ld/st with error";
1434 # end if;
1435 #
1436 # -- Slow ops (load miss, NC, stores)
1437 # if r1.slow_valid = '1' then
1438 # report
1439 # "completing store or load miss data="
1440 # & to_hstring(data_out);
1441 # end if;
1442 #
1443 # else
1444 # -- Request came from MMU
1445 # if r1.hit_load_valid = '1' then
1446 # report "completing load hit to MMU, data="
1447 # & to_hstring(m_out.data);
1448 # end if;
1449 #
1450 # -- error cases complete without stalling
1451 # if r1.mmu_error = '1' then
1452 # report "completing MMU ld with error";
1453 # end if;
1454 #
1455 # -- Slow ops (i.e. load miss)
1456 # if r1.slow_valid = '1' then
1457 # report "completing MMU load miss, data="
1458 # & to_hstring(m_out.data);
1459 # end if;
1460 # end if;
1461 #
1462 # end process;
1463 #
1464 #
1465 # -- Generate a cache RAM for each way. This handles the normal
1466 # -- reads, writes from reloads and the special store-hit update
1467 # -- path as well.
1468 # --
1469 # -- Note: the BRAMs have an extra read buffer, meaning the output
1470 # -- is pipelined an extra cycle. This differs from the
1471 # -- icache. The writeback logic needs to take that into
1472 # -- account by using 1-cycle delayed signals for load hits.
1473 # --
1474 # rams: for i in 0 to NUM_WAYS-1 generate
1475 # signal do_read : std_ulogic;
1476 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1477 # signal do_write : std_ulogic;
1478 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1479 # signal wr_data :
1480 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1481 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1482 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1483 # signal dout : cache_row_t;
1484 # begin
1485 # way: entity work.cache_ram
1486 # generic map (
1487 # ROW_BITS => ROW_BITS,
1488 # WIDTH => wishbone_data_bits,
1489 # ADD_BUF => true
1490 # )
1491 # port map (
1492 # clk => clk,
1493 # rd_en => do_read,
1494 # rd_addr => rd_addr,
1495 # rd_data => dout,
1496 # wr_sel => wr_sel_m,
1497 # wr_addr => wr_addr,
1498 # wr_data => wr_data
1499 # );
1500 # process(all)
1501 # begin
1502 # -- Cache hit reads
1503 # do_read <= '1';
1504 # rd_addr <=
1505 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1506 # cache_out(i) <= dout;
1507 #
1508 # -- Write mux:
1509 # --
1510 # -- Defaults to wishbone read responses (cache refill)
1511 # --
1512 # -- For timing, the mux on wr_data/sel/addr is not
1513 # -- dependent on anything other than the current state.
1514 # wr_sel_m <= (others => '0');
1515 #
1516 # do_write <= '0';
1517 # if r1.write_bram = '1' then
1518 # -- Write store data to BRAM. This happens one
1519 # -- cycle after the store is in r0.
1520 # wr_data <= r1.req.data;
1521 # wr_sel <= r1.req.byte_sel;
1522 # wr_addr <= std_ulogic_vector(to_unsigned(
1523 # get_row(r1.req.real_addr), ROW_BITS
1524 # ));
1525 # if i = r1.req.hit_way then
1526 # do_write <= '1';
1527 # end if;
1528 # else
1529 # -- Otherwise, we might be doing a reload or a DCBZ
1530 # if r1.dcbz = '1' then
1531 # wr_data <= (others => '0');
1532 # else
1533 # wr_data <= wishbone_in.dat;
1534 # end if;
1535 # wr_addr <= std_ulogic_vector(to_unsigned(
1536 # r1.store_row, ROW_BITS
1537 # ));
1538 # wr_sel <= (others => '1');
1539 #
1540 # if r1.state = RELOAD_WAIT_ACK and
1541 # wishbone_in.ack = '1' and replace_way = i then
1542 # do_write <= '1';
1543 # end if;
1544 # end if;
1545 #
1546 # -- Mask write selects with do_write since BRAM
1547 # -- doesn't have a global write-enable
1548 # if do_write = '1' then
1549 # wr_sel_m <= wr_sel;
1550 # end if;
1551 #
1552 # end process;
1553 # end generate;
1554 #
1555 # -- Cache hit synchronous machine for the easy case.
1556 # -- This handles load hits.
1557 # -- It also handles error cases (TLB miss, cache paradox)
1558 # dcache_fast_hit : process(clk)
1559 # begin
1560 # if rising_edge(clk) then
1561 # if req_op /= OP_NONE then
1562 # report "op:" & op_t'image(req_op) &
1563 # " addr:" & to_hstring(r0.req.addr) &
1564 # " nc:" & std_ulogic'image(r0.req.nc) &
1565 # " idx:" & integer'image(req_index) &
1566 # " tag:" & to_hstring(req_tag) &
1567 # " way: " & integer'image(req_hit_way);
1568 # end if;
1569 # if r0_valid = '1' then
1570 # r1.mmu_req <= r0.mmu_req;
1571 # end if;
1572 #
1573 # -- Fast path for load/store hits.
1574 # -- Set signals for the writeback controls.
1575 # r1.hit_way <= req_hit_way;
1576 # r1.hit_index <= req_index;
1577 # if req_op = OP_LOAD_HIT then
1578 # r1.hit_load_valid <= '1';
1579 # else
1580 # r1.hit_load_valid <= '0';
1581 # end if;
1582 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1583 # r1.cache_hit <= '1';
1584 # else
1585 # r1.cache_hit <= '0';
1586 # end if;
1587 #
1588 # if req_op = OP_BAD then
1589 # report "Signalling ld/st error valid_ra=" &
1590 # std_ulogic'image(valid_ra) & " rc_ok=" &
1591 # std_ulogic'image(rc_ok) & " perm_ok=" &
1592 # std_ulogic'image(perm_ok);
1593 # r1.ls_error <= not r0.mmu_req;
1594 # r1.mmu_error <= r0.mmu_req;
1595 # r1.cache_paradox <= access_ok;
1596 # else
1597 # r1.ls_error <= '0';
1598 # r1.mmu_error <= '0';
1599 # r1.cache_paradox <= '0';
1600 # end if;
1601 #
1602 # if req_op = OP_STCX_FAIL then
1603 # r1.stcx_fail <= '1';
1604 # else
1605 # r1.stcx_fail <= '0';
1606 # end if;
1607 #
1608 # -- Record TLB hit information for updating TLB PLRU
1609 # r1.tlb_hit <= tlb_hit;
1610 # r1.tlb_hit_way <= tlb_hit_way;
1611 # r1.tlb_hit_index <= tlb_req_index;
1612 #
1613 # end if;
1614 # end process;
1615 #
1616 # -- Memory accesses are handled by this state machine:
1617 # --
1618 # -- * Cache load miss/reload (in conjunction with "rams")
1619 # -- * Load hits for non-cachable forms
1620 # -- * Stores (the collision case is handled in "rams")
1621 # --
1622 # -- All wishbone requests generation is done here.
1623 # -- This machine operates at stage 1.
1624 # dcache_slow : process(clk)
1625 # variable stbs_done : boolean;
1626 # variable req : mem_access_request_t;
1627 # variable acks : unsigned(2 downto 0);
1628 # begin
1629 # if rising_edge(clk) then
1630 # r1.use_forward1 <= use_forward1_next;
1631 # r1.forward_sel <= (others => '0');
1632 # if use_forward1_next = '1' then
1633 # r1.forward_sel <= r1.req.byte_sel;
1634 # elsif use_forward2_next = '1' then
1635 # r1.forward_sel <= r1.forward_sel1;
1636 # end if;
1637 #
1638 # r1.forward_data2 <= r1.forward_data1;
1639 # if r1.write_bram = '1' then
1640 # r1.forward_data1 <= r1.req.data;
1641 # r1.forward_sel1 <= r1.req.byte_sel;
1642 # r1.forward_way1 <= r1.req.hit_way;
1643 # r1.forward_row1 <= get_row(r1.req.real_addr);
1644 # r1.forward_valid1 <= '1';
1645 # else
1646 # if r1.dcbz = '1' then
1647 # r1.forward_data1 <= (others => '0');
1648 # else
1649 # r1.forward_data1 <= wishbone_in.dat;
1650 # end if;
1651 # r1.forward_sel1 <= (others => '1');
1652 # r1.forward_way1 <= replace_way;
1653 # r1.forward_row1 <= r1.store_row;
1654 # r1.forward_valid1 <= '0';
1655 # end if;
1656 #
1657 # -- On reset, clear all valid bits to force misses
1658 # if rst = '1' then
1659 # for i in index_t loop
1660 # cache_valids(i) <= (others => '0');
1661 # end loop;
1662 # r1.state <= IDLE;
1663 # r1.full <= '0';
1664 # r1.slow_valid <= '0';
1665 # r1.wb.cyc <= '0';
1666 # r1.wb.stb <= '0';
1667 # r1.ls_valid <= '0';
1668 # r1.mmu_done <= '0';
1669 #
1670 # -- Not useful normally but helps avoiding
1671 # -- tons of sim warnings
1672 # r1.wb.adr <= (others => '0');
1673 # else
1674 # -- One cycle pulses reset
1675 # r1.slow_valid <= '0';
1676 # r1.write_bram <= '0';
1677 # r1.inc_acks <= '0';
1678 # r1.dec_acks <= '0';
1679 #
1680 # r1.ls_valid <= '0';
1681 # -- complete tlbies and TLB loads in the third cycle
1682 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
1683 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
1684 # if r0.mmu_req = '0' then
1685 # r1.ls_valid <= '1';
1686 # else
1687 # r1.mmu_done <= '1';
1688 # end if;
1689 # end if;
1690 #
1691 # if r1.write_tag = '1' then
1692 # -- Store new tag in selected way
1693 # for i in 0 to NUM_WAYS-1 loop
1694 # if i = replace_way then
1695 # cache_tags(r1.store_index)(
1696 # (i + 1) * TAG_WIDTH - 1
1697 # downto i * TAG_WIDTH
1698 # ) <=
1699 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
1700 # & r1.reload_tag;
1701 # end if;
1702 # end loop;
1703 # r1.store_way <= replace_way;
1704 # r1.write_tag <= '0';
1705 # end if;
1706 #
1707 # -- Take request from r1.req if there is one there,
1708 # -- else from req_op, ra, etc.
1709 # if r1.full = '1' then
1710 # req := r1.req;
1711 # else
1712 # req.op := req_op;
1713 # req.valid := req_go;
1714 # req.mmu_req := r0.mmu_req;
1715 # req.dcbz := r0.req.dcbz;
1716 # req.real_addr := ra;
1717 # -- Force data to 0 for dcbz
1718 # if r0.req.dcbz = '0' then
1719 # req.data := r0.req.data;
1720 # else
1721 # req.data := (others => '0');
1722 # end if;
1723 # -- Select all bytes for dcbz
1724 # -- and for cacheable loads
1725 # if r0.req.dcbz = '1'
1726 # or (r0.req.load = '1' and r0.req.nc = '0') then
1727 # req.byte_sel := (others => '1');
1728 # else
1729 # req.byte_sel := r0.req.byte_sel;
1730 # end if;
1731 # req.hit_way := req_hit_way;
1732 # req.same_tag := req_same_tag;
1733 #
1734 # -- Store the incoming request from r0,
1735 # -- if it is a slow request
1736 # -- Note that r1.full = 1 implies req_op = OP_NONE
1737 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
1738 # or req_op = OP_STORE_MISS
1739 # or req_op = OP_STORE_HIT then
1740 # r1.req <= req;
1741 # r1.full <= '1';
1742 # end if;
1743 # end if;
1744 #
1745 # -- Main state machine
1746 # case r1.state is
1747 # when IDLE =>
1748 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1749 # r1.wb.sel <= req.byte_sel;
1750 # r1.wb.dat <= req.data;
1751 # r1.dcbz <= req.dcbz;
1752 #
1753 # -- Keep track of our index and way
1754 # -- for subsequent stores.
1755 # r1.store_index <= get_index(req.real_addr);
1756 # r1.store_row <= get_row(req.real_addr);
1757 # r1.end_row_ix <=
1758 # get_row_of_line(get_row(req.real_addr)) - 1;
1759 # r1.reload_tag <= get_tag(req.real_addr);
1760 # r1.req.same_tag <= '1';
1761 #
1762 # if req.op = OP_STORE_HIT then
1763 # r1.store_way <= req.hit_way;
1764 # end if;
1765 #
1766 # -- Reset per-row valid bits,
1767 # -- ready for handling OP_LOAD_MISS
1768 # for i in 0 to ROW_PER_LINE - 1 loop
1769 # r1.rows_valid(i) <= '0';
1770 # end loop;
1771 #
1772 # case req.op is
1773 # when OP_LOAD_HIT =>
1774 # -- stay in IDLE state
1775 #
1776 # when OP_LOAD_MISS =>
1777 # -- Normal load cache miss,
1778 # -- start the reload machine
1779 # report "cache miss real addr:" &
1780 # to_hstring(req.real_addr) & " idx:" &
1781 # integer'image(get_index(req.real_addr)) &
1782 # " tag:" & to_hstring(get_tag(req.real_addr));
1783 #
1784 # -- Start the wishbone cycle
1785 # r1.wb.we <= '0';
1786 # r1.wb.cyc <= '1';
1787 # r1.wb.stb <= '1';
1788 #
1789 # -- Track that we had one request sent
1790 # r1.state <= RELOAD_WAIT_ACK;
1791 # r1.write_tag <= '1';
1792 #
1793 # when OP_LOAD_NC =>
1794 # r1.wb.cyc <= '1';
1795 # r1.wb.stb <= '1';
1796 # r1.wb.we <= '0';
1797 # r1.state <= NC_LOAD_WAIT_ACK;
1798 #
1799 # when OP_STORE_HIT | OP_STORE_MISS =>
1800 # if req.dcbz = '0' then
1801 # r1.state <= STORE_WAIT_ACK;
1802 # r1.acks_pending <= to_unsigned(1, 3);
1803 # r1.full <= '0';
1804 # r1.slow_valid <= '1';
1805 # if req.mmu_req = '0' then
1806 # r1.ls_valid <= '1';
1807 # else
1808 # r1.mmu_done <= '1';
1809 # end if;
1810 # if req.op = OP_STORE_HIT then
1811 # r1.write_bram <= '1';
1812 # end if;
1813 # else
1814 # -- dcbz is handled much like a load
1815 # -- miss except that we are writing
1816 # -- to memory instead of reading
1817 # r1.state <= RELOAD_WAIT_ACK;
1818 # if req.op = OP_STORE_MISS then
1819 # r1.write_tag <= '1';
1820 # end if;
1821 # end if;
1822 # r1.wb.we <= '1';
1823 # r1.wb.cyc <= '1';
1824 # r1.wb.stb <= '1';
1825 #
1826 # -- OP_NONE and OP_BAD do nothing
1827 # -- OP_BAD & OP_STCX_FAIL were handled above already
1828 # when OP_NONE =>
1829 # when OP_BAD =>
1830 # when OP_STCX_FAIL =>
1831 # end case;
1832 #
1833 # when RELOAD_WAIT_ACK =>
1834 # -- Requests are all sent if stb is 0
1835 # stbs_done := r1.wb.stb = '0';
1836 #
1837 # -- If we are still sending requests,
1838 # -- was one accepted?
1839 # if wishbone_in.stall = '0' and not stbs_done then
1840 # -- That was the last word ? We are done sending.
1841 # -- Clear stb and set stbs_done so we can handle
1842 # -- an eventual last ack on the same cycle.
1843 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
1844 # r1.wb.stb <= '0';
1845 # stbs_done := true;
1846 # end if;
1847 #
1848 # -- Calculate the next row address
1849 # r1.wb.adr <= next_row_addr(r1.wb.adr);
1850 # end if;
1851 #
1852 # -- Incoming acks processing
1853 # r1.forward_valid1 <= wishbone_in.ack;
1854 # if wishbone_in.ack = '1' then
1855 # r1.rows_valid(
1856 # r1.store_row mod ROW_PER_LINE
1857 # ) <= '1';
1858 # -- If this is the data we were looking for,
1859 # -- we can complete the request next cycle.
1860 # -- Compare the whole address in case the
1861 # -- request in r1.req is not the one that
1862 # -- started this refill.
1863 # if r1.full = '1' and r1.req.same_tag = '1'
1864 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
1865 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
1866 # and r1.store_row = get_row(r1.req.real_addr) then
1867 # r1.full <= '0';
1868 # r1.slow_valid <= '1';
1869 # if r1.mmu_req = '0' then
1870 # r1.ls_valid <= '1';
1871 # else
1872 # r1.mmu_done <= '1';
1873 # end if;
1874 # r1.forward_sel <= (others => '1');
1875 # r1.use_forward1 <= '1';
1876 # end if;
1877 #
1878 # -- Check for completion
1879 # if stbs_done and is_last_row(r1.store_row,
1880 # r1.end_row_ix) then
1881 # -- Complete wishbone cycle
1882 # r1.wb.cyc <= '0';
1883 #
1884 # -- Cache line is now valid
1885 # cache_valids(r1.store_index)(
1886 # r1.store_way
1887 # ) <= '1';
1888 #
1889 # r1.state <= IDLE;
1890 # end if;
1891 #
1892 # -- Increment store row counter
1893 # r1.store_row <= next_row(r1.store_row);
1894 # end if;
1895 #
1896 # when STORE_WAIT_ACK =>
1897 # stbs_done := r1.wb.stb = '0';
1898 # acks := r1.acks_pending;
1899 # if r1.inc_acks /= r1.dec_acks then
1900 # if r1.inc_acks = '1' then
1901 # acks := acks + 1;
1902 # else
1903 # acks := acks - 1;
1904 # end if;
1905 # end if;
1906 # r1.acks_pending <= acks;
1907 # -- Clear stb when slave accepted request
1908 # if wishbone_in.stall = '0' then
1909 # -- See if there is another store waiting
1910 # -- to be done which is in the same real page.
1911 # if req.valid = '1' then
1912 # r1.wb.adr(
1913 # SET_SIZE_BITS - 1 downto 0
1914 # ) <= req.real_addr(
1915 # SET_SIZE_BITS - 1 downto 0
1916 # );
1917 # r1.wb.dat <= req.data;
1918 # r1.wb.sel <= req.byte_sel;
1919 # end if;
1920 # if acks < 7 and req.same_tag = '1'
1921 # and (req.op = OP_STORE_MISS
1922 # or req.op = OP_STORE_HIT) then
1923 # r1.wb.stb <= '1';
1924 # stbs_done := false;
1925 # if req.op = OP_STORE_HIT then
1926 # r1.write_bram <= '1';
1927 # end if;
1928 # r1.full <= '0';
1929 # r1.slow_valid <= '1';
1930 # -- Store requests never come from the MMU
1931 # r1.ls_valid <= '1';
1932 # stbs_done := false;
1933 # r1.inc_acks <= '1';
1934 # else
1935 # r1.wb.stb <= '0';
1936 # stbs_done := true;
1937 # end if;
1938 # end if;
1939 #
1940 # -- Got ack ? See if complete.
1941 # if wishbone_in.ack = '1' then
1942 # if stbs_done and acks = 1 then
1943 # r1.state <= IDLE;
1944 # r1.wb.cyc <= '0';
1945 # r1.wb.stb <= '0';
1946 # end if;
1947 # r1.dec_acks <= '1';
1948 # end if;
1949 #
1950 # when NC_LOAD_WAIT_ACK =>
1951 # -- Clear stb when slave accepted request
1952 # if wishbone_in.stall = '0' then
1953 # r1.wb.stb <= '0';
1954 # end if;
1955 #
1956 # -- Got ack ? complete.
1957 # if wishbone_in.ack = '1' then
1958 # r1.state <= IDLE;
1959 # r1.full <= '0';
1960 # r1.slow_valid <= '1';
1961 # if r1.mmu_req = '0' then
1962 # r1.ls_valid <= '1';
1963 # else
1964 # r1.mmu_done <= '1';
1965 # end if;
1966 # r1.forward_sel <= (others => '1');
1967 # r1.use_forward1 <= '1';
1968 # r1.wb.cyc <= '0';
1969 # r1.wb.stb <= '0';
1970 # end if;
1971 # end case;
1972 # end if;
1973 # end if;
1974 # end process;
1975 #
1976 # dc_log: if LOG_LENGTH > 0 generate
1977 # signal log_data : std_ulogic_vector(19 downto 0);
1978 # begin
1979 # dcache_log: process(clk)
1980 # begin
1981 # if rising_edge(clk) then
1982 # log_data <= r1.wb.adr(5 downto 3) &
1983 # wishbone_in.stall &
1984 # wishbone_in.ack &
1985 # r1.wb.stb & r1.wb.cyc &
1986 # d_out.error &
1987 # d_out.valid &
1988 # std_ulogic_vector(
1989 # to_unsigned(op_t'pos(req_op), 3)) &
1990 # stall_out &
1991 # std_ulogic_vector(
1992 # to_unsigned(tlb_hit_way, 3)) &
1993 # valid_ra &
1994 # std_ulogic_vector(
1995 # to_unsigned(state_t'pos(r1.state), 3));
1996 # end if;
1997 # end process;
1998 # log_out <= log_data;
1999 # end generate;
2000 # end;