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