xref: /XiangShan/src/main/scala/xiangshan/cache/L1Cache.scala (revision 38c29594d00da67087523376f6a6f3243679884e)
1c6d43980SLemover/***************************************************************************************
2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
4c6d43980SLemover*
5c6d43980SLemover* XiangShan is licensed under Mulan PSL v2.
6c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
7c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at:
8c6d43980SLemover*          http://license.coscl.org.cn/MulanPSL2
9c6d43980SLemover*
10c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13c6d43980SLemover*
14c6d43980SLemover* See the Mulan PSL v2 for more details.
15c6d43980SLemover***************************************************************************************/
16c6d43980SLemover
17152b56fdSAllen// See LICENSE.SiFive for license details.
18152b56fdSAllen
19152b56fdSAllenpackage xiangshan.cache
20152b56fdSAllen
218891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
22152b56fdSAllenimport chisel3._
23152b56fdSAllenimport chisel3.util._
24222e17e5Slinjiaweiimport xiangshan.{HasXSParameter, XSBundle, XSModule}
25152b56fdSAllen
26152b56fdSAllen// this file contains common building blocks that can be shared by ICache and DCache
27152b56fdSAllen// this is the common parameter base for L1 ICache and L1 DCache
28152b56fdSAllentrait L1CacheParameters {
29152b56fdSAllen  def nSets:         Int
30152b56fdSAllen  def nWays:         Int
31152b56fdSAllen  def rowBits:       Int
32152b56fdSAllen  def blockBytes:    Int
331f0e2dc7SJiawei Lin  val pageSize = 4 * 1024
34152b56fdSAllen}
35152b56fdSAllen
36152b56fdSAllentrait HasL1CacheParameters extends HasXSParameter
37152b56fdSAllen  with MemoryOpConstants {
38152b56fdSAllen  val cacheParams: L1CacheParameters
39152b56fdSAllen
40152b56fdSAllen  def nSets = cacheParams.nSets
414948f48aSAllen  def nWays = cacheParams.nWays
424948f48aSAllen  def blockBytes = cacheParams.blockBytes
43594ba8acSWilliam Wang  def refillBytes = l1BusDataWidth / 8
44b3fc7151SAllen  def blockBits = blockBytes * 8
454948f48aSAllen
46152b56fdSAllen  def idxBits = log2Up(cacheParams.nSets)
474948f48aSAllen  def wayBits = log2Up(nWays)
484948f48aSAllen  def blockOffBits = log2Up(cacheParams.blockBytes)
49594ba8acSWilliam Wang  def refillOffBits = log2Up(l1BusDataWidth / 8)
504948f48aSAllen
51152b56fdSAllen  def untagBits = blockOffBits + idxBits
52152b56fdSAllen  // 4K page
53152b56fdSAllen  def pgIdxBits = 12
54152b56fdSAllen  def pgUntagBits = untagBits min pgIdxBits
55152b56fdSAllen  def tagBits = PAddrBits - pgUntagBits
5604665835SMaxpicca-Li  def vtagBits = VAddrBits - untagBits
574948f48aSAllen
584948f48aSAllen  // the basic unit at which we store contents
594948f48aSAllen  // SRAM bank width
60152b56fdSAllen  def rowBits = cacheParams.rowBits
61152b56fdSAllen  def rowBytes = rowBits/8
62152b56fdSAllen  def rowOffBits = log2Up(rowBytes)
634948f48aSAllen  // the number of rows in a block
644948f48aSAllen  def blockRows = blockBytes / rowBytes
65152b56fdSAllen
664948f48aSAllen  // outer bus width
674948f48aSAllen  def beatBits = l1BusDataWidth
684948f48aSAllen  def beatBytes = beatBits / 8
694948f48aSAllen  def refillCycles = blockBytes / beatBytes
704948f48aSAllen  def beatOffBits = log2Up(beatBytes)
714948f48aSAllen
724948f48aSAllen  // inner bus width(determined by XLEN)
734948f48aSAllen  def wordBits = DataBits
744948f48aSAllen  def wordBytes = wordBits / 8
754948f48aSAllen  def wordOffBits = log2Up(wordBytes)
76*38c29594Szhanglinjuan  def quadWordOffBits = log2Up(QuadWordBytes)
774948f48aSAllen  // the number of words in a block
784948f48aSAllen  def blockWords = blockBytes / wordBytes
79594ba8acSWilliam Wang  def refillWords = refillBytes / wordBytes
804948f48aSAllen
81935edac4STang Haojin  def get_phy_tag(paddr: UInt) = (paddr >> pgUntagBits).asUInt
8204665835SMaxpicca-Li  def get_vir_tag(vaddr: UInt) = (vaddr >> untagBits).asUInt
831f0e2dc7SJiawei Lin  def get_tag(addr: UInt) = get_phy_tag(addr)
844948f48aSAllen  def get_idx(addr: UInt) = addr(untagBits-1, blockOffBits)
851f0e2dc7SJiawei Lin  def get_untag(addr: UInt) = addr(pgUntagBits-1, 0)
864948f48aSAllen  def get_block(addr: UInt) = addr >> blockOffBits
874948f48aSAllen  def get_block_addr(addr: UInt) = (addr >> blockOffBits) << blockOffBits
88594ba8acSWilliam Wang  def get_refill_addr(addr: UInt) = (addr >> refillOffBits) << refillOffBits
894948f48aSAllen
904948f48aSAllen  def get_beat(addr: UInt) = addr(blockOffBits - 1, beatOffBits)
914948f48aSAllen  def get_row(addr: UInt) = addr(blockOffBits - 1, rowOffBits)
924948f48aSAllen  def get_word(addr: UInt) = addr(blockOffBits - 1, wordOffBits)
93*38c29594Szhanglinjuan  def get_quad_word(addr: UInt) = addr(blockOffBits - 1, quadWordOffBits)
944948f48aSAllen
954948f48aSAllen  def beatRows = beatBits/rowBits
964948f48aSAllen  def rowWords = rowBits/wordBits
97ef90f6bdSzhanglinjuan  def blockBeats = blockBytes / beatBytes
984948f48aSAllen
994948f48aSAllen  def full_divide(a: Int, b: Int) = a >= b && isPow2(a / b)
100152b56fdSAllen}
101152b56fdSAllen
1022225d46eSJiawei Linabstract class L1CacheModule(implicit p: Parameters) extends XSModule
103152b56fdSAllen  with HasL1CacheParameters
104152b56fdSAllen
1052225d46eSJiawei Linabstract class L1CacheBundle(implicit p: Parameters) extends XSBundle
106152b56fdSAllen  with HasL1CacheParameters
107