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