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