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 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 70class BranchPrediction extends XSBundle with HasIFUConst { 71 // val redirect = Bool() 72 val takens = UInt(PredictWidth.W) 73 // val jmpIdx = UInt(log2Up(PredictWidth).W) 74 val brMask = UInt(PredictWidth.W) 75 val jalMask = UInt(PredictWidth.W) 76 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 77 78 // marks the last 2 bytes of this fetch packet 79 // val endsAtTheEndOfFirstBank = Bool() 80 // val endsAtTheEndOfLastBank = Bool() 81 82 // half RVI could only start at the end of a bank 83 val firstBankHasHalfRVI = Bool() 84 val lastBankHasHalfRVI = Bool() 85 86 // assumes that only one of the two conditions could be true 87 def lastHalfRVIMask = Cat(lastBankHasHalfRVI.asUInt, 0.U(7.W), firstBankHasHalfRVI.asUInt, 0.U(7.W)) 88 89 def lastHalfRVIClearMask = ~lastHalfRVIMask 90 // is taken from half RVI 91 def lastHalfRVITaken = (takens(bankWidth-1) && firstBankHasHalfRVI) || (takens(PredictWidth-1) && lastBankHasHalfRVI) 92 93 def lastHalfRVIIdx = Mux(firstBankHasHalfRVI, (bankWidth-1).U, (PredictWidth-1).U) 94 // should not be used if not lastHalfRVITaken 95 def lastHalfRVITarget = Mux(firstBankHasHalfRVI, targets(bankWidth-1), targets(PredictWidth-1)) 96 97 def realTakens = takens & lastHalfRVIClearMask 98 def realBrMask = brMask & lastHalfRVIClearMask 99 def realJalMask = jalMask & lastHalfRVIClearMask 100 101 def brNotTakens = ~takens & realBrMask 102 def sawNotTakenBr = VecInit((0 until PredictWidth).map(i => 103 (if (i == 0) false.B else ParallelORR(brNotTakens(i-1,0))))) 104 // def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR 105 def unmaskedJmpIdx = ParallelPriorityEncoder(takens) 106 // if not taken before the half RVI inst 107 def saveHalfRVI = (firstBankHasHalfRVI && !(ParallelORR(takens(bankWidth-2,0)))) || 108 (lastBankHasHalfRVI && !(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 = 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 BpuMeta 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 CfiUpdateInfo extends XSBundle { 158 // from backend 159 val pc = UInt(VAddrBits.W) 160 val pnpc = UInt(VAddrBits.W) 161 val fetchIdx = UInt(log2Up(FetchWidth*2).W) 162 // frontend -> backend -> frontend 163 val pd = new PreDecodeInfo 164 val bpuMeta = new BpuMeta 165 166 // need pipeline update 167 val target = UInt(VAddrBits.W) 168 val brTarget = UInt(VAddrBits.W) 169 val taken = Bool() 170 val isMisPred = Bool() 171 val brTag = new BrqPtr 172 val isReplay = Bool() 173} 174 175// Dequeue DecodeWidth insts from Ibuffer 176class CtrlFlow extends XSBundle { 177 val instr = UInt(32.W) 178 val pc = UInt(VAddrBits.W) 179 val exceptionVec = Vec(16, Bool()) 180 val intrVec = Vec(12, Bool()) 181 val brUpdate = new CfiUpdateInfo 182 val crossPageIPFFix = Bool() 183} 184 185// Decode DecodeWidth insts at Decode Stage 186class CtrlSignals extends XSBundle { 187 val src1Type, src2Type, src3Type = SrcType() 188 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 189 val ldest = UInt(5.W) 190 val fuType = FuType() 191 val fuOpType = FuOpType() 192 val rfWen = Bool() 193 val fpWen = Bool() 194 val isXSTrap = Bool() 195 val noSpecExec = Bool() // wait forward 196 val blockBackward = Bool() // block backward 197 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 198 val isRVF = Bool() 199 val selImm = SelImm() 200 val imm = UInt(XLEN.W) 201 val commitType = CommitType() 202 203 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 204 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 205 val signals = 206 Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen, 207 isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm) 208 signals zip decoder map { case(s, d) => s := d } 209 commitType := DontCare 210 this 211 } 212} 213 214class CfCtrl extends XSBundle { 215 val cf = new CtrlFlow 216 val ctrl = new CtrlSignals 217 val brTag = new BrqPtr 218} 219 220// Load / Store Index 221// 222// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type. 223trait HasLSIdx { this: HasXSParameter => 224 // Separate LSQ 225 val lqIdx = new LqPtr 226 val sqIdx = new SqPtr 227} 228 229class LSIdx extends XSBundle with HasLSIdx {} 230 231// CfCtrl -> MicroOp at Rename Stage 232class MicroOp extends CfCtrl with HasLSIdx { 233 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 234 val src1State, src2State, src3State = SrcState() 235 val roqIdx = new RoqPtr 236 val diffTestDebugLrScValid = Bool() 237} 238 239class Redirect extends XSBundle { 240 val roqIdx = new RoqPtr 241 val isException = Bool() 242 val isMisPred = Bool() 243 val isReplay = Bool() 244 val isFlushPipe = Bool() 245 val pc = UInt(VAddrBits.W) 246 val target = UInt(VAddrBits.W) 247 val brTag = new BrqPtr 248} 249 250class Dp1ToDp2IO extends XSBundle { 251 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 252 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 253 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 254} 255 256class ReplayPregReq extends XSBundle { 257 // NOTE: set isInt and isFp both to 'false' when invalid 258 val isInt = Bool() 259 val isFp = Bool() 260 val preg = UInt(PhyRegIdxWidth.W) 261} 262 263class DebugBundle extends XSBundle{ 264 val isMMIO = Bool() 265} 266 267class ExuInput extends XSBundle { 268 val uop = new MicroOp 269 val src1, src2, src3 = UInt((XLEN+1).W) 270} 271 272class ExuOutput extends XSBundle { 273 val uop = new MicroOp 274 val data = UInt((XLEN+1).W) 275 val fflags = new Fflags 276 val redirectValid = Bool() 277 val redirect = new Redirect 278 val brUpdate = new CfiUpdateInfo 279 val debug = new DebugBundle 280} 281 282class ExternalInterruptIO extends XSBundle { 283 val mtip = Input(Bool()) 284 val msip = Input(Bool()) 285 val meip = Input(Bool()) 286} 287 288class CSRSpecialIO extends XSBundle { 289 val exception = Flipped(ValidIO(new MicroOp)) 290 val isInterrupt = Input(Bool()) 291 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 292 val trapTarget = Output(UInt(VAddrBits.W)) 293 val externalInterrupt = new ExternalInterruptIO 294 val interrupt = Output(Bool()) 295} 296 297//class ExuIO extends XSBundle { 298// val in = Flipped(DecoupledIO(new ExuInput)) 299// val redirect = Flipped(ValidIO(new Redirect)) 300// val out = DecoupledIO(new ExuOutput) 301// // for csr 302// val csrOnly = new CSRSpecialIO 303// val mcommit = Input(UInt(3.W)) 304//} 305 306class RoqCommitIO extends XSBundle { 307 val isWalk = Output(Bool()) 308 val valid = Vec(CommitWidth, Output(Bool())) 309 val uop = Vec(CommitWidth, Output(new MicroOp)) 310 311 def hasWalkInstr = isWalk && valid.asUInt.orR 312 def hasCommitInstr = !isWalk && valid.asUInt.orR 313} 314 315class TlbFeedback extends XSBundle { 316 val roqIdx = new RoqPtr 317 val hit = Bool() 318} 319 320class FrontendToBackendIO extends XSBundle { 321 // to backend end 322 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 323 // from backend 324 val redirect = Flipped(ValidIO(UInt(VAddrBits.W))) 325 // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 326 val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo)) 327} 328 329class TlbCsrBundle extends XSBundle { 330 val satp = new Bundle { 331 val mode = UInt(4.W) // TODO: may change number to parameter 332 val asid = UInt(16.W) 333 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 334 } 335 val priv = new Bundle { 336 val mxr = Bool() 337 val sum = Bool() 338 val imode = UInt(2.W) 339 val dmode = UInt(2.W) 340 } 341 342 override def toPrintable: Printable = { 343 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 344 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 345 } 346} 347 348class SfenceBundle extends XSBundle { 349 val valid = Bool() 350 val bits = new Bundle { 351 val rs1 = Bool() 352 val rs2 = Bool() 353 val addr = UInt(VAddrBits.W) 354 } 355 356 override def toPrintable: Printable = { 357 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 358 } 359} 360