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