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