xref: /XiangShan/src/main/scala/xiangshan/frontend/SC.scala (revision 4b2c87ba1d7965f6f2b0a396be707a6e2f6fb345)
109c6f1ddSLingrui98/***************************************************************************************
209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory
409c6f1ddSLingrui98*
509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2.
609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2.
709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at:
809c6f1ddSLingrui98*          http://license.coscl.org.cn/MulanPSL2
909c6f1ddSLingrui98*
1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1309c6f1ddSLingrui98*
1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details.
15c49ebec8SHaoyuan Feng*
16c49ebec8SHaoyuan Feng*
17c49ebec8SHaoyuan Feng* Acknowledgement
18c49ebec8SHaoyuan Feng*
19c49ebec8SHaoyuan Feng* This implementation is inspired by several key papers:
20c49ebec8SHaoyuan Feng* [1] André Seznec. "[Tage-sc-l branch predictors.](https://inria.hal.science/hal-01086920)" The Journal of
21c49ebec8SHaoyuan Feng* Instruction-Level Parallelism (JILP) 4th JILP Workshop on Computer Architecture Competitions (JWAC): Championship
22c49ebec8SHaoyuan Feng* Branch Prediction (CBP). 2014.
23c49ebec8SHaoyuan Feng* [2] André Seznec. "[Tage-sc-l branch predictors again.](https://inria.hal.science/hal-01354253)" The Journal of
24c49ebec8SHaoyuan Feng* Instruction-Level Parallelism (JILP) 5th JILP Workshop on Computer Architecture Competitions (JWAC): Championship
25c49ebec8SHaoyuan Feng* Branch Prediction (CBP). 2016.
2609c6f1ddSLingrui98***************************************************************************************/
2709c6f1ddSLingrui98
2809c6f1ddSLingrui98package xiangshan.frontend
2909c6f1ddSLingrui98
3009c6f1ddSLingrui98import chisel3._
3109c6f1ddSLingrui98import chisel3.util._
32cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters
33adc0b8dfSGuokai Chenimport scala.{Tuple2 => &}
34cf7d6b7aSMuziimport scala.math.min
35cf7d6b7aSMuziimport utility._
36*4b2c87baS梁森 Liang Senimport utility.mbist.MbistPipeline
37cf7d6b7aSMuziimport xiangshan._
3809c6f1ddSLingrui98
39cf7d6b7aSMuzitrait HasSCParameter extends TageParams {}
4009c6f1ddSLingrui98
4109c6f1ddSLingrui98class SCReq(implicit p: Parameters) extends TageReq
4209c6f1ddSLingrui98
4309c6f1ddSLingrui98abstract class SCBundle(implicit p: Parameters) extends TageBundle with HasSCParameter {}
4409c6f1ddSLingrui98abstract class SCModule(implicit p: Parameters) extends TageModule with HasSCParameter {}
4509c6f1ddSLingrui98
4634ed6fbcSLingrui98class SCMeta(val ntables: Int)(implicit p: Parameters) extends XSBundle with HasSCParameter {
4734ed6fbcSLingrui98  val scPreds = Vec(numBr, Bool())
4809c6f1ddSLingrui98  // Suppose ctrbits of all tables are identical
4934ed6fbcSLingrui98  val ctrs = Vec(numBr, Vec(ntables, SInt(SCCtrBits.W)))
5009c6f1ddSLingrui98}
5109c6f1ddSLingrui98
5209c6f1ddSLingrui98class SCResp(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
5334ed6fbcSLingrui98  val ctrs = Vec(numBr, Vec(2, SInt(ctrBits.W)))
5409c6f1ddSLingrui98}
5509c6f1ddSLingrui98
5609c6f1ddSLingrui98class SCUpdate(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
5709c6f1ddSLingrui98  val pc        = UInt(VAddrBits.W)
58a72b131fSGao-Zeyu  val ghist     = UInt(HistoryLength.W)
5934ed6fbcSLingrui98  val mask      = Vec(numBr, Bool())
6034ed6fbcSLingrui98  val oldCtrs   = Vec(numBr, SInt(ctrBits.W))
6134ed6fbcSLingrui98  val tagePreds = Vec(numBr, Bool())
6234ed6fbcSLingrui98  val takens    = Vec(numBr, Bool())
6309c6f1ddSLingrui98}
6409c6f1ddSLingrui98
6509c6f1ddSLingrui98class SCTableIO(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
6609c6f1ddSLingrui98  val req    = Input(Valid(new SCReq))
6709c6f1ddSLingrui98  val resp   = Output(new SCResp(ctrBits))
6809c6f1ddSLingrui98  val update = Input(new SCUpdate(ctrBits))
6909c6f1ddSLingrui98}
7009c6f1ddSLingrui98
7109c6f1ddSLingrui98class SCTable(val nRows: Int, val ctrBits: Int, val histLen: Int)(implicit p: Parameters)
7209c6f1ddSLingrui98    extends SCModule with HasFoldedHistory {
7309c6f1ddSLingrui98  val io = IO(new SCTableIO(ctrBits))
7409c6f1ddSLingrui98
7509c6f1ddSLingrui98  // val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false))
76cf7d6b7aSMuzi  val table = Module(new SRAMTemplate(
77cf7d6b7aSMuzi    SInt(ctrBits.W),
78cf7d6b7aSMuzi    set = nRows,
79cf7d6b7aSMuzi    way = 2 * TageBanks,
80cf7d6b7aSMuzi    shouldReset = true,
81cf7d6b7aSMuzi    holdRead = true,
82cf7d6b7aSMuzi    singlePort = false,
8339d55402Spengxiao    bypassWrite = true,
84*4b2c87baS梁森 Liang Sen    withClockGate = true,
85*4b2c87baS梁森 Liang Sen    hasMbist = hasMbist
86cf7d6b7aSMuzi  ))
87*4b2c87baS梁森 Liang Sen  private val mbistPl = MbistPipeline.PlaceMbistPipeline(1, "MbistPipeSc", hasMbist)
88dd6c0695SLingrui98  // def getIdx(hist: UInt, pc: UInt) = {
89dd6c0695SLingrui98  //   (compute_folded_ghist(hist, log2Ceil(nRows)) ^ (pc >> instOffsetBits))(log2Ceil(nRows)-1,0)
90dd6c0695SLingrui98  // }
91dd6c0695SLingrui98
92dd6c0695SLingrui98  val idxFhInfo = (histLen, min(log2Ceil(nRows), histLen))
93dd6c0695SLingrui98
94dd6c0695SLingrui98  def getFoldedHistoryInfo = Set(idxFhInfo).filter(_._1 > 0)
95dd6c0695SLingrui98
96cf7d6b7aSMuzi  def getIdx(pc: UInt, allFh: AllFoldedHistories) =
97dd6c0695SLingrui98    if (histLen > 0) {
98dd6c0695SLingrui98      val idx_fh = allFh.getHistWithInfo(idxFhInfo).folded_hist
99dd6c0695SLingrui98      // require(idx_fh.getWidth == log2Ceil(nRows))
100dd6c0695SLingrui98      ((pc >> instOffsetBits) ^ idx_fh)(log2Ceil(nRows) - 1, 0)
101cf7d6b7aSMuzi    } else {
10234ed6fbcSLingrui98      (pc >> instOffsetBits)(log2Ceil(nRows) - 1, 0)
103dd6c0695SLingrui98    }
10481d86739SLingrui98
10509c6f1ddSLingrui98  def ctrUpdate(ctr: SInt, cond: Bool): SInt = signedSatUpdate(ctr, ctrBits, cond)
10609c6f1ddSLingrui98
107dd6c0695SLingrui98  val s0_idx = getIdx(io.req.bits.pc, io.req.bits.folded_hist)
108005e809bSJiuyang Liu  val s1_idx = RegEnable(s0_idx, io.req.valid)
10909c6f1ddSLingrui98
110935edac4STang Haojin  val s1_pc           = RegEnable(io.req.bits.pc, io.req.fire)
11181d86739SLingrui98  val s1_unhashed_idx = s1_pc >> instOffsetBits
11281d86739SLingrui98
11309c6f1ddSLingrui98  table.io.r.req.valid       := io.req.valid
11409c6f1ddSLingrui98  table.io.r.req.bits.setIdx := s0_idx
11509c6f1ddSLingrui98
11681d86739SLingrui98  val update_wdata        = Wire(Vec(numBr, SInt(ctrBits.W))) // correspond to physical bridx
11734ed6fbcSLingrui98  val update_wdata_packed = VecInit(update_wdata.map(Seq.fill(2)(_)).reduce(_ ++ _))
11881d86739SLingrui98  val updateWayMask       = Wire(Vec(2 * numBr, Bool()))      // correspond to physical bridx
11934ed6fbcSLingrui98
12081d86739SLingrui98  val update_unhashed_idx = io.update.pc >> instOffsetBits
12181d86739SLingrui98  for (pi <- 0 until numBr) {
12281d86739SLingrui98    updateWayMask(2 * pi) := Seq.tabulate(numBr)(li =>
12381d86739SLingrui98      io.update.mask(li) && get_phy_br_idx(update_unhashed_idx, li) === pi.U && !io.update.tagePreds(li)
12481d86739SLingrui98    ).reduce(_ || _)
12581d86739SLingrui98    updateWayMask(2 * pi + 1) := Seq.tabulate(numBr)(li =>
12681d86739SLingrui98      io.update.mask(li) && get_phy_br_idx(update_unhashed_idx, li) === pi.U && io.update.tagePreds(li)
12781d86739SLingrui98    ).reduce(_ || _)
12834ed6fbcSLingrui98  }
12909c6f1ddSLingrui98
130a72b131fSGao-Zeyu  val update_folded_hist = WireInit(0.U.asTypeOf(new AllFoldedHistories(foldedGHistInfos)))
131a72b131fSGao-Zeyu  if (histLen > 0) {
132a72b131fSGao-Zeyu    update_folded_hist.getHistWithInfo(idxFhInfo).folded_hist := compute_folded_ghist(io.update.ghist, log2Ceil(nRows))
133a72b131fSGao-Zeyu  }
134a72b131fSGao-Zeyu  val update_idx = getIdx(io.update.pc, update_folded_hist)
13509c6f1ddSLingrui98
136b2564f6cSYuandongliang  // SCTable dual port SRAM reads and writes to the same address processing
137b2564f6cSYuandongliang  val conflict_buffer_valid   = RegInit(false.B)
138b2564f6cSYuandongliang  val conflict_buffer_data    = RegInit(0.U.asTypeOf(update_wdata_packed))
139b2564f6cSYuandongliang  val conflict_buffer_idx     = RegInit(0.U.asTypeOf(update_idx))
140b2564f6cSYuandongliang  val conflict_buffer_waymask = RegInit(0.U.asTypeOf(updateWayMask))
141b2564f6cSYuandongliang
142b2564f6cSYuandongliang  val write_conflict = update_idx === s0_idx && io.update.mask.reduce(_ || _) && io.req.valid
143b2564f6cSYuandongliang  val can_write      = (conflict_buffer_idx =/= s0_idx || !io.req.valid) && conflict_buffer_valid
144b2564f6cSYuandongliang
145b2564f6cSYuandongliang  when(write_conflict) {
146b2564f6cSYuandongliang    conflict_buffer_valid   := true.B
147b2564f6cSYuandongliang    conflict_buffer_data    := update_wdata_packed
148b2564f6cSYuandongliang    conflict_buffer_idx     := update_idx
149b2564f6cSYuandongliang    conflict_buffer_waymask := updateWayMask
150b2564f6cSYuandongliang  }
151b2564f6cSYuandongliang  when(can_write) {
152b2564f6cSYuandongliang    conflict_buffer_valid := false.B
153b2564f6cSYuandongliang  }
154b2564f6cSYuandongliang
155b2564f6cSYuandongliang  // Using buffer data for prediction
156b2564f6cSYuandongliang  val use_conflict_data = conflict_buffer_valid && conflict_buffer_idx === s1_idx
157cf7d6b7aSMuzi  val conflict_data_bypass = conflict_buffer_data.zip(conflict_buffer_waymask).map { case (data, mask) =>
158cf7d6b7aSMuzi    Mux(mask, data, 0.U.asTypeOf(data))
159cf7d6b7aSMuzi  }
160b2564f6cSYuandongliang  val conflict_prediction_data = conflict_data_bypass.sliding(2, 2).toSeq.map(VecInit(_))
161b2564f6cSYuandongliang  val per_br_ctrs_unshuffled   = table.io.r.resp.data.sliding(2, 2).toSeq.map(VecInit(_))
162cf7d6b7aSMuzi  val per_br_ctrs = VecInit((0 until numBr).map(i =>
163cf7d6b7aSMuzi    Mux1H(
164b2564f6cSYuandongliang      UIntToOH(get_phy_br_idx(s1_unhashed_idx, i), numBr),
165b2564f6cSYuandongliang      per_br_ctrs_unshuffled
166cf7d6b7aSMuzi    )
167cf7d6b7aSMuzi  ))
168cf7d6b7aSMuzi  val conflict_br_ctrs = VecInit((0 until numBr).map(i =>
169cf7d6b7aSMuzi    Mux1H(
170b2564f6cSYuandongliang      UIntToOH(get_phy_br_idx(s1_unhashed_idx, i), numBr),
171b2564f6cSYuandongliang      conflict_prediction_data
172cf7d6b7aSMuzi    )
173cf7d6b7aSMuzi  ))
174b2564f6cSYuandongliang
175b2564f6cSYuandongliang  io.resp.ctrs := Mux(use_conflict_data, conflict_br_ctrs, per_br_ctrs)
176b2564f6cSYuandongliang
17709c6f1ddSLingrui98  table.io.w.apply(
178b2564f6cSYuandongliang    valid = (io.update.mask.reduce(_ || _) && !write_conflict) || can_write,
179b2564f6cSYuandongliang    data = Mux(can_write, conflict_buffer_data, update_wdata_packed),
180b2564f6cSYuandongliang    setIdx = Mux(can_write, conflict_buffer_idx, update_idx),
181b2564f6cSYuandongliang    waymask = Mux(can_write, conflict_buffer_waymask.asUInt, updateWayMask.asUInt)
18209c6f1ddSLingrui98  )
18309c6f1ddSLingrui98
18412cedb6fSLingrui98  val wrBypassEntries = 16
18509c6f1ddSLingrui98
18681d86739SLingrui98  // let it corresponds to logical brIdx
18712cedb6fSLingrui98  val wrbypasses = Seq.fill(numBr)(Module(new WrBypass(SInt(ctrBits.W), wrBypassEntries, log2Ceil(nRows), numWays = 2)))
18809c6f1ddSLingrui98
18981d86739SLingrui98  for (pi <- 0 until numBr) {
19081d86739SLingrui98    val br_lidx = get_lgc_br_idx(update_unhashed_idx, pi.U(log2Ceil(numBr).W))
19112cedb6fSLingrui98
19281d86739SLingrui98    val wrbypass_io = Mux1H(UIntToOH(br_lidx, numBr), wrbypasses.map(_.io))
19381d86739SLingrui98
19481d86739SLingrui98    val ctrPos        = Mux1H(UIntToOH(br_lidx, numBr), io.update.tagePreds)
19581d86739SLingrui98    val bypass_ctr    = wrbypass_io.hit_data(ctrPos)
19681d86739SLingrui98    val previous_ctr  = Mux1H(UIntToOH(br_lidx, numBr), io.update.oldCtrs)
19781d86739SLingrui98    val hit_and_valid = wrbypass_io.hit && bypass_ctr.valid
19881d86739SLingrui98    val oldCtr        = Mux(hit_and_valid, bypass_ctr.bits, previous_ctr)
19981d86739SLingrui98    val taken         = Mux1H(UIntToOH(br_lidx, numBr), io.update.takens)
20081d86739SLingrui98    update_wdata(pi) := ctrUpdate(oldCtr, taken)
20181d86739SLingrui98  }
20281d86739SLingrui98
20381d86739SLingrui98  val per_br_update_wdata_packed = update_wdata_packed.sliding(2, 2).map(VecInit(_)).toSeq
20481d86739SLingrui98  val per_br_update_way_mask     = updateWayMask.sliding(2, 2).map(VecInit(_)).toSeq
20581d86739SLingrui98  for (li <- 0 until numBr) {
20681d86739SLingrui98    val wrbypass = wrbypasses(li)
20781d86739SLingrui98    val br_pidx  = get_phy_br_idx(update_unhashed_idx, li)
20881d86739SLingrui98    wrbypass.io.wen        := io.update.mask(li)
20912cedb6fSLingrui98    wrbypass.io.write_idx  := update_idx
21081d86739SLingrui98    wrbypass.io.write_data := Mux1H(UIntToOH(br_pidx, numBr), per_br_update_wdata_packed)
21181d86739SLingrui98    wrbypass.io.write_way_mask.map(_ := Mux1H(UIntToOH(br_pidx, numBr), per_br_update_way_mask))
21234ed6fbcSLingrui98  }
21309c6f1ddSLingrui98
21409c6f1ddSLingrui98  val u = io.update
215cf7d6b7aSMuzi  XSDebug(
216cf7d6b7aSMuzi    io.req.valid,
21709c6f1ddSLingrui98    p"scTableReq: pc=0x${Hexadecimal(io.req.bits.pc)}, " +
218cf7d6b7aSMuzi      p"s0_idx=${s0_idx}\n"
219cf7d6b7aSMuzi  )
220cf7d6b7aSMuzi  XSDebug(
221cf7d6b7aSMuzi    RegNext(io.req.valid),
22209c6f1ddSLingrui98    p"scTableResp: s1_idx=${s1_idx}," +
223cf7d6b7aSMuzi      p"ctr:${io.resp.ctrs}\n"
224cf7d6b7aSMuzi  )
225cf7d6b7aSMuzi  XSDebug(
226cf7d6b7aSMuzi    io.update.mask.reduce(_ || _),
227e69b7315SLingrui98    p"update Table: pc:${Hexadecimal(u.pc)}, " +
228cf7d6b7aSMuzi      p"tageTakens:${u.tagePreds}, taken:${u.takens}, oldCtr:${u.oldCtrs}\n"
229cf7d6b7aSMuzi  )
23009c6f1ddSLingrui98}
23109c6f1ddSLingrui98
23209c6f1ddSLingrui98class SCThreshold(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
23309c6f1ddSLingrui98  val ctr = UInt(ctrBits.W)
23409c6f1ddSLingrui98  def satPos(ctr: UInt = this.ctr) = ctr === ((1.U << ctrBits) - 1.U)
23509c6f1ddSLingrui98  def satNeg(ctr: UInt = this.ctr) = ctr === 0.U
23667ba96b4SYinan Xu  def neutralVal = (1 << (ctrBits - 1)).U
23709c6f1ddSLingrui98  val thres      = UInt(8.W)
23809c6f1ddSLingrui98  def initVal    = 6.U
23909c6f1ddSLingrui98  def minThres   = 6.U
24009c6f1ddSLingrui98  def maxThres   = 31.U
24109c6f1ddSLingrui98  def update(cause: Bool): SCThreshold = {
24209c6f1ddSLingrui98    val res    = Wire(new SCThreshold(this.ctrBits))
24309c6f1ddSLingrui98    val newCtr = satUpdate(this.ctr, this.ctrBits, cause)
244cf7d6b7aSMuzi    val newThres = Mux(
245cf7d6b7aSMuzi      res.satPos(newCtr) && this.thres <= maxThres,
246cf7d6b7aSMuzi      this.thres + 2.U,
247cf7d6b7aSMuzi      Mux(res.satNeg(newCtr) && this.thres >= minThres, this.thres - 2.U, this.thres)
248cf7d6b7aSMuzi    )
24909c6f1ddSLingrui98    res.thres := newThres
25009c6f1ddSLingrui98    res.ctr   := Mux(res.satPos(newCtr) || res.satNeg(newCtr), res.neutralVal, newCtr)
25109c6f1ddSLingrui98    // XSDebug(true.B, p"scThres Update: cause${cause} newCtr ${newCtr} newThres ${newThres}\n")
25209c6f1ddSLingrui98    res
25309c6f1ddSLingrui98  }
25409c6f1ddSLingrui98}
25509c6f1ddSLingrui98
25609c6f1ddSLingrui98object SCThreshold {
25709c6f1ddSLingrui98  def apply(bits: Int)(implicit p: Parameters) = {
25809c6f1ddSLingrui98    val t = Wire(new SCThreshold(ctrBits = bits))
25909c6f1ddSLingrui98    t.ctr   := t.neutralVal
26009c6f1ddSLingrui98    t.thres := t.initVal
26109c6f1ddSLingrui98    t
26209c6f1ddSLingrui98  }
26309c6f1ddSLingrui98}
26409c6f1ddSLingrui98
2651ca0e4f3SYinan Xutrait HasSC extends HasSCParameter with HasPerfEvents { this: Tage =>
266efe3f3bbSSteve Gou  val update_on_mispred, update_on_unconf = WireInit(0.U.asTypeOf(Vec(TageBanks, Bool())))
267dd6c0695SLingrui98  var sc_fh_info                          = Set[FoldedHistoryInfo]()
268bf358e08SLingrui98  if (EnableSC) {
26934ed6fbcSLingrui98    val scTables = SCTableInfos.map {
27009c6f1ddSLingrui98      case (nRows, ctrBits, histLen) => {
27109c6f1ddSLingrui98        val t   = Module(new SCTable(nRows / TageBanks, ctrBits, histLen))
27209c6f1ddSLingrui98        val req = t.io.req
273adc0b8dfSGuokai Chen        req.valid            := io.s0_fire(3)
274adc0b8dfSGuokai Chen        req.bits.pc          := s0_pc_dup(3)
275adc0b8dfSGuokai Chen        req.bits.folded_hist := io.in.bits.folded_hist(3)
27686d9c530SLingrui98        req.bits.ghist       := DontCare
27709c6f1ddSLingrui98        if (!EnableSC) { t.io.update := DontCare }
27809c6f1ddSLingrui98        t
27909c6f1ddSLingrui98      }
28009c6f1ddSLingrui98    }
28134ed6fbcSLingrui98    sc_fh_info = scTables.map(_.getFoldedHistoryInfo).reduce(_ ++ _).toSet
28209c6f1ddSLingrui98
28309c6f1ddSLingrui98    val scThresholds  = List.fill(TageBanks)(RegInit(SCThreshold(5)))
28409c6f1ddSLingrui98    val useThresholds = VecInit(scThresholds map (_.thres))
2857e8b966aSLingrui98
286d71e9942SLingrui98    def sign(x: SInt) = x(x.getWidth - 1)
287d71e9942SLingrui98    def pos(x:  SInt) = !sign(x)
288d71e9942SLingrui98    def neg(x:  SInt) = sign(x)
2897e8b966aSLingrui98
2907e8b966aSLingrui98    def aboveThreshold(scSum: SInt, tagePvdr: SInt, threshold: UInt): Bool = {
291d71e9942SLingrui98      val signedThres = threshold.zext
2927e8b966aSLingrui98      val totalSum    = scSum +& tagePvdr
2937e8b966aSLingrui98      (scSum > signedThres - tagePvdr) && pos(totalSum) ||
2947e8b966aSLingrui98      (scSum < -signedThres - tagePvdr) && neg(totalSum)
295d71e9942SLingrui98    }
29609c6f1ddSLingrui98    val updateThresholds = VecInit(useThresholds map (t => (t << 3) +& 21.U))
29709c6f1ddSLingrui98
29834ed6fbcSLingrui98    val s1_scResps = VecInit(scTables.map(t => t.io.resp))
29909c6f1ddSLingrui98
30034ed6fbcSLingrui98    val scUpdateMask      = WireInit(0.U.asTypeOf(Vec(numBr, Vec(SCNTables, Bool()))))
30109c6f1ddSLingrui98    val scUpdateTagePreds = Wire(Vec(TageBanks, Bool()))
30209c6f1ddSLingrui98    val scUpdateTakens    = Wire(Vec(TageBanks, Bool()))
30334ed6fbcSLingrui98    val scUpdateOldCtrs   = Wire(Vec(numBr, Vec(SCNTables, SInt(SCCtrBits.W))))
30409c6f1ddSLingrui98    scUpdateTagePreds := DontCare
30509c6f1ddSLingrui98    scUpdateTakens    := DontCare
30609c6f1ddSLingrui98    scUpdateOldCtrs   := DontCare
30709c6f1ddSLingrui98
30834ed6fbcSLingrui98    val updateSCMeta = updateMeta.scMeta.get
30909c6f1ddSLingrui98
31009c6f1ddSLingrui98    val s2_sc_used, s2_conf, s2_unconf, s2_agree, s2_disagree =
311ff1cd593SLingrui98      WireInit(0.U.asTypeOf(Vec(TageBanks, Bool())))
31209c6f1ddSLingrui98    val update_sc_used, update_conf, update_unconf, update_agree, update_disagree =
313ff1cd593SLingrui98      WireInit(0.U.asTypeOf(Vec(TageBanks, Bool())))
314efe3f3bbSSteve Gou    val sc_misp_tage_corr, sc_corr_tage_misp =
315ff1cd593SLingrui98      WireInit(0.U.asTypeOf(Vec(TageBanks, Bool())))
31609c6f1ddSLingrui98
31709c6f1ddSLingrui98    // for sc ctrs
318238c84b9SLingrui98    def getCentered(ctr: SInt): SInt = Cat(ctr, 1.U(1.W)).asSInt
319238c84b9SLingrui98    // for tage ctrs, (2*(ctr-4)+1)*8
320238c84b9SLingrui98    def getPvdrCentered(ctr: UInt): SInt = Cat(ctr ^ (1 << (TageCtrBits - 1)).U, 1.U(1.W), 0.U(3.W)).asSInt
32109c6f1ddSLingrui98
32234ed6fbcSLingrui98    val scMeta = resp_meta.scMeta.get
32309c6f1ddSLingrui98    scMeta := DontCare
32434ed6fbcSLingrui98    for (w <- 0 until TageBanks) {
32509c6f1ddSLingrui98      // do summation in s2
32609c6f1ddSLingrui98      val s1_scTableSums = VecInit(
32709c6f1ddSLingrui98        (0 to 1) map { i =>
32834ed6fbcSLingrui98          ParallelSingedExpandingAdd(s1_scResps map (r => getCentered(r.ctrs(w)(i)))) // TODO: rewrite with wallace tree
32909c6f1ddSLingrui98        }
33009c6f1ddSLingrui98      )
331adc0b8dfSGuokai Chen      val s2_scTableSums         = RegEnable(s1_scTableSums, io.s1_fire(3))
332adc0b8dfSGuokai Chen      val s2_tagePrvdCtrCentered = getPvdrCentered(RegEnable(s1_providerResps(w).ctr, io.s1_fire(3)))
333cb4f77ceSLingrui98      val s2_totalSums           = s2_scTableSums.map(_ +& s2_tagePrvdCtrCentered)
334cf7d6b7aSMuzi      val s2_sumAboveThresholds =
335cf7d6b7aSMuzi        VecInit((0 to 1).map(i => aboveThreshold(s2_scTableSums(i), s2_tagePrvdCtrCentered, useThresholds(w))))
336cb4f77ceSLingrui98      val s2_scPreds = VecInit(s2_totalSums.map(_ >= 0.S))
33709c6f1ddSLingrui98
338adc0b8dfSGuokai Chen      val s2_scResps   = VecInit(RegEnable(s1_scResps, io.s1_fire(3)).map(_.ctrs(w)))
339adc0b8dfSGuokai Chen      val s2_scCtrs    = VecInit(s2_scResps.map(_(s2_tageTakens_dup(3)(w).asUInt)))
340adc0b8dfSGuokai Chen      val s2_chooseBit = s2_tageTakens_dup(3)(w)
34109c6f1ddSLingrui98
342cb4f77ceSLingrui98      val s2_pred =
343cf7d6b7aSMuzi        Mux(s2_provideds(w) && s2_sumAboveThresholds(s2_chooseBit), s2_scPreds(s2_chooseBit), s2_tageTakens_dup(3)(w))
344cb4f77ceSLingrui98
345adc0b8dfSGuokai Chen      val s3_disagree = RegEnable(s2_disagree, io.s2_fire(3))
346abdc3a32Sxu_zh      io.out.last_stage_spec_info.sc_disagree.map(_ := s3_disagree)
347d2b20d1aSTang Haojin
348adc0b8dfSGuokai Chen      scMeta.scPreds(w) := RegEnable(s2_scPreds(s2_chooseBit), io.s2_fire(3))
349adc0b8dfSGuokai Chen      scMeta.ctrs(w)    := RegEnable(s2_scCtrs, io.s2_fire(3))
35034ed6fbcSLingrui98
3518b33cd30Sklin02      val pred     = s2_scPreds(s2_chooseBit)
3528b33cd30Sklin02      val debug_pc = Cat(debug_pc_s2, w.U, 0.U(instOffsetBits.W))
3534813e060SLingrui98      when(s2_provideds(w)) {
35409c6f1ddSLingrui98        s2_sc_used(w) := true.B
355b30c10d6SLingrui98        s2_unconf(w)  := !s2_sumAboveThresholds(s2_chooseBit)
356b30c10d6SLingrui98        s2_conf(w)    := s2_sumAboveThresholds(s2_chooseBit)
35709c6f1ddSLingrui98        // Use prediction from Statistical Corrector
358b30c10d6SLingrui98        when(s2_sumAboveThresholds(s2_chooseBit)) {
359adc0b8dfSGuokai Chen          s2_agree(w)    := s2_tageTakens_dup(3)(w) === pred
360adc0b8dfSGuokai Chen          s2_disagree(w) := s2_tageTakens_dup(3)(w) =/= pred
36109c6f1ddSLingrui98          // fit to always-taken condition
362c2d1ec7dSLingrui98          // io.out.s2.full_pred.br_taken_mask(w) := pred
36309c6f1ddSLingrui98        }
36409c6f1ddSLingrui98      }
3658b33cd30Sklin02      XSDebug(s2_provideds(w), p"---------tage_bank_${w} provided so that sc used---------\n")
3668b33cd30Sklin02      XSDebug(
3678b33cd30Sklin02        s2_provideds(w) && s2_sumAboveThresholds(s2_chooseBit),
3688b33cd30Sklin02        p"pc(${Hexadecimal(debug_pc)}) SC(${w.U}) overriden pred to ${pred}\n"
3698b33cd30Sklin02      )
37009c6f1ddSLingrui98
371adc0b8dfSGuokai Chen      val s3_pred_dup   = io.s2_fire.map(f => RegEnable(s2_pred, f))
372adc0b8dfSGuokai Chen      val sc_enable_dup = dup(RegNext(io.ctrl.sc_enable))
373cf7d6b7aSMuzi      for (
374cf7d6b7aSMuzi        sc_enable & fp & s3_pred <-
375cf7d6b7aSMuzi          sc_enable_dup zip io.out.s3.full_pred zip s3_pred_dup
376cf7d6b7aSMuzi      ) {
377adc0b8dfSGuokai Chen        when(sc_enable) {
378adc0b8dfSGuokai Chen          fp.br_taken_mask(w) := s3_pred
379adc0b8dfSGuokai Chen        }
3806ee06c7aSSteve Gou      }
381b30c10d6SLingrui98
38234ed6fbcSLingrui98      val updateTageMeta    = updateMeta
38334ed6fbcSLingrui98      val scPred            = updateSCMeta.scPreds(w)
384deb3a97eSGao-Zeyu      val tagePred          = updateTageMeta.takens(w)
385803124a6SLingrui98      val taken             = update.br_taken_mask(w)
38634ed6fbcSLingrui98      val scOldCtrs         = updateSCMeta.ctrs(w)
3874813e060SLingrui98      val pvdrCtr           = updateTageMeta.providerResps(w).ctr
388ffa09ba7SEaston Man      val tableSum          = ParallelSingedExpandingAdd(scOldCtrs.map(getCentered))
389ffa09ba7SEaston Man      val totalSumAbs       = (tableSum +& getPvdrCentered(pvdrCtr)).abs.asUInt
390ff1cd593SLingrui98      val updateThres       = updateThresholds(w)
391ffa09ba7SEaston Man      val sumAboveThreshold = aboveThreshold(tableSum, getPvdrCentered(pvdrCtr), updateThres)
3928b33cd30Sklin02      val thres             = useThresholds(w)
3938b33cd30Sklin02      val newThres          = scThresholds(w).update(scPred =/= taken)
3948b33cd30Sklin02      when(updateValids(w) && updateTageMeta.providers(w).valid) {
39509c6f1ddSLingrui98        scUpdateTagePreds(w) := tagePred
39609c6f1ddSLingrui98        scUpdateTakens(w)    := taken
39709c6f1ddSLingrui98        (scUpdateOldCtrs(w) zip scOldCtrs).foreach { case (t, c) => t := c }
39809c6f1ddSLingrui98
39909c6f1ddSLingrui98        update_sc_used(w)    := true.B
400b30c10d6SLingrui98        update_unconf(w)     := !sumAboveThreshold
401b30c10d6SLingrui98        update_conf(w)       := sumAboveThreshold
40209c6f1ddSLingrui98        update_agree(w)      := scPred === tagePred
40309c6f1ddSLingrui98        update_disagree(w)   := scPred =/= tagePred
40409c6f1ddSLingrui98        sc_corr_tage_misp(w) := scPred === taken && tagePred =/= taken && update_conf(w)
40509c6f1ddSLingrui98        sc_misp_tage_corr(w) := scPred =/= taken && tagePred === taken && update_conf(w)
40609c6f1ddSLingrui98
407ffa09ba7SEaston Man        when(scPred =/= tagePred && totalSumAbs >= thres - 4.U && totalSumAbs <= thres - 2.U) {
40809c6f1ddSLingrui98          scThresholds(w) := newThres
40909c6f1ddSLingrui98        }
41009c6f1ddSLingrui98
411b30c10d6SLingrui98        when(scPred =/= taken || !sumAboveThreshold) {
41209c6f1ddSLingrui98          scUpdateMask(w).foreach(_ := true.B)
4138b33cd30Sklin02          update_on_mispred(w) := scPred =/= taken
4148b33cd30Sklin02          update_on_unconf(w)  := scPred === taken
4158b33cd30Sklin02        }
4168b33cd30Sklin02      }
417cf7d6b7aSMuzi      XSDebug(
4188b33cd30Sklin02        updateValids(w) && updateTageMeta.providers(w).valid &&
4198b33cd30Sklin02          scPred =/= tagePred && totalSumAbs >= thres - 4.U && totalSumAbs <= thres - 2.U,
4208b33cd30Sklin02        p"scThres $w update: old ${useThresholds(w)} --> new ${newThres.thres}\n"
4218b33cd30Sklin02      )
4228b33cd30Sklin02      XSDebug(
4238b33cd30Sklin02        updateValids(w) && updateTageMeta.providers(w).valid &&
4248b33cd30Sklin02          (scPred =/= taken || !sumAboveThreshold) &&
425cf7d6b7aSMuzi          tableSum < 0.S,
42609c6f1ddSLingrui98        p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " +
427ffa09ba7SEaston Man          p"scSum(-${tableSum.abs}), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n"
42809c6f1ddSLingrui98      )
429cf7d6b7aSMuzi      XSDebug(
4308b33cd30Sklin02        updateValids(w) && updateTageMeta.providers(w).valid &&
4318b33cd30Sklin02          (scPred =/= taken || !sumAboveThreshold) &&
432cf7d6b7aSMuzi          tableSum >= 0.S,
43309c6f1ddSLingrui98        p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " +
434ffa09ba7SEaston Man          p"scSum(+${tableSum.abs}), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n"
43509c6f1ddSLingrui98      )
4368b33cd30Sklin02      XSDebug(
4378b33cd30Sklin02        updateValids(w) && updateTageMeta.providers(w).valid &&
4388b33cd30Sklin02          (scPred =/= taken || !sumAboveThreshold),
4398b33cd30Sklin02        p"bank(${w}), update: sc: ${updateSCMeta}\n"
4408b33cd30Sklin02      )
44109c6f1ddSLingrui98    }
44209c6f1ddSLingrui98
4437af6acb0SEaston Man    val realWens = scUpdateMask.transpose.map(v => v.reduce(_ | _))
44409c6f1ddSLingrui98    for (b <- 0 until TageBanks) {
44534ed6fbcSLingrui98      for (i <- 0 until SCNTables) {
4467af6acb0SEaston Man        val realWen = realWens(i)
44734ed6fbcSLingrui98        scTables(i).io.update.mask(b)      := RegNext(scUpdateMask(b)(i))
4487af6acb0SEaston Man        scTables(i).io.update.tagePreds(b) := RegEnable(scUpdateTagePreds(b), realWen)
4497af6acb0SEaston Man        scTables(i).io.update.takens(b)    := RegEnable(scUpdateTakens(b), realWen)
4507af6acb0SEaston Man        scTables(i).io.update.oldCtrs(b)   := RegEnable(scUpdateOldCtrs(b)(i), realWen)
45103426fe2Spengxiao        scTables(i).io.update.pc           := RegEnable(update_pc, realWen)
45203426fe2Spengxiao        scTables(i).io.update.ghist        := RegEnable(update.ghist, realWen)
45309c6f1ddSLingrui98      }
45409c6f1ddSLingrui98    }
45509c6f1ddSLingrui98
45609c6f1ddSLingrui98    tage_perf("sc_conf", PopCount(s2_conf), PopCount(update_conf))
45709c6f1ddSLingrui98    tage_perf("sc_unconf", PopCount(s2_unconf), PopCount(update_unconf))
45809c6f1ddSLingrui98    tage_perf("sc_agree", PopCount(s2_agree), PopCount(update_agree))
45909c6f1ddSLingrui98    tage_perf("sc_disagree", PopCount(s2_disagree), PopCount(update_disagree))
46009c6f1ddSLingrui98    tage_perf("sc_used", PopCount(s2_sc_used), PopCount(update_sc_used))
46109c6f1ddSLingrui98    XSPerfAccumulate("sc_update_on_mispred", PopCount(update_on_mispred))
46209c6f1ddSLingrui98    XSPerfAccumulate("sc_update_on_unconf", PopCount(update_on_unconf))
46309c6f1ddSLingrui98    XSPerfAccumulate("sc_mispred_but_tage_correct", PopCount(sc_misp_tage_corr))
46409c6f1ddSLingrui98    XSPerfAccumulate("sc_correct_and_tage_wrong", PopCount(sc_corr_tage_misp))
465cd365d4cSrvcoresjw
466efe3f3bbSSteve Gou  }
467efe3f3bbSSteve Gou
468dd6c0695SLingrui98  override def getFoldedHistoryInfo = Some(tage_fh_info ++ sc_fh_info)
469dd6c0695SLingrui98
4704813e060SLingrui98  override val perfEvents = Seq(
4714813e060SLingrui98    ("tage_tht_hit                  ", PopCount(updateMeta.providers.map(_.valid))),
472cd365d4cSrvcoresjw    ("sc_update_on_mispred          ", PopCount(update_on_mispred)),
473cf7d6b7aSMuzi    ("sc_update_on_unconf           ", PopCount(update_on_unconf))
474cd365d4cSrvcoresjw  )
4751ca0e4f3SYinan Xu  generatePerfEvent()
476bf358e08SLingrui98}
477