XSCore.scala 8.5 KB
Newer Older
L
LinJiawei 已提交
1 2 3 4 5
package xiangshan

import chisel3._
import chisel3.util._
import noop.{Cache, CacheConfig, HasExceptionNO, TLB, TLBConfig}
L
LinJiawei 已提交
6
import top.Parameters
L
LinJiawei 已提交
7
import xiangshan.backend._
8
import xiangshan.backend.dispatch.DispatchParameters
L
LinJiawei 已提交
9
import xiangshan.backend.exu.ExuParameters
G
GouLingrui 已提交
10
import xiangshan.frontend._
11
import xiangshan.mem._
A
Allen 已提交
12
import xiangshan.cache.{ICache, DCache, DCacheParameters, ICacheParameters, L1plusCacheParameters, PTW, Uncache}
L
linjiawei 已提交
13 14
import chipsalliance.rocketchip.config
import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
15
import freechips.rocketchip.tilelink.{TLBundleParameters, TLCacheCork, TLBuffer, TLClientNode, TLIdentityNode, TLXbar}
L
LinJiawei 已提交
16
import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters}
L
LinJiawei 已提交
17
import utils._
L
LinJiawei 已提交
18

L
LinJiawei 已提交
19 20 21 22 23 24 25 26 27 28 29
case class XSCoreParameters
(
  XLEN: Int = 64,
  HasMExtension: Boolean = true,
  HasCExtension: Boolean = true,
  HasDiv: Boolean = true,
  HasICache: Boolean = true,
  HasDCache: Boolean = true,
  EnableStoreQueue: Boolean = true,
  AddrBits: Int = 64,
  VAddrBits: Int = 39,
30
  PAddrBits: Int = 40,
Y
Yinan Xu 已提交
31
  HasFPU: Boolean = false,
L
LinJiawei 已提交
32 33
  FectchWidth: Int = 8,
  EnableBPU: Boolean = true,
L
Lingrui98 已提交
34
  EnableBPD: Boolean = true,
G
GouLingrui 已提交
35
  EnableRAS: Boolean = true,
L
LinJiawei 已提交
36
  EnableLB: Boolean = false,
37
  EnableLoop: Boolean = false,
L
LinJiawei 已提交
38
  HistoryLength: Int = 64,
39
  BtbSize: Int = 2048,
L
LinJiawei 已提交
40 41 42 43 44 45 46 47 48 49
  JbtacSize: Int = 1024,
  JbtacBanks: Int = 8,
  RasSize: Int = 16,
  CacheLineSize: Int = 512,
  UBtbWays: Int = 16,
  BtbWays: Int = 2,
  IBufSize: Int = 64,
  DecodeWidth: Int = 6,
  RenameWidth: Int = 6,
  CommitWidth: Int = 6,
50
  BrqSize: Int = 12,
L
LinJiawei 已提交
51
  IssQueSize: Int = 8,
52
  NRPhyRegs: Int = 72,
L
LinJiawei 已提交
53 54 55
  NRIntReadPorts: Int = 8,
  NRIntWritePorts: Int = 8,
  NRFpReadPorts: Int = 14,
56
  NRFpWritePorts: Int = 8, 
57
  EnableUnifiedLSQ: Boolean = false,
Y
Yinan Xu 已提交
58
  LsroqSize: Int = 16,
59 60
  LoadQueueSize: Int = 12,
  StoreQueueSize: Int = 10,
L
LinJiawei 已提交
61
  RoqSize: Int = 32,
L
LinJiawei 已提交
62 63
  dpParams: DispatchParameters = DispatchParameters(
    DqEnqWidth = 4,
64 65 66
    IntDqSize = 24,
    FpDqSize = 16,
    LsDqSize = 16,
L
LinJiawei 已提交
67 68
    IntDqDeqWidth = 4,
    FpDqDeqWidth = 4,
Y
Yinan Xu 已提交
69 70
    LsDqDeqWidth = 4,
    IntDqReplayWidth = 4,
71 72
    FpDqReplayWidth = 4,
    LsDqReplayWidth = 4
L
LinJiawei 已提交
73 74
  ),
  exuParameters: ExuParameters = ExuParameters(
L
LinJiawei 已提交
75
    JmpCnt = 1,
76
    AluCnt = 4,
L
LinJiawei 已提交
77 78
    MulCnt = 0,
    MduCnt = 2,
79 80 81
    FmacCnt = 0,
    FmiscCnt = 0,
    FmiscDivSqrtCnt = 0,
L
LinJiawei 已提交
82 83 84 85 86 87
    LduCnt = 2,
    StuCnt = 2
  ),
  LoadPipelineWidth: Int = 2,
  StorePipelineWidth: Int = 2,
  StoreBufferSize: Int = 16,
88 89 90 91 92
  RefillSize: Int = 512,
  TlbEntrySize: Int = 32,
  TlbL2EntrySize: Int = 256, // or 512
  PtwL1EntrySize: Int = 16,
  PtwL2EntrySize: Int = 256
L
LinJiawei 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
)

trait HasXSParameter {

  val core = Parameters.get.coreParameters
  val env = Parameters.get.envParameters

  val XLEN = core.XLEN
  val HasMExtension = core.HasMExtension
  val HasCExtension = core.HasCExtension
  val HasDiv = core.HasDiv
  val HasIcache = core.HasICache
  val HasDcache = core.HasDCache
  val EnableStoreQueue = core.EnableStoreQueue
  val AddrBits = core.AddrBits // AddrBits is used in some cases
  val VAddrBits = core.VAddrBits // VAddrBits is Virtual Memory addr bits
  val PAddrBits = core.PAddrBits // PAddrBits is Phyical Memory addr bits
  val AddrBytes = AddrBits / 8 // unused
  val DataBits = XLEN
  val DataBytes = DataBits / 8
  val HasFPU = core.HasFPU
  val FetchWidth = core.FectchWidth
  val PredictWidth = FetchWidth * 2
  val EnableBPU = core.EnableBPU
  val EnableBPD = core.EnableBPD // enable backing predictor(like Tage) in BPUStage3
  val EnableRAS = core.EnableRAS
  val EnableLB = core.EnableLB
120
  val EnableLoop = core.EnableLoop
L
LinJiawei 已提交
121 122
  val HistoryLength = core.HistoryLength
  val BtbSize = core.BtbSize
G
GouLingrui 已提交
123
  // val BtbWays = 4
L
LinJiawei 已提交
124
  val BtbBanks = PredictWidth
G
GouLingrui 已提交
125
  // val BtbSets = BtbSize / BtbWays
L
LinJiawei 已提交
126 127 128
  val JbtacSize = core.JbtacSize
  val JbtacBanks = core.JbtacBanks
  val RasSize = core.RasSize
L
LinJiawei 已提交
129 130
  val CacheLineSize = core.CacheLineSize
  val CacheLineHalfWord = CacheLineSize / 16
G
GouLingrui 已提交
131
  val ExtHistoryLength = HistoryLength + 64
L
LinJiawei 已提交
132 133
  val UBtbWays = core.UBtbWays
  val BtbWays = core.BtbWays
L
LinJiawei 已提交
134 135 136 137 138 139 140 141 142 143
  val IBufSize = core.IBufSize
  val DecodeWidth = core.DecodeWidth
  val RenameWidth = core.RenameWidth
  val CommitWidth = core.CommitWidth
  val BrqSize = core.BrqSize
  val IssQueSize = core.IssQueSize
  val BrTagWidth = log2Up(BrqSize)
  val NRPhyRegs = core.NRPhyRegs
  val PhyRegIdxWidth = log2Up(NRPhyRegs)
  val RoqSize = core.RoqSize
144 145
  val EnableUnifiedLSQ = core.EnableUnifiedLSQ
  val LsroqSize = core.LsroqSize // 64
Y
Yinan Xu 已提交
146 147
  val InnerLsroqIdxWidth = log2Up(LsroqSize)
  val LsroqIdxWidth = InnerLsroqIdxWidth + 1
148 149
  val LoadQueueSize = core.LoadQueueSize
  val StoreQueueSize = core.StoreQueueSize
L
LinJiawei 已提交
150
  val dpParams = core.dpParams
Y
Yinan Xu 已提交
151
  val ReplayWidth = dpParams.IntDqReplayWidth + dpParams.FpDqReplayWidth + dpParams.LsDqReplayWidth
L
LinJiawei 已提交
152
  val exuParameters = core.exuParameters
L
LinJiawei 已提交
153 154 155 156 157 158 159 160 161
  val NRIntReadPorts = core.NRIntReadPorts
  val NRIntWritePorts = core.NRIntWritePorts
  val NRMemReadPorts = exuParameters.LduCnt + 2*exuParameters.StuCnt
  val NRFpReadPorts = core.NRFpReadPorts
  val NRFpWritePorts = core.NRFpWritePorts
  val LoadPipelineWidth = core.LoadPipelineWidth
  val StorePipelineWidth = core.StorePipelineWidth
  val StoreBufferSize = core.StoreBufferSize
  val RefillSize = core.RefillSize
162
  val DTLBWidth = core.LoadPipelineWidth + core.StorePipelineWidth
163 164 165 166
  val TlbEntrySize = core.TlbEntrySize
  val TlbL2EntrySize = core.TlbL2EntrySize
  val PtwL1EntrySize = core.PtwL1EntrySize
  val PtwL2EntrySize = core.PtwL2EntrySize
167

A
Allen 已提交
168
  val l1BusDataWidth = 256
169 170 171 172

  val icacheParameters = ICacheParameters(
  )

A
Allen 已提交
173 174 175 176 177 178
  val l1plusCacheParameters = L1plusCacheParameters(
    tagECC = Some("secded"),
    dataECC = Some("secded"),
    nMissEntries = 8
  )

179
  val dcacheParameters = DCacheParameters(
180
    tagECC = Some("secded"),
181 182 183 184
    dataECC = Some("secded"),
    nMissEntries = 16,
    nLoadMissEntries = 8,
    nStoreMissEntries = 8
185
  )
A
Allen 已提交
186 187

  val LRSCCycles = 100
L
LinJiawei 已提交
188 189
}

L
linjiawei 已提交
190
trait HasXSLog { this: RawModule =>
191
  implicit val moduleName: String = this.name
L
LinJiawei 已提交
192 193
}

L
LinJiawei 已提交
194 195 196
abstract class XSModule extends Module
  with HasXSParameter
  with HasExceptionNO
L
LinJiawei 已提交
197
  with HasXSLog
L
LinJiawei 已提交
198

199 200 201 202 203 204 205 206 207
//remove this trait after impl module logic
trait NeedImpl { this: Module =>
  override protected def IO[T <: Data](iodef: T): T = {
    val io = chisel3.experimental.IO(iodef)
    io <> DontCare
    io
  }
}

L
LinJiawei 已提交
208 209 210
abstract class XSBundle extends Bundle
  with HasXSParameter

L
LinJiawei 已提交
211
case class EnviromentParameters
L
LinJiawei 已提交
212 213
(
  FPGAPlatform: Boolean = true,
L
LinJiawei 已提交
214
  EnableDebug: Boolean = false
L
LinJiawei 已提交
215 216
)

L
LinJiawei 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
object AddressSpace extends HasXSParameter {
  // (start, size)
  // address out of MMIO will be considered as DRAM
  def mmio = List(
    (0x30000000L, 0x10000000L),  // internal devices, such as CLINT and PLIC
    (0x40000000L, 0x40000000L) // external devices
  )

  def isMMIO(addr: UInt): Bool = mmio.map(range => {
    require(isPow2(range._2))
    val bits = log2Up(range._2)
    (addr ^ range._1.U)(PAddrBits-1, bits) === 0.U
  }).reduce(_ || _)
}


L
LinJiawei 已提交
233

L
linjiawei 已提交
234
class XSCore()(implicit p: config.Parameters) extends LazyModule {
235

L
linjiawei 已提交
236 237
  val dcache = LazyModule(new DCache())
  val uncache = LazyModule(new Uncache())
J
jinyue110 已提交
238
  val icache = LazyModule(new ICache())
L
linjiawei 已提交
239
  val ptw = LazyModule(new PTW())
240

L
LinJiawei 已提交
241
  val mem = TLIdentityNode()
L
linjiawei 已提交
242
  val mmio = uncache.clientNode
L
LinJiawei 已提交
243

L
LinJiawei 已提交
244 245 246 247 248 249 250
  // TODO: refactor these params
  private val l2 = LazyModule(new InclusiveCache(
    CacheParameters(
      level = 2,
      ways = 4,
      sets = 512 * 1024 / (64 * 4),
      blockBytes = 64,
251
      beatBytes = 32 // beatBytes = l1BusDataWidth / 8
L
LinJiawei 已提交
252 253 254 255 256 257 258 259
    ),
    InclusiveCacheMicroParameters(
      writeBytes = 8
    )
  ))

  private val xbar = TLXbar()

260
  xbar := TLBuffer() := DebugIdentityNode() := dcache.clientNode
A
Allen 已提交
261 262
  xbar := TLBuffer() := DebugIdentityNode() := icache.clientNode
  xbar := TLBuffer() := DebugIdentityNode() := ptw.node
L
LinJiawei 已提交
263 264 265

  l2.node := xbar

266
  mem := TLBuffer() := TLCacheCork() := TLBuffer() := l2.node
L
LinJiawei 已提交
267

L
linjiawei 已提交
268 269 270 271
  lazy val module = new XSCoreImp(this)
}

class XSCoreImp(outer: XSCore) extends LazyModuleImp(outer) with HasXSParameter {
L
LinJiawei 已提交
272

273
  val front = Module(new Frontend)
L
LinJiawei 已提交
274
  val backend = Module(new Backend)
275
  val mem = Module(new Memend)
L
linjiawei 已提交
276 277 278

  val dcache = outer.dcache.module
  val uncache = outer.uncache.module
J
jinyue110 已提交
279
  val icache = outer.icache.module
L
linjiawei 已提交
280
  val ptw = outer.ptw.module
L
LinJiawei 已提交
281

L
linjiawei 已提交
282
  // TODO: connect this
L
LinJiawei 已提交
283

284
  front.io.backend <> backend.io.frontend
J
jinyue110 已提交
285
  front.io.icacheResp <> icache.io.resp
Z
zhanglinjuan 已提交
286
  front.io.icacheToTlb <> icache.io.tlb
J
jinyue110 已提交
287 288
  icache.io.req <> front.io.icacheReq
  icache.io.flush <> front.io.icacheFlush
289
  mem.io.backend   <> backend.io.mem
290 291

  ptw.io.tlb(0) <> mem.io.ptw
Z
zhanglinjuan 已提交
292
  ptw.io.tlb(1) <> front.io.ptw
293

294 295 296 297 298
  dcache.io.lsu.load    <> mem.io.loadUnitToDcacheVec
  dcache.io.lsu.lsroq   <> mem.io.loadMiss
  dcache.io.lsu.atomics <> mem.io.atomics
  dcache.io.lsu.store   <> mem.io.sbufferToDcache
  uncache.io.lsroq      <> mem.io.uncache
L
LinJiawei 已提交
299 300

}