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.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._ 17import scala.math.max 18 19// Fetch FetchWidth x 32-bit insts from Icache 20class FetchPacket extends XSBundle { 21 val instrs = Vec(PredictWidth, UInt(32.W)) 22 val mask = UInt(PredictWidth.W) 23 val pdmask = UInt(PredictWidth.W) 24 // val pc = UInt(VAddrBits.W) 25 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 26 val pnpc = Vec(PredictWidth, UInt(VAddrBits.W)) 27 val bpuMeta = Vec(PredictWidth, new BpuMeta) 28 val pd = Vec(PredictWidth, new PreDecodeInfo) 29 val ipf = Bool() 30 val acf = Bool() 31 val crossPageIPFFix = Bool() 32 val predTaken = Bool() 33} 34 35class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 36 val valid = Bool() 37 val bits = gen.cloneType.asInstanceOf[T] 38 override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type] 39} 40 41object ValidUndirectioned { 42 def apply[T <: Data](gen: T) = { 43 new ValidUndirectioned[T](gen) 44 } 45} 46 47class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter { 48 def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_) 49 def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_)) 50 def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1 51 val tageTaken = if (useSC) Bool() else UInt(0.W) 52 val scUsed = if (useSC) Bool() else UInt(0.W) 53 val scPred = if (useSC) Bool() else UInt(0.W) 54 // Suppose ctrbits of all tables are identical 55 val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W)) 56 val sumAbs = if (useSC) UInt(sumCtrBits.W) else UInt(0.W) 57} 58 59class TageMeta extends XSBundle with HasTageParameter { 60 val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 61 val altDiffers = Bool() 62 val providerU = UInt(2.W) 63 val providerCtr = UInt(3.W) 64 val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 65 val taken = Bool() 66 val scMeta = new SCMeta(EnableSC) 67} 68 69class BranchPrediction extends XSBundle with HasIFUConst { 70 // val redirect = Bool() 71 val takens = UInt(PredictWidth.W) 72 // val jmpIdx = UInt(log2Up(PredictWidth).W) 73 val brMask = UInt(PredictWidth.W) 74 val jalMask = UInt(PredictWidth.W) 75 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 76 77 // marks the last 2 bytes of this fetch packet 78 // val endsAtTheEndOfFirstBank = Bool() 79 // val endsAtTheEndOfLastBank = Bool() 80 81 // half RVI could only start at the end of a bank 82 val firstBankHasHalfRVI = Bool() 83 val lastBankHasHalfRVI = Bool() 84 85 // assumes that only one of the two conditions could be true 86 def lastHalfRVIMask = Cat(lastBankHasHalfRVI.asUInt, 0.U(7.W), firstBankHasHalfRVI.asUInt, 0.U(7.W)) 87 88 def lastHalfRVIClearMask = ~lastHalfRVIMask 89 // is taken from half RVI 90 def lastHalfRVITaken = (takens(bankWidth-1) && firstBankHasHalfRVI) || (takens(PredictWidth-1) && lastBankHasHalfRVI) 91 92 def lastHalfRVIIdx = Mux(firstBankHasHalfRVI, (bankWidth-1).U, (PredictWidth-1).U) 93 // should not be used if not lastHalfRVITaken 94 def lastHalfRVITarget = Mux(firstBankHasHalfRVI, targets(bankWidth-1), targets(PredictWidth-1)) 95 96 def realTakens = takens & lastHalfRVIClearMask 97 def realBrMask = brMask & lastHalfRVIClearMask 98 def realJalMask = jalMask & lastHalfRVIClearMask 99 100 def brNotTakens = ~takens & realBrMask 101 def sawNotTakenBr = VecInit((0 until PredictWidth).map(i => 102 (if (i == 0) false.B else ParallelORR(brNotTakens(i-1,0))))) 103 // def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR 104 def unmaskedJmpIdx = ParallelPriorityEncoder(takens) 105 // if not taken before the half RVI inst 106 def saveHalfRVI = (firstBankHasHalfRVI && !(ParallelORR(takens(bankWidth-2,0)))) || 107 (lastBankHasHalfRVI && !(ParallelORR(takens(PredictWidth-2,0)))) 108 // could get PredictWidth-1 when only the first bank is valid 109 def jmpIdx = ParallelPriorityEncoder(realTakens) 110 // only used when taken 111 def target = ParallelPriorityMux(realTakens, targets) 112 def taken = ParallelORR(realTakens) 113 def takenOnBr = taken && ParallelPriorityMux(realTakens, realBrMask.asBools) 114 def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(realTakens, sawNotTakenBr), ParallelORR(brNotTakens)) 115} 116 117class BpuMeta extends XSBundle with HasBPUParameter { 118 val ubtbWriteWay = UInt(log2Up(UBtbWays).W) 119 val ubtbHits = Bool() 120 val btbWriteWay = UInt(log2Up(BtbWays).W) 121 val btbHitJal = Bool() 122 val bimCtr = UInt(2.W) 123 val tageMeta = new TageMeta 124 val rasSp = UInt(log2Up(RasSize).W) 125 val rasTopCtr = UInt(8.W) 126 val rasToqAddr = UInt(VAddrBits.W) 127 val fetchIdx = UInt(log2Up(PredictWidth).W) 128 val specCnt = UInt(10.W) 129 // for global history 130 val predTaken = Bool() 131 val hist = new GlobalHistory 132 val predHist = new GlobalHistory 133 val sawNotTakenBranch = Bool() 134 135 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 136 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 137 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 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 def fromUInt(x: UInt) = x.asTypeOf(this) 148} 149 150class Predecode extends XSBundle with HasIFUConst { 151 val hasLastHalfRVI = Bool() 152 val mask = UInt((FetchWidth*2).W) 153 val lastHalf = UInt(nBanksInPacket.W) 154 val pd = Vec(FetchWidth*2, (new PreDecodeInfo)) 155} 156 157class CfiUpdateInfo extends XSBundle { 158 // from backend 159 val pc = UInt(VAddrBits.W) 160 val pnpc = UInt(VAddrBits.W) 161 val fetchIdx = UInt(log2Up(FetchWidth*2).W) 162 // frontend -> backend -> frontend 163 val pd = new PreDecodeInfo 164 val bpuMeta = new BpuMeta 165 166 // need pipeline update 167 val target = UInt(VAddrBits.W) 168 val brTarget = UInt(VAddrBits.W) 169 val taken = Bool() 170 val isMisPred = Bool() 171 val brTag = new BrqPtr 172 val isReplay = Bool() 173} 174 175// Dequeue DecodeWidth insts from Ibuffer 176class CtrlFlow extends XSBundle { 177 val instr = UInt(32.W) 178 val pc = UInt(VAddrBits.W) 179 val exceptionVec = Vec(16, Bool()) 180 val intrVec = Vec(12, Bool()) 181 val brUpdate = new CfiUpdateInfo 182 val crossPageIPFFix = Bool() 183} 184 185 186class FPUCtrlSignals extends XSBundle { 187 val isAddSub = Bool() // swap23 188 val typeTagIn = UInt(2.W) 189 val typeTagOut = UInt(2.W) 190 val fromInt = Bool() 191 val wflags = Bool() 192 val fpWen = Bool() 193 val fmaCmd = UInt(2.W) 194 val div = Bool() 195 val sqrt = Bool() 196 val fcvt = Bool() 197 val typ = UInt(2.W) 198 val fmt = UInt(2.W) 199 val ren3 = Bool() //TODO: remove SrcType.fp 200} 201 202// Decode DecodeWidth insts at Decode Stage 203class CtrlSignals extends XSBundle { 204 val src1Type, src2Type, src3Type = SrcType() 205 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 206 val ldest = UInt(5.W) 207 val fuType = FuType() 208 val fuOpType = FuOpType() 209 val rfWen = Bool() 210 val fpWen = Bool() 211 val isXSTrap = Bool() 212 val noSpecExec = Bool() // wait forward 213 val blockBackward = Bool() // block backward 214 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 215 val isRVF = Bool() 216 val selImm = SelImm() 217 val imm = UInt(XLEN.W) 218 val commitType = CommitType() 219 val fpu = new FPUCtrlSignals 220 221 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 222 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 223 val signals = 224 Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen, 225 isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm) 226 signals zip decoder map { case(s, d) => s := d } 227 commitType := DontCare 228 this 229 } 230} 231 232class CfCtrl extends XSBundle { 233 val cf = new CtrlFlow 234 val ctrl = new CtrlSignals 235 val brTag = new BrqPtr 236} 237 238class PerfDebugInfo extends XSBundle { 239 // val fetchTime = UInt(64.W) 240 val renameTime = UInt(64.W) 241 val dispatchTime = UInt(64.W) 242 val issueTime = UInt(64.W) 243 val writebackTime = UInt(64.W) 244 // val commitTime = UInt(64.W) 245} 246 247// Separate LSQ 248class LSIdx extends XSBundle { 249 val lqIdx = new LqPtr 250 val sqIdx = new SqPtr 251} 252 253// CfCtrl -> MicroOp at Rename Stage 254class MicroOp extends CfCtrl { 255 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 256 val src1State, src2State, src3State = SrcState() 257 val roqIdx = new RoqPtr 258 val lqIdx = new LqPtr 259 val sqIdx = new SqPtr 260 val diffTestDebugLrScValid = Bool() 261 val debugInfo = new PerfDebugInfo 262} 263 264class Redirect extends XSBundle { 265 val roqIdx = new RoqPtr 266 val level = RedirectLevel() 267 val interrupt = Bool() 268 val pc = UInt(VAddrBits.W) 269 val target = UInt(VAddrBits.W) 270 val brTag = new BrqPtr 271 272 def isUnconditional() = RedirectLevel.isUnconditional(level) 273 def flushItself() = RedirectLevel.flushItself(level) 274 def isException() = RedirectLevel.isException(level) 275} 276 277class Dp1ToDp2IO extends XSBundle { 278 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 279 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 280 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 281} 282 283class ReplayPregReq extends XSBundle { 284 // NOTE: set isInt and isFp both to 'false' when invalid 285 val isInt = Bool() 286 val isFp = Bool() 287 val preg = UInt(PhyRegIdxWidth.W) 288} 289 290class DebugBundle extends XSBundle{ 291 val isMMIO = Bool() 292} 293 294class ExuInput extends XSBundle { 295 val uop = new MicroOp 296 val src1, src2, src3 = UInt((XLEN+1).W) 297} 298 299class ExuOutput extends XSBundle { 300 val uop = new MicroOp 301 val data = UInt((XLEN+1).W) 302 val fflags = UInt(5.W) 303 val redirectValid = Bool() 304 val redirect = new Redirect 305 val brUpdate = new CfiUpdateInfo 306 val debug = new DebugBundle 307} 308 309class ExternalInterruptIO extends XSBundle { 310 val mtip = Input(Bool()) 311 val msip = Input(Bool()) 312 val meip = Input(Bool()) 313} 314 315class CSRSpecialIO extends XSBundle { 316 val exception = Flipped(ValidIO(new MicroOp)) 317 val isInterrupt = Input(Bool()) 318 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 319 val trapTarget = Output(UInt(VAddrBits.W)) 320 val externalInterrupt = new ExternalInterruptIO 321 val interrupt = Output(Bool()) 322} 323 324class RoqCommitInfo extends XSBundle { 325 val ldest = UInt(5.W) 326 val rfWen = Bool() 327 val fpWen = Bool() 328 val wflags = Bool() 329 val commitType = CommitType() 330 val pdest = UInt(PhyRegIdxWidth.W) 331 val old_pdest = UInt(PhyRegIdxWidth.W) 332 val lqIdx = new LqPtr 333 val sqIdx = new SqPtr 334 335 // these should be optimized for synthesis verilog 336 val pc = UInt(VAddrBits.W) 337} 338 339class RoqCommitIO extends XSBundle { 340 val isWalk = Output(Bool()) 341 val valid = Vec(CommitWidth, Output(Bool())) 342 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 343 344 def hasWalkInstr = isWalk && valid.asUInt.orR 345 def hasCommitInstr = !isWalk && valid.asUInt.orR 346} 347 348class TlbFeedback extends XSBundle { 349 val roqIdx = new RoqPtr 350 val hit = Bool() 351} 352 353class FrontendToBackendIO extends XSBundle { 354 // to backend end 355 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 356 // from backend 357 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 358 // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 359 val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 360} 361 362class TlbCsrBundle extends XSBundle { 363 val satp = new Bundle { 364 val mode = UInt(4.W) // TODO: may change number to parameter 365 val asid = UInt(16.W) 366 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 367 } 368 val priv = new Bundle { 369 val mxr = Bool() 370 val sum = Bool() 371 val imode = UInt(2.W) 372 val dmode = UInt(2.W) 373 } 374 375 override def toPrintable: Printable = { 376 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 377 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 378 } 379} 380 381class SfenceBundle extends XSBundle { 382 val valid = Bool() 383 val bits = new Bundle { 384 val rs1 = Bool() 385 val rs2 = Bool() 386 val addr = UInt(VAddrBits.W) 387 } 388 389 override def toPrintable: Printable = { 390 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 391 } 392} 393