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