1package xiangshan.frontend 2 3import chisel3._ 4import chisel3.util._ 5import utils._ 6import xiangshan._ 7import xiangshan.backend.ALUOpType 8import xiangshan.backend.JumpOpType 9 10trait HasBPUParameter extends HasXSParameter { 11 val BPUDebug = false 12 val EnableCFICommitLog = false 13 val EnbaleCFIPredLog = false 14 val EnableBPUTimeRecord = false 15} 16 17class TableAddr(val idxBits: Int, val banks: Int) extends XSBundle { 18 def tagBits = VAddrBits - idxBits - 1 19 20 val tag = UInt(tagBits.W) 21 val idx = UInt(idxBits.W) 22 val offset = UInt(1.W) 23 24 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 25 def getTag(x: UInt) = fromUInt(x).tag 26 def getIdx(x: UInt) = fromUInt(x).idx 27 def getBank(x: UInt) = getIdx(x)(log2Up(banks) - 1, 0) 28 def getBankIdx(x: UInt) = getIdx(x)(idxBits - 1, log2Up(banks)) 29} 30 31class PredictorResponse extends XSBundle { 32 class UbtbResp extends XSBundle { 33 // the valid bits indicates whether a target is hit 34 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 35 val hits = Vec(PredictWidth, Bool()) 36 val takens = Vec(PredictWidth, Bool()) 37 val brMask = Vec(PredictWidth, Bool()) 38 val is_RVC = Vec(PredictWidth, Bool()) 39 } 40 class BtbResp extends XSBundle { 41 // the valid bits indicates whether a target is hit 42 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 43 val hits = Vec(PredictWidth, Bool()) 44 val types = Vec(PredictWidth, UInt(2.W)) 45 val isRVC = Vec(PredictWidth, Bool()) 46 } 47 class BimResp extends XSBundle { 48 val ctrs = Vec(PredictWidth, UInt(2.W)) 49 } 50 class TageResp extends XSBundle { 51 // the valid bits indicates whether a prediction is hit 52 val takens = Vec(PredictWidth, Bool()) 53 val hits = Vec(PredictWidth, Bool()) 54 } 55 class LoopResp extends XSBundle { 56 val exit = Vec(PredictWidth, Bool()) 57 } 58 59 val ubtb = new UbtbResp 60 val btb = new BtbResp 61 val bim = new BimResp 62 val tage = new TageResp 63 val loop = new LoopResp 64} 65 66trait PredictorUtils { 67 // circular shifting 68 def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = { 69 val res = Wire(UInt(len.W)) 70 val higher = source << shamt 71 val lower = source >> (len.U - shamt) 72 res := higher | lower 73 res 74 } 75 76 def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = { 77 val res = Wire(UInt(len.W)) 78 val higher = source << (len.U - shamt) 79 val lower = source >> shamt 80 res := higher | lower 81 res 82 } 83 84 def satUpdate(old: UInt, len: Int, taken: Bool): UInt = { 85 val oldSatTaken = old === ((1 << len)-1).U 86 val oldSatNotTaken = old === 0.U 87 Mux(oldSatTaken && taken, ((1 << len)-1).U, 88 Mux(oldSatNotTaken && !taken, 0.U, 89 Mux(taken, old + 1.U, old - 1.U))) 90 } 91} 92abstract class BasePredictor extends XSModule with HasBPUParameter with PredictorUtils { 93 val metaLen = 0 94 95 // An implementation MUST extend the IO bundle with a response 96 // and the special input from other predictors, as well as 97 // the metas to store in BRQ 98 abstract class Resp extends XSBundle {} 99 abstract class FromOthers extends XSBundle {} 100 abstract class Meta extends XSBundle {} 101 102 class DefaultBasePredictorIO extends XSBundle { 103 val flush = Input(Bool()) 104 val pc = Flipped(ValidIO(UInt(VAddrBits.W))) 105 val hist = Input(UInt(HistoryLength.W)) 106 val inMask = Input(UInt(PredictWidth.W)) 107 val update = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 108 } 109 110 val io = new DefaultBasePredictorIO 111 112 val debug = false 113} 114 115class BPUStageIO extends XSBundle { 116 val pc = UInt(VAddrBits.W) 117 val mask = UInt(PredictWidth.W) 118 val resp = new PredictorResponse 119 val target = UInt(VAddrBits.W) 120 val brInfo = Vec(PredictWidth, new BranchInfo) 121 val saveHalfRVI = Bool() 122} 123 124 125abstract class BPUStage extends XSModule with HasBPUParameter{ 126 class DefaultIO extends XSBundle { 127 val flush = Input(Bool()) 128 val in = Flipped(Decoupled(new BPUStageIO)) 129 val pred = Decoupled(new BranchPrediction) 130 val out = Decoupled(new BPUStageIO) 131 val predecode = Flipped(ValidIO(new Predecode)) 132 val recover = Flipped(ValidIO(new BranchUpdateInfo)) 133 val cacheValid = Input(Bool()) 134 val debug_hist = Input(UInt(HistoryLength.W)) 135 val debug_histPtr = Input(UInt(log2Up(ExtHistoryLength).W)) 136 } 137 val io = IO(new DefaultIO) 138 139 val predValid = RegInit(false.B) 140 141 io.in.ready := !predValid || io.out.fire() && io.pred.fire() || io.flush 142 143 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 144 145 val inFire = io.in.fire() 146 val inLatch = RegEnable(io.in.bits, inFire) 147 148 val outFire = io.out.fire() 149 150 // Each stage has its own logic to decide 151 // takens, notTakens and target 152 153 val takens = Wire(Vec(PredictWidth, Bool())) 154 val notTakens = Wire(Vec(PredictWidth, Bool())) 155 val brMask = Wire(Vec(PredictWidth, Bool())) 156 val jmpIdx = PriorityEncoder(takens) 157 val hasNTBr = (0 until PredictWidth).map(i => i.U <= jmpIdx && notTakens(i) && brMask(i)).reduce(_||_) 158 val taken = takens.reduce(_||_) 159 // get the last valid inst 160 val lastValidPos = WireInit(PriorityMux(Reverse(inLatch.mask), (PredictWidth-1 to 0 by -1).map(i => i.U))) 161 val lastHit = Wire(Bool()) 162 val lastIsRVC = Wire(Bool()) 163 val saveHalfRVI = ((lastValidPos === jmpIdx && taken) || !taken ) && !lastIsRVC && lastHit 164 165 val targetSrc = Wire(Vec(PredictWidth, UInt(VAddrBits.W))) 166 val target = Mux(taken, targetSrc(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 167 168 io.pred.bits <> DontCare 169 io.pred.bits.redirect := target =/= inLatch.target || inLatch.saveHalfRVI && !saveHalfRVI 170 io.pred.bits.taken := taken 171 io.pred.bits.jmpIdx := jmpIdx 172 io.pred.bits.hasNotTakenBrs := hasNTBr 173 io.pred.bits.target := target 174 io.pred.bits.saveHalfRVI := saveHalfRVI 175 io.pred.bits.takenOnBr := taken && brMask(jmpIdx) 176 177 io.out.bits <> DontCare 178 io.out.bits.pc := inLatch.pc 179 io.out.bits.mask := inLatch.mask 180 io.out.bits.target := target 181 io.out.bits.resp <> inLatch.resp 182 io.out.bits.brInfo := inLatch.brInfo 183 io.out.bits.saveHalfRVI := saveHalfRVI 184 (0 until PredictWidth).map(i => 185 io.out.bits.brInfo(i).sawNotTakenBranch := (if (i == 0) false.B else (brMask.asUInt & notTakens.asUInt)(i-1,0).orR)) 186 187 // Default logic 188 // pred.ready not taken into consideration 189 // could be broken 190 when (io.flush) { predValid := false.B } 191 .elsewhen (inFire) { predValid := true.B } 192 .elsewhen (outFire) { predValid := false.B } 193 .otherwise { predValid := predValid } 194 195 io.out.valid := predValid && !io.flush 196 io.pred.valid := predValid && !io.flush 197 198 if (BPUDebug) { 199 XSDebug(io.in.fire(), "in:(%d %d) pc=%x, mask=%b, target=%x\n", 200 io.in.valid, io.in.ready, io.in.bits.pc, io.in.bits.mask, io.in.bits.target) 201 XSDebug(io.out.fire(), "out:(%d %d) pc=%x, mask=%b, target=%x\n", 202 io.out.valid, io.out.ready, io.out.bits.pc, io.out.bits.mask, io.out.bits.target) 203 XSDebug("flush=%d\n", io.flush) 204 XSDebug("taken=%d, takens=%b, notTakens=%b, jmpIdx=%d, hasNTBr=%d, lastValidPos=%d, target=%x\n", 205 taken, takens.asUInt, notTakens.asUInt, jmpIdx, hasNTBr, lastValidPos, target) 206 val p = io.pred.bits 207 XSDebug(io.pred.fire(), "outPred: redirect=%d, taken=%d, jmpIdx=%d, hasNTBrs=%d, target=%x, saveHalfRVI=%d\n", 208 p.redirect, p.taken, p.jmpIdx, p.hasNotTakenBrs, p.target, p.saveHalfRVI) 209 XSDebug(io.pred.fire() && p.taken, "outPredTaken: fetchPC:%x, jmpPC:%x\n", 210 inLatch.pc, inLatch.pc + (jmpIdx << 1.U)) 211 XSDebug(io.pred.fire() && p.redirect, "outPred: previous target:%x redirected to %x \n", 212 inLatch.target, p.target) 213 XSDebug(io.pred.fire(), "outPred targetSrc: ") 214 for (i <- 0 until PredictWidth) { 215 XSDebug(false, io.pred.fire(), "(%d):%x ", i.U, targetSrc(i)) 216 } 217 XSDebug(false, io.pred.fire(), "\n") 218 } 219} 220 221class BPUStage1 extends BPUStage { 222 223 // 'overrides' default logic 224 // when flush, the prediction should also starts 225 when (inFire) { predValid := true.B } 226 .elsewhen (io.flush) { predValid := false.B } 227 .elsewhen (outFire) { predValid := false.B } 228 .otherwise { predValid := predValid } 229 // io.out.valid := predValid 230 231 // ubtb is accessed with inLatch pc in s1, 232 // so we use io.in instead of inLatch 233 val ubtbResp = io.in.bits.resp.ubtb 234 // the read operation is already masked, so we do not need to mask here 235 takens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.takens(i))) 236 notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && !ubtbResp.takens(i) && ubtbResp.brMask(i))) 237 targetSrc := ubtbResp.targets 238 brMask := ubtbResp.brMask 239 240 lastIsRVC := ubtbResp.is_RVC(lastValidPos) 241 lastHit := ubtbResp.hits(lastValidPos) 242 243 // resp and brInfo are from the components, 244 // so it does not need to be latched 245 io.out.bits.resp <> io.in.bits.resp 246 io.out.bits.brInfo := io.in.bits.brInfo 247 248 if (BPUDebug) { 249 XSDebug(io.pred.fire(), "outPred using ubtb resp: hits:%b, takens:%b, notTakens:%b, isRVC:%b\n", 250 ubtbResp.hits.asUInt, ubtbResp.takens.asUInt, ~ubtbResp.takens.asUInt & brMask.asUInt, ubtbResp.is_RVC.asUInt) 251 } 252 if (EnableBPUTimeRecord) { 253 io.out.bits.brInfo.map(_.debug_ubtb_cycle := GTimer()) 254 } 255} 256 257class BPUStage2 extends BPUStage { 258 259 io.out.valid := predValid && !io.flush && io.cacheValid 260 // Use latched response from s1 261 val btbResp = inLatch.resp.btb 262 val bimResp = inLatch.resp.bim 263 takens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.types(i) === BTBtype.B && bimResp.ctrs(i)(1) || btbResp.types(i) =/= BTBtype.B))) 264 notTakens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && btbResp.types(i) === BTBtype.B && !bimResp.ctrs(i)(1))) 265 targetSrc := btbResp.targets 266 brMask := VecInit(btbResp.types.map(_ === BTBtype.B)) 267 268 lastIsRVC := btbResp.isRVC(lastValidPos) 269 lastHit := btbResp.hits(lastValidPos) 270 271 272 if (BPUDebug) { 273 XSDebug(io.pred.fire(), "outPred using btb&bim resp: hits:%b, ctrTakens:%b\n", 274 btbResp.hits.asUInt, VecInit(bimResp.ctrs.map(_(1))).asUInt) 275 } 276 if (EnableBPUTimeRecord) { 277 io.out.bits.brInfo.map(_.debug_btb_cycle := GTimer()) 278 } 279} 280 281class BPUStage3 extends BPUStage { 282 283 284 io.out.valid := predValid && io.predecode.valid && !io.flush 285 // TAGE has its own pipelines and the 286 // response comes directly from s3, 287 // so we do not use those from inLatch 288 val tageResp = io.in.bits.resp.tage 289 val tageTakens = tageResp.takens 290 val tageHits = tageResp.hits 291 val tageValidTakens = VecInit((tageTakens zip tageHits).map{case (t, h) => t && h}) 292 293 val loopResp = io.in.bits.resp.loop.exit 294 295 val pdMask = io.predecode.bits.mask 296 val pds = io.predecode.bits.pd 297 298 val btbHits = inLatch.resp.btb.hits.asUInt 299 val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1))) 300 301 val brs = pdMask & Reverse(Cat(pds.map(_.isBr))) 302 val jals = pdMask & Reverse(Cat(pds.map(_.isJal))) 303 val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr))) 304 val calls = pdMask & Reverse(Cat(pds.map(_.isCall))) 305 val rets = pdMask & Reverse(Cat(pds.map(_.isRet))) 306 val RVCs = pdMask & Reverse(Cat(pds.map(_.isRVC))) 307 308 val callIdx = PriorityEncoder(calls) 309 val retIdx = PriorityEncoder(rets) 310 311 // Use bim results for those who tage does not have an entry for 312 val brTakens = brs & 313 (if (EnableBPD) Reverse(Cat((0 until PredictWidth).map(i => tageValidTakens(i) || !tageHits(i) && bimTakens(i)))) else Reverse(Cat((0 until PredictWidth).map(i => bimTakens(i))))) & 314 (if (EnableLoop) ~loopResp.asUInt else Fill(PredictWidth, 1.U(1.W))) 315 // if (EnableBPD) { 316 // brs & Reverse(Cat((0 until PredictWidth).map(i => tageValidTakens(i)))) 317 // } else { 318 // brs & Reverse(Cat((0 until PredictWidth).map(i => bimTakens(i)))) 319 // } 320 321 // predict taken only if btb has a target, jal targets will be provided by IFU 322 takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jalrs(i)) && btbHits(i) || jals(i))) 323 // Whether should we count in branches that are not recorded in btb? 324 // PS: Currently counted in. Whenever tage does not provide a valid 325 // taken prediction, the branch is counted as a not taken branch 326 notTakens := ((VecInit((0 until PredictWidth).map(i => brs(i) && !takens(i)))).asUInt | 327 (if (EnableLoop) { VecInit((0 until PredictWidth).map(i => brs(i) && loopResp(i)))} 328 else { WireInit(0.U.asTypeOf(UInt(PredictWidth.W))) }).asUInt).asTypeOf(Vec(PredictWidth, Bool())) 329 targetSrc := inLatch.resp.btb.targets 330 brMask := WireInit(brs.asTypeOf(Vec(PredictWidth, Bool()))) 331 332 //RAS 333 if(EnableRAS){ 334 val ras = Module(new RAS) 335 ras.io <> DontCare 336 ras.io.pc.bits := inLatch.pc 337 ras.io.pc.valid := io.out.fire()//predValid 338 ras.io.is_ret := rets.orR && (retIdx === jmpIdx) && io.predecode.valid 339 ras.io.callIdx.valid := calls.orR && (callIdx === jmpIdx) && io.predecode.valid 340 ras.io.callIdx.bits := callIdx 341 ras.io.isRVC := (calls & RVCs).orR //TODO: this is ugly 342 ras.io.recover := io.recover 343 344 for(i <- 0 until PredictWidth){ 345 io.out.bits.brInfo(i).rasSp := ras.io.branchInfo.rasSp 346 io.out.bits.brInfo(i).rasTopCtr := ras.io.branchInfo.rasTopCtr 347 io.out.bits.brInfo(i).rasToqAddr := ras.io.branchInfo.rasToqAddr 348 } 349 takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jalrs(i)) && btbHits(i) || jals(i)|| rets(i))) 350 when(ras.io.is_ret && ras.io.out.valid){targetSrc(retIdx) := ras.io.out.bits.target} 351 } 352 353 354 // when (!io.predecode.bits.isFetchpcEqualFirstpc) { 355 // lastValidPos := PriorityMux(Reverse(inLatch.mask), (PredictWidth-1 to 0 by -1).map(i => i.U)) + 1.U 356 // } 357 358 lastIsRVC := pds(lastValidPos).isRVC 359 when (lastValidPos === 1.U) { 360 lastHit := pdMask(1) | 361 !pdMask(0) & !pdMask(1) | 362 pdMask(0) & !pdMask(1) & (pds(0).isRVC | !io.predecode.bits.isFetchpcEqualFirstpc) 363 }.elsewhen (lastValidPos > 0.U) { 364 lastHit := pdMask(lastValidPos) | 365 !pdMask(lastValidPos - 1.U) & !pdMask(lastValidPos) | 366 pdMask(lastValidPos - 1.U) & !pdMask(lastValidPos) & pds(lastValidPos - 1.U).isRVC 367 }.otherwise { 368 lastHit := pdMask(0) | !pdMask(0) & !pds(0).isRVC 369 } 370 371 372 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken && !(jmpIdx === 0.U && !io.predecode.bits.isFetchpcEqualFirstpc)) || !taken ) && !lastIsRVC && lastHit 373 374 // Wrap tage resp and tage meta in 375 // This is ugly 376 io.out.bits.resp.tage <> io.in.bits.resp.tage 377 io.out.bits.resp.loop <> io.in.bits.resp.loop 378 for (i <- 0 until PredictWidth) { 379 io.out.bits.brInfo(i).tageMeta := io.in.bits.brInfo(i).tageMeta 380 io.out.bits.brInfo(i).specCnt := io.in.bits.brInfo(i).specCnt 381 } 382 383 if (BPUDebug) { 384 XSDebug(io.predecode.valid, "predecode: pc:%x, mask:%b\n", inLatch.pc, io.predecode.bits.mask) 385 for (i <- 0 until PredictWidth) { 386 val p = io.predecode.bits.pd(i) 387 XSDebug(io.predecode.valid && io.predecode.bits.mask(i), "predecode(%d): brType:%d, br:%d, jal:%d, jalr:%d, call:%d, ret:%d, RVC:%d, excType:%d\n", 388 i.U, p.brType, p.isBr, p.isJal, p.isJalr, p.isCall, p.isRet, p.isRVC, p.excType) 389 } 390 } 391 392 if (EnbaleCFIPredLog) { 393 val out = io.out 394 XSDebug(out.fire(), p"cfi_pred: fetchpc(${Hexadecimal(out.bits.pc)}) mask(${out.bits.mask}) brmask(${brMask.asUInt}) hist(${Hexadecimal(io.debug_hist)}) histPtr(${io.debug_histPtr})\n") 395 } 396 397 if (EnableBPUTimeRecord) { 398 io.out.bits.brInfo.map(_.debug_tage_cycle := GTimer()) 399 } 400} 401 402trait BranchPredictorComponents extends HasXSParameter { 403 val ubtb = Module(new MicroBTB) 404 val btb = Module(new BTB) 405 val bim = Module(new BIM) 406 val tage = (if(EnableBPD) { Module(new Tage) } 407 else { Module(new FakeTage) }) 408 val loop = Module(new LoopPredictor) 409 val preds = Seq(ubtb, btb, bim, tage, loop) 410 preds.map(_.io := DontCare) 411} 412 413class BPUReq extends XSBundle { 414 val pc = UInt(VAddrBits.W) 415 val hist = UInt(HistoryLength.W) 416 val inMask = UInt(PredictWidth.W) 417 val histPtr = UInt(log2Up(ExtHistoryLength).W) // only for debug 418} 419 420class BranchUpdateInfoWithHist extends XSBundle { 421 val ui = new BranchUpdateInfo 422 val hist = UInt(HistoryLength.W) 423} 424 425object BranchUpdateInfoWithHist { 426 def apply (brInfo: BranchUpdateInfo, hist: UInt) = { 427 val b = Wire(new BranchUpdateInfoWithHist) 428 b.ui <> brInfo 429 b.hist := hist 430 b 431 } 432} 433 434abstract class BaseBPU extends XSModule with BranchPredictorComponents with HasBPUParameter{ 435 val io = IO(new Bundle() { 436 // from backend 437 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 438 val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 439 // from ifu, frontend redirect 440 val flush = Input(Vec(3, Bool())) 441 val cacheValid = Input(Bool()) 442 // from if1 443 val in = Flipped(ValidIO(new BPUReq)) 444 // to if2/if3/if4 445 val out = Vec(3, Decoupled(new BranchPrediction)) 446 // from if4 447 val predecode = Flipped(ValidIO(new Predecode)) 448 // to if4, some bpu info used for updating 449 val branchInfo = Decoupled(Vec(PredictWidth, new BranchInfo)) 450 }) 451 452 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 453 454 preds.map(_.io.update <> io.outOfOrderBrInfo) 455 tage.io.update <> io.inOrderBrInfo 456 457 val s1 = Module(new BPUStage1) 458 val s2 = Module(new BPUStage2) 459 val s3 = Module(new BPUStage3) 460 461 s1.io.flush := io.flush(0) 462 s2.io.flush := io.flush(1) 463 s3.io.flush := io.flush(2) 464 465 s1.io.in <> DontCare 466 s2.io.in <> s1.io.out 467 s3.io.in <> s2.io.out 468 469 io.out(0) <> s1.io.pred 470 io.out(1) <> s2.io.pred 471 io.out(2) <> s3.io.pred 472 473 s1.io.predecode <> DontCare 474 s2.io.predecode <> DontCare 475 s3.io.predecode <> io.predecode 476 477 io.branchInfo.valid := s3.io.out.valid 478 io.branchInfo.bits := s3.io.out.bits.brInfo 479 s3.io.out.ready := io.branchInfo.ready 480 481 s1.io.recover <> DontCare 482 s2.io.recover <> DontCare 483 s3.io.recover.valid <> io.inOrderBrInfo.valid 484 s3.io.recover.bits <> io.inOrderBrInfo.bits.ui 485 486 s1.io.cacheValid := DontCare 487 s2.io.cacheValid := io.cacheValid 488 s3.io.cacheValid := io.cacheValid 489 490 491 if (BPUDebug) { 492 XSDebug(io.branchInfo.fire(), "branchInfo sent!\n") 493 for (i <- 0 until PredictWidth) { 494 val b = io.branchInfo.bits(i) 495 XSDebug(io.branchInfo.fire(), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, btbHitJal:%d, bimCtr:%d, fetchIdx:%d\n", 496 i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.btbHitJal, b.bimCtr, b.fetchIdx) 497 val t = b.tageMeta 498 XSDebug(io.branchInfo.fire(), " tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n", 499 t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits) 500 } 501 } 502 val debug_verbose = false 503} 504 505 506class FakeBPU extends BaseBPU { 507 io.out.foreach(i => { 508 // Provide not takens 509 i.valid := true.B 510 i.bits <> DontCare 511 i.bits.redirect := false.B 512 }) 513 io.branchInfo <> DontCare 514} 515 516class BPU extends BaseBPU { 517 518 //**********************Stage 1****************************// 519 val s1_fire = s1.io.in.fire() 520 val s1_resp_in = Wire(new PredictorResponse) 521 val s1_brInfo_in = Wire(Vec(PredictWidth, new BranchInfo)) 522 523 s1_resp_in.tage := DontCare 524 s1_resp_in.loop := DontCare 525 s1_brInfo_in := DontCare 526 (0 until PredictWidth).foreach(i => s1_brInfo_in(i).fetchIdx := i.U) 527 528 val s1_inLatch = RegEnable(io.in, s1_fire) 529 ubtb.io.flush := io.flush(0) // TODO: fix this 530 ubtb.io.pc.valid := s1_inLatch.valid 531 ubtb.io.pc.bits := s1_inLatch.bits.pc 532 ubtb.io.inMask := s1_inLatch.bits.inMask 533 534 535 536 // Wrap ubtb response into resp_in and brInfo_in 537 s1_resp_in.ubtb <> ubtb.io.out 538 for (i <- 0 until PredictWidth) { 539 s1_brInfo_in(i).ubtbWriteWay := ubtb.io.uBTBBranchInfo.writeWay(i) 540 s1_brInfo_in(i).ubtbHits := ubtb.io.uBTBBranchInfo.hits(i) 541 } 542 543 btb.io.flush := io.flush(0) // TODO: fix this 544 btb.io.pc.valid := io.in.valid 545 btb.io.pc.bits := io.in.bits.pc 546 btb.io.inMask := io.in.bits.inMask 547 548 549 550 // Wrap btb response into resp_in and brInfo_in 551 s1_resp_in.btb <> btb.io.resp 552 for (i <- 0 until PredictWidth) { 553 s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i) 554 s1_brInfo_in(i).btbHitJal := btb.io.meta.hitJal(i) 555 } 556 557 bim.io.flush := io.flush(0) // TODO: fix this 558 bim.io.pc.valid := io.in.valid 559 bim.io.pc.bits := io.in.bits.pc 560 bim.io.inMask := io.in.bits.inMask 561 562 563 // Wrap bim response into resp_in and brInfo_in 564 s1_resp_in.bim <> bim.io.resp 565 for (i <- 0 until PredictWidth) { 566 s1_brInfo_in(i).bimCtr := bim.io.meta.ctrs(i) 567 } 568 569 570 s1.io.in.valid := io.in.valid 571 s1.io.in.bits.pc := io.in.bits.pc 572 s1.io.in.bits.mask := io.in.bits.inMask 573 s1.io.in.bits.target := npc(io.in.bits.pc, PopCount(io.in.bits.inMask)) // Deault target npc 574 s1.io.in.bits.resp <> s1_resp_in 575 s1.io.in.bits.brInfo <> s1_brInfo_in 576 s1.io.in.bits.saveHalfRVI := false.B 577 578 val s1_hist = RegEnable(io.in.bits.hist, enable=s1_fire) 579 val s2_hist = RegEnable(s1_hist, enable=s2.io.in.fire()) 580 val s3_hist = RegEnable(s2_hist, enable=s3.io.in.fire()) 581 582 s1.io.debug_hist := s1_hist 583 s2.io.debug_hist := s2_hist 584 s3.io.debug_hist := s3_hist 585 586 val s1_histPtr = RegEnable(io.in.bits.histPtr, enable=s1_fire) 587 val s2_histPtr = RegEnable(s1_histPtr, enable=s2.io.in.fire()) 588 val s3_histPtr = RegEnable(s2_histPtr, enable=s3.io.in.fire()) 589 590 s1.io.debug_histPtr := s1_histPtr 591 s2.io.debug_histPtr := s2_histPtr 592 s3.io.debug_histPtr := s3_histPtr 593 594 //**********************Stage 2****************************// 595 tage.io.flush := io.flush(1) // TODO: fix this 596 tage.io.pc.valid := s1.io.out.fire() 597 tage.io.pc.bits := s1.io.out.bits.pc // PC from s1 598 tage.io.hist := s1_hist // The inst is from s1 599 tage.io.inMask := s1.io.out.bits.mask 600 tage.io.s3Fire := s3.io.in.fire() // Tell tage to march 1 stage 601 tage.io.bim <> s1.io.out.bits.resp.bim // Use bim results from s1 602 603 //**********************Stage 3****************************// 604 // Wrap tage response and meta into s3.io.in.bits 605 // This is ugly 606 607 loop.io.flush := io.flush(2) 608 loop.io.pc.valid := s2.io.out.fire() 609 loop.io.pc.bits := s2.io.out.bits.pc 610 loop.io.inMask := s2.io.out.bits.mask 611 612 s3.io.in.bits.resp.tage <> tage.io.resp 613 s3.io.in.bits.resp.loop <> loop.io.resp 614 for (i <- 0 until PredictWidth) { 615 s3.io.in.bits.brInfo(i).tageMeta := tage.io.meta(i) 616 s3.io.in.bits.brInfo(i).specCnt := loop.io.meta.specCnts(i) 617 } 618 619 if (BPUDebug) { 620 if (debug_verbose) { 621 val uo = ubtb.io.out 622 XSDebug("debug: ubtb hits:%b, takens:%b, notTakens:%b\n", uo.hits.asUInt, uo.takens.asUInt, ~uo.takens.asUInt & uo.brMask.asUInt) 623 val bio = bim.io.resp 624 XSDebug("debug: bim takens:%b\n", VecInit(bio.ctrs.map(_(1))).asUInt) 625 val bo = btb.io.resp 626 XSDebug("debug: btb hits:%b\n", bo.hits.asUInt) 627 } 628 } 629 630 631 632 if (EnableCFICommitLog) { 633 val buValid = io.inOrderBrInfo.valid 634 val buinfo = io.inOrderBrInfo.bits.ui 635 val pd = buinfo.pd 636 val tage_cycle = buinfo.brInfo.debug_tage_cycle 637 XSDebug(buValid, p"cfi_update: isBr(${pd.isBr}) pc(${Hexadecimal(buinfo.pc)}) taken(${buinfo.taken}) mispred(${buinfo.isMisPred}) cycle($tage_cycle) hist(${Hexadecimal(io.inOrderBrInfo.bits.hist)})\n") 638 } 639 640} 641 642object BPU{ 643 def apply(enableBPU: Boolean = true) = { 644 if(enableBPU) { 645 val BPU = Module(new BPU) 646 BPU 647 } 648 else { 649 val FakeBPU = Module(new FakeBPU) 650 FakeBPU 651 } 652 } 653} 654