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 unmaskedJmpIdx = PriorityEncoder(takens) 104 def saveHalfRVI = (firstBankHasHalfRVI && (unmaskedJmpIdx === (bankWidth-1).U || !(takens.orR))) || 105 (lastBankHasHalfRVI && unmaskedJmpIdx === (PredictWidth-1).U) 106 // could get PredictWidth-1 when only the first bank is valid 107 def jmpIdx = PriorityEncoder(realTakens) 108 // only used when taken 109 def target = targets(jmpIdx) 110 def taken = realTakens.orR 111 def takenOnBr = taken && realBrMask(jmpIdx) 112} 113 114class BranchInfo extends XSBundle with HasBPUParameter { 115 val ubtbWriteWay = UInt(log2Up(UBtbWays).W) 116 val ubtbHits = Bool() 117 val btbWriteWay = UInt(log2Up(BtbWays).W) 118 val btbHitJal = Bool() 119 val bimCtr = UInt(2.W) 120 val histPtr = UInt(log2Up(ExtHistoryLength).W) 121 val predHistPtr = UInt(log2Up(ExtHistoryLength).W) 122 val tageMeta = new TageMeta 123 val rasSp = UInt(log2Up(RasSize).W) 124 val rasTopCtr = UInt(8.W) 125 val rasToqAddr = UInt(VAddrBits.W) 126 val fetchIdx = UInt(log2Up(PredictWidth).W) 127 val specCnt = UInt(10.W) 128 val sawNotTakenBranch = Bool() 129 130 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 131 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 132 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 133 134 def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = { 135 this.histPtr := histPtr 136 this.tageMeta := tageMeta 137 this.rasSp := rasSp 138 this.rasTopCtr := rasTopCtr 139 this.asUInt 140 } 141 def size = 0.U.asTypeOf(this).getWidth 142 def fromUInt(x: UInt) = x.asTypeOf(this) 143} 144 145class Predecode extends XSBundle with HasIFUConst { 146 val hasLastHalfRVI = Bool() 147 val mask = UInt((FetchWidth*2).W) 148 val lastHalf = UInt(nBanksInPacket.W) 149 val pd = Vec(FetchWidth*2, (new PreDecodeInfo)) 150} 151 152class BranchUpdateInfo extends XSBundle { 153 // from backend 154 val pc = UInt(VAddrBits.W) 155 val pnpc = UInt(VAddrBits.W) 156 val target = UInt(VAddrBits.W) 157 val brTarget = UInt(VAddrBits.W) 158 val taken = Bool() 159 val fetchIdx = UInt(log2Up(FetchWidth*2).W) 160 val isMisPred = Bool() 161 val brTag = new BrqPtr 162 163 // frontend -> backend -> frontend 164 val pd = new PreDecodeInfo 165 val brInfo = new BranchInfo 166} 167 168// Dequeue DecodeWidth insts from Ibuffer 169class CtrlFlow extends XSBundle { 170 val instr = UInt(32.W) 171 val pc = UInt(VAddrBits.W) 172 val exceptionVec = Vec(16, Bool()) 173 val intrVec = Vec(12, Bool()) 174 val brUpdate = new BranchUpdateInfo 175 val crossPageIPFFix = Bool() 176} 177 178// Decode DecodeWidth insts at Decode Stage 179class CtrlSignals extends XSBundle { 180 val src1Type, src2Type, src3Type = SrcType() 181 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 182 val ldest = UInt(5.W) 183 val fuType = FuType() 184 val fuOpType = FuOpType() 185 val rfWen = Bool() 186 val fpWen = Bool() 187 val isXSTrap = Bool() 188 val noSpecExec = Bool() // wait forward 189 val blockBackward = Bool() // block backward 190 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 191 val isRVF = Bool() 192 val imm = UInt(XLEN.W) 193 val commitType = CommitType() 194} 195 196class CfCtrl extends XSBundle { 197 val cf = new CtrlFlow 198 val ctrl = new CtrlSignals 199 val brTag = new BrqPtr 200} 201 202 203class PerfDebugInfo extends XSBundle { 204 // val fetchTime = UInt(64.W) 205 val renameTime = UInt(64.W) 206 val dispatchTime = UInt(64.W) 207 val issueTime = UInt(64.W) 208 val writebackTime = UInt(64.W) 209 // val commitTime = UInt(64.W) 210} 211 212// Load / Store Index 213// 214// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type. 215trait HasLSIdx { this: HasXSParameter => 216 // Separate LSQ 217 val lqIdx = new LqPtr 218 val sqIdx = new SqPtr 219} 220 221class LSIdx extends XSBundle with HasLSIdx {} 222 223// CfCtrl -> MicroOp at Rename Stage 224class MicroOp extends CfCtrl with HasLSIdx { 225 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 226 val src1State, src2State, src3State = SrcState() 227 val roqIdx = new RoqPtr 228 val diffTestDebugLrScValid = Bool() 229 val debugInfo = new PerfDebugInfo 230} 231 232class Redirect extends XSBundle { 233 val roqIdx = new RoqPtr 234 val isException = Bool() 235 val isMisPred = Bool() 236 val isReplay = Bool() 237 val isFlushPipe = Bool() 238 val pc = UInt(VAddrBits.W) 239 val target = UInt(VAddrBits.W) 240 val brTag = new BrqPtr 241} 242 243class Dp1ToDp2IO extends XSBundle { 244 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 245 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 246 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 247} 248 249class ReplayPregReq extends XSBundle { 250 // NOTE: set isInt and isFp both to 'false' when invalid 251 val isInt = Bool() 252 val isFp = Bool() 253 val preg = UInt(PhyRegIdxWidth.W) 254} 255 256class DebugBundle extends XSBundle{ 257 val isMMIO = Bool() 258} 259 260class ExuInput extends XSBundle { 261 val uop = new MicroOp 262 val src1, src2, src3 = UInt((XLEN+1).W) 263} 264 265class ExuOutput extends XSBundle { 266 val uop = new MicroOp 267 val data = UInt((XLEN+1).W) 268 val fflags = new Fflags 269 val redirectValid = Bool() 270 val redirect = new Redirect 271 val brUpdate = new BranchUpdateInfo 272 val debug = new DebugBundle 273} 274 275class ExternalInterruptIO extends XSBundle { 276 val mtip = Input(Bool()) 277 val msip = Input(Bool()) 278 val meip = Input(Bool()) 279} 280 281class CSRSpecialIO extends XSBundle { 282 val exception = Flipped(ValidIO(new MicroOp)) 283 val isInterrupt = Input(Bool()) 284 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 285 val trapTarget = Output(UInt(VAddrBits.W)) 286 val externalInterrupt = new ExternalInterruptIO 287 val interrupt = Output(Bool()) 288} 289 290//class ExuIO extends XSBundle { 291// val in = Flipped(DecoupledIO(new ExuInput)) 292// val redirect = Flipped(ValidIO(new Redirect)) 293// val out = DecoupledIO(new ExuOutput) 294// // for csr 295// val csrOnly = new CSRSpecialIO 296// val mcommit = Input(UInt(3.W)) 297//} 298 299class RoqCommit extends XSBundle { 300 val uop = new MicroOp 301 val isWalk = Bool() 302} 303 304class TlbFeedback extends XSBundle { 305 val roqIdx = new RoqPtr 306 val hit = Bool() 307} 308 309class FrontendToBackendIO extends XSBundle { 310 // to backend end 311 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 312 // from backend 313 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 314 val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 315 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 316} 317 318class TlbCsrBundle extends XSBundle { 319 val satp = new Bundle { 320 val mode = UInt(4.W) // TODO: may change number to parameter 321 val asid = UInt(16.W) 322 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 323 } 324 val priv = new Bundle { 325 val mxr = Bool() 326 val sum = Bool() 327 val imode = UInt(2.W) 328 val dmode = UInt(2.W) 329 } 330 331 override def toPrintable: Printable = { 332 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 333 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 334 } 335} 336 337class SfenceBundle extends XSBundle { 338 val valid = Bool() 339 val bits = new Bundle { 340 val rs1 = Bool() 341 val rs2 = Bool() 342 val addr = UInt(VAddrBits.W) 343 } 344 345 override def toPrintable: Printable = { 346 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 347 } 348} 349