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