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