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