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