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