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