xref: /XiangShan/src/main/scala/xiangshan/frontend/SC.scala (revision 34ed6fbc4d883f30e58d568c1589a0d683bae712)
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.
1509c6f1ddSLingrui98***************************************************************************************/
1609c6f1ddSLingrui98
1709c6f1ddSLingrui98package xiangshan.frontend
1809c6f1ddSLingrui98
1909c6f1ddSLingrui98import chipsalliance.rocketchip.config.Parameters
2009c6f1ddSLingrui98import chisel3._
2109c6f1ddSLingrui98import chisel3.util._
2209c6f1ddSLingrui98import xiangshan._
2309c6f1ddSLingrui98import utils._
2409c6f1ddSLingrui98import chisel3.experimental.chiselName
2509c6f1ddSLingrui98
2609c6f1ddSLingrui98import scala.math.min
2709c6f1ddSLingrui98
2809c6f1ddSLingrui98trait HasSCParameter extends TageParams {
2909c6f1ddSLingrui98}
3009c6f1ddSLingrui98
3109c6f1ddSLingrui98class SCReq(implicit p: Parameters) extends TageReq
3209c6f1ddSLingrui98
3309c6f1ddSLingrui98abstract class SCBundle(implicit p: Parameters) extends TageBundle with HasSCParameter {}
3409c6f1ddSLingrui98abstract class SCModule(implicit p: Parameters) extends TageModule with HasSCParameter {}
3509c6f1ddSLingrui98
3609c6f1ddSLingrui98
37*34ed6fbcSLingrui98class SCMeta(val ntables: Int)(implicit p: Parameters) extends XSBundle with HasSCParameter {
38*34ed6fbcSLingrui98  val tageTakens = Vec(numBr, Bool())
39*34ed6fbcSLingrui98  val scUsed = Bool()
40*34ed6fbcSLingrui98  val scPreds = Vec(numBr, Bool())
4109c6f1ddSLingrui98  // Suppose ctrbits of all tables are identical
42*34ed6fbcSLingrui98  val ctrs = Vec(numBr, Vec(ntables, SInt(SCCtrBits.W)))
4309c6f1ddSLingrui98}
4409c6f1ddSLingrui98
4509c6f1ddSLingrui98
4609c6f1ddSLingrui98class SCResp(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
47*34ed6fbcSLingrui98  val ctrs = Vec(numBr, Vec(2, SInt(ctrBits.W)))
4809c6f1ddSLingrui98}
4909c6f1ddSLingrui98
5009c6f1ddSLingrui98class SCUpdate(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
5109c6f1ddSLingrui98  val pc = UInt(VAddrBits.W)
52dd6c0695SLingrui98  val folded_hist = new AllFoldedHistories(foldedGHistInfos)
53*34ed6fbcSLingrui98  val mask = Vec(numBr, Bool())
54*34ed6fbcSLingrui98  val oldCtrs = Vec(numBr, SInt(ctrBits.W))
55*34ed6fbcSLingrui98  val tagePreds = Vec(numBr, Bool())
56*34ed6fbcSLingrui98  val takens = Vec(numBr, Bool())
5709c6f1ddSLingrui98}
5809c6f1ddSLingrui98
5909c6f1ddSLingrui98class SCTableIO(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
6009c6f1ddSLingrui98  val req = Input(Valid(new SCReq))
6109c6f1ddSLingrui98  val resp = Output(new SCResp(ctrBits))
6209c6f1ddSLingrui98  val update = Input(new SCUpdate(ctrBits))
6309c6f1ddSLingrui98}
6409c6f1ddSLingrui98
6509c6f1ddSLingrui98@chiselName
6609c6f1ddSLingrui98class SCTable(val nRows: Int, val ctrBits: Int, val histLen: Int)(implicit p: Parameters)
6709c6f1ddSLingrui98  extends SCModule with HasFoldedHistory {
6809c6f1ddSLingrui98  val io = IO(new SCTableIO(ctrBits))
6909c6f1ddSLingrui98
7009c6f1ddSLingrui98  // val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false))
71*34ed6fbcSLingrui98  val table = Module(new SRAMTemplate(SInt(ctrBits.W), set=nRows, way=2*TageBanks, shouldReset=true, holdRead=true, singlePort=false))
7209c6f1ddSLingrui98
73dd6c0695SLingrui98  // def getIdx(hist: UInt, pc: UInt) = {
74dd6c0695SLingrui98  //   (compute_folded_ghist(hist, log2Ceil(nRows)) ^ (pc >> instOffsetBits))(log2Ceil(nRows)-1,0)
75dd6c0695SLingrui98  // }
76dd6c0695SLingrui98
77dd6c0695SLingrui98
78dd6c0695SLingrui98  val idxFhInfo = (histLen, min(log2Ceil(nRows), histLen))
79dd6c0695SLingrui98
80dd6c0695SLingrui98  def getFoldedHistoryInfo = Set(idxFhInfo).filter(_._1 > 0)
81dd6c0695SLingrui98
82dd6c0695SLingrui98  def getIdx(pc: UInt, allFh: AllFoldedHistories) = {
83dd6c0695SLingrui98    if (histLen > 0) {
84dd6c0695SLingrui98      val idx_fh = allFh.getHistWithInfo(idxFhInfo).folded_hist
85dd6c0695SLingrui98      // require(idx_fh.getWidth == log2Ceil(nRows))
86dd6c0695SLingrui98      ((pc >> instOffsetBits) ^ idx_fh)(log2Ceil(nRows)-1,0)
87dd6c0695SLingrui98    }
88dd6c0695SLingrui98    else {
89*34ed6fbcSLingrui98      (pc >> instOffsetBits)(log2Ceil(nRows)-1,0)
90dd6c0695SLingrui98    }
9109c6f1ddSLingrui98  }
9209c6f1ddSLingrui98
9309c6f1ddSLingrui98  def ctrUpdate(ctr: SInt, cond: Bool): SInt = signedSatUpdate(ctr, ctrBits, cond)
9409c6f1ddSLingrui98
95dd6c0695SLingrui98  val s0_idx = getIdx(io.req.bits.pc, io.req.bits.folded_hist)
9609c6f1ddSLingrui98  val s1_idx = RegEnable(s0_idx, enable=io.req.valid)
9709c6f1ddSLingrui98
9809c6f1ddSLingrui98  table.io.r.req.valid := io.req.valid
9909c6f1ddSLingrui98  table.io.r.req.bits.setIdx := s0_idx
10009c6f1ddSLingrui98
101*34ed6fbcSLingrui98  for (i <- 0 until numBr) {
102*34ed6fbcSLingrui98    io.resp.ctrs(i)(0) := table.io.r.resp.data(2*i)
103*34ed6fbcSLingrui98    io.resp.ctrs(i)(1) := table.io.r.resp.data(2*i+1)
104*34ed6fbcSLingrui98  }
10509c6f1ddSLingrui98
106*34ed6fbcSLingrui98  val update_wdata = Wire(Vec(numBr, SInt(ctrBits.W)))
107*34ed6fbcSLingrui98  val update_wdata_packed = VecInit(update_wdata.map(Seq.fill(2)(_)).reduce(_++_))
108*34ed6fbcSLingrui98  val updateWayMask = Wire(Vec(2*numBr, Bool()))
109*34ed6fbcSLingrui98
110*34ed6fbcSLingrui98  for (i <- 0 until numBr) {
111*34ed6fbcSLingrui98    updateWayMask(2*i)   := io.update.mask(i) && !io.update.tagePreds(i)
112*34ed6fbcSLingrui98    updateWayMask(2*i+1) := io.update.mask(i) &&  io.update.tagePreds(i)
113*34ed6fbcSLingrui98  }
11409c6f1ddSLingrui98
115dd6c0695SLingrui98  val update_idx = getIdx(io.update.pc, io.update.folded_hist)
11609c6f1ddSLingrui98
11709c6f1ddSLingrui98  table.io.w.apply(
118*34ed6fbcSLingrui98    valid = io.update.mask.reduce(_||_),
119*34ed6fbcSLingrui98    data = update_wdata_packed,
12009c6f1ddSLingrui98    setIdx = update_idx,
121*34ed6fbcSLingrui98    waymask = updateWayMask.asUInt
12209c6f1ddSLingrui98  )
12309c6f1ddSLingrui98
12409c6f1ddSLingrui98  val wrBypassEntries = 4
12509c6f1ddSLingrui98
126*34ed6fbcSLingrui98  val wrbypass = Module(new WrBypass(SInt(ctrBits.W), wrBypassEntries, log2Ceil(nRows), numWays=2*numBr))
12709c6f1ddSLingrui98
128*34ed6fbcSLingrui98  for (i <- 0 until numBr) {
129*34ed6fbcSLingrui98    val ctrPos = io.update.tagePreds(i)
130*34ed6fbcSLingrui98    val altPos = !io.update.tagePreds(i)
131*34ed6fbcSLingrui98    val bypass_ctr = wrbypass.io.hit_data((i << 1).U | ctrPos)
13209c6f1ddSLingrui98    val hit_and_valid = wrbypass.io.hit && bypass_ctr.valid
133*34ed6fbcSLingrui98    val oldCtr = Mux(hit_and_valid, bypass_ctr.bits, io.update.oldCtrs(i))
134*34ed6fbcSLingrui98    update_wdata(i) := ctrUpdate(oldCtr, io.update.takens(i))
135*34ed6fbcSLingrui98  }
13609c6f1ddSLingrui98
137*34ed6fbcSLingrui98  wrbypass.io.wen := io.update.mask.reduce(_||_)
138*34ed6fbcSLingrui98  wrbypass.io.write_data := update_wdata_packed // only one of them are used
139569b279fSLingrui98  wrbypass.io.write_idx := update_idx
140*34ed6fbcSLingrui98  wrbypass.io.write_way_mask.map(_ := updateWayMask)
14109c6f1ddSLingrui98
14209c6f1ddSLingrui98  val u = io.update
14309c6f1ddSLingrui98  XSDebug(io.req.valid,
14409c6f1ddSLingrui98    p"scTableReq: pc=0x${Hexadecimal(io.req.bits.pc)}, " +
145e69b7315SLingrui98    p"s0_idx=${s0_idx}\n")
14609c6f1ddSLingrui98  XSDebug(RegNext(io.req.valid),
14709c6f1ddSLingrui98    p"scTableResp: s1_idx=${s1_idx}," +
148*34ed6fbcSLingrui98    p"ctr:${io.resp.ctrs}\n")
149*34ed6fbcSLingrui98  XSDebug(io.update.mask.reduce(_||_),
150e69b7315SLingrui98    p"update Table: pc:${Hexadecimal(u.pc)}, " +
151*34ed6fbcSLingrui98    p"tageTakens:${u.tagePreds}, taken:${u.takens}, oldCtr:${u.oldCtrs}\n")
15209c6f1ddSLingrui98}
15309c6f1ddSLingrui98
15409c6f1ddSLingrui98class SCThreshold(val ctrBits: Int = 6)(implicit p: Parameters) extends SCBundle {
15509c6f1ddSLingrui98  val ctr = UInt(ctrBits.W)
15609c6f1ddSLingrui98  def satPos(ctr: UInt = this.ctr) = ctr === ((1.U << ctrBits) - 1.U)
15709c6f1ddSLingrui98  def satNeg(ctr: UInt = this.ctr) = ctr === 0.U
15809c6f1ddSLingrui98  def neutralVal = (1.U << (ctrBits - 1))
15909c6f1ddSLingrui98  val thres = UInt(8.W)
16009c6f1ddSLingrui98  def initVal = 6.U
16109c6f1ddSLingrui98  def minThres = 6.U
16209c6f1ddSLingrui98  def maxThres = 31.U
16309c6f1ddSLingrui98  def update(cause: Bool): SCThreshold = {
16409c6f1ddSLingrui98    val res = Wire(new SCThreshold(this.ctrBits))
16509c6f1ddSLingrui98    val newCtr = satUpdate(this.ctr, this.ctrBits, cause)
16609c6f1ddSLingrui98    val newThres = Mux(res.satPos(newCtr) && this.thres <= maxThres, this.thres + 2.U,
16709c6f1ddSLingrui98                      Mux(res.satNeg(newCtr) && this.thres >= minThres, this.thres - 2.U,
16809c6f1ddSLingrui98                      this.thres))
16909c6f1ddSLingrui98    res.thres := newThres
17009c6f1ddSLingrui98    res.ctr := Mux(res.satPos(newCtr) || res.satNeg(newCtr), res.neutralVal, newCtr)
17109c6f1ddSLingrui98    // XSDebug(true.B, p"scThres Update: cause${cause} newCtr ${newCtr} newThres ${newThres}\n")
17209c6f1ddSLingrui98    res
17309c6f1ddSLingrui98  }
17409c6f1ddSLingrui98}
17509c6f1ddSLingrui98
17609c6f1ddSLingrui98object SCThreshold {
17709c6f1ddSLingrui98  def apply(bits: Int)(implicit p: Parameters) = {
17809c6f1ddSLingrui98    val t = Wire(new SCThreshold(ctrBits=bits))
17909c6f1ddSLingrui98    t.ctr := t.neutralVal
18009c6f1ddSLingrui98    t.thres := t.initVal
18109c6f1ddSLingrui98    t
18209c6f1ddSLingrui98  }
18309c6f1ddSLingrui98}
18409c6f1ddSLingrui98
18509c6f1ddSLingrui98
1861ca0e4f3SYinan Xutrait HasSC extends HasSCParameter with HasPerfEvents { this: Tage =>
187efe3f3bbSSteve Gou  val update_on_mispred, update_on_unconf = WireInit(0.U.asTypeOf(Vec(TageBanks, Bool())))
188dd6c0695SLingrui98  var sc_fh_info = Set[FoldedHistoryInfo]()
189bf358e08SLingrui98  if (EnableSC) {
190*34ed6fbcSLingrui98    val scTables = SCTableInfos.map {
19109c6f1ddSLingrui98      case (nRows, ctrBits, histLen) => {
19209c6f1ddSLingrui98        val t = Module(new SCTable(nRows/TageBanks, ctrBits, histLen))
19309c6f1ddSLingrui98        val req = t.io.req
19409c6f1ddSLingrui98        req.valid := io.s0_fire
19509c6f1ddSLingrui98        req.bits.pc := s0_pc
196dd6c0695SLingrui98        req.bits.folded_hist := io.in.bits.folded_hist
19786d9c530SLingrui98        req.bits.ghist := DontCare
19809c6f1ddSLingrui98        if (!EnableSC) {t.io.update := DontCare}
19909c6f1ddSLingrui98        t
20009c6f1ddSLingrui98      }
20109c6f1ddSLingrui98    }
202*34ed6fbcSLingrui98    sc_fh_info = scTables.map(_.getFoldedHistoryInfo).reduce(_++_).toSet
20309c6f1ddSLingrui98
20409c6f1ddSLingrui98    val scThresholds = List.fill(TageBanks)(RegInit(SCThreshold(5)))
20509c6f1ddSLingrui98    val useThresholds = VecInit(scThresholds map (_.thres))
2067e8b966aSLingrui98
207d71e9942SLingrui98    def sign(x: SInt) = x(x.getWidth-1)
208d71e9942SLingrui98    def pos(x: SInt) = !sign(x)
209d71e9942SLingrui98    def neg(x: SInt) = sign(x)
2107e8b966aSLingrui98
2117e8b966aSLingrui98    def aboveThreshold(scSum: SInt, tagePvdr: SInt, threshold: UInt): Bool = {
212d71e9942SLingrui98      val signedThres = threshold.zext
2137e8b966aSLingrui98      val totalSum = scSum +& tagePvdr
2147e8b966aSLingrui98      (scSum >  signedThres - tagePvdr) && pos(totalSum) ||
2157e8b966aSLingrui98      (scSum < -signedThres - tagePvdr) && neg(totalSum)
216d71e9942SLingrui98    }
21709c6f1ddSLingrui98    val updateThresholds = VecInit(useThresholds map (t => (t << 3) +& 21.U))
21809c6f1ddSLingrui98
219*34ed6fbcSLingrui98    val s1_scResps = VecInit(scTables.map(t => t.io.resp))
22009c6f1ddSLingrui98
221*34ed6fbcSLingrui98    val scUpdateMask = WireInit(0.U.asTypeOf(Vec(numBr, Vec(SCNTables, Bool()))))
22209c6f1ddSLingrui98    val scUpdateTagePreds = Wire(Vec(TageBanks, Bool()))
22309c6f1ddSLingrui98    val scUpdateTakens = Wire(Vec(TageBanks, Bool()))
224*34ed6fbcSLingrui98    val scUpdateOldCtrs = Wire(Vec(numBr, Vec(SCNTables, SInt(SCCtrBits.W))))
22509c6f1ddSLingrui98    scUpdateTagePreds := DontCare
22609c6f1ddSLingrui98    scUpdateTakens := DontCare
22709c6f1ddSLingrui98    scUpdateOldCtrs := DontCare
22809c6f1ddSLingrui98
229*34ed6fbcSLingrui98    val updateSCMeta = updateMeta.scMeta.get
23009c6f1ddSLingrui98
23109c6f1ddSLingrui98    val s2_sc_used, s2_conf, s2_unconf, s2_agree, s2_disagree =
23209c6f1ddSLingrui98      0.U.asTypeOf(Vec(TageBanks, Bool()))
23309c6f1ddSLingrui98    val update_sc_used, update_conf, update_unconf, update_agree, update_disagree =
23409c6f1ddSLingrui98      0.U.asTypeOf(Vec(TageBanks, Bool()))
235efe3f3bbSSteve Gou    val sc_misp_tage_corr, sc_corr_tage_misp =
23609c6f1ddSLingrui98      0.U.asTypeOf(Vec(TageBanks, Bool()))
23709c6f1ddSLingrui98
23809c6f1ddSLingrui98    // for sc ctrs
239238c84b9SLingrui98    def getCentered(ctr: SInt): SInt = Cat(ctr, 1.U(1.W)).asSInt
240238c84b9SLingrui98    // for tage ctrs, (2*(ctr-4)+1)*8
241238c84b9SLingrui98    def getPvdrCentered(ctr: UInt): SInt = Cat(ctr ^ (1 << (TageCtrBits-1)).U, 1.U(1.W), 0.U(3.W)).asSInt
24209c6f1ddSLingrui98
243*34ed6fbcSLingrui98    val scMeta = resp_meta.scMeta.get
24409c6f1ddSLingrui98    scMeta := DontCare
245*34ed6fbcSLingrui98    for (w <- 0 until TageBanks) {
24609c6f1ddSLingrui98      // do summation in s2
24709c6f1ddSLingrui98      val s1_scTableSums = VecInit(
24809c6f1ddSLingrui98        (0 to 1) map { i =>
249*34ed6fbcSLingrui98          ParallelSingedExpandingAdd(s1_scResps map (r => getCentered(r.ctrs(w)(i)))) // TODO: rewrite with wallace tree
25009c6f1ddSLingrui98        }
25109c6f1ddSLingrui98      )
25209c6f1ddSLingrui98
253*34ed6fbcSLingrui98      val tage_hit_vec = VecInit(s1_resps.map(_.valid))
254*34ed6fbcSLingrui98      val tage_pvdr_oh = VecInit((0 until TageNTables).map(i =>
255238c84b9SLingrui98        tage_hit_vec(i) && !tage_hit_vec.drop(i+1).reduceOption(_||_).getOrElse(false.B)
256238c84b9SLingrui98      ))
257*34ed6fbcSLingrui98      val tage_table_centered_ctrs = s1_resps.map(r => getPvdrCentered(r.bits.ctrs(w)))
258238c84b9SLingrui98
2597e8b966aSLingrui98      val s1_sumAboveThresholdsForAllTageCtrs =
2607e8b966aSLingrui98        VecInit(s1_scTableSums.map(s =>
2617e8b966aSLingrui98          VecInit(tage_table_centered_ctrs.map(tctr =>
2627e8b966aSLingrui98            aboveThreshold(s, tctr, useThresholds(w))
2637e8b966aSLingrui98          ))
2647e8b966aSLingrui98        ))
2657e8b966aSLingrui98      val s1_totalSumsForAllTageCtrs =
2667e8b966aSLingrui98        VecInit(s1_scTableSums.map(s =>
2677e8b966aSLingrui98          VecInit(tage_table_centered_ctrs.map(tctr =>
2687e8b966aSLingrui98            s +& tctr
2697e8b966aSLingrui98          ))
2707e8b966aSLingrui98        ))
2717e8b966aSLingrui98      val s1_totalSums = VecInit(s1_totalSumsForAllTageCtrs.map(i => Mux1H(tage_pvdr_oh, i)))
2727e8b966aSLingrui98      val s1_sumAboveThresholds = VecInit(s1_sumAboveThresholdsForAllTageCtrs.map(i => Mux1H(tage_pvdr_oh, i)))
27309c6f1ddSLingrui98      val s1_scPreds = VecInit(s1_totalSums.map (_ >= 0.S))
27409c6f1ddSLingrui98
275b30c10d6SLingrui98      val s2_sumAboveThresholds = RegEnable(s1_sumAboveThresholds, io.s1_fire)
27609c6f1ddSLingrui98      val s2_scPreds = RegEnable(s1_scPreds, io.s1_fire)
277*34ed6fbcSLingrui98      val s2_scResps = VecInit(RegEnable(s1_scResps, io.s1_fire).map(_.ctrs(w)))
278b30c10d6SLingrui98      val s2_scCtrs = VecInit(s2_scResps.map(_(s2_tageTakens(w).asUInt)))
27909c6f1ddSLingrui98      val s2_chooseBit = s2_tageTakens(w)
28009c6f1ddSLingrui98
281*34ed6fbcSLingrui98      scMeta.tageTakens(w) := s2_tageTakens(w)
282*34ed6fbcSLingrui98      scMeta.scUsed        := s2_provided
283*34ed6fbcSLingrui98      scMeta.scPreds(w)    := s2_scPreds(s2_chooseBit)
284*34ed6fbcSLingrui98      scMeta.ctrs(w)       := s2_scCtrs
285*34ed6fbcSLingrui98
286*34ed6fbcSLingrui98      when (s2_provided) {
28709c6f1ddSLingrui98        s2_sc_used(w) := true.B
288b30c10d6SLingrui98        s2_unconf(w) := !s2_sumAboveThresholds(s2_chooseBit)
289b30c10d6SLingrui98        s2_conf(w) := s2_sumAboveThresholds(s2_chooseBit)
29009c6f1ddSLingrui98        // Use prediction from Statistical Corrector
29109c6f1ddSLingrui98        XSDebug(p"---------tage_bank_${w} provided so that sc used---------\n")
292b30c10d6SLingrui98        when (s2_sumAboveThresholds(s2_chooseBit)) {
29309c6f1ddSLingrui98          val pred = s2_scPreds(s2_chooseBit)
29409c6f1ddSLingrui98          val debug_pc = Cat(debug_pc_s2, w.U, 0.U(instOffsetBits.W))
29509c6f1ddSLingrui98          s2_agree(w) := s2_tageTakens(w) === pred
29609c6f1ddSLingrui98          s2_disagree(w) := s2_tageTakens(w) =/= pred
29709c6f1ddSLingrui98          // fit to always-taken condition
298b37e4b45SLingrui98          // io.out.resp.s2.full_pred.br_taken_mask(w) := pred
29909c6f1ddSLingrui98          XSDebug(p"pc(${Hexadecimal(debug_pc)}) SC(${w.U}) overriden pred to ${pred}\n")
30009c6f1ddSLingrui98        }
30109c6f1ddSLingrui98      }
30209c6f1ddSLingrui98
303b37e4b45SLingrui98      io.out.resp.s2.full_pred.br_taken_mask(w) :=
304*34ed6fbcSLingrui98        Mux(s2_provided && s2_sumAboveThresholds(s2_chooseBit),
305b30c10d6SLingrui98          s2_scPreds(s2_chooseBit), s2_tageTakens(w))
306b30c10d6SLingrui98
307*34ed6fbcSLingrui98      val updateTageMeta = updateMeta
30809c6f1ddSLingrui98      when (updateValids(w) && updateSCMeta.scUsed.asBool) {
309*34ed6fbcSLingrui98        val scPred = updateSCMeta.scPreds(w)
310*34ed6fbcSLingrui98        val tagePred = updateSCMeta.tageTakens(w)
311b37e4b45SLingrui98        val taken = update.full_pred.br_taken_mask(w)
312*34ed6fbcSLingrui98        val scOldCtrs = updateSCMeta.ctrs(w)
313*34ed6fbcSLingrui98        val pvdrCtr = updateTageMeta.providerResp.ctrs(w)
31409c6f1ddSLingrui98        val sum = ParallelSingedExpandingAdd(scOldCtrs.map(getCentered)) +& getPvdrCentered(pvdrCtr)
31509c6f1ddSLingrui98        val sumAbs = sum.abs.asUInt
3167e8b966aSLingrui98        val sumAboveThreshold = aboveThreshold(sum, getPvdrCentered(pvdrCtr), useThresholds(w))
31709c6f1ddSLingrui98        scUpdateTagePreds(w) := tagePred
31809c6f1ddSLingrui98        scUpdateTakens(w) := taken
31909c6f1ddSLingrui98        (scUpdateOldCtrs(w) zip scOldCtrs).foreach{case (t, c) => t := c}
32009c6f1ddSLingrui98
32109c6f1ddSLingrui98        update_sc_used(w) := true.B
322b30c10d6SLingrui98        update_unconf(w) := !sumAboveThreshold
323b30c10d6SLingrui98        update_conf(w) := sumAboveThreshold
32409c6f1ddSLingrui98        update_agree(w) := scPred === tagePred
32509c6f1ddSLingrui98        update_disagree(w) := scPred =/= tagePred
32609c6f1ddSLingrui98        sc_corr_tage_misp(w) := scPred === taken && tagePred =/= taken && update_conf(w)
32709c6f1ddSLingrui98        sc_misp_tage_corr(w) := scPred =/= taken && tagePred === taken && update_conf(w)
32809c6f1ddSLingrui98
32909c6f1ddSLingrui98        val thres = useThresholds(w)
33009c6f1ddSLingrui98        when (scPred =/= tagePred && sumAbs >= thres - 4.U && sumAbs <= thres - 2.U) {
33109c6f1ddSLingrui98          val newThres = scThresholds(w).update(scPred =/= taken)
33209c6f1ddSLingrui98          scThresholds(w) := newThres
33309c6f1ddSLingrui98          XSDebug(p"scThres $w update: old ${useThresholds(w)} --> new ${newThres.thres}\n")
33409c6f1ddSLingrui98        }
33509c6f1ddSLingrui98
33609c6f1ddSLingrui98        val updateThres = updateThresholds(w)
337b30c10d6SLingrui98        when (scPred =/= taken || !sumAboveThreshold) {
33809c6f1ddSLingrui98          scUpdateMask(w).foreach(_ := true.B)
33909c6f1ddSLingrui98          XSDebug(sum < 0.S,
34009c6f1ddSLingrui98            p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " +
34109c6f1ddSLingrui98            p"scSum(-$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n"
34209c6f1ddSLingrui98          )
34309c6f1ddSLingrui98          XSDebug(sum >= 0.S,
34409c6f1ddSLingrui98            p"scUpdate: bank(${w}), scPred(${scPred}), tagePred(${tagePred}), " +
34509c6f1ddSLingrui98            p"scSum(+$sumAbs), mispred: sc(${scPred =/= taken}), tage(${updateMisPreds(w)})\n"
34609c6f1ddSLingrui98          )
34709c6f1ddSLingrui98          XSDebug(p"bank(${w}), update: sc: ${updateSCMeta}\n")
34809c6f1ddSLingrui98          update_on_mispred(w) := scPred =/= taken
34909c6f1ddSLingrui98          update_on_unconf(w) := scPred === taken
35009c6f1ddSLingrui98        }
35109c6f1ddSLingrui98      }
35209c6f1ddSLingrui98    }
35309c6f1ddSLingrui98
35409c6f1ddSLingrui98
35509c6f1ddSLingrui98    for (b <- 0 until TageBanks) {
356*34ed6fbcSLingrui98      for (i <- 0 until SCNTables) {
357*34ed6fbcSLingrui98        scTables(i).io.update.mask(b) := RegNext(scUpdateMask(b)(i))
358*34ed6fbcSLingrui98        scTables(i).io.update.tagePreds(b) := RegNext(scUpdateTagePreds(b))
359*34ed6fbcSLingrui98        scTables(i).io.update.takens(b)    := RegNext(scUpdateTakens(b))
360*34ed6fbcSLingrui98        scTables(i).io.update.oldCtrs(b)   := RegNext(scUpdateOldCtrs(b)(i))
361*34ed6fbcSLingrui98        scTables(i).io.update.pc := RegNext(update.pc)
362*34ed6fbcSLingrui98        scTables(i).io.update.folded_hist := RegNext(updateFHist)
36309c6f1ddSLingrui98      }
36409c6f1ddSLingrui98    }
36509c6f1ddSLingrui98
36609c6f1ddSLingrui98    tage_perf("sc_conf", PopCount(s2_conf), PopCount(update_conf))
36709c6f1ddSLingrui98    tage_perf("sc_unconf", PopCount(s2_unconf), PopCount(update_unconf))
36809c6f1ddSLingrui98    tage_perf("sc_agree", PopCount(s2_agree), PopCount(update_agree))
36909c6f1ddSLingrui98    tage_perf("sc_disagree", PopCount(s2_disagree), PopCount(update_disagree))
37009c6f1ddSLingrui98    tage_perf("sc_used", PopCount(s2_sc_used), PopCount(update_sc_used))
37109c6f1ddSLingrui98    XSPerfAccumulate("sc_update_on_mispred", PopCount(update_on_mispred))
37209c6f1ddSLingrui98    XSPerfAccumulate("sc_update_on_unconf", PopCount(update_on_unconf))
37309c6f1ddSLingrui98    XSPerfAccumulate("sc_mispred_but_tage_correct", PopCount(sc_misp_tage_corr))
37409c6f1ddSLingrui98    XSPerfAccumulate("sc_correct_and_tage_wrong", PopCount(sc_corr_tage_misp))
375cd365d4cSrvcoresjw
376efe3f3bbSSteve Gou  }
377efe3f3bbSSteve Gou
378dd6c0695SLingrui98  override def getFoldedHistoryInfo = Some(tage_fh_info ++ sc_fh_info)
379dd6c0695SLingrui98
380cd365d4cSrvcoresjw  val perfEvents = Seq(
381*34ed6fbcSLingrui98    ("tage_tht_hit                  ", updateMeta.provider.valid),
382cd365d4cSrvcoresjw    ("sc_update_on_mispred          ", PopCount(update_on_mispred) ),
383cd365d4cSrvcoresjw    ("sc_update_on_unconf           ", PopCount(update_on_unconf)  ),
384cd365d4cSrvcoresjw  )
3851ca0e4f3SYinan Xu  generatePerfEvent()
386bf358e08SLingrui98}
387