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