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