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