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