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 if(EnableUnifiedLSQ){ 169 val lsroqIdx = UInt(LsroqIdxWidth.W) 170 def isEqual(thatIdx: UInt): Bool = { 171 this.lsroqIdx === thatIdx 172 } 173 174 def isAfter(thatIdx: UInt): Bool = { 175 Mux( 176 this.lsroqIdx.head(1) === thatIdx.head(1), 177 this.lsroqIdx.tail(1) > thatIdx.tail(1), 178 this.lsroqIdx.tail(1) < thatIdx.tail(1) 179 ) 180 } 181 182 def isAfter[ T<: HasLSIdx ](that: T): Bool = { 183 isAfter(that.lsroqIdx) 184 } 185 } else { 186 val lqIdx = UInt(LoadQueueIdxWidth) 187 val sqIdx = UInt(StoreQueueIdxWidth) 188 val instIsLoad = Bool() 189 190 def isLoad(): Bool = this.instIsLoad 191 192 def isLoadAfter(thatLqIdx: UInt): Bool = { 193 Mux( 194 this.lqIdx.head(1) === thatLqIdx.head(1), 195 this.lqIdx.tail(1) > thatLqIdx.tail(1), 196 this.lqIdx.tail(1) < thatLqIdx.tail(1) 197 ) 198 } 199 200 def isLoadAfter[ T<: HasLSIdx ](that: T): Bool = { 201 isLoadAfter(that.lqIdx) 202 } 203 204 def isStoreAfter(thatSqIdx: UInt): Bool = { 205 Mux( 206 this.sqIdx.head(1) === thatSqIdx.head(1), 207 this.sqIdx.tail(1) > thatSqIdx.tail(1), 208 this.sqIdx.tail(1) < thatSqIdx.tail(1) 209 ) 210 } 211 212 def isStoreAfter[ T<: HasLSIdx ](that: T): Bool = { 213 isStoreAfter(that.sqIdx) 214 } 215 216 // TODO: refactor isAfter 217 218 // def isAfter(lqIdx: UInt, sqIdx: UInt, instIsLoad: Bool): Bool = { 219 // // there are 4 cases: 220 // // load <-> load 221 // // load <-> store 222 // // store <-> load 223 // // store <-> store 224 // Mux( 225 // this.lsroqIdx.head(1) === thatIdx.head(1), 226 // this.lsroqIdx.tail(1) > thatIdx.tail(1), 227 // this.lsroqIdx.tail(1) < thatIdx.tail(1) 228 // ) 229 // } 230 231 // def isAfter[ T<: HasLSIdx ](that: T): Bool = { 232 // isAfter(that.lsroqIdx) 233 // } 234 } 235} 236 237class LSIdx extends XSBundle with HasLSIdx {} 238 239// CfCtrl -> MicroOp at Rename Stage 240class MicroOp extends CfCtrl with HasRoqIdx with HasLSIdx { 241 val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W) 242 val src1State, src2State, src3State = SrcState() 243 val diffTestDebugLrScValid = Bool() 244} 245 246class Redirect extends XSBundle with HasRoqIdx { 247 val isException = Bool() 248 val isMisPred = Bool() 249 val isReplay = Bool() 250 val isFlushPipe = Bool() 251 val pc = UInt(VAddrBits.W) 252 val target = UInt(VAddrBits.W) 253 val brTag = new BrqPtr 254} 255 256class Dp1ToDp2IO extends XSBundle { 257 val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp)) 258 val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp)) 259 val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp)) 260} 261 262class ReplayPregReq extends XSBundle { 263 // NOTE: set isInt and isFp both to 'false' when invalid 264 val isInt = Bool() 265 val isFp = Bool() 266 val preg = UInt(PhyRegIdxWidth.W) 267} 268 269class DebugBundle extends XSBundle{ 270 val isMMIO = Bool() 271} 272 273class ExuInput extends XSBundle { 274 val uop = new MicroOp 275 val src1, src2, src3 = UInt(XLEN.W) 276} 277 278class ExuOutput extends XSBundle { 279 val uop = new MicroOp 280 val data = UInt(XLEN.W) 281 val redirectValid = Bool() 282 val redirect = new Redirect 283 val brUpdate = new BranchUpdateInfo 284 val debug = new DebugBundle 285} 286 287class ExuIO extends XSBundle { 288 val in = Flipped(DecoupledIO(new ExuInput)) 289 val redirect = Flipped(ValidIO(new Redirect)) 290 val out = DecoupledIO(new ExuOutput) 291 // for csr 292 val exception = Flipped(ValidIO(new MicroOp)) 293 // for Lsu 294 val dmem = new SimpleBusUC 295 val mcommit = Input(UInt(3.W)) 296} 297 298class RoqCommit extends XSBundle { 299 val uop = new MicroOp 300 val isWalk = Bool() 301} 302 303class TlbFeedback extends XSBundle with HasRoqIdx{ 304 val hit = Bool() 305} 306 307class FrontendToBackendIO extends XSBundle { 308 // to backend end 309 val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 310 // from backend 311 val redirect = Flipped(ValidIO(new Redirect)) 312 val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 313 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 314} 315 316class TlbCsrBundle extends XSBundle { 317 val satp = new Bundle { 318 val mode = UInt(4.W) // TODO: may change number to parameter 319 val asid = UInt(16.W) 320 val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen 321 } 322 val priv = new Bundle { 323 val mxr = Bool() 324 val sum = Bool() 325 val imode = UInt(2.W) 326 val dmode = UInt(2.W) 327 } 328 329 override def toPrintable: Printable = { 330 p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " + 331 p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}" 332 } 333} 334 335class SfenceBundle extends XSBundle { 336 val valid = Bool() 337 val bits = new Bundle { 338 val rs1 = Bool() 339 val rs2 = Bool() 340 val addr = UInt(VAddrBits.W) 341 } 342 343 override def toPrintable: Printable = { 344 p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}" 345 } 346} 347