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