xref: /XiangShan/src/main/scala/xiangshan/frontend/Bim.scala (revision 3fae98ac4fa4aeb3c8ed035932edb8a13ce11e3c)
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
33  val bimAddr = new TableAddr(log2Up(BimSize), BimBanks)
34
35  val pcLatch = RegEnable(io.pc.bits, io.pc.valid)
36
37  val bim = List.fill(BimBanks) {
38    Module(new SRAMTemplate(UInt(2.W), set = nRows, shouldReset = false, holdRead = true))
39  }
40
41  val doing_reset = RegInit(true.B)
42  val resetRow = RegInit(0.U(log2Ceil(nRows).W))
43  resetRow := resetRow + doing_reset
44  when (resetRow === (nRows-1).U) { doing_reset := false.B }
45
46  val baseBank = bimAddr.getBank(io.pc.bits)
47
48  val realMask = circularShiftRight(io.inMask, BimBanks, baseBank)
49
50  // those banks whose indexes are less than baseBank are in the next row
51  val isInNextRow = VecInit((0 until BtbBanks).map(_.U < baseBank))
52
53  val baseRow = bimAddr.getBankIdx(io.pc.bits)
54
55  val realRow = VecInit((0 until BimBanks).map(b => Mux(isInNextRow(b.U), (baseRow+1.U)(log2Up(nRows)-1, 0), baseRow)))
56
57  val realRowLatch = VecInit(realRow.map(RegEnable(_, enable=io.pc.valid)))
58
59  for (b <- 0 until BimBanks) {
60    bim(b).reset                := reset.asBool
61    bim(b).io.r.req.valid       := realMask(b) && io.pc.valid
62    bim(b).io.r.req.bits.setIdx := realRow(b)
63  }
64
65  val bimRead = VecInit(bim.map(_.io.r.resp.data(0)))
66
67  val baseBankLatch = bimAddr.getBank(pcLatch)
68
69  // e.g: baseBank == 5 => (5, 6,..., 15, 0, 1, 2, 3, 4)
70  val bankIdxInOrder = VecInit((0 until BimBanks).map(b => (baseBankLatch +& b.U)(log2Up(BimBanks)-1, 0)))
71
72  for (b <- 0 until BimBanks) {
73    val ctr = bimRead(bankIdxInOrder(b))
74    io.resp.ctrs(b)  := ctr
75    io.meta.ctrs(b)  := ctr
76  }
77
78  val u = io.update.bits.ui
79
80  val updateBank = bimAddr.getBank(u.pc)
81  val updateRow = bimAddr.getBankIdx(u.pc)
82
83
84  val wrbypass_ctrs       = Reg(Vec(bypassEntries, Vec(BimBanks, UInt(2.W))))
85  val wrbypass_ctr_valids = Reg(Vec(bypassEntries, Vec(BimBanks, Bool())))
86  val wrbypass_rows     = Reg(Vec(bypassEntries, UInt(log2Up(nRows).W)))
87  val wrbypass_enq_idx  = RegInit(0.U(log2Up(bypassEntries).W))
88
89  val wrbypass_hits = VecInit((0 until bypassEntries).map( i =>
90    !doing_reset && wrbypass_rows(i) === updateRow))
91  val wrbypass_hit = wrbypass_hits.reduce(_||_)
92  val wrbypass_hit_idx = PriorityEncoder(wrbypass_hits)
93
94  val oldCtr = Mux(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(updateBank), wrbypass_ctrs(wrbypass_hit_idx)(updateBank), u.brInfo.bimCtr)
95  val newTaken = u.taken
96  val newCtr = satUpdate(oldCtr, 2, newTaken)
97  // val oldSaturated = newCtr === oldCtr
98
99  val needToUpdate = io.update.valid && u.pd.isBr
100
101  when (reset.asBool) { wrbypass_ctr_valids.foreach(_.foreach(_ := false.B))}
102
103  when (needToUpdate) {
104    when (wrbypass_hit) {
105      wrbypass_ctrs(wrbypass_hit_idx)(updateBank) := newCtr
106      wrbypass_ctr_valids(wrbypass_enq_idx)(updateBank) := true.B
107    } .otherwise {
108      wrbypass_ctrs(wrbypass_hit_idx)(updateBank) := newCtr
109      (0 until BimBanks).foreach(b => wrbypass_ctr_valids(wrbypass_enq_idx)(b) := false.B) // reset valid bits
110      wrbypass_ctr_valids(wrbypass_enq_idx)(updateBank) := true.B
111      wrbypass_rows(wrbypass_enq_idx) := updateRow
112      wrbypass_enq_idx := (wrbypass_enq_idx + 1.U)(log2Up(bypassEntries)-1,0)
113    }
114  }
115
116  for (b <- 0 until BimBanks) {
117    bim(b).io.w.req.valid := needToUpdate && b.U === updateBank || doing_reset
118    bim(b).io.w.req.bits.setIdx := Mux(doing_reset, resetRow, updateRow)
119    bim(b).io.w.req.bits.data := Mux(doing_reset, 2.U(2.W), newCtr)
120  }
121
122  if (BPUDebug && debug) {
123    XSDebug(doing_reset, "Reseting...\n")
124    XSDebug("[update] v=%d pc=%x pnpc=%x tgt=%x brTgt=%x\n", io.update.valid, u.pc, u.pnpc, u.target, u.brTarget)
125    XSDebug("[update] taken=%d isMisPred=%d", u.taken, u.isMisPred)
126    XSDebug(false, true.B, p"brTag=${u.brTag} pd.isBr=${u.pd.isBr} brInfo.bimCtr=${Binary(u.brInfo.bimCtr)}\n")
127    XSDebug("needToUpdate=%d updateBank=%x updateRow=%x newCtr=%b oldCtr=%b\n", needToUpdate, updateBank, updateRow, newCtr, oldCtr)
128    XSDebug("[wrbypass] hit=%d hits=%b\n", wrbypass_hit, wrbypass_hits.asUInt)
129  }
130
131}