xref: /XiangShan/src/main/scala/xiangshan/frontend/Bim.scala (revision 735e00f7de54bd3593eac9ad97afc062cb0dc949)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import xiangshan.backend.ALUOpType
7import utils._
8import xiangshan.backend.decode.XSTrap
9
10trait BimParams extends HasXSParameter {
11  val BimBanks = PredictWidth
12  val BimSize = 4096
13  val nRows = BimSize / BimBanks
14  val bypassEntries = 4
15}
16
17class BIM extends BasePredictor with BimParams{
18  class BIMResp extends Resp {
19    val ctrs = Vec(PredictWidth, UInt(2.W))
20  }
21  class BIMMeta extends Meta {
22    val ctrs = Vec(PredictWidth, UInt(2.W))
23  }
24  class BIMFromOthers extends FromOthers {}
25
26  class BIMIO extends DefaultBasePredictorIO {
27    val resp = Output(new BIMResp)
28    val meta = Output(new BIMMeta)
29  }
30
31  override val io = IO(new BIMIO)
32  // Update logic
33  // 1 calculate new 2-bit saturated counter value
34  def satUpdate(old: UInt, len: Int, taken: Bool): UInt = {
35    val oldSatTaken = old === ((1 << len)-1).U
36    val oldSatNotTaken = old === 0.U
37    Mux(oldSatTaken && taken, ((1 << len)-1).U,
38      Mux(oldSatNotTaken && !taken, 0.U,
39        Mux(taken, old + 1.U, old - 1.U)))
40  }
41
42  val bimAddr = new TableAddr(log2Up(BimSize), BimBanks)
43
44  val pcLatch = RegEnable(io.pc.bits, io.pc.valid)
45
46  val bim = List.fill(BimBanks) {
47    Module(new SRAMTemplate(UInt(2.W), set = nRows, shouldReset = false, holdRead = true))
48  }
49
50  val doing_reset = RegInit(true.B)
51  val resetRow = RegInit(0.U(log2Ceil(nRows).W))
52  resetRow := resetRow + doing_reset
53  when (resetRow === (nRows-1).U) { doing_reset := false.B }
54
55  val baseBank = bimAddr.getBank(io.pc.bits)
56
57  val realMask = circularShiftRight(io.inMask, BimBanks, baseBank)
58
59  // those banks whose indexes are less than baseBank are in the next row
60  val isInNextRow = VecInit((0 until BtbBanks).map(_.U < baseBank))
61
62  val baseRow = bimAddr.getBankIdx(io.pc.bits)
63
64  val realRow = VecInit((0 until BimBanks).map(b => Mux(isInNextRow(b.U), (baseRow+1.U)(log2Up(nRows)-1, 0), baseRow)))
65
66  val realRowLatch = VecInit(realRow.map(RegEnable(_, enable=io.pc.valid)))
67
68  for (b <- 0 until BimBanks) {
69    bim(b).reset                := reset.asBool
70    bim(b).io.r.req.valid       := realMask(b) && io.pc.valid
71    bim(b).io.r.req.bits.setIdx := realRow(b)
72  }
73
74  val bimRead = VecInit(bim.map(_.io.r.resp.data(0)))
75
76  val baseBankLatch = bimAddr.getBank(pcLatch)
77
78  // e.g: baseBank == 5 => (5, 6,..., 15, 0, 1, 2, 3, 4)
79  val bankIdxInOrder = VecInit((0 until BimBanks).map(b => (baseBankLatch +& b.U)(log2Up(BimBanks)-1, 0)))
80
81  for (b <- 0 until BimBanks) {
82    val ctr = bimRead(bankIdxInOrder(b))
83    io.resp.ctrs(b)  := ctr
84    io.meta.ctrs(b)  := ctr
85  }
86
87  val u = io.update.bits.ui
88
89  val updateBank = bimAddr.getBank(u.pc)
90  val updateRow = bimAddr.getBankIdx(u.pc)
91
92
93  val wrbypass_ctrs       = Reg(Vec(bypassEntries, Vec(BimBanks, UInt(2.W))))
94  val wrbypass_ctr_valids = Reg(Vec(bypassEntries, Vec(BimBanks, Bool())))
95  val wrbypass_rows     = Reg(Vec(bypassEntries, UInt(log2Up(nRows).W)))
96  val wrbypass_enq_idx  = RegInit(0.U(log2Up(bypassEntries).W))
97
98  val wrbypass_hits = VecInit((0 until bypassEntries).map( i =>
99    !doing_reset && wrbypass_rows(i) === updateRow))
100  val wrbypass_hit = wrbypass_hits.reduce(_||_)
101  val wrbypass_hit_idx = PriorityEncoder(wrbypass_hits)
102
103  val oldCtr = Mux(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(updateBank), wrbypass_ctrs(wrbypass_hit_idx)(updateBank), u.brInfo.bimCtr)
104  val newTaken = u.taken
105  val newCtr = satUpdate(oldCtr, 2, newTaken)
106  // val oldSaturated = newCtr === oldCtr
107
108  val needToUpdate = io.update.valid && u.pd.isBr
109
110  when (reset.asBool) { wrbypass_ctr_valids.foreach(_.foreach(_ := false.B))}
111
112  when (needToUpdate) {
113    when (wrbypass_hit) {
114      wrbypass_ctrs(wrbypass_hit_idx)(updateBank) := newCtr
115      wrbypass_ctr_valids(wrbypass_enq_idx)(updateBank) := true.B
116    } .otherwise {
117      wrbypass_ctrs(wrbypass_hit_idx)(updateBank) := newCtr
118      (0 until BimBanks).foreach(b => wrbypass_ctr_valids(wrbypass_enq_idx)(b) := false.B) // reset valid bits
119      wrbypass_ctr_valids(wrbypass_enq_idx)(updateBank) := true.B
120      wrbypass_rows(wrbypass_enq_idx) := updateRow
121      wrbypass_enq_idx := (wrbypass_enq_idx + 1.U)(log2Up(bypassEntries)-1,0)
122    }
123  }
124
125  for (b <- 0 until BimBanks) {
126    bim(b).io.w.req.valid := needToUpdate && b.U === updateBank || doing_reset
127    bim(b).io.w.req.bits.setIdx := Mux(doing_reset, resetRow, updateRow)
128    bim(b).io.w.req.bits.data := Mux(doing_reset, 2.U(2.W), newCtr)
129  }
130
131  if (BPUDebug && debug) {
132    XSDebug(doing_reset, "Reseting...\n")
133    XSDebug("[update] v=%d pc=%x pnpc=%x tgt=%x brTgt=%x\n", io.update.valid, u.pc, u.pnpc, u.target, u.brTarget)
134    XSDebug("[update] taken=%d isMisPred=%d", u.taken, u.isMisPred)
135    XSDebug(false, true.B, p"brTag=${u.brTag} pd.isBr=${u.pd.isBr} brInfo.bimCtr=${Binary(u.brInfo.bimCtr)}\n")
136    XSDebug("needToUpdate=%d updateBank=%x updateRow=%x newCtr=%b oldCtr=%b\n", needToUpdate, updateBank, updateRow, newCtr, oldCtr)
137    XSDebug("[wrbypass] hit=%d hits=%b\n", wrbypass_hit, wrbypass_hits.asUInt)
138  }
139
140}