1package xiangshan 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan.backend.brq.BrqPtr 6import xiangshan.backend.fu.fpu.Fflags 7import xiangshan.backend.rename.FreeListPtr 8import xiangshan.backend.roq.RoqPtr 9import xiangshan.mem.{LqPtr, SqPtr} 10import xiangshan.frontend.PreDecodeInfo 11import xiangshan.frontend.HasBPUParameter 12import xiangshan.frontend.HasTageParameter 13import xiangshan.frontend.HasIFUConst 14import utils._ 15import scala.math.max 16 17// Fetch FetchWidth x 32-bit insts from Icache 18class FetchPacket extends XSBundle { 19 val instrs = Vec(PredictWidth, UInt(32.W)) 20 val mask = UInt(PredictWidth.W) 21 // val pc = UInt(VAddrBits.W) 22 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 23 val pnpc = Vec(PredictWidth, UInt(VAddrBits.W)) 24 val brInfo = Vec(PredictWidth, new BranchInfo) 25 val pd = Vec(PredictWidth, new PreDecodeInfo) 26 val ipf = Bool() 27 val crossPageIPFFix = Bool() 28 val predTaken = Bool() 29} 30 31class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 32 val valid = Bool() 33 val bits = gen.cloneType.asInstanceOf[T] 34 override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type] 35} 36 37object ValidUndirectioned { 38 def apply[T <: Data](gen: T) = { 39 new ValidUndirectioned[T](gen) 40 } 41} 42 43class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter { 44 def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_) 45 def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_)) 46 def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1 47 val tageTaken = if (useSC) Bool() else UInt(0.W) 48 val scUsed = if (useSC) Bool() else UInt(0.W) 49 val scPred = if (useSC) Bool() else UInt(0.W) 50 // Suppose ctrbits of all tables are identical 51 val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W)) 52 val sumAbs = if (useSC) UInt(sumCtrBits.W) else UInt(0.W) 53} 54 55class TageMeta extends XSBundle with HasTageParameter { 56 val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 57 val altDiffers = Bool() 58 val providerU = UInt(2.W) 59 val providerCtr = UInt(3.W) 60 val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 61 val taken = Bool() 62 val scMeta = new SCMeta(EnableSC) 63} 64 65class BranchPrediction extends XSBundle with HasIFUConst { 66 // val redirect = Bool() 67 val takens = UInt(PredictWidth.W) 68 // val jmpIdx = UInt(log2Up(PredictWidth).W) 69 val brMask = UInt(PredictWidth.W) 70 val jalMask = UInt(PredictWidth.W) 71 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 72 73 // marks the last 2 bytes of this fetch packet 74 // val endsAtTheEndOfFirstBank = Bool() 75 // val endsAtTheEndOfLastBank = Bool() 76 77 // half RVI could only start at the end of a bank 78 val firstBankHasHalfRVI = Bool() 79 val lastBankHasHalfRVI = Bool() 80 81 def lastHalfRVIMask = Mux(firstBankHasHalfRVI, UIntToOH((bankWidth-1).U), 82 Mux(lastBankHasHalfRVI, UIntToOH((PredictWidth-1).U), 83 0.U(PredictWidth.W) 84 ) 85 ) 86 87 def lastHalfRVIClearMask = ~lastHalfRVIMask 88 // is taken from half RVI 89 def lastHalfRVITaken = (takens & lastHalfRVIMask).orR 90 91 def lastHalfRVIIdx = Mux(firstBankHasHalfRVI, (bankWidth-1).U, (PredictWidth-1).U) 92 // should not be used if not lastHalfRVITaken 93 def lastHalfRVITarget = Mux(firstBankHasHalfRVI, targets(bankWidth-1), targets(PredictWidth-1)) 94 95 def realTakens = takens & lastHalfRVIClearMask 96 def realBrMask = brMask & lastHalfRVIClearMask 97 def realJalMask = jalMask & lastHalfRVIClearMask 98 99 def brNotTakens = ~realTakens & realBrMask 100 def sawNotTakenBr = VecInit((0 until PredictWidth).map(i => 101 (if (i == 0) false.B else brNotTakens(i-1,0).orR))) 102 def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR 103 def saveHalfRVI = (firstBankHasHalfRVI && (jmpIdx === (bankWidth-1).U || !(takens.orR))) || 104 (lastBankHasHalfRVI && jmpIdx === (PredictWidth-1).U) 105 // could get PredictWidth-1 when only the first bank is valid 106 def jmpIdx = PriorityEncoder(realTakens) 107 // only used when taken 108 def target = targets(jmpIdx) 109 def taken = realTakens.orR 110 def takenOnBr = taken && realBrMask(jmpIdx) 111} 112 113class BranchInfo extends XSBundle with HasBPUParameter { 114 val ubtbWriteWay = UInt(log2Up(UBtbWays).W) 115 val ubtbHits = Bool() 116 val btbWriteWay = UInt(log2Up(BtbWays).W) 117 val btbHitJal = Bool() 118 val bimCtr = UInt(2.W) 119 val histPtr = UInt(log2Up(ExtHistoryLength).W) 120 val predHistPtr = UInt(log2Up(ExtHistoryLength).W) 121 val tageMeta = new TageMeta 122 val rasSp = UInt(log2Up(RasSize).W) 123 val rasTopCtr = UInt(8.W) 124 val rasToqAddr = UInt(VAddrBits.W) 125 val fetchIdx = UInt(log2Up(PredictWidth).W) 126 val specCnt = UInt(10.W) 127 val sawNotTakenBranch = Bool() 128 129 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 130 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 131 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 132 133 def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = { 134 this.histPtr := histPtr 135 this.tageMeta := tageMeta 136 this.rasSp := rasSp 137 this.rasTopCtr := rasTopCtr 138 this.asUInt 139 } 140 def size = 0.U.asTypeOf(this).getWidth 141 def fromUInt(x: UInt) = x.asTypeOf(this) 142} 143 144class Predecode extends XSBundle with HasIFUConst { 145 val hasLastHalfRVI = Bool() 146 val mask = UInt((FetchWidth*2).W) 147 val endMask = UInt(nBanksInPacket.W) 148 val pd = Vec(FetchWidth*2, (new PreDecodeInfo)) 149} 150 151class BranchUpdateInfo extends XSBundle { 152 // from backend 153 val pc = UInt(VAddrBits.W) 154 val pnpc = UInt(VAddrBits.W) 155 val target = UInt(VAddrBits.W) 156 val brTarget = UInt(VAddrBits.W) 157 val taken = Bool() 158 val fetchIdx = UInt(log2Up(FetchWidth*2).W) 159 val isMisPred = Bool() 160 val brTag = new BrqPtr 161 162 // frontend -> backend -> frontend 163 val pd = new PreDecodeInfo 164 val brInfo = new BranchInfo 165} 166 167// Dequeue DecodeWidth insts from Ibuffer 168class CtrlFlow extends XSBundle { 169 val instr = UInt(32.W) 170 val pc = UInt(VAddrBits.W) 171 val exceptionVec = Vec(16, Bool()) 172 val intrVec = Vec(12, Bool()) 173 val brUpdate = new BranchUpdateInfo 174 val crossPageIPFFix = Bool() 175} 176 177// Decode DecodeWidth insts at Decode Stage 178class CtrlSignals extends XSBundle { 179 val src1Type, src2Type, src3Type = SrcType() 180 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 181 val ldest = UInt(5.W) 182 val fuType = FuType() 183 val fuOpType = FuOpType() 184 val rfWen = Bool() 185 val fpWen = Bool() 186 val isXSTrap = Bool() 187 val noSpecExec = Bool() // wait forward 188 val blockBackward = Bool() // block backward 189 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 190 val isRVF = Bool() 191 val imm = UInt(XLEN.W) 192 val commitType = CommitType() 193} 194 195class CfCtrl extends XSBundle { 196 val cf = new CtrlFlow 197 val ctrl = new CtrlSignals 198 val brTag = new BrqPtr 199} 200 201// Load / Store Index 202// 203// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type. 204trait HasLSIdx { this: HasXSParameter => 205 // Separate LSQ 206 val lqIdx = new LqPtr 207 val sqIdx = new SqPtr 208} 209 210class LSIdx extends XSBundle with HasLSIdx {} 211 212// CfCtrl -> MicroOp at Rename Stage 213class MicroOp extends CfCtrl with HasLSIdx { 214 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 215 val src1State, src2State, src3State = SrcState() 216 val roqIdx = new RoqPtr 217 val diffTestDebugLrScValid = Bool() 218} 219 220class Redirect extends XSBundle { 221 val roqIdx = new RoqPtr 222 val isException = Bool() 223 val isMisPred = Bool() 224 val isReplay = Bool() 225 val isFlushPipe = Bool() 226 val pc = UInt(VAddrBits.W) 227 val target = UInt(VAddrBits.W) 228 val brTag = new BrqPtr 229} 230 231class Dp1ToDp2IO extends XSBundle { 232 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 233 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 234 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 235} 236 237class ReplayPregReq extends XSBundle { 238 // NOTE: set isInt and isFp both to 'false' when invalid 239 val isInt = Bool() 240 val isFp = Bool() 241 val preg = UInt(PhyRegIdxWidth.W) 242} 243 244class DebugBundle extends XSBundle{ 245 val isMMIO = Bool() 246} 247 248class ExuInput extends XSBundle { 249 val uop = new MicroOp 250 val src1, src2, src3 = UInt((XLEN+1).W) 251} 252 253class ExuOutput extends XSBundle { 254 val uop = new MicroOp 255 val data = UInt((XLEN+1).W) 256 val fflags = new Fflags 257 val redirectValid = Bool() 258 val redirect = new Redirect 259 val brUpdate = new BranchUpdateInfo 260 val debug = new DebugBundle 261} 262 263class ExternalInterruptIO extends XSBundle { 264 val mtip = Input(Bool()) 265 val msip = Input(Bool()) 266 val meip = Input(Bool()) 267} 268 269class CSRSpecialIO extends XSBundle { 270 val exception = Flipped(ValidIO(new MicroOp)) 271 val isInterrupt = Input(Bool()) 272 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 273 val trapTarget = Output(UInt(VAddrBits.W)) 274 val externalInterrupt = new ExternalInterruptIO 275 val interrupt = Output(Bool()) 276} 277 278//class ExuIO extends XSBundle { 279// val in = Flipped(DecoupledIO(new ExuInput)) 280// val redirect = Flipped(ValidIO(new Redirect)) 281// val out = DecoupledIO(new ExuOutput) 282// // for csr 283// val csrOnly = new CSRSpecialIO 284// val mcommit = Input(UInt(3.W)) 285//} 286 287class RoqCommit extends XSBundle { 288 val uop = new MicroOp 289 val isWalk = Bool() 290} 291 292class TlbFeedback extends XSBundle { 293 val roqIdx = new RoqPtr 294 val hit = Bool() 295} 296 297class FrontendToBackendIO extends XSBundle { 298 // to backend end 299 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 300 // from backend 301 val redirect = Flipped(ValidIO(new Redirect)) 302 val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 303 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 304} 305 306class TlbCsrBundle extends XSBundle { 307 val satp = new Bundle { 308 val mode = UInt(4.W) // TODO: may change number to parameter 309 val asid = UInt(16.W) 310 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 311 } 312 val priv = new Bundle { 313 val mxr = Bool() 314 val sum = Bool() 315 val imode = UInt(2.W) 316 val dmode = UInt(2.W) 317 } 318 319 override def toPrintable: Printable = { 320 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 321 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 322 } 323} 324 325class SfenceBundle extends XSBundle { 326 val valid = Bool() 327 val bits = new Bundle { 328 val rs1 = Bool() 329 val rs2 = Bool() 330 val addr = UInt(VAddrBits.W) 331 } 332 333 override def toPrintable: Printable = { 334 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 335 } 336} 337