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: $ftqPC valids:${Binary(valids.asUInt())} cfi valid: ${cfiIndex.valid} " + 230 p"cfi index: ${cfiIndex.bits} isCall:$cfiIsCall isRet:$cfiIsRet isRvc:$cfiIsRVC " + 231 p"mispred:$mispred target:${Hexadecimal(target)}\n" 232 } 233 234} 235 236 237class FPUCtrlSignals extends XSBundle { 238 val isAddSub = Bool() // swap23 239 val typeTagIn = UInt(2.W) 240 val typeTagOut = UInt(2.W) 241 val fromInt = Bool() 242 val wflags = Bool() 243 val fpWen = Bool() 244 val fmaCmd = UInt(2.W) 245 val div = Bool() 246 val sqrt = Bool() 247 val fcvt = Bool() 248 val typ = UInt(2.W) 249 val fmt = UInt(2.W) 250 val ren3 = Bool() //TODO: remove SrcType.fp 251} 252 253// Decode DecodeWidth insts at Decode Stage 254class CtrlSignals extends XSBundle { 255 val src1Type, src2Type, src3Type = SrcType() 256 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 257 val ldest = UInt(5.W) 258 val fuType = FuType() 259 val fuOpType = FuOpType() 260 val rfWen = Bool() 261 val fpWen = Bool() 262 val isXSTrap = Bool() 263 val noSpecExec = Bool() // wait forward 264 val blockBackward = Bool() // block backward 265 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 266 val isRVF = Bool() 267 val selImm = SelImm() 268 val imm = UInt(ImmUnion.maxLen.W) 269 val commitType = CommitType() 270 val fpu = new FPUCtrlSignals 271 272 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 273 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 274 val signals = 275 Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen, 276 isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm) 277 signals zip decoder map { case (s, d) => s := d } 278 commitType := DontCare 279 this 280 } 281} 282 283class CfCtrl extends XSBundle { 284 val cf = new CtrlFlow 285 val ctrl = new CtrlSignals 286} 287 288class PerfDebugInfo extends XSBundle { 289 // val fetchTime = UInt(64.W) 290 val renameTime = UInt(64.W) 291 val dispatchTime = UInt(64.W) 292 val issueTime = UInt(64.W) 293 val writebackTime = UInt(64.W) 294 // val commitTime = UInt(64.W) 295} 296 297// Separate LSQ 298class LSIdx extends XSBundle { 299 val lqIdx = new LqPtr 300 val sqIdx = new SqPtr 301} 302 303// CfCtrl -> MicroOp at Rename Stage 304class MicroOp extends CfCtrl { 305 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 306 val src1State, src2State, src3State = SrcState() 307 val roqIdx = new RoqPtr 308 val lqIdx = new LqPtr 309 val sqIdx = new SqPtr 310 val diffTestDebugLrScValid = Bool() 311 val debugInfo = new PerfDebugInfo 312} 313 314class Redirect extends XSBundle { 315 val roqIdx = new RoqPtr 316 val ftqIdx = new FtqPtr 317 val ftqOffset = UInt(log2Up(PredictWidth).W) 318 val level = RedirectLevel() 319 val interrupt = Bool() 320 val cfiUpdate = new CfiUpdateInfo 321 322 def isUnconditional() = RedirectLevel.isUnconditional(level) 323 324 def flushItself() = RedirectLevel.flushItself(level) 325 326 def isException() = RedirectLevel.isException(level) 327} 328 329class Dp1ToDp2IO extends XSBundle { 330 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 331 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 332 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 333} 334 335class ReplayPregReq extends XSBundle { 336 // NOTE: set isInt and isFp both to 'false' when invalid 337 val isInt = Bool() 338 val isFp = Bool() 339 val preg = UInt(PhyRegIdxWidth.W) 340} 341 342class DebugBundle extends XSBundle { 343 val isMMIO = Bool() 344 val isPerfCnt = Bool() 345} 346 347class ExuInput extends XSBundle { 348 val uop = new MicroOp 349 val src1, src2, src3 = UInt((XLEN + 1).W) 350} 351 352class ExuOutput extends XSBundle { 353 val uop = new MicroOp 354 val data = UInt((XLEN + 1).W) 355 val fflags = UInt(5.W) 356 val redirectValid = Bool() 357 val redirect = new Redirect 358 val debug = new DebugBundle 359} 360 361class ExternalInterruptIO extends XSBundle { 362 val mtip = Input(Bool()) 363 val msip = Input(Bool()) 364 val meip = Input(Bool()) 365} 366 367class CSRSpecialIO extends XSBundle { 368 val exception = Flipped(ValidIO(new MicroOp)) 369 val isInterrupt = Input(Bool()) 370 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 371 val trapTarget = Output(UInt(VAddrBits.W)) 372 val externalInterrupt = new ExternalInterruptIO 373 val interrupt = Output(Bool()) 374} 375 376class RoqCommitInfo extends XSBundle { 377 val ldest = UInt(5.W) 378 val rfWen = Bool() 379 val fpWen = Bool() 380 val wflags = Bool() 381 val commitType = CommitType() 382 val pdest = UInt(PhyRegIdxWidth.W) 383 val old_pdest = UInt(PhyRegIdxWidth.W) 384 val lqIdx = new LqPtr 385 val sqIdx = new SqPtr 386 val ftqIdx = new FtqPtr 387 val ftqOffset = UInt(log2Up(PredictWidth).W) 388 389 // these should be optimized for synthesis verilog 390 val pc = UInt(VAddrBits.W) 391} 392 393class RoqCommitIO extends XSBundle { 394 val isWalk = Output(Bool()) 395 val valid = Vec(CommitWidth, Output(Bool())) 396 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 397 398 def hasWalkInstr = isWalk && valid.asUInt.orR 399 400 def hasCommitInstr = !isWalk && valid.asUInt.orR 401} 402 403class TlbFeedback extends XSBundle { 404 val roqIdx = new RoqPtr 405 val hit = Bool() 406} 407 408class FrontendToBackendIO extends XSBundle { 409 // to backend end 410 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 411 val fetchInfo = DecoupledIO(new FtqEntry) 412 // from backend 413 val redirect_cfiUpdate = Flipped(ValidIO(new Redirect)) 414 val commit_cfiUpdate = Flipped(ValidIO(new FtqEntry)) 415 val ftqEnqPtr = Input(new FtqPtr) 416 val ftqLeftOne = Input(Bool()) 417} 418 419class TlbCsrBundle extends XSBundle { 420 val satp = new Bundle { 421 val mode = UInt(4.W) // TODO: may change number to parameter 422 val asid = UInt(16.W) 423 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 424 } 425 val priv = new Bundle { 426 val mxr = Bool() 427 val sum = Bool() 428 val imode = UInt(2.W) 429 val dmode = UInt(2.W) 430 } 431 432 override def toPrintable: Printable = { 433 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 434 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 435 } 436} 437 438class SfenceBundle extends XSBundle { 439 val valid = Bool() 440 val bits = new Bundle { 441 val rs1 = Bool() 442 val rs2 = Bool() 443 val addr = UInt(VAddrBits.W) 444 } 445 446 override def toPrintable: Printable = { 447 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 448 } 449} 450