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