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