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