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