xref: /XiangShan/src/main/scala/xiangshan/cache/L1Cache.scala (revision 38c29594d00da67087523376f6a6f3243679884e)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17// See LICENSE.SiFive for license details.
18
19package xiangshan.cache
20
21import org.chipsalliance.cde.config.Parameters
22import chisel3._
23import chisel3.util._
24import xiangshan.{HasXSParameter, XSBundle, XSModule}
25
26// this file contains common building blocks that can be shared by ICache and DCache
27// this is the common parameter base for L1 ICache and L1 DCache
28trait L1CacheParameters {
29  def nSets:         Int
30  def nWays:         Int
31  def rowBits:       Int
32  def blockBytes:    Int
33  val pageSize = 4 * 1024
34}
35
36trait HasL1CacheParameters extends HasXSParameter
37  with MemoryOpConstants {
38  val cacheParams: L1CacheParameters
39
40  def nSets = cacheParams.nSets
41  def nWays = cacheParams.nWays
42  def blockBytes = cacheParams.blockBytes
43  def refillBytes = l1BusDataWidth / 8
44  def blockBits = blockBytes * 8
45
46  def idxBits = log2Up(cacheParams.nSets)
47  def wayBits = log2Up(nWays)
48  def blockOffBits = log2Up(cacheParams.blockBytes)
49  def refillOffBits = log2Up(l1BusDataWidth / 8)
50
51  def untagBits = blockOffBits + idxBits
52  // 4K page
53  def pgIdxBits = 12
54  def pgUntagBits = untagBits min pgIdxBits
55  def tagBits = PAddrBits - pgUntagBits
56  def vtagBits = VAddrBits - untagBits
57
58  // the basic unit at which we store contents
59  // SRAM bank width
60  def rowBits = cacheParams.rowBits
61  def rowBytes = rowBits/8
62  def rowOffBits = log2Up(rowBytes)
63  // the number of rows in a block
64  def blockRows = blockBytes / rowBytes
65
66  // outer bus width
67  def beatBits = l1BusDataWidth
68  def beatBytes = beatBits / 8
69  def refillCycles = blockBytes / beatBytes
70  def beatOffBits = log2Up(beatBytes)
71
72  // inner bus width(determined by XLEN)
73  def wordBits = DataBits
74  def wordBytes = wordBits / 8
75  def wordOffBits = log2Up(wordBytes)
76  def quadWordOffBits = log2Up(QuadWordBytes)
77  // the number of words in a block
78  def blockWords = blockBytes / wordBytes
79  def refillWords = refillBytes / wordBytes
80
81  def get_phy_tag(paddr: UInt) = (paddr >> pgUntagBits).asUInt
82  def get_vir_tag(vaddr: UInt) = (vaddr >> untagBits).asUInt
83  def get_tag(addr: UInt) = get_phy_tag(addr)
84  def get_idx(addr: UInt) = addr(untagBits-1, blockOffBits)
85  def get_untag(addr: UInt) = addr(pgUntagBits-1, 0)
86  def get_block(addr: UInt) = addr >> blockOffBits
87  def get_block_addr(addr: UInt) = (addr >> blockOffBits) << blockOffBits
88  def get_refill_addr(addr: UInt) = (addr >> refillOffBits) << refillOffBits
89
90  def get_beat(addr: UInt) = addr(blockOffBits - 1, beatOffBits)
91  def get_row(addr: UInt) = addr(blockOffBits - 1, rowOffBits)
92  def get_word(addr: UInt) = addr(blockOffBits - 1, wordOffBits)
93  def get_quad_word(addr: UInt) = addr(blockOffBits - 1, quadWordOffBits)
94
95  def beatRows = beatBits/rowBits
96  def rowWords = rowBits/wordBits
97  def blockBeats = blockBytes / beatBytes
98
99  def full_divide(a: Int, b: Int) = a >= b && isPow2(a / b)
100}
101
102abstract class L1CacheModule(implicit p: Parameters) extends XSModule
103  with HasL1CacheParameters
104
105abstract class L1CacheBundle(implicit p: Parameters) extends XSBundle
106  with HasL1CacheParameters
107