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