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