1package xiangshan 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan.backend.brq.BrqPtr 6import xiangshan.backend.fu.fpu.Fflags 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 scala.math.max 15 16// Fetch FetchWidth x 32-bit insts from Icache 17class FetchPacket extends XSBundle { 18 val instrs = Vec(PredictWidth, UInt(32.W)) 19 val mask = UInt(PredictWidth.W) 20 // val pc = UInt(VAddrBits.W) 21 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 22 val pnpc = Vec(PredictWidth, UInt(VAddrBits.W)) 23 val brInfo = Vec(PredictWidth, new BranchInfo) 24 val pd = Vec(PredictWidth, new PreDecodeInfo) 25 val ipf = Bool() 26 val crossPageIPFFix = Bool() 27 val predTaken = Bool() 28} 29 30class ValidUndirectioned[T <: Data](gen: T) extends Bundle { 31 val valid = Bool() 32 val bits = gen.cloneType.asInstanceOf[T] 33 override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type] 34} 35 36object ValidUndirectioned { 37 def apply[T <: Data](gen: T) = { 38 new ValidUndirectioned[T](gen) 39 } 40} 41 42class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter { 43 def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_) 44 def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_)) 45 def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1 46 val tageTaken = if (useSC) Bool() else UInt(0.W) 47 val scUsed = if (useSC) Bool() else UInt(0.W) 48 val scPred = if (useSC) Bool() else UInt(0.W) 49 // Suppose ctrbits of all tables are identical 50 val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W)) 51 val sumAbs = if (useSC) UInt(sumCtrBits.W) else UInt(0.W) 52} 53 54class TageMeta extends XSBundle with HasTageParameter { 55 val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 56 val altDiffers = Bool() 57 val providerU = UInt(2.W) 58 val providerCtr = UInt(3.W) 59 val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W)) 60 val taken = Bool() 61 val scMeta = new SCMeta(EnableSC) 62} 63 64class BranchPrediction extends XSBundle { 65 val redirect = Bool() 66 val taken = Bool() 67 val jmpIdx = UInt(log2Up(PredictWidth).W) 68 val hasNotTakenBrs = Bool() 69 val target = UInt(VAddrBits.W) 70 val saveHalfRVI = Bool() 71 val takenOnBr = Bool() 72} 73 74class BranchInfo extends XSBundle with HasBPUParameter { 75 val ubtbWriteWay = UInt(log2Up(UBtbWays).W) 76 val ubtbHits = Bool() 77 val btbWriteWay = UInt(log2Up(BtbWays).W) 78 val btbHitJal = Bool() 79 val bimCtr = UInt(2.W) 80 val histPtr = UInt(log2Up(ExtHistoryLength).W) 81 val predHistPtr = UInt(log2Up(ExtHistoryLength).W) 82 val tageMeta = new TageMeta 83 val rasSp = UInt(log2Up(RasSize).W) 84 val rasTopCtr = UInt(8.W) 85 val rasToqAddr = UInt(VAddrBits.W) 86 val fetchIdx = UInt(log2Up(PredictWidth).W) 87 val specCnt = UInt(10.W) 88 val sawNotTakenBranch = Bool() 89 90 val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 91 val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 92 val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W) 93 94 def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = { 95 this.histPtr := histPtr 96 this.tageMeta := tageMeta 97 this.rasSp := rasSp 98 this.rasTopCtr := rasTopCtr 99 this.asUInt 100 } 101 def size = 0.U.asTypeOf(this).getWidth 102 def fromUInt(x: UInt) = x.asTypeOf(this) 103} 104 105class Predecode extends XSBundle { 106 val isFetchpcEqualFirstpc = Bool() 107 val mask = UInt((FetchWidth*2).W) 108 val pd = Vec(FetchWidth*2, (new PreDecodeInfo)) 109} 110 111class BranchUpdateInfo extends XSBundle { 112 // from backend 113 val pc = UInt(VAddrBits.W) 114 val pnpc = UInt(VAddrBits.W) 115 val target = UInt(VAddrBits.W) 116 val brTarget = UInt(VAddrBits.W) 117 val taken = Bool() 118 val fetchIdx = UInt(log2Up(FetchWidth*2).W) 119 val isMisPred = Bool() 120 val brTag = new BrqPtr 121 122 // frontend -> backend -> frontend 123 val pd = new PreDecodeInfo 124 val brInfo = new BranchInfo 125} 126 127// Dequeue DecodeWidth insts from Ibuffer 128class CtrlFlow extends XSBundle { 129 val instr = UInt(32.W) 130 val pc = UInt(VAddrBits.W) 131 val exceptionVec = Vec(16, Bool()) 132 val intrVec = Vec(12, Bool()) 133 val brUpdate = new BranchUpdateInfo 134 val crossPageIPFFix = Bool() 135} 136 137// Decode DecodeWidth insts at Decode Stage 138class CtrlSignals extends XSBundle { 139 val src1Type, src2Type, src3Type = SrcType() 140 val lsrc1, lsrc2, lsrc3 = UInt(5.W) 141 val ldest = UInt(5.W) 142 val fuType = FuType() 143 val fuOpType = FuOpType() 144 val rfWen = Bool() 145 val fpWen = Bool() 146 val isXSTrap = Bool() 147 val noSpecExec = Bool() // wait forward 148 val blockBackward = Bool() // block backward 149 val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit 150 val isRVF = Bool() 151 val imm = UInt(XLEN.W) 152 val commitType = CommitType() 153 154 def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = { 155 val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table) 156 val signals = 157 Seq(src1Type, src2Type, src3Type, /* lsrc1, lsrc2, lsrc3, ldest, */ 158 fuType, fuOpType, rfWen, fpWen, /* isXSTrap, noSpecExec, blockBackward, flushPipe, */ 159 /* isRVF, imm, commitType */) 160 signals zip decoder map { case(s, d) => s := d } 161 this 162 } 163} 164 165class CfCtrl extends XSBundle { 166 val cf = new CtrlFlow 167 val ctrl = new CtrlSignals 168 val brTag = new BrqPtr 169} 170 171// Load / Store Index 172// 173// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type. 174trait HasLSIdx { this: HasXSParameter => 175 // Separate LSQ 176 val lqIdx = new LqPtr 177 val sqIdx = new SqPtr 178} 179 180class LSIdx extends XSBundle with HasLSIdx {} 181 182// CfCtrl -> MicroOp at Rename Stage 183class MicroOp extends CfCtrl with HasLSIdx { 184 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 185 val src1State, src2State, src3State = SrcState() 186 val roqIdx = new RoqPtr 187 val diffTestDebugLrScValid = Bool() 188} 189 190class Redirect extends XSBundle { 191 val roqIdx = new RoqPtr 192 val isException = Bool() 193 val isMisPred = Bool() 194 val isReplay = Bool() 195 val isFlushPipe = Bool() 196 val pc = UInt(VAddrBits.W) 197 val target = UInt(VAddrBits.W) 198 val brTag = new BrqPtr 199} 200 201class Dp1ToDp2IO extends XSBundle { 202 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 203 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 204 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 205} 206 207class ReplayPregReq extends XSBundle { 208 // NOTE: set isInt and isFp both to 'false' when invalid 209 val isInt = Bool() 210 val isFp = Bool() 211 val preg = UInt(PhyRegIdxWidth.W) 212} 213 214class DebugBundle extends XSBundle{ 215 val isMMIO = Bool() 216} 217 218class ExuInput extends XSBundle { 219 val uop = new MicroOp 220 val src1, src2, src3 = UInt((XLEN+1).W) 221} 222 223class ExuOutput extends XSBundle { 224 val uop = new MicroOp 225 val data = UInt((XLEN+1).W) 226 val fflags = new Fflags 227 val redirectValid = Bool() 228 val redirect = new Redirect 229 val brUpdate = new BranchUpdateInfo 230 val debug = new DebugBundle 231} 232 233class ExternalInterruptIO extends XSBundle { 234 val mtip = Input(Bool()) 235 val msip = Input(Bool()) 236 val meip = Input(Bool()) 237} 238 239class CSRSpecialIO extends XSBundle { 240 val exception = Flipped(ValidIO(new MicroOp)) 241 val isInterrupt = Input(Bool()) 242 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 243 val trapTarget = Output(UInt(VAddrBits.W)) 244 val externalInterrupt = new ExternalInterruptIO 245 val interrupt = Output(Bool()) 246} 247 248//class ExuIO extends XSBundle { 249// val in = Flipped(DecoupledIO(new ExuInput)) 250// val redirect = Flipped(ValidIO(new Redirect)) 251// val out = DecoupledIO(new ExuOutput) 252// // for csr 253// val csrOnly = new CSRSpecialIO 254// val mcommit = Input(UInt(3.W)) 255//} 256 257class RoqCommit extends XSBundle { 258 val uop = new MicroOp 259 val isWalk = Bool() 260} 261 262class TlbFeedback extends XSBundle { 263 val roqIdx = new RoqPtr 264 val hit = Bool() 265} 266 267class FrontendToBackendIO extends XSBundle { 268 // to backend end 269 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 270 // from backend 271 val redirect = Flipped(ValidIO(new Redirect)) 272 val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 273 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 274} 275 276class TlbCsrBundle extends XSBundle { 277 val satp = new Bundle { 278 val mode = UInt(4.W) // TODO: may change number to parameter 279 val asid = UInt(16.W) 280 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 281 } 282 val priv = new Bundle { 283 val mxr = Bool() 284 val sum = Bool() 285 val imode = UInt(2.W) 286 val dmode = UInt(2.W) 287 } 288 289 override def toPrintable: Printable = { 290 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 291 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 292 } 293} 294 295class SfenceBundle extends XSBundle { 296 val valid = Bool() 297 val bits = new Bundle { 298 val rs1 = Bool() 299 val rs2 = Bool() 300 val addr = UInt(VAddrBits.W) 301 } 302 303 override def toPrintable: Printable = { 304 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 305 } 306} 307