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 xiangshan.frontend.RASEntry 17import utils._ 18 19import scala.math.max 20import Chisel.experimental.chiselName 21 22// Fetch FetchWidth x 32-bit insts from Icache 23class FetchPacket extends XSBundle { 24 val instrs = Vec(PredictWidth, UInt(32.W)) 25 val mask = UInt(PredictWidth.W) 26 val pdmask = UInt(PredictWidth.W) 27 // val pc = UInt(VAddrBits.W) 28 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 29 val pnpc = Vec(PredictWidth, UInt(VAddrBits.W)) 30 val bpuMeta = Vec(PredictWidth, new BpuMeta) 31 val pd = Vec(PredictWidth, new PreDecodeInfo) 32 val ipf = Bool() 33 val acf = Bool() 34 val crossPageIPFFix = Bool() 35 val predTaken = Bool() 36} 37 38class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 39 val valid = Bool() 40 val bits = gen.cloneType.asInstanceOf[T] 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 def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_)) 53 def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1 54 val tageTaken = if (useSC) Bool() else UInt(0.W) 55 val scUsed = if (useSC) Bool() else UInt(0.W) 56 val scPred = if (useSC) Bool() else UInt(0.W) 57 // Suppose ctrbits of all tables are identical 58 val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W)) 59 val sumAbs = if (useSC) UInt(sumCtrBits.W) else UInt(0.W) 60} 61 62class TageMeta extends XSBundle with HasTageParameter { 63 val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 64 val altDiffers = Bool() 65 val providerU = UInt(2.W) 66 val providerCtr = UInt(3.W) 67 val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 68 val taken = Bool() 69 val scMeta = new SCMeta(EnableSC) 70} 71 72@chiselName 73class BranchPrediction extends XSBundle with HasIFUConst { 74 // val redirect = Bool() 75 val takens = UInt(PredictWidth.W) 76 // val jmpIdx = UInt(log2Up(PredictWidth).W) 77 val brMask = UInt(PredictWidth.W) 78 val jalMask = UInt(PredictWidth.W) 79 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 80 81 // marks the last 2 bytes of this fetch packet 82 // val endsAtTheEndOfFirstBank = Bool() 83 // val endsAtTheEndOfLastBank = Bool() 84 85 // half RVI could only start at the end of a packet 86 val hasHalfRVI = Bool() 87 88 89 // assumes that only one of the two conditions could be true 90 def lastHalfRVIMask = Cat(hasHalfRVI.asUInt, 0.U((PredictWidth-1).W)) 91 92 def lastHalfRVIClearMask = ~lastHalfRVIMask 93 // is taken from half RVI 94 def lastHalfRVITaken = takens(PredictWidth-1) && hasHalfRVI 95 96 def lastHalfRVIIdx = (PredictWidth-1).U 97 // should not be used if not lastHalfRVITaken 98 def lastHalfRVITarget = targets(PredictWidth-1) 99 100 def realTakens = takens & lastHalfRVIClearMask 101 def realBrMask = brMask & lastHalfRVIClearMask 102 def realJalMask = jalMask & lastHalfRVIClearMask 103 104 def brNotTakens = (~takens & realBrMask) 105 def sawNotTakenBr = VecInit((0 until PredictWidth).map(i => 106 (if (i == 0) false.B else ParallelORR(brNotTakens(i-1,0))))) 107 // def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR 108 def unmaskedJmpIdx = ParallelPriorityEncoder(takens) 109 // if not taken before the half RVI inst 110 def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth-2,0))) 111 // could get PredictWidth-1 when only the first bank is valid 112 def jmpIdx = ParallelPriorityEncoder(realTakens) 113 // only used when taken 114 def target = { 115 val generator = new PriorityMuxGenerator[UInt] 116 generator.register(realTakens.asBools, targets, List.fill(PredictWidth)(None)) 117 generator() 118 } 119 def taken = ParallelORR(realTakens) 120 def takenOnBr = taken && ParallelPriorityMux(realTakens, realBrMask.asBools) 121 def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(realTakens, sawNotTakenBr), ParallelORR(brNotTakens)) 122} 123 124class BpuMeta extends XSBundle with HasBPUParameter { 125 val ubtbWriteWay = UInt(log2Up(UBtbWays).W) 126 val ubtbHits = Bool() 127 val btbWriteWay = UInt(log2Up(BtbWays).W) 128 val btbHitJal = Bool() 129 val bimCtr = UInt(2.W) 130 val tageMeta = new TageMeta 131 val specCnt = UInt(10.W) 132 // for global history 133 val predTaken = Bool() 134 val sawNotTakenBranch = Bool() 135 136 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 137 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 138 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 139 140 val predictor = if (BPUDebug) UInt(log2Up(4).W) else UInt(0.W) // Mark which component this prediction comes from {ubtb, btb, tage, loopPredictor} 141 142 // def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = { 143 // this.histPtr := histPtr 144 // this.tageMeta := tageMeta 145 // this.rasSp := rasSp 146 // this.rasTopCtr := rasTopCtr 147 // this.asUInt 148 // } 149 def size = 0.U.asTypeOf(this).getWidth 150 def fromUInt(x: UInt) = x.asTypeOf(this) 151} 152 153class Predecode extends XSBundle with HasIFUConst { 154 val hasLastHalfRVI = Bool() 155 val mask = UInt(PredictWidth.W) 156 val lastHalf = Bool() 157 val pd = Vec(PredictWidth, (new PreDecodeInfo)) 158} 159 160class CfiUpdateInfo extends XSBundle with HasBPUParameter { 161 // from backend 162 val pc = UInt(VAddrBits.W) 163 // frontend -> backend -> frontend 164 val pd = new PreDecodeInfo 165 val bpuMeta = new BpuMeta 166 val rasSp = UInt(log2Up(RasSize).W) 167 val rasTopCtr = UInt(8.W) 168 val rasToqAddr = UInt(VAddrBits.W) 169 val hist = new GlobalHistory 170 val predHist = new GlobalHistory 171 // need pipeline update 172 val jalr_target = UInt(VAddrBits.W) 173 val taken = Bool() 174 val isMisPred = Bool() 175} 176 177// Dequeue DecodeWidth insts from Ibuffer 178class CtrlFlow extends XSBundle { 179 val instr = UInt(32.W) 180 val pc = UInt(VAddrBits.W) 181 val exceptionVec = ExceptionVec() 182 val intrVec = Vec(12, Bool()) 183 val brUpdate = new CfiUpdateInfo 184 val crossPageIPFFix = Bool() 185} 186 187class FtqEntry extends XSBundle { 188 // fetch pc, pc of each inst could be generated by concatenation 189 val pc = UInt(VAddrBits.W) 190 191 // prediction metas 192 val hist = new GlobalHistory 193 val predHist = new GlobalHistory 194 val rasSp = UInt(log2Ceil(RasSize).W) 195 val rasTop = new RASEntry() 196 val metas = Vec(PredictWidth, new BpuMeta) 197 198 val brMask = UInt(PredictWidth.W) 199 val jalMask = UInt(PredictWidth.W) 200 201 val mispred = UInt(PredictWidth.W) 202} 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} 314 315class ExuInput extends XSBundle { 316 val uop = new MicroOp 317 val src1, src2, src3 = UInt((XLEN+1).W) 318} 319 320class ExuOutput extends XSBundle { 321 val uop = new MicroOp 322 val data = UInt((XLEN+1).W) 323 val fflags = UInt(5.W) 324 val redirectValid = Bool() 325 val redirect = new Redirect 326 val brUpdate = new CfiUpdateInfo 327 val debug = new DebugBundle 328} 329 330class ExternalInterruptIO extends XSBundle { 331 val mtip = Input(Bool()) 332 val msip = Input(Bool()) 333 val meip = Input(Bool()) 334} 335 336class CSRSpecialIO extends XSBundle { 337 val exception = Flipped(ValidIO(new MicroOp)) 338 val isInterrupt = Input(Bool()) 339 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 340 val trapTarget = Output(UInt(VAddrBits.W)) 341 val externalInterrupt = new ExternalInterruptIO 342 val interrupt = Output(Bool()) 343} 344 345class RoqCommitInfo extends XSBundle { 346 val ldest = UInt(5.W) 347 val rfWen = Bool() 348 val fpWen = Bool() 349 val wflags = Bool() 350 val commitType = CommitType() 351 val pdest = UInt(PhyRegIdxWidth.W) 352 val old_pdest = UInt(PhyRegIdxWidth.W) 353 val lqIdx = new LqPtr 354 val sqIdx = new SqPtr 355 356 // these should be optimized for synthesis verilog 357 val pc = UInt(VAddrBits.W) 358} 359 360class RoqCommitIO extends XSBundle { 361 val isWalk = Output(Bool()) 362 val valid = Vec(CommitWidth, Output(Bool())) 363 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 364 365 def hasWalkInstr = isWalk && valid.asUInt.orR 366 def hasCommitInstr = !isWalk && valid.asUInt.orR 367} 368 369class TlbFeedback extends XSBundle { 370 val roqIdx = new RoqPtr 371 val hit = Bool() 372} 373 374class FrontendToBackendIO extends XSBundle { 375 // to backend end 376 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 377 val fetchInfo = DecoupledIO(new FtqEntry) 378 // from backend 379 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 380 val cfiUpdateInfo = Flipped(Vec(CommitWidth, ValidIO(new CfiUpdateInfo))) 381} 382 383class TlbCsrBundle extends XSBundle { 384 val satp = new Bundle { 385 val mode = UInt(4.W) // TODO: may change number to parameter 386 val asid = UInt(16.W) 387 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 388 } 389 val priv = new Bundle { 390 val mxr = Bool() 391 val sum = Bool() 392 val imode = UInt(2.W) 393 val dmode = UInt(2.W) 394 } 395 396 override def toPrintable: Printable = { 397 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 398 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 399 } 400} 401 402class SfenceBundle extends XSBundle { 403 val valid = Bool() 404 val bits = new Bundle { 405 val rs1 = Bool() 406 val rs2 = Bool() 407 val addr = UInt(VAddrBits.W) 408 } 409 410 override def toPrintable: Printable = { 411 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 412 } 413} 414