1package xiangshan 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan.backend.SelImm 6import xiangshan.backend.brq.BrqPtr 7import xiangshan.backend.rename.FreeListPtr 8import xiangshan.backend.roq.RoqPtr 9import xiangshan.backend.decode.{ImmUnion, XDecode} 10import xiangshan.mem.{LqPtr, SqPtr} 11import xiangshan.frontend.PreDecodeInfo 12import xiangshan.frontend.HasBPUParameter 13import xiangshan.frontend.HasTageParameter 14import xiangshan.frontend.HasIFUConst 15import xiangshan.frontend.GlobalHistory 16import utils._ 17 18import scala.math.max 19import Chisel.experimental.chiselName 20 21// Fetch FetchWidth x 32-bit insts from Icache 22class FetchPacket extends XSBundle { 23 val instrs = Vec(PredictWidth, UInt(32.W)) 24 val mask = UInt(PredictWidth.W) 25 val pdmask = UInt(PredictWidth.W) 26 // val pc = UInt(VAddrBits.W) 27 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 28 val pnpc = Vec(PredictWidth, UInt(VAddrBits.W)) 29 val bpuMeta = Vec(PredictWidth, new BpuMeta) 30 val pd = Vec(PredictWidth, new PreDecodeInfo) 31 val ipf = Bool() 32 val acf = Bool() 33 val crossPageIPFFix = Bool() 34 val predTaken = Bool() 35} 36 37class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 38 val valid = Bool() 39 val bits = gen.cloneType.asInstanceOf[T] 40 override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type] 41} 42 43object ValidUndirectioned { 44 def apply[T <: Data](gen: T) = { 45 new ValidUndirectioned[T](gen) 46 } 47} 48 49class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter { 50 def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_) 51 def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_)) 52 def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1 53 val tageTaken = if (useSC) Bool() else UInt(0.W) 54 val scUsed = if (useSC) Bool() else UInt(0.W) 55 val scPred = if (useSC) Bool() else UInt(0.W) 56 // Suppose ctrbits of all tables are identical 57 val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W)) 58 val sumAbs = if (useSC) UInt(sumCtrBits.W) else UInt(0.W) 59} 60 61class TageMeta extends XSBundle with HasTageParameter { 62 val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 63 val altDiffers = Bool() 64 val providerU = UInt(2.W) 65 val providerCtr = UInt(3.W) 66 val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 67 val taken = Bool() 68 val scMeta = new SCMeta(EnableSC) 69} 70 71@chiselName 72class BranchPrediction extends XSBundle with HasIFUConst { 73 // val redirect = Bool() 74 val takens = UInt(PredictWidth.W) 75 // val jmpIdx = UInt(log2Up(PredictWidth).W) 76 val brMask = UInt(PredictWidth.W) 77 val jalMask = UInt(PredictWidth.W) 78 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 79 80 // marks the last 2 bytes of this fetch packet 81 // val endsAtTheEndOfFirstBank = Bool() 82 // val endsAtTheEndOfLastBank = Bool() 83 84 // half RVI could only start at the end of a packet 85 val hasHalfRVI = Bool() 86 87 88 // assumes that only one of the two conditions could be true 89 def lastHalfRVIMask = Cat(hasHalfRVI.asUInt, 0.U((PredictWidth-1).W)) 90 91 def lastHalfRVIClearMask = ~lastHalfRVIMask 92 // is taken from half RVI 93 def lastHalfRVITaken = takens(PredictWidth-1) && hasHalfRVI 94 95 def lastHalfRVIIdx = (PredictWidth-1).U 96 // should not be used if not lastHalfRVITaken 97 def lastHalfRVITarget = targets(PredictWidth-1) 98 99 def realTakens = takens & lastHalfRVIClearMask 100 def realBrMask = brMask & lastHalfRVIClearMask 101 def realJalMask = jalMask & lastHalfRVIClearMask 102 103 def brNotTakens = (~takens & realBrMask) 104 def sawNotTakenBr = VecInit((0 until PredictWidth).map(i => 105 (if (i == 0) false.B else ParallelORR(brNotTakens(i-1,0))))) 106 // def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR 107 def unmaskedJmpIdx = ParallelPriorityEncoder(takens) 108 // if not taken before the half RVI inst 109 def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth-2,0))) 110 // could get PredictWidth-1 when only the first bank is valid 111 def jmpIdx = ParallelPriorityEncoder(realTakens) 112 // only used when taken 113 def target = { 114 val generator = new PriorityMuxGenerator[UInt] 115 generator.register(realTakens.asBools, targets, List.fill(PredictWidth)(None)) 116 generator() 117 } 118 def taken = ParallelORR(realTakens) 119 def takenOnBr = taken && ParallelPriorityMux(realTakens, realBrMask.asBools) 120 def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(realTakens, sawNotTakenBr), ParallelORR(brNotTakens)) 121} 122 123class PredictorAnswer extends XSBundle { 124 val hit = Bool() 125 val taken = Bool() 126 val target = UInt(VAddrBits.W) 127} 128 129class BpuMeta extends XSBundle with HasBPUParameter { 130 val ubtbWriteWay = UInt(log2Up(UBtbWays).W) 131 val ubtbHits = Bool() 132 val btbWriteWay = UInt(log2Up(BtbWays).W) 133 val btbHitJal = Bool() 134 val bimCtr = UInt(2.W) 135 val tageMeta = new TageMeta 136 val rasSp = UInt(log2Up(RasSize).W) 137 val rasTopCtr = UInt(8.W) 138 val rasToqAddr = UInt(VAddrBits.W) 139 val fetchIdx = UInt(log2Up(PredictWidth).W) 140 val specCnt = UInt(10.W) 141 // for global history 142 val predTaken = Bool() 143 val hist = new GlobalHistory 144 val predHist = new GlobalHistory 145 val sawNotTakenBranch = Bool() 146 147 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 148 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 149 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 150 151 val predictor = if (BPUDebug) UInt(log2Up(4).W) else UInt(0.W) // Mark which component this prediction comes from {ubtb, btb, tage, loopPredictor} 152 153 val ubtbAns = new PredictorAnswer 154 val btbAns = new PredictorAnswer 155 val tageAns = new PredictorAnswer 156 val rasAns = new PredictorAnswer 157 val loopAns = new PredictorAnswer 158 159 // def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = { 160 // this.histPtr := histPtr 161 // this.tageMeta := tageMeta 162 // this.rasSp := rasSp 163 // this.rasTopCtr := rasTopCtr 164 // this.asUInt 165 // } 166 def size = 0.U.asTypeOf(this).getWidth 167 def fromUInt(x: UInt) = x.asTypeOf(this) 168} 169 170class Predecode extends XSBundle with HasIFUConst { 171 val hasLastHalfRVI = Bool() 172 val mask = UInt(PredictWidth.W) 173 val lastHalf = Bool() 174 val pd = Vec(PredictWidth, (new PreDecodeInfo)) 175} 176 177class CfiUpdateInfo extends XSBundle with HasBPUParameter { 178 // from backend 179 val pc = UInt(VAddrBits.W) 180 val pnpc = UInt(VAddrBits.W) 181 val fetchIdx = UInt(log2Up(PredictWidth).W) 182 // frontend -> backend -> frontend 183 val pd = new PreDecodeInfo 184 val bpuMeta = new BpuMeta 185 186 // need pipeline update 187 val target = UInt(VAddrBits.W) 188 val brTarget = UInt(VAddrBits.W) 189 val taken = Bool() 190 val isMisPred = Bool() 191 val brTag = new BrqPtr 192 val isReplay = Bool() 193} 194 195// Dequeue DecodeWidth insts from Ibuffer 196class CtrlFlow extends XSBundle { 197 val instr = UInt(32.W) 198 val pc = UInt(VAddrBits.W) 199 val exceptionVec = ExceptionVec() 200 val intrVec = Vec(12, Bool()) 201 val brUpdate = new CfiUpdateInfo 202 val crossPageIPFFix = Bool() 203} 204 205 206class FPUCtrlSignals extends XSBundle { 207 val isAddSub = Bool() // swap23 208 val typeTagIn = UInt(2.W) 209 val typeTagOut = UInt(2.W) 210 val fromInt = Bool() 211 val wflags = Bool() 212 val fpWen = Bool() 213 val fmaCmd = UInt(2.W) 214 val div = Bool() 215 val sqrt = Bool() 216 val fcvt = Bool() 217 val typ = UInt(2.W) 218 val fmt = UInt(2.W) 219 val ren3 = Bool() //TODO: remove SrcType.fp 220} 221 222// Decode DecodeWidth insts at Decode Stage 223class CtrlSignals extends XSBundle { 224 val src1Type, src2Type, src3Type = SrcType() 225 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 226 val ldest = UInt(5.W) 227 val fuType = FuType() 228 val fuOpType = FuOpType() 229 val rfWen = Bool() 230 val fpWen = Bool() 231 val isXSTrap = Bool() 232 val noSpecExec = Bool() // wait forward 233 val blockBackward = Bool() // block backward 234 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 235 val isRVF = Bool() 236 val selImm = SelImm() 237 val imm = UInt(ImmUnion.maxLen.W) 238 val commitType = CommitType() 239 val fpu = new FPUCtrlSignals 240 241 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 242 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 243 val signals = 244 Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen, 245 isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm) 246 signals zip decoder map { case(s, d) => s := d } 247 commitType := DontCare 248 this 249 } 250} 251 252class CfCtrl extends XSBundle { 253 val cf = new CtrlFlow 254 val ctrl = new CtrlSignals 255 val brTag = new BrqPtr 256} 257 258class PerfDebugInfo extends XSBundle { 259 // val fetchTime = UInt(64.W) 260 val renameTime = UInt(64.W) 261 val dispatchTime = UInt(64.W) 262 val issueTime = UInt(64.W) 263 val writebackTime = UInt(64.W) 264 // val commitTime = UInt(64.W) 265} 266 267// Separate LSQ 268class LSIdx extends XSBundle { 269 val lqIdx = new LqPtr 270 val sqIdx = new SqPtr 271} 272 273// CfCtrl -> MicroOp at Rename Stage 274class MicroOp extends CfCtrl { 275 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 276 val src1State, src2State, src3State = SrcState() 277 val roqIdx = new RoqPtr 278 val lqIdx = new LqPtr 279 val sqIdx = new SqPtr 280 val diffTestDebugLrScValid = Bool() 281 val debugInfo = new PerfDebugInfo 282} 283 284class Redirect extends XSBundle { 285 val roqIdx = new RoqPtr 286 val level = RedirectLevel() 287 val interrupt = Bool() 288 val pc = UInt(VAddrBits.W) 289 val target = UInt(VAddrBits.W) 290 val brTag = new BrqPtr 291 292 def isUnconditional() = RedirectLevel.isUnconditional(level) 293 def flushItself() = RedirectLevel.flushItself(level) 294 def isException() = RedirectLevel.isException(level) 295} 296 297class Dp1ToDp2IO extends XSBundle { 298 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 299 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 300 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 301} 302 303class ReplayPregReq extends XSBundle { 304 // NOTE: set isInt and isFp both to 'false' when invalid 305 val isInt = Bool() 306 val isFp = Bool() 307 val preg = UInt(PhyRegIdxWidth.W) 308} 309 310class DebugBundle extends XSBundle{ 311 val isMMIO = Bool() 312 val isPerfCnt = Bool() 313 val paddr = UInt(PAddrBits.W) 314} 315 316class ExuInput extends XSBundle { 317 val uop = new MicroOp 318 val src1, src2, src3 = UInt((XLEN+1).W) 319} 320 321class ExuOutput extends XSBundle { 322 val uop = new MicroOp 323 val data = UInt((XLEN+1).W) 324 val fflags = UInt(5.W) 325 val redirectValid = Bool() 326 val redirect = new Redirect 327 val brUpdate = new CfiUpdateInfo 328 val debug = new DebugBundle 329} 330 331class ExternalInterruptIO extends XSBundle { 332 val mtip = Input(Bool()) 333 val msip = Input(Bool()) 334 val meip = Input(Bool()) 335} 336 337class CSRSpecialIO extends XSBundle { 338 val exception = Flipped(ValidIO(new MicroOp)) 339 val isInterrupt = Input(Bool()) 340 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 341 val trapTarget = Output(UInt(VAddrBits.W)) 342 val externalInterrupt = new ExternalInterruptIO 343 val interrupt = Output(Bool()) 344} 345 346class RoqCommitInfo extends XSBundle { 347 val ldest = UInt(5.W) 348 val rfWen = Bool() 349 val fpWen = Bool() 350 val wflags = Bool() 351 val commitType = CommitType() 352 val pdest = UInt(PhyRegIdxWidth.W) 353 val old_pdest = UInt(PhyRegIdxWidth.W) 354 355 // these should be optimized for synthesis verilog 356 val pc = UInt(VAddrBits.W) 357} 358 359class RoqCommitIO extends XSBundle { 360 val isWalk = Output(Bool()) 361 val valid = Vec(CommitWidth, Output(Bool())) 362 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 363 364 def hasWalkInstr = isWalk && valid.asUInt.orR 365 def hasCommitInstr = !isWalk && valid.asUInt.orR 366} 367 368class TlbFeedback extends XSBundle { 369 val roqIdx = new RoqPtr 370 val hit = Bool() 371} 372 373class FrontendToBackendIO extends XSBundle { 374 // to backend end 375 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 376 // from backend 377 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 378 // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 379 val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 380} 381 382class TlbCsrBundle extends XSBundle { 383 val satp = new Bundle { 384 val mode = UInt(4.W) // TODO: may change number to parameter 385 val asid = UInt(16.W) 386 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 387 } 388 val priv = new Bundle { 389 val mxr = Bool() 390 val sum = Bool() 391 val imode = UInt(2.W) 392 val dmode = UInt(2.W) 393 } 394 395 override def toPrintable: Printable = { 396 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 397 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 398 } 399} 400 401class SfenceBundle extends XSBundle { 402 val valid = Bool() 403 val bits = new Bundle { 404 val rs1 = Bool() 405 val rs2 = Bool() 406 val addr = UInt(VAddrBits.W) 407 } 408 409 override def toPrintable: Printable = { 410 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 411 } 412} 413 414class DifftestBundle extends XSBundle { 415 val fromSbuffer = new Bundle() { 416 val sbufferResp = Output(Bool()) 417 val sbufferAddr = Output(UInt(64.W)) 418 val sbufferData = Output(Vec(64, UInt(8.W))) 419 val sbufferMask = Output(UInt(64.W)) 420 } 421 val fromSQ = new Bundle() { 422 val storeCommit = Output(UInt(2.W)) 423 val storeAddr = Output(Vec(2, UInt(64.W))) 424 val storeData = Output(Vec(2, UInt(64.W))) 425 val storeMask = Output(Vec(2, UInt(8.W))) 426 } 427 val fromXSCore = new Bundle() { 428 val r = Output(Vec(64, UInt(XLEN.W))) 429 } 430 val fromCSR = new Bundle() { 431 val intrNO = Output(UInt(64.W)) 432 val cause = Output(UInt(64.W)) 433 val priviledgeMode = Output(UInt(2.W)) 434 val mstatus = Output(UInt(64.W)) 435 val sstatus = Output(UInt(64.W)) 436 val mepc = Output(UInt(64.W)) 437 val sepc = Output(UInt(64.W)) 438 val mtval = Output(UInt(64.W)) 439 val stval = Output(UInt(64.W)) 440 val mtvec = Output(UInt(64.W)) 441 val stvec = Output(UInt(64.W)) 442 val mcause = Output(UInt(64.W)) 443 val scause = Output(UInt(64.W)) 444 val satp = Output(UInt(64.W)) 445 val mip = Output(UInt(64.W)) 446 val mie = Output(UInt(64.W)) 447 val mscratch = Output(UInt(64.W)) 448 val sscratch = Output(UInt(64.W)) 449 val mideleg = Output(UInt(64.W)) 450 val medeleg = Output(UInt(64.W)) 451 } 452 val fromRoq = new Bundle() { 453 val commit = Output(UInt(32.W)) 454 val thisPC = Output(UInt(XLEN.W)) 455 val thisINST = Output(UInt(32.W)) 456 val skip = Output(UInt(32.W)) 457 val wen = Output(UInt(32.W)) 458 val wdata = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6 459 val wdst = Output(Vec(CommitWidth, UInt(32.W))) // set difftest width to 6 460 val wpc = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6 461 val lpaddr = Output(Vec(CommitWidth, UInt(64.W))) 462 val ltype = Output(Vec(CommitWidth, UInt(32.W))) 463 val lfu = Output(Vec(CommitWidth, UInt(4.W))) 464 val isRVC = Output(UInt(32.W)) 465 val scFailed = Output(Bool()) 466 } 467 val fromAtomic = new Bundle() { 468 val atomicResp = Output(Bool()) 469 val atomicAddr = Output(UInt(64.W)) 470 val atomicData = Output(UInt(64.W)) 471 val atomicMask = Output(UInt(8.W)) 472 } 473} 474 475class TrapIO extends XSBundle { 476 val valid = Output(Bool()) 477 val code = Output(UInt(3.W)) 478 val pc = Output(UInt(VAddrBits.W)) 479 val cycleCnt = Output(UInt(XLEN.W)) 480 val instrCnt = Output(UInt(XLEN.W)) 481}