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 = ExceptionVec() 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 LSIdx extends XSBundle { 244 val lqIdx = new LqPtr 245 val sqIdx = new SqPtr 246} 247 248// CfCtrl -> MicroOp at Rename Stage 249class MicroOp extends CfCtrl { 250 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 251 val src1State, src2State, src3State = SrcState() 252 val roqIdx = new RoqPtr 253 val lqIdx = new LqPtr 254 val sqIdx = new SqPtr 255 val diffTestDebugLrScValid = Bool() 256} 257 258class Redirect extends XSBundle { 259 val roqIdx = new RoqPtr 260 val level = RedirectLevel() 261 val interrupt = Bool() 262 val pc = UInt(VAddrBits.W) 263 val target = UInt(VAddrBits.W) 264 val brTag = new BrqPtr 265 266 def isUnconditional() = RedirectLevel.isUnconditional(level) 267 def flushItself() = RedirectLevel.flushItself(level) 268 def isException() = RedirectLevel.isException(level) 269} 270 271class Dp1ToDp2IO extends XSBundle { 272 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 273 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 274 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 275} 276 277class ReplayPregReq extends XSBundle { 278 // NOTE: set isInt and isFp both to 'false' when invalid 279 val isInt = Bool() 280 val isFp = Bool() 281 val preg = UInt(PhyRegIdxWidth.W) 282} 283 284class DebugBundle extends XSBundle{ 285 val isMMIO = Bool() 286} 287 288class ExuInput extends XSBundle { 289 val uop = new MicroOp 290 val src1, src2, src3 = UInt((XLEN+1).W) 291} 292 293class ExuOutput extends XSBundle { 294 val uop = new MicroOp 295 val data = UInt((XLEN+1).W) 296 val fflags = UInt(5.W) 297 val redirectValid = Bool() 298 val redirect = new Redirect 299 val brUpdate = new CfiUpdateInfo 300 val debug = new DebugBundle 301} 302 303class ExternalInterruptIO extends XSBundle { 304 val mtip = Input(Bool()) 305 val msip = Input(Bool()) 306 val meip = Input(Bool()) 307} 308 309class CSRSpecialIO extends XSBundle { 310 val exception = Flipped(ValidIO(new MicroOp)) 311 val isInterrupt = Input(Bool()) 312 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 313 val trapTarget = Output(UInt(VAddrBits.W)) 314 val externalInterrupt = new ExternalInterruptIO 315 val interrupt = Output(Bool()) 316} 317 318class RoqCommitInfo extends XSBundle { 319 val ldest = UInt(5.W) 320 val rfWen = Bool() 321 val fpWen = Bool() 322 val wflags = Bool() 323 val commitType = CommitType() 324 val pdest = UInt(PhyRegIdxWidth.W) 325 val old_pdest = UInt(PhyRegIdxWidth.W) 326 val lqIdx = new LqPtr 327 val sqIdx = new SqPtr 328 329 // these should be optimized for synthesis verilog 330 val pc = UInt(VAddrBits.W) 331} 332 333class RoqCommitIO extends XSBundle { 334 val isWalk = Output(Bool()) 335 val valid = Vec(CommitWidth, Output(Bool())) 336 val info = Vec(CommitWidth, Output(new RoqCommitInfo)) 337 338 def hasWalkInstr = isWalk && valid.asUInt.orR 339 def hasCommitInstr = !isWalk && valid.asUInt.orR 340} 341 342class TlbFeedback extends XSBundle { 343 val roqIdx = new RoqPtr 344 val hit = Bool() 345} 346 347class FrontendToBackendIO extends XSBundle { 348 // to backend end 349 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 350 // from backend 351 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 352 // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 353 val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 354} 355 356class TlbCsrBundle extends XSBundle { 357 val satp = new Bundle { 358 val mode = UInt(4.W) // TODO: may change number to parameter 359 val asid = UInt(16.W) 360 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 361 } 362 val priv = new Bundle { 363 val mxr = Bool() 364 val sum = Bool() 365 val imode = UInt(2.W) 366 val dmode = UInt(2.W) 367 } 368 369 override def toPrintable: Printable = { 370 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 371 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 372 } 373} 374 375class SfenceBundle extends XSBundle { 376 val valid = Bool() 377 val bits = new Bundle { 378 val rs1 = Bool() 379 val rs2 = Bool() 380 val addr = UInt(VAddrBits.W) 381 } 382 383 override def toPrintable: Printable = { 384 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 385 } 386} 387