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 102 with HasBPUParameter with HasIFUConst with PredictorUtils { 103 val metaLen = 0 104 105 // An implementation MUST extend the IO bundle with a response 106 // and the special input from other predictors, as well as 107 // the metas to store in BRQ 108 abstract class Resp extends XSBundle {} 109 abstract class FromOthers extends XSBundle {} 110 abstract class Meta extends XSBundle {} 111 112 class DefaultBasePredictorIO extends XSBundle { 113 val flush = Input(Bool()) 114 val pc = Flipped(ValidIO(UInt(VAddrBits.W))) 115 val hist = Input(UInt(HistoryLength.W)) 116 val inMask = Input(UInt(PredictWidth.W)) 117 val update = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 118 val outFire = Input(Bool()) 119 } 120 121 val io = new DefaultBasePredictorIO 122 123 val debug = false 124} 125 126class BPUStageIO extends XSBundle { 127 val pc = UInt(VAddrBits.W) 128 val mask = UInt(PredictWidth.W) 129 val resp = new PredictorResponse 130 // val target = UInt(VAddrBits.W) 131 val brInfo = Vec(PredictWidth, new BranchInfo) 132 // val saveHalfRVI = Bool() 133} 134 135 136abstract class BPUStage extends XSModule with HasBPUParameter with HasIFUConst { 137 class DefaultIO extends XSBundle { 138 val flush = Input(Bool()) 139 val in = Input(new BPUStageIO) 140 val inFire = 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 145 val debug_hist = Input(UInt((if (BPUDebug) (HistoryLength) else 0).W)) 146 val debug_histPtr = Input(UInt((if (BPUDebug) (ExtHistoryLength) else 0).W)) 147 } 148 val io = IO(new DefaultIO) 149 150 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 151 152 val inLatch = RegEnable(io.in, io.inFire) 153 154 // Each stage has its own logic to decide 155 // takens, notTakens and target 156 157 val takens = Wire(Vec(PredictWidth, Bool())) 158 // val notTakens = Wire(Vec(PredictWidth, Bool())) 159 val brMask = Wire(Vec(PredictWidth, Bool())) 160 val jalMask = Wire(Vec(PredictWidth, Bool())) 161 162 val targets = Wire(Vec(PredictWidth, UInt(VAddrBits.W))) 163 164 val firstBankHasHalfRVI = Wire(Bool()) 165 val lastBankHasHalfRVI = Wire(Bool()) 166 // val jmpIdx = PriorityEncoder(takens) 167 // val hasNTBr = (0 until PredictWidth).map(i => i.U <= jmpIdx && notTakens(i) && brMask(i)).reduce(_||_) 168 // val taken = takens.reduce(_||_) 169 val lastBankHasInst = WireInit(inLatch.mask(PredictWidth-1, bankWidth).orR) 170 // get the last valid inst 171 // val lastValidPos = WireInit(PriorityMux(Reverse(inLatch.mask), (PredictWidth-1 to 0 by -1).map(i => i.U))) 172 // val lastHit = Wire(Bool()) 173 // val lastIsRVC = Wire(Bool()) 174 // val saveHalfRVI = ((lastValidPos === jmpIdx && taken) || !taken ) && !lastIsRVC && lastHit 175 176 // val targetSrc = Wire(Vec(PredictWidth, UInt(VAddrBits.W))) 177 // val target = Mux(taken, targetSrc(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 178 179 io.pred <> DontCare 180 // io.pred.redirect := target =/= inLatch.target || inLatch.saveHalfRVI && !saveHalfRVI 181 io.pred.takens := takens 182 io.pred.brMask := brMask 183 io.pred.jalMask := jalMask 184 io.pred.targets := targets 185 io.pred.firstBankHasHalfRVI := firstBankHasHalfRVI 186 io.pred.lastBankHasHalfRVI := lastBankHasHalfRVI 187 // io.pred.jmpIdx := jmpIdx 188 // io.pred.hasNotTakenBrs := hasNTBr 189 // io.pred.target := target 190 // io.pred.saveHalfRVI := saveHalfRVI 191 // io.pred.takenOnBr := taken && brMask(jmpIdx) 192 193 io.out <> DontCare 194 io.out.pc := inLatch.pc 195 io.out.mask := inLatch.mask 196 // io.out.target := target 197 io.out.resp <> inLatch.resp 198 io.out.brInfo := inLatch.brInfo 199 // io.out.saveHalfRVI := saveHalfRVI 200 (0 until PredictWidth).map(i => io.out.brInfo(i).sawNotTakenBranch := io.pred.sawNotTakenBr(i)) 201 202 if (BPUDebug) { 203 val jmpIdx = io.pred.jmpIdx 204 val taken = io.pred.taken 205 val target = Mux(taken, io.pred.targets(jmpIdx), snpc(inLatch.pc)) 206 XSDebug("in(%d): pc=%x, mask=%b\n", io.inFire, io.in.pc, io.in.mask) 207 XSDebug("inLatch: pc=%x, mask=%b\n", inLatch.pc, inLatch.mask) 208 XSDebug("out(%d): pc=%x, mask=%b, taken=%d, jmpIdx=%d, target=%x, firstHasHalfRVI=%d, lastHasHalfRVI=%d\n", 209 io.outFire, io.out.pc, io.out.mask, taken, jmpIdx, target, firstBankHasHalfRVI, lastBankHasHalfRVI) 210 XSDebug("flush=%d\n", io.flush) 211 val p = io.pred 212 } 213} 214 215class BPUStage1 extends BPUStage { 216 217 // ubtb is accessed with inLatch pc in s1, 218 // so we use io.in instead of inLatch 219 val ubtbResp = io.in.resp.ubtb 220 // the read operation is already masked, so we do not need to mask here 221 takens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.takens(i))).asUInt 222 // notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && !ubtbResp.takens(i) && ubtbResp.brMask(i))) 223 brMask := ubtbResp.brMask 224 jalMask := DontCare 225 targets := ubtbResp.targets 226 227 firstBankHasHalfRVI := Mux(lastBankHasInst, false.B, ubtbResp.hits(bankWidth-1) && !ubtbResp.is_RVC(bankWidth-1) && inLatch.mask(bankWidth-1)) 228 lastBankHasHalfRVI := ubtbResp.hits(PredictWidth-1) && !ubtbResp.is_RVC(PredictWidth-1) && inLatch.mask(PredictWidth-1) 229 // lastIsRVC := ubtbResp.is_RVC(lastValidPos) 230 // lastHit := ubtbResp.hits(lastValidPos) 231 232 // resp and brInfo are from the components, 233 // so it does not need to be latched 234 io.out.resp <> io.in.resp 235 io.out.brInfo := io.in.brInfo 236 237 // we do not need to compare target in stage1 238 // io.pred.redirect := taken 239 240 if (BPUDebug) { 241 XSDebug(io.outFire, "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.brInfo.map(_.debug_ubtb_cycle := GTimer()) 246 } 247} 248 249class BPUStage2 extends BPUStage { 250 // Use latched response from s1 251 val btbResp = inLatch.resp.btb 252 val bimResp = inLatch.resp.bim 253 takens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.types(i) === BTBtype.B && bimResp.ctrs(i)(1) || btbResp.types(i) =/= BTBtype.B))).asUInt 254 // notTakens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && btbResp.types(i) === BTBtype.B && !bimResp.ctrs(i)(1))) 255 targets := btbResp.targets 256 brMask := VecInit(btbResp.types.map(_ === BTBtype.B)).asUInt 257 jalMask := DontCare 258 259 firstBankHasHalfRVI := Mux(lastBankHasInst, false.B, btbResp.hits(bankWidth-1) && !btbResp.isRVC(bankWidth-1) && inLatch.mask(bankWidth-1)) 260 lastBankHasHalfRVI := btbResp.hits(PredictWidth-1) && !btbResp.isRVC(PredictWidth-1) && inLatch.mask(PredictWidth-1) 261 262 if (BPUDebug) { 263 XSDebug(io.outFire, "outPred using btb&bim resp: hits:%b, ctrTakens:%b\n", 264 btbResp.hits.asUInt, VecInit(bimResp.ctrs.map(_(1))).asUInt) 265 } 266 if (EnableBPUTimeRecord) { 267 io.out.brInfo.map(_.debug_btb_cycle := GTimer()) 268 } 269} 270 271class BPUStage3 extends BPUStage { 272 class S3IO extends DefaultIO { 273 val predecode = Input(new Predecode) 274 val realMask = Input(UInt(PredictWidth.W)) 275 val prevHalf = Input(new PrevHalfInstr) 276 val recover = Flipped(ValidIO(new BranchUpdateInfo)) 277 } 278 override val io = new S3IO 279 // TAGE has its own pipelines and the 280 // response comes directly from s3, 281 // so we do not use those from inLatch 282 val tageResp = io.in.resp.tage 283 val tageTakens = tageResp.takens 284 // val tageHits = tageResp.hits 285 // val tageValidTakens = VecInit((tageTakens zip tageHits).map{case (t, h) => t && h}) 286 287 val loopResp = io.in.resp.loop.exit 288 289 // realMask is in it 290 val pdMask = io.predecode.mask 291 val pds = io.predecode.pd 292 293 val btbResp = inLatch.resp.btb 294 val btbHits = btbResp.hits.asUInt 295 val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1))) 296 297 val brs = pdMask & Reverse(Cat(pds.map(_.isBr))) 298 val jals = pdMask & Reverse(Cat(pds.map(_.isJal))) 299 val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr))) 300 val calls = pdMask & Reverse(Cat(pds.map(_.isCall))) 301 val rets = pdMask & Reverse(Cat(pds.map(_.isRet))) 302 val RVCs = pdMask & Reverse(Cat(pds.map(_.isRVC))) 303 304 val callIdx = PriorityEncoder(calls) 305 val retIdx = PriorityEncoder(rets) 306 307 val brPred = (if(EnableBPD) tageTakens else bimTakens) 308 val loopRes = (if (EnableLoop) loopResp else VecInit(Fill(PredictWidth, 1.U(1.W)))) 309 val prevHalfTaken = io.prevHalf.valid && io.prevHalf.taken 310 val brTakens = VecInit((0 until PredictWidth).map(i => brs(i) && (brPred(i) || (if (i == 0) prevHalfTaken else false.B)) && !loopRes(i))) 311 312 // predict taken only if btb has a target, jal targets will be provided by IFU 313 takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jalrs(i)) && btbHits(i) || jals(i))) 314 315 // we should provide the prediction for the first half RVI of the end of a fetch packet 316 // branch taken information would be lost in the prediction of the next packet, 317 // so we preserve this information here 318 when (firstBankHasHalfRVI && btbResp.types(bankWidth-1) === BTBtype.B) { 319 takens(bankWidth-1) := brPred(bankWidth-1) && !loopRes(bankWidth-1) 320 } 321 when (lastBankHasHalfRVI && btbResp.types(PredictWidth-1) === BTBtype.B) { 322 takens(PredictWidth-1) := brPred(PredictWidth-1) && !loopRes(PredictWidth-1) 323 } 324 325 targets := inLatch.resp.btb.targets 326 327 // targets would be lost as well, since it is from btb 328 // unless it is a ret, which target is from ras 329 when (prevHalfTaken && !rets(0)) { 330 targets(0) := io.prevHalf.target 331 } 332 brMask := WireInit(brs.asTypeOf(Vec(PredictWidth, Bool()))) 333 jalMask := WireInit(jals.asTypeOf(Vec(PredictWidth, Bool()))) 334 335 firstBankHasHalfRVI := Mux(lastBankHasInst, false.B, io.realMask(bankWidth-1) && !pdMask(bankWidth-1)) 336 lastBankHasHalfRVI := io.realMask(PredictWidth-1) && !pdMask(PredictWidth-1) 337 338 //RAS 339 if(EnableRAS){ 340 val ras = Module(new RAS) 341 ras.io <> DontCare 342 ras.io.pc.bits := bankAligned(inLatch.pc) 343 ras.io.pc.valid := io.outFire//predValid 344 ras.io.is_ret := rets.orR && (retIdx === io.pred.jmpIdx) 345 ras.io.callIdx.valid := calls.orR && (callIdx === io.pred.jmpIdx) 346 ras.io.callIdx.bits := callIdx 347 ras.io.isRVC := (calls & RVCs).orR //TODO: this is ugly 348 ras.io.isLastHalfRVI := io.predecode.hasLastHalfRVI 349 ras.io.recover := io.recover 350 351 for(i <- 0 until PredictWidth){ 352 io.out.brInfo(i).rasSp := ras.io.branchInfo.rasSp 353 io.out.brInfo(i).rasTopCtr := ras.io.branchInfo.rasTopCtr 354 io.out.brInfo(i).rasToqAddr := ras.io.branchInfo.rasToqAddr 355 } 356 takens := VecInit((0 until PredictWidth).map(i => { 357 ((brTakens(i) || jalrs(i)) && btbHits(i)) || 358 jals(i) || 359 (!ras.io.out.bits.specEmpty && rets(i)) || 360 (ras.io.out.bits.specEmpty && btbHits(i)) 361 } 362 )) 363 when(ras.io.is_ret && ras.io.out.valid){ 364 targets(retIdx) := ras.io.out.bits.target 365 } 366 } 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 // to if2/if3/if4 439 val out = Vec(3, Output(new BranchPrediction)) 440 // from if4 441 val predecode = Input(new Predecode) 442 val realMask = Input(UInt(PredictWidth.W)) 443 val prevHalf = Input(new PrevHalfInstr) 444 // to if4, some bpu info used for updating 445 val branchInfo = Output(Vec(PredictWidth, new BranchInfo)) 446 }) 447 448 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 449 450 preds.map(_.io.update <> io.outOfOrderBrInfo) 451 tage.io.update <> io.inOrderBrInfo 452 453 val s1 = Module(new BPUStage1) 454 val s2 = Module(new BPUStage2) 455 val s3 = Module(new BPUStage3) 456 457 val s1_fire = io.inFire(0) 458 val s2_fire = io.inFire(1) 459 val s3_fire = io.inFire(2) 460 val s4_fire = io.inFire(3) 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 s1.io.inFire := s1_fire 471 s2.io.inFire := s2_fire 472 s3.io.inFire := s3_fire 473 474 s1.io.outFire := s2_fire 475 s2.io.outFire := s3_fire 476 s3.io.outFire := s4_fire 477 478 io.out(0) <> s1.io.pred 479 io.out(1) <> s2.io.pred 480 io.out(2) <> s3.io.pred 481 482 s3.io.predecode <> io.predecode 483 484 s3.io.realMask := io.realMask 485 486 s3.io.prevHalf := io.prevHalf 487 488 io.branchInfo := s3.io.out.brInfo 489 490 s3.io.recover.valid <> io.inOrderBrInfo.valid 491 s3.io.recover.bits <> io.inOrderBrInfo.bits.ui 492 493 if (BPUDebug) { 494 XSDebug(io.inFire(3), "branchInfo sent!\n") 495 for (i <- 0 until PredictWidth) { 496 val b = io.branchInfo(i) 497 XSDebug(io.inFire(3), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, btbHitJal:%d, bimCtr:%d, fetchIdx:%d\n", 498 i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.btbHitJal, b.bimCtr, b.fetchIdx) 499 val t = b.tageMeta 500 XSDebug(io.inFire(3), " tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n", 501 t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits) 502 } 503 } 504 val debug_verbose = false 505} 506 507 508class FakeBPU extends BaseBPU { 509 io.out.foreach(i => { 510 // Provide not takens 511 i <> DontCare 512 i.takens := 0.U 513 }) 514 io.branchInfo <> DontCare 515} 516 517class BPU extends BaseBPU { 518 519 //**********************Stage 1****************************// 520 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 := s2_fire 532 ubtb.io.pc.bits := s1_inLatch.pc 533 ubtb.io.inMask := s1_inLatch.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 := s1_fire 546 btb.io.pc.bits := io.in.pc 547 btb.io.inMask := io.in.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 := s1_fire 560 bim.io.pc.bits := io.in.pc 561 bim.io.inMask := io.in.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.inFire := s1_fire 572 s1.io.in.pc := io.in.pc 573 s1.io.in.mask := io.in.inMask 574 s1.io.in.resp <> s1_resp_in 575 s1.io.in.brInfo <> s1_brInfo_in 576 577 val s1_hist = RegEnable(io.in.hist, enable=s1_fire) 578 val s2_hist = RegEnable(s1_hist, enable=s2_fire) 579 val s3_hist = RegEnable(s2_hist, enable=s3_fire) 580 581 s1.io.debug_hist := s1_hist 582 s2.io.debug_hist := s2_hist 583 s3.io.debug_hist := s3_hist 584 585 val s1_histPtr = RegEnable(io.in.histPtr, enable=s1_fire) 586 val s2_histPtr = RegEnable(s1_histPtr, enable=s2_fire) 587 val s3_histPtr = RegEnable(s2_histPtr, enable=s3_fire) 588 589 s1.io.debug_histPtr := s1_histPtr 590 s2.io.debug_histPtr := s2_histPtr 591 s3.io.debug_histPtr := s3_histPtr 592 593 //**********************Stage 2****************************// 594 tage.io.flush := io.flush(1) // TODO: fix this 595 tage.io.pc.valid := s2_fire 596 tage.io.pc.bits := s2.io.in.pc // PC from s1 597 tage.io.hist := s1_hist // The inst is from s1 598 tage.io.inMask := s2.io.in.mask 599 tage.io.s3Fire := s3_fire // Tell tage to march 1 stage 600 tage.io.bim <> s1.io.out.resp.bim // Use bim results from s1 601 602 //**********************Stage 3****************************// 603 // Wrap tage response and meta into s3.io.in.bits 604 // This is ugly 605 606 loop.io.flush := io.flush(2) 607 loop.io.pc.valid := s3_fire 608 loop.io.pc.bits := s3.io.in.pc 609 loop.io.inMask := s3.io.in.mask 610 loop.io.outFire := s4_fire 611 loop.io.respIn.taken := s3.io.pred.taken 612 loop.io.respIn.jmpIdx := s3.io.pred.jmpIdx 613 614 615 s3.io.in.resp.tage <> tage.io.resp 616 s3.io.in.resp.loop <> loop.io.resp 617 for (i <- 0 until PredictWidth) { 618 s3.io.in.brInfo(i).tageMeta := tage.io.meta(i) 619 s3.io.in.brInfo(i).specCnt := loop.io.meta.specCnts(i) 620 } 621 622 if (BPUDebug) { 623 if (debug_verbose) { 624 val uo = ubtb.io.out 625 XSDebug("debug: ubtb hits:%b, takens:%b, notTakens:%b\n", uo.hits.asUInt, uo.takens.asUInt, ~uo.takens.asUInt & uo.brMask.asUInt) 626 val bio = bim.io.resp 627 XSDebug("debug: bim takens:%b\n", VecInit(bio.ctrs.map(_(1))).asUInt) 628 val bo = btb.io.resp 629 XSDebug("debug: btb hits:%b\n", bo.hits.asUInt) 630 } 631 } 632 633 634 635 if (EnableCFICommitLog) { 636 val buValid = io.inOrderBrInfo.valid 637 val buinfo = io.inOrderBrInfo.bits.ui 638 val pd = buinfo.pd 639 val tage_cycle = buinfo.brInfo.debug_tage_cycle 640 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") 641 } 642 643} 644 645object BPU{ 646 def apply(enableBPU: Boolean = true) = { 647 if(enableBPU) { 648 val BPU = Module(new BPU) 649 BPU 650 } 651 else { 652 val FakeBPU = Module(new FakeBPU) 653 FakeBPU 654 } 655 } 656} 657