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// Load / Store Index 203// 204// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type. 205trait HasLSIdx { this: HasXSParameter => 206 // Separate LSQ 207 val lqIdx = new LqPtr 208 val sqIdx = new SqPtr 209} 210 211class LSIdx extends XSBundle with HasLSIdx {} 212 213// CfCtrl -> MicroOp at Rename Stage 214class MicroOp extends CfCtrl with HasLSIdx { 215 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 216 val src1State, src2State, src3State = SrcState() 217 val roqIdx = new RoqPtr 218 val diffTestDebugLrScValid = Bool() 219} 220 221class Redirect extends XSBundle { 222 val roqIdx = new RoqPtr 223 val isException = Bool() 224 val isMisPred = Bool() 225 val isReplay = Bool() 226 val isFlushPipe = Bool() 227 val pc = UInt(VAddrBits.W) 228 val target = UInt(VAddrBits.W) 229 val brTag = new BrqPtr 230} 231 232class Dp1ToDp2IO extends XSBundle { 233 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 234 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 235 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 236} 237 238class ReplayPregReq extends XSBundle { 239 // NOTE: set isInt and isFp both to 'false' when invalid 240 val isInt = Bool() 241 val isFp = Bool() 242 val preg = UInt(PhyRegIdxWidth.W) 243} 244 245class DebugBundle extends XSBundle{ 246 val isMMIO = Bool() 247} 248 249class ExuInput extends XSBundle { 250 val uop = new MicroOp 251 val src1, src2, src3 = UInt((XLEN+1).W) 252} 253 254class ExuOutput extends XSBundle { 255 val uop = new MicroOp 256 val data = UInt((XLEN+1).W) 257 val fflags = new Fflags 258 val redirectValid = Bool() 259 val redirect = new Redirect 260 val brUpdate = new BranchUpdateInfo 261 val debug = new DebugBundle 262} 263 264class ExternalInterruptIO extends XSBundle { 265 val mtip = Input(Bool()) 266 val msip = Input(Bool()) 267 val meip = Input(Bool()) 268} 269 270class CSRSpecialIO extends XSBundle { 271 val exception = Flipped(ValidIO(new MicroOp)) 272 val isInterrupt = Input(Bool()) 273 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 274 val trapTarget = Output(UInt(VAddrBits.W)) 275 val externalInterrupt = new ExternalInterruptIO 276 val interrupt = Output(Bool()) 277} 278 279//class ExuIO extends XSBundle { 280// val in = Flipped(DecoupledIO(new ExuInput)) 281// val redirect = Flipped(ValidIO(new Redirect)) 282// val out = DecoupledIO(new ExuOutput) 283// // for csr 284// val csrOnly = new CSRSpecialIO 285// val mcommit = Input(UInt(3.W)) 286//} 287 288class RoqCommit extends XSBundle { 289 val uop = new MicroOp 290 val isWalk = Bool() 291} 292 293class TlbFeedback extends XSBundle { 294 val roqIdx = new RoqPtr 295 val hit = Bool() 296} 297 298class FrontendToBackendIO extends XSBundle { 299 // to backend end 300 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 301 // from backend 302 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 303 val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 304 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 305} 306 307class TlbCsrBundle extends XSBundle { 308 val satp = new Bundle { 309 val mode = UInt(4.W) // TODO: may change number to parameter 310 val asid = UInt(16.W) 311 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 312 } 313 val priv = new Bundle { 314 val mxr = Bool() 315 val sum = Bool() 316 val imode = UInt(2.W) 317 val dmode = UInt(2.W) 318 } 319 320 override def toPrintable: Printable = { 321 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 322 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 323 } 324} 325 326class SfenceBundle extends XSBundle { 327 val valid = Bool() 328 val bits = new Bundle { 329 val rs1 = Bool() 330 val rs2 = Bool() 331 val addr = UInt(VAddrBits.W) 332 } 333 334 override def toPrintable: Printable = { 335 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 336 } 337} 338