xref: /XiangShan/src/main/scala/xiangshan/frontend/FrontendBundle.scala (revision 50780602f4b92c9abaa470881d254139c155bace)
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***************************************************************************************/
1609c6f1ddSLingrui98package xiangshan.frontend
1709c6f1ddSLingrui98
1809c6f1ddSLingrui98import chipsalliance.rocketchip.config.Parameters
1909c6f1ddSLingrui98import chisel3._
2009c6f1ddSLingrui98import chisel3.util._
21bf358e08SLingrui98import chisel3.experimental.chiselName
2209c6f1ddSLingrui98import xiangshan._
23*50780602SJeniusimport xiangshan.frontend.icache._
2409c6f1ddSLingrui98import utils._
25c2ad24ebSLingrui98import scala.math._
2609c6f1ddSLingrui98
27bf358e08SLingrui98@chiselName
28b37e4b45SLingrui98class FetchRequestBundle(implicit p: Parameters) extends XSBundle with HasICacheParameters {
29c5c5edaeSJenius
30c5c5edaeSJenius  //fast path: Timing critical
3109c6f1ddSLingrui98  val startAddr       = UInt(VAddrBits.W)
3234a88126SJinYue  val nextlineStart   = UInt(VAddrBits.W)
33c5c5edaeSJenius  val nextStartAddr   = UInt(VAddrBits.W)
34c5c5edaeSJenius  //slow path
3509c6f1ddSLingrui98  val ftqIdx          = new FtqPtr
3609c6f1ddSLingrui98  val ftqOffset       = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))
3709c6f1ddSLingrui98
386ce52296SJinYue  def crossCacheline =  startAddr(blockOffBits - 1) === 1.U
396ce52296SJinYue
4009c6f1ddSLingrui98  def fromFtqPcBundle(b: Ftq_RF_Components) = {
4109c6f1ddSLingrui98    this.startAddr := b.startAddr
42b37e4b45SLingrui98    this.nextlineStart := b.nextLineAddr
43b37e4b45SLingrui98    when (b.fallThruError) {
44b37e4b45SLingrui98      val nextBlockHigherTemp = Mux(startAddr(log2Ceil(PredictWidth)+instOffsetBits), b.startAddr, b.nextLineAddr)
45b37e4b45SLingrui98      val nextBlockHigher = nextBlockHigherTemp(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1)
46b37e4b45SLingrui98      this.nextStartAddr :=
47b37e4b45SLingrui98        Cat(nextBlockHigher,
48b37e4b45SLingrui98          startAddr(log2Ceil(PredictWidth)+instOffsetBits) ^ 1.U(1.W),
49b37e4b45SLingrui98          startAddr(log2Ceil(PredictWidth)+instOffsetBits-1, instOffsetBits),
50b37e4b45SLingrui98          0.U(instOffsetBits.W)
51b37e4b45SLingrui98        )
5209c6f1ddSLingrui98    }
5309c6f1ddSLingrui98    this
5409c6f1ddSLingrui98  }
5509c6f1ddSLingrui98  override def toPrintable: Printable = {
56b37e4b45SLingrui98    p"[start] ${Hexadecimal(startAddr)} [next] ${Hexadecimal(nextlineStart)}" +
57b37e4b45SLingrui98      p"[tgt] ${Hexadecimal(nextStartAddr)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" +
5809c6f1ddSLingrui98      p" offset: ${ftqOffset.bits}\n"
5909c6f1ddSLingrui98  }
6009c6f1ddSLingrui98}
6109c6f1ddSLingrui98
62f22cf846SJeniusclass FtqICacheInfo(implicit p: Parameters)extends XSBundle with HasICacheParameters{
63c5c5edaeSJenius  val startAddr           = UInt(VAddrBits.W)
64c5c5edaeSJenius  val nextlineStart       = UInt(VAddrBits.W)
65c5c5edaeSJenius  def crossCacheline =  startAddr(blockOffBits - 1) === 1.U
66f22cf846SJenius}
67f22cf846SJenius
68*50780602SJeniusclass IFUICacheIO(implicit p: Parameters)extends XSBundle with HasICacheParameters{
69*50780602SJenius  val icacheReady       = Output(Bool())
70*50780602SJenius  val resp              = Vec(PortNumber, ValidIO(new ICacheMainPipeResp))
71*50780602SJenius}
72*50780602SJenius
73f22cf846SJeniusclass FtqToICacheRequestBundle(implicit p: Parameters)extends XSBundle with HasICacheParameters{
74f22cf846SJenius  val pcMemRead           = new FtqICacheInfo
75f22cf846SJenius  val bpuBypassWrite      = Vec(4, new FtqICacheInfo)
76f22cf846SJenius  val bypassSelect        = Bool()
77c5c5edaeSJenius  def fromFtqPcBundle(b: Ftq_RF_Components) = {
78f22cf846SJenius    this.pcMemRead.startAddr := b.startAddr
79f22cf846SJenius    this.pcMemRead.nextlineStart := b.nextLineAddr
80c5c5edaeSJenius    this
81c5c5edaeSJenius  }
82c5c5edaeSJenius}
83c5c5edaeSJenius
84c5c5edaeSJenius
8509c6f1ddSLingrui98class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle {
8609c6f1ddSLingrui98  val pc           = Vec(PredictWidth, UInt(VAddrBits.W))
8709c6f1ddSLingrui98  val pd           = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode
8809c6f1ddSLingrui98  val ftqIdx       = new FtqPtr
8909c6f1ddSLingrui98  val ftqOffset    = UInt(log2Ceil(PredictWidth).W)
9009c6f1ddSLingrui98  val misOffset    = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))
9109c6f1ddSLingrui98  val cfiOffset    = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))
9209c6f1ddSLingrui98  val target       = UInt(VAddrBits.W)
9309c6f1ddSLingrui98  val jalTarget    = UInt(VAddrBits.W)
9409c6f1ddSLingrui98  val instrRange   = Vec(PredictWidth, Bool())
9509c6f1ddSLingrui98}
9609c6f1ddSLingrui98
977052722fSJay// Ftq send req to Prefetch
987052722fSJayclass PrefetchRequest(implicit p:Parameters) extends XSBundle {
997052722fSJay  val target          = UInt(VAddrBits.W)
1007052722fSJay}
10109c6f1ddSLingrui98
1027052722fSJayclass FtqPrefechBundle(implicit p:Parameters) extends XSBundle {
1037052722fSJay  val req = DecoupledIO(new PrefetchRequest)
10409c6f1ddSLingrui98}
10509c6f1ddSLingrui98
10609c6f1ddSLingrui98class FetchToIBuffer(implicit p: Parameters) extends XSBundle {
10709c6f1ddSLingrui98  val instrs    = Vec(PredictWidth, UInt(32.W))
10809c6f1ddSLingrui98  val valid     = UInt(PredictWidth.W)
1092a3050c2SJay  val enqEnable = UInt(PredictWidth.W)
11009c6f1ddSLingrui98  val pd        = Vec(PredictWidth, new PreDecodeInfo)
11109c6f1ddSLingrui98  val pc        = Vec(PredictWidth, UInt(VAddrBits.W))
11209c6f1ddSLingrui98  val foldpc    = Vec(PredictWidth, UInt(MemPredPCWidth.W))
11309c6f1ddSLingrui98  val ftqPtr       = new FtqPtr
11409c6f1ddSLingrui98  val ftqOffset    = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
11509c6f1ddSLingrui98  val ipf          = Vec(PredictWidth, Bool())
11609c6f1ddSLingrui98  val acf          = Vec(PredictWidth, Bool())
11709c6f1ddSLingrui98  val crossPageIPFFix = Vec(PredictWidth, Bool())
11872951335SLi Qianruo  val triggered    = Vec(PredictWidth, new TriggerCf)
11909c6f1ddSLingrui98}
12009c6f1ddSLingrui98
121c2ad24ebSLingrui98// class BitWiseUInt(val width: Int, val init: UInt) extends Module {
122c2ad24ebSLingrui98//   val io = IO(new Bundle {
123c2ad24ebSLingrui98//     val set
124c2ad24ebSLingrui98//   })
125c2ad24ebSLingrui98// }
12609c6f1ddSLingrui98// Move from BPU
127c2ad24ebSLingrui98abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst {
128c2ad24ebSLingrui98  def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory
129c2ad24ebSLingrui98}
130c2ad24ebSLingrui98
131c2ad24ebSLingrui98class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory {
13209c6f1ddSLingrui98  val predHist = UInt(HistoryLength.W)
13309c6f1ddSLingrui98
134c2ad24ebSLingrui98  def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = {
135c2ad24ebSLingrui98    val g = Wire(new ShiftingGlobalHistory)
13609c6f1ddSLingrui98    g.predHist := (hist << shift) | taken
13709c6f1ddSLingrui98    g
13809c6f1ddSLingrui98  }
13909c6f1ddSLingrui98
140c2ad24ebSLingrui98  def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = {
141eeb5ff92SLingrui98    require(br_valids.length == numBr)
142eeb5ff92SLingrui98    require(real_taken_mask.length == numBr)
143eeb5ff92SLingrui98    val last_valid_idx = PriorityMux(
144eeb5ff92SLingrui98      br_valids.reverse :+ true.B,
145eeb5ff92SLingrui98      (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W))
146eeb5ff92SLingrui98    )
147eeb5ff92SLingrui98    val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask)
148eeb5ff92SLingrui98    val smaller = Mux(last_valid_idx < first_taken_idx,
149eeb5ff92SLingrui98      last_valid_idx,
150eeb5ff92SLingrui98      first_taken_idx
151eeb5ff92SLingrui98    )
152eeb5ff92SLingrui98    val shift = smaller
153eeb5ff92SLingrui98    val taken = real_taken_mask.reduce(_||_)
154eeb5ff92SLingrui98    update(shift, taken, this.predHist)
155eeb5ff92SLingrui98  }
156eeb5ff92SLingrui98
157c2ad24ebSLingrui98  // static read
158c2ad24ebSLingrui98  def read(n: Int): Bool = predHist.asBools()(n)
159c2ad24ebSLingrui98
160c2ad24ebSLingrui98  final def === (that: ShiftingGlobalHistory): Bool = {
16109c6f1ddSLingrui98    predHist === that.predHist
16209c6f1ddSLingrui98  }
16309c6f1ddSLingrui98
164c2ad24ebSLingrui98  final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that)
165c2ad24ebSLingrui98}
16609c6f1ddSLingrui98
167c2ad24ebSLingrui98// circular global history pointer
168c2ad24ebSLingrui98class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr](
169c2ad24ebSLingrui98  p => p(XSCoreParamsKey).HistoryLength
170c2ad24ebSLingrui98){
171c2ad24ebSLingrui98}
172c7fabd05SSteve Gou
173c7fabd05SSteve Gouobject CGHPtr {
174c7fabd05SSteve Gou  def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = {
175c7fabd05SSteve Gou    val ptr = Wire(new CGHPtr)
176c7fabd05SSteve Gou    ptr.flag := f
177c7fabd05SSteve Gou    ptr.value := v
178c7fabd05SSteve Gou    ptr
179c7fabd05SSteve Gou  }
180c7fabd05SSteve Gou  def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = {
181c7fabd05SSteve Gou    apply(!ptr.flag, ptr.value)
182c7fabd05SSteve Gou  }
183c7fabd05SSteve Gou}
184c7fabd05SSteve Gou
185c2ad24ebSLingrui98class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory {
186c2ad24ebSLingrui98  val buffer = Vec(HistoryLength, Bool())
187c2ad24ebSLingrui98  type HistPtr = UInt
188c2ad24ebSLingrui98  def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = {
189c2ad24ebSLingrui98    this
190c2ad24ebSLingrui98  }
191c2ad24ebSLingrui98}
192c2ad24ebSLingrui98
193dd6c0695SLingrui98class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters)
194c2ad24ebSLingrui98  extends XSBundle with HasBPUConst {
195dd6c0695SLingrui98  require(compLen >= 1)
196c2ad24ebSLingrui98  require(len > 0)
197c2ad24ebSLingrui98  // require(folded_len <= len)
198dd6c0695SLingrui98  require(compLen >= max_update_num)
199dd6c0695SLingrui98  val folded_hist = UInt(compLen.W)
200dd6c0695SLingrui98
20167402d75SLingrui98  def need_oldest_bits = len > compLen
202dd6c0695SLingrui98  def info = (len, compLen)
203c2ad24ebSLingrui98  def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1)
204c2ad24ebSLingrui98  def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen)
205c2ad24ebSLingrui98  def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0)
206c2ad24ebSLingrui98  def oldest_bit_start = oldest_bit_pos_in_folded.head
207c2ad24ebSLingrui98
208dd6c0695SLingrui98  def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = {
209c2ad24ebSLingrui98    // TODO: wrap inc for histPtr value
210dd6c0695SLingrui98    oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value))
211c2ad24ebSLingrui98  }
212c2ad24ebSLingrui98
213ab890bfeSLingrui98  def circular_shift_left(src: UInt, shamt: Int) = {
214c2ad24ebSLingrui98    val srcLen = src.getWidth
215c2ad24ebSLingrui98    val src_doubled = Cat(src, src)
216ab890bfeSLingrui98    val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt)
217ab890bfeSLingrui98    shifted
218c2ad24ebSLingrui98  }
219c2ad24ebSLingrui98
22067402d75SLingrui98  // slow path, read bits from ghr
221ab890bfeSLingrui98  def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = {
22267402d75SLingrui98    val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr))
22367402d75SLingrui98    update(oldest_bits, num, taken)
22467402d75SLingrui98  }
22567402d75SLingrui98
22667402d75SLingrui98
22767402d75SLingrui98  // fast path, use pre-read oldest bits
22867402d75SLingrui98  def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = {
229c2ad24ebSLingrui98    // do xors for several bitsets at specified bits
230c2ad24ebSLingrui98    def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = {
231c2ad24ebSLingrui98      val res = Wire(Vec(len, Bool()))
232c2ad24ebSLingrui98      // println(f"num bitsets: ${bitsets.length}")
233c2ad24ebSLingrui98      // println(f"bitsets $bitsets")
234c2ad24ebSLingrui98      val resArr = Array.fill(len)(List[Bool]())
235c2ad24ebSLingrui98      for (bs <- bitsets) {
236c2ad24ebSLingrui98        for ((n, b) <- bs) {
237c2ad24ebSLingrui98          resArr(n) = b :: resArr(n)
238c2ad24ebSLingrui98        }
239c2ad24ebSLingrui98      }
240c2ad24ebSLingrui98      // println(f"${resArr.mkString}")
241c2ad24ebSLingrui98      // println(f"histLen: ${this.len}, foldedLen: $folded_len")
242c2ad24ebSLingrui98      for (i <- 0 until len) {
243c2ad24ebSLingrui98        // println(f"bit[$i], ${resArr(i).mkString}")
244c2ad24ebSLingrui98        if (resArr(i).length > 2) {
245c2ad24ebSLingrui98          println(f"[warning] update logic of foldest history has two or more levels of xor gates! " +
24686d9c530SLingrui98            f"histlen:${this.len}, compLen:$compLen, at bit $i")
247c2ad24ebSLingrui98        }
248c2ad24ebSLingrui98        if (resArr(i).length == 0) {
249dd6c0695SLingrui98          println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen")
250c2ad24ebSLingrui98        }
251c2ad24ebSLingrui98        res(i) := resArr(i).foldLeft(false.B)(_^_)
252c2ad24ebSLingrui98      }
253c2ad24ebSLingrui98      res.asUInt
254c2ad24ebSLingrui98    }
255c2ad24ebSLingrui98
25667402d75SLingrui98    val new_folded_hist = if (need_oldest_bits) {
25767402d75SLingrui98      val oldest_bits = ob
25867402d75SLingrui98      require(oldest_bits.length == max_update_num)
259c2ad24ebSLingrui98      // mask off bits that do not update
260c2ad24ebSLingrui98      val oldest_bits_masked = oldest_bits.zipWithIndex.map{
261ab890bfeSLingrui98        case (ob, i) => ob && (i < num).B
262c2ad24ebSLingrui98      }
263c2ad24ebSLingrui98      // if a bit does not wrap around, it should not be xored when it exits
264c2ad24ebSLingrui98      val oldest_bits_set = (0 until max_update_num).filter(oldest_bit_wrap_around).map(i => (oldest_bit_pos_in_folded(i), oldest_bits_masked(i)))
265c2ad24ebSLingrui98
266c2ad24ebSLingrui98      // println(f"old bits pos ${oldest_bits_set.map(_._1)}")
267c2ad24ebSLingrui98
268c2ad24ebSLingrui98      // only the last bit could be 1, as we have at most one taken branch at a time
269ab890bfeSLingrui98      val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt
270c2ad24ebSLingrui98      // if a bit does not wrap around, newest bits should not be xored onto it either
271e992912cSLingrui98      val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i)))
272c2ad24ebSLingrui98
273c2ad24ebSLingrui98      // println(f"new bits set ${newest_bits_set.map(_._1)}")
274c2ad24ebSLingrui98      //
275c2ad24ebSLingrui98      val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{
276ab890bfeSLingrui98        case (fb, i) => fb && !(num >= (len-i)).B
277c2ad24ebSLingrui98      })
278c2ad24ebSLingrui98      val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i)))
279c2ad24ebSLingrui98
280c2ad24ebSLingrui98      // do xor then shift
281c2ad24ebSLingrui98      val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set))
282ab890bfeSLingrui98      circular_shift_left(xored, num)
28367402d75SLingrui98    } else {
28467402d75SLingrui98      // histLen too short to wrap around
28567402d75SLingrui98      ((folded_hist << num) | taken)(compLen-1,0)
286c2ad24ebSLingrui98    }
28767402d75SLingrui98
288c2ad24ebSLingrui98    val fh = WireInit(this)
289c2ad24ebSLingrui98    fh.folded_hist := new_folded_hist
290c2ad24ebSLingrui98    fh
291c2ad24ebSLingrui98  }
29209c6f1ddSLingrui98}
29309c6f1ddSLingrui98
29467402d75SLingrui98class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle {
29567402d75SLingrui98  val bits = Vec(max_update_num*2, Bool())
29667402d75SLingrui98  // def info = (len, compLen)
29767402d75SLingrui98  def getRealOb(brNumOH: UInt): Vec[Bool] = {
29867402d75SLingrui98    val ob = Wire(Vec(max_update_num, Bool()))
29967402d75SLingrui98    for (i <- 0 until max_update_num) {
30067402d75SLingrui98      ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1))
30167402d75SLingrui98    }
30267402d75SLingrui98    ob
30367402d75SLingrui98  }
30467402d75SLingrui98}
30567402d75SLingrui98
30667402d75SLingrui98class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst {
30767402d75SLingrui98  val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1}
30867402d75SLingrui98    .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates
30967402d75SLingrui98  require(gen.toSet.toList.equals(gen))
31067402d75SLingrui98  def getObWithInfo(info: Tuple2[Int, Int]) = {
31167402d75SLingrui98    val selected = afhob.filter(_.len == info._1)
31267402d75SLingrui98    require(selected.length == 1)
31367402d75SLingrui98    selected(0)
31467402d75SLingrui98  }
31567402d75SLingrui98  def read(ghv: Vec[Bool], ptr: CGHPtr) = {
31667402d75SLingrui98    val hisLens = afhob.map(_.len)
31767402d75SLingrui98    val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates
31867402d75SLingrui98    val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value)))
31967402d75SLingrui98    for (ob <- afhob) {
32067402d75SLingrui98      for (i <- 0 until numBr*2) {
32167402d75SLingrui98        val pos = ob.len - i - 1
32267402d75SLingrui98        val bit_found = bitsWithInfo.filter(_._1 == pos).toList
32367402d75SLingrui98        require(bit_found.length == 1)
32467402d75SLingrui98        ob.bits(i) := bit_found(0)._2
32567402d75SLingrui98      }
32667402d75SLingrui98    }
32767402d75SLingrui98  }
32867402d75SLingrui98}
32967402d75SLingrui98
33067402d75SLingrui98class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst {
33167402d75SLingrui98  val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)})
33267402d75SLingrui98  // println(gen.mkString)
33367402d75SLingrui98  require(gen.toSet.toList.equals(gen))
33467402d75SLingrui98  def getHistWithInfo(info: Tuple2[Int, Int]) = {
33567402d75SLingrui98    val selected = hist.filter(_.info.equals(info))
33667402d75SLingrui98    require(selected.length == 1)
33767402d75SLingrui98    selected(0)
33867402d75SLingrui98  }
33967402d75SLingrui98  def autoConnectFrom(that: AllFoldedHistories) = {
34067402d75SLingrui98    require(this.hist.length <= that.hist.length)
34167402d75SLingrui98    for (h <- this.hist) {
34267402d75SLingrui98      h := that.getHistWithInfo(h.info)
34367402d75SLingrui98    }
34467402d75SLingrui98  }
34567402d75SLingrui98  def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = {
34667402d75SLingrui98    val res = WireInit(this)
34767402d75SLingrui98    for (i <- 0 until this.hist.length) {
34867402d75SLingrui98      res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken)
34967402d75SLingrui98    }
35067402d75SLingrui98    res
35167402d75SLingrui98  }
35267402d75SLingrui98  def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = {
35367402d75SLingrui98    val res = WireInit(this)
35467402d75SLingrui98    for (i <- 0 until this.hist.length) {
35567402d75SLingrui98      val fh = this.hist(i)
35667402d75SLingrui98      if (fh.need_oldest_bits) {
35767402d75SLingrui98        val info = fh.info
35867402d75SLingrui98        val selectedAfhob = afhob.getObWithInfo(info)
35967402d75SLingrui98        val ob = selectedAfhob.getRealOb(lastBrNumOH)
36067402d75SLingrui98        res.hist(i) := this.hist(i).update(ob, shift, taken)
36167402d75SLingrui98      } else {
36267402d75SLingrui98        val dumb = Wire(Vec(numBr, Bool())) // not needed
36367402d75SLingrui98        dumb := DontCare
36467402d75SLingrui98        res.hist(i) := this.hist(i).update(dumb, shift, taken)
36567402d75SLingrui98      }
36667402d75SLingrui98    }
36767402d75SLingrui98    res
36867402d75SLingrui98  }
36967402d75SLingrui98
37067402d75SLingrui98  def display(cond: Bool) = {
37167402d75SLingrui98    for (h <- hist) {
37267402d75SLingrui98      XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n")
37367402d75SLingrui98    }
37467402d75SLingrui98  }
37567402d75SLingrui98}
37667402d75SLingrui98
37709c6f1ddSLingrui98class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{
37809c6f1ddSLingrui98  def tagBits = VAddrBits - idxBits - instOffsetBits
37909c6f1ddSLingrui98
38009c6f1ddSLingrui98  val tag = UInt(tagBits.W)
38109c6f1ddSLingrui98  val idx = UInt(idxBits.W)
38209c6f1ddSLingrui98  val offset = UInt(instOffsetBits.W)
38309c6f1ddSLingrui98
38409c6f1ddSLingrui98  def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this)
38509c6f1ddSLingrui98  def getTag(x: UInt) = fromUInt(x).tag
38609c6f1ddSLingrui98  def getIdx(x: UInt) = fromUInt(x).idx
38709c6f1ddSLingrui98  def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U
38809c6f1ddSLingrui98  def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x)
38909c6f1ddSLingrui98}
390eeb5ff92SLingrui98
391b37e4b45SLingrui98trait BasicPrediction extends HasXSParameter {
392b37e4b45SLingrui98  def cfiIndex: ValidUndirectioned[UInt]
393b37e4b45SLingrui98  def target(pc: UInt): UInt
394b37e4b45SLingrui98  def lastBrPosOH: Vec[Bool]
395b37e4b45SLingrui98  def brTaken: Bool
396b37e4b45SLingrui98  def shouldShiftVec: Vec[Bool]
397b37e4b45SLingrui98  def fallThruError: Bool
398b37e4b45SLingrui98}
399b37e4b45SLingrui98class MinimalBranchPrediction(implicit p: Parameters) extends NewMicroBTBEntry with BasicPrediction {
400b37e4b45SLingrui98  val valid = Bool()
401b37e4b45SLingrui98  def cfiIndex = {
402b37e4b45SLingrui98    val res = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
403b37e4b45SLingrui98    res.valid := taken && valid
404b37e4b45SLingrui98    res.bits := cfiOffset | Fill(res.bits.getWidth, !valid)
405b37e4b45SLingrui98    res
406b37e4b45SLingrui98  }
407b37e4b45SLingrui98  def target(pc: UInt) = nextAddr
408b37e4b45SLingrui98  def lastBrPosOH: Vec[Bool] = VecInit(brNumOH.asBools())
409b37e4b45SLingrui98  def brTaken = takenOnBr
410b37e4b45SLingrui98  def shouldShiftVec: Vec[Bool] = VecInit((0 until numBr).map(i => lastBrPosOH.drop(i+1).reduce(_||_)))
411a60a2901SLingrui98  def fallThruError: Bool = false.B // we do this check on the following stages
412b37e4b45SLingrui98
413b37e4b45SLingrui98  def fromMicroBTBEntry(valid: Bool, entry: NewMicroBTBEntry, pc: UInt) = {
414b37e4b45SLingrui98    this.valid := valid
415b37e4b45SLingrui98    this.nextAddr := Mux(valid, entry.nextAddr, pc + (FetchWidth*4).U)
416b37e4b45SLingrui98    this.cfiOffset := entry.cfiOffset | Fill(cfiOffset.getWidth, !valid)
417b37e4b45SLingrui98    this.taken := entry.taken && valid
418b37e4b45SLingrui98    this.takenOnBr := entry.takenOnBr && valid
419bf6aaf09SLingrui98    this.brNumOH := Mux(valid, entry.brNumOH, 1.U((numBr+1).W))
420b37e4b45SLingrui98  }
421b37e4b45SLingrui98}
422eeb5ff92SLingrui98@chiselName
423b37e4b45SLingrui98class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction {
424eeb5ff92SLingrui98  val br_taken_mask = Vec(numBr, Bool())
42509c6f1ddSLingrui98
426eeb5ff92SLingrui98  val slot_valids = Vec(totalSlot, Bool())
42709c6f1ddSLingrui98
428eeb5ff92SLingrui98  val targets = Vec(totalSlot, UInt(VAddrBits.W))
429b30c10d6SLingrui98  val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors
430a229ab6cSLingrui98  val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W))
431a229ab6cSLingrui98  val fallThroughAddr = UInt(VAddrBits.W)
432b37e4b45SLingrui98  val fallThroughErr = Bool()
43309c6f1ddSLingrui98
43409c6f1ddSLingrui98  val is_jal = Bool()
43509c6f1ddSLingrui98  val is_jalr = Bool()
43609c6f1ddSLingrui98  val is_call = Bool()
43709c6f1ddSLingrui98  val is_ret = Bool()
438f4ebc4b2SLingrui98  val last_may_be_rvi_call = Bool()
439eeb5ff92SLingrui98  val is_br_sharing = Bool()
44009c6f1ddSLingrui98
44109c6f1ddSLingrui98  // val call_is_rvc = Bool()
44209c6f1ddSLingrui98  val hit = Bool()
44309c6f1ddSLingrui98
444eeb5ff92SLingrui98  def br_slot_valids = slot_valids.init
445eeb5ff92SLingrui98  def tail_slot_valid = slot_valids.last
446eeb5ff92SLingrui98
447eeb5ff92SLingrui98  def br_valids = {
448b37e4b45SLingrui98    VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing))
449eeb5ff92SLingrui98  }
450eeb5ff92SLingrui98
451eeb5ff92SLingrui98  def taken_mask_on_slot = {
452eeb5ff92SLingrui98    VecInit(
453eeb5ff92SLingrui98      (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ (
454b30c10d6SLingrui98        tail_slot_valid && (
455b30c10d6SLingrui98          is_br_sharing && br_taken_mask.last || !is_br_sharing
456b30c10d6SLingrui98        )
457eeb5ff92SLingrui98      )
458eeb5ff92SLingrui98    )
459eeb5ff92SLingrui98  }
460eeb5ff92SLingrui98
461b37e4b45SLingrui98  def real_slot_taken_mask(): Vec[Bool] = {
462b37e4b45SLingrui98    VecInit(taken_mask_on_slot.map(_ && hit))
463b37e4b45SLingrui98  }
464b37e4b45SLingrui98
465b37e4b45SLingrui98  // len numBr
466b37e4b45SLingrui98  def real_br_taken_mask(): Vec[Bool] = {
467b37e4b45SLingrui98    VecInit(
468b37e4b45SLingrui98      taken_mask_on_slot.map(_ && hit).init :+
469b37e4b45SLingrui98      (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit)
470b37e4b45SLingrui98    )
471b37e4b45SLingrui98  }
472b37e4b45SLingrui98
473b37e4b45SLingrui98  // the vec indicating if ghr should shift on each branch
474b37e4b45SLingrui98  def shouldShiftVec =
475b37e4b45SLingrui98    VecInit(br_valids.zipWithIndex.map{ case (v, i) =>
476b37e4b45SLingrui98      v && !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B)})
477b37e4b45SLingrui98
478b37e4b45SLingrui98  def lastBrPosOH =
479b37e4b45SLingrui98    VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry
480b37e4b45SLingrui98      (0 until numBr).map(i =>
481b37e4b45SLingrui98        br_valids(i) &&
482b37e4b45SLingrui98        !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it
483b37e4b45SLingrui98        (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it
484b37e4b45SLingrui98        hit
485b37e4b45SLingrui98      )
486b37e4b45SLingrui98    )
487b37e4b45SLingrui98
48886d9c530SLingrui98  def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_)
489b37e4b45SLingrui98
490b37e4b45SLingrui98  def target(pc: UInt): UInt = {
491d3854a00SLingrui98    val targetVec = targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U)
492d3854a00SLingrui98    val tm = taken_mask_on_slot
493d3854a00SLingrui98    val selVecOH =
494d3854a00SLingrui98      tm.zipWithIndex.map{ case (t, i) => !tm.take(i).fold(false.B)(_||_) && t && hit} :+
495d3854a00SLingrui98      (!tm.asUInt.orR && hit) :+ !hit
496d3854a00SLingrui98    Mux1H(selVecOH, targetVec)
497b37e4b45SLingrui98  }
498b37e4b45SLingrui98
499b37e4b45SLingrui98  def fallThruError: Bool = hit && fallThroughErr
500b37e4b45SLingrui98
501b37e4b45SLingrui98  def hit_taken_on_jmp =
502b37e4b45SLingrui98    !real_slot_taken_mask().init.reduce(_||_) &&
503b37e4b45SLingrui98    real_slot_taken_mask().last && !is_br_sharing
504b37e4b45SLingrui98  def hit_taken_on_call = hit_taken_on_jmp && is_call
505b37e4b45SLingrui98  def hit_taken_on_ret  = hit_taken_on_jmp && is_ret
506b37e4b45SLingrui98  def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr
507b37e4b45SLingrui98
508b37e4b45SLingrui98  def cfiIndex = {
509b37e4b45SLingrui98    val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
510b37e4b45SLingrui98    cfiIndex.valid := real_slot_taken_mask().asUInt.orR
511b37e4b45SLingrui98    // when no takens, set cfiIndex to PredictWidth-1
512b37e4b45SLingrui98    cfiIndex.bits :=
513b37e4b45SLingrui98      ParallelPriorityMux(real_slot_taken_mask(), offsets) |
514b37e4b45SLingrui98      Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt)
515b37e4b45SLingrui98    cfiIndex
516b37e4b45SLingrui98  }
517b37e4b45SLingrui98
518eeb5ff92SLingrui98  def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr)
51909c6f1ddSLingrui98
520b30c10d6SLingrui98  def fromFtbEntry(entry: FTBEntry, pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = {
521eeb5ff92SLingrui98    slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid
522eeb5ff92SLingrui98    targets := entry.getTargetVec(pc)
523b30c10d6SLingrui98    jalr_target := targets.last
524a229ab6cSLingrui98    offsets := entry.getOffsetVec
525eeb5ff92SLingrui98    is_jal := entry.tailSlot.valid && entry.isJal
526eeb5ff92SLingrui98    is_jalr := entry.tailSlot.valid && entry.isJalr
527eeb5ff92SLingrui98    is_call := entry.tailSlot.valid && entry.isCall
528eeb5ff92SLingrui98    is_ret := entry.tailSlot.valid && entry.isRet
529f4ebc4b2SLingrui98    last_may_be_rvi_call := entry.last_may_be_rvi_call
530eeb5ff92SLingrui98    is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing
531a229ab6cSLingrui98
532a60a2901SLingrui98    val startLower        = Cat(0.U(1.W),    pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits))
533b37e4b45SLingrui98    val endLowerwithCarry = Cat(entry.carry, entry.pftAddr)
534a60a2901SLingrui98    fallThroughErr := startLower >= endLowerwithCarry
53586d9c530SLingrui98    fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc))
536a229ab6cSLingrui98  }
53709c6f1ddSLingrui98
53809c6f1ddSLingrui98  def display(cond: Bool): Unit = {
539eeb5ff92SLingrui98    XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n")
54009c6f1ddSLingrui98  }
54109c6f1ddSLingrui98}
54209c6f1ddSLingrui98
543bf358e08SLingrui98@chiselName
544b37e4b45SLingrui98class BranchPredictionBundle(implicit p: Parameters) extends XSBundle
545b37e4b45SLingrui98  with HasBPUConst with BPUUtils {
546b37e4b45SLingrui98  // def full_pred_info[T <: Data](x: T) = if (is_minimal) None else Some(x)
54709c6f1ddSLingrui98  val pc = UInt(VAddrBits.W)
54809c6f1ddSLingrui98
54909c6f1ddSLingrui98  val valid = Bool()
55009c6f1ddSLingrui98
55109c6f1ddSLingrui98  val hasRedirect = Bool()
55209c6f1ddSLingrui98  val ftq_idx = new FtqPtr
55309c6f1ddSLingrui98  // val hit = Bool()
554b37e4b45SLingrui98  val is_minimal = Bool()
555b37e4b45SLingrui98  val minimal_pred = new MinimalBranchPrediction
556b37e4b45SLingrui98  val full_pred = new FullBranchPrediction
557b37e4b45SLingrui98
55809c6f1ddSLingrui98
559dd6c0695SLingrui98  val folded_hist = new AllFoldedHistories(foldedGHistInfos)
56067402d75SLingrui98  val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)
56167402d75SLingrui98  val lastBrNumOH = UInt((numBr+1).W)
562c2ad24ebSLingrui98  val histPtr = new CGHPtr
56309c6f1ddSLingrui98  val rasSp = UInt(log2Ceil(RasSize).W)
56409c6f1ddSLingrui98  val rasTop = new RASEntry
565b37e4b45SLingrui98  // val specCnt = Vec(numBr, UInt(10.W))
56609c6f1ddSLingrui98  // val meta = UInt(MaxMetaLength.W)
56709c6f1ddSLingrui98
568b37e4b45SLingrui98  val ftb_entry = new FTBEntry()
56909c6f1ddSLingrui98
570b37e4b45SLingrui98  def target(pc: UInt) = Mux(is_minimal, minimal_pred.target(pc),     full_pred.target(pc))
571b37e4b45SLingrui98  def cfiIndex         = Mux(is_minimal, minimal_pred.cfiIndex,       full_pred.cfiIndex)
572b37e4b45SLingrui98  def lastBrPosOH      = Mux(is_minimal, minimal_pred.lastBrPosOH,    full_pred.lastBrPosOH)
573b37e4b45SLingrui98  def brTaken          = Mux(is_minimal, minimal_pred.brTaken,        full_pred.brTaken)
574b37e4b45SLingrui98  def shouldShiftVec   = Mux(is_minimal, minimal_pred.shouldShiftVec, full_pred.shouldShiftVec)
575b37e4b45SLingrui98  def fallThruError    = Mux(is_minimal, minimal_pred.fallThruError,  full_pred.fallThruError)
576eeb5ff92SLingrui98
577b37e4b45SLingrui98  def getTarget = target(pc)
578b37e4b45SLingrui98  def taken = cfiIndex.valid
57909c6f1ddSLingrui98
58009c6f1ddSLingrui98  def display(cond: Bool): Unit = {
58109c6f1ddSLingrui98    XSDebug(cond, p"[pc] ${Hexadecimal(pc)}\n")
582dd6c0695SLingrui98    folded_hist.display(cond)
583b37e4b45SLingrui98    full_pred.display(cond)
58409c6f1ddSLingrui98    ftb_entry.display(cond)
58509c6f1ddSLingrui98  }
58609c6f1ddSLingrui98}
58709c6f1ddSLingrui98
588bf358e08SLingrui98@chiselName
58909c6f1ddSLingrui98class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst {
59009c6f1ddSLingrui98  // val valids = Vec(3, Bool())
591b37e4b45SLingrui98  val s1 = new BranchPredictionBundle
592b37e4b45SLingrui98  val s2 = new BranchPredictionBundle
593cb4f77ceSLingrui98  val s3 = new BranchPredictionBundle
59409c6f1ddSLingrui98
595b37e4b45SLingrui98  def selectedResp ={
596b37e4b45SLingrui98    val res =
59709c6f1ddSLingrui98      PriorityMux(Seq(
598cb4f77ceSLingrui98        ((s3.valid && s3.hasRedirect) -> s3),
59909c6f1ddSLingrui98        ((s2.valid && s2.hasRedirect) -> s2),
60009c6f1ddSLingrui98        (s1.valid -> s1)
60109c6f1ddSLingrui98      ))
602b37e4b45SLingrui98    // println("is minimal: ", res.is_minimal)
603b37e4b45SLingrui98    res
604b37e4b45SLingrui98  }
60509c6f1ddSLingrui98  def selectedRespIdx =
60609c6f1ddSLingrui98    PriorityMux(Seq(
607cb4f77ceSLingrui98      ((s3.valid && s3.hasRedirect) -> BP_S3),
60809c6f1ddSLingrui98      ((s2.valid && s2.hasRedirect) -> BP_S2),
60909c6f1ddSLingrui98      (s1.valid -> BP_S1)
61009c6f1ddSLingrui98    ))
611cb4f77ceSLingrui98  def lastStage = s3
61209c6f1ddSLingrui98}
61309c6f1ddSLingrui98
61409c6f1ddSLingrui98class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp with HasBPUConst {
61509c6f1ddSLingrui98  val meta = UInt(MaxMetaLength.W)
61609c6f1ddSLingrui98}
61709c6f1ddSLingrui98
61809c6f1ddSLingrui98object BpuToFtqBundle {
61909c6f1ddSLingrui98  def apply(resp: BranchPredictionResp)(implicit p: Parameters): BpuToFtqBundle = {
62009c6f1ddSLingrui98    val e = Wire(new BpuToFtqBundle())
62109c6f1ddSLingrui98    e.s1 := resp.s1
62209c6f1ddSLingrui98    e.s2 := resp.s2
623cb4f77ceSLingrui98    e.s3 := resp.s3
62409c6f1ddSLingrui98
62509c6f1ddSLingrui98    e.meta := DontCare
62609c6f1ddSLingrui98    e
62709c6f1ddSLingrui98  }
62809c6f1ddSLingrui98}
62909c6f1ddSLingrui98
63009c6f1ddSLingrui98class BranchPredictionUpdate(implicit p: Parameters) extends BranchPredictionBundle with HasBPUConst {
63109c6f1ddSLingrui98  val mispred_mask = Vec(numBr+1, Bool())
632edc18578SLingrui98  val pred_hit = Bool()
63309c6f1ddSLingrui98  val false_hit = Bool()
63409c6f1ddSLingrui98  val new_br_insert_pos = Vec(numBr, Bool())
63509c6f1ddSLingrui98  val old_entry = Bool()
63609c6f1ddSLingrui98  val meta = UInt(MaxMetaLength.W)
637abdbe4b7SLingrui98  val full_target = UInt(VAddrBits.W)
638edc18578SLingrui98  val from_stage = UInt(2.W)
63986d9c530SLingrui98  val ghist = UInt(HistoryLength.W)
64009c6f1ddSLingrui98
64109c6f1ddSLingrui98  def fromFtqRedirectSram(entry: Ftq_Redirect_SRAMEntry) = {
642dd6c0695SLingrui98    folded_hist := entry.folded_hist
64367402d75SLingrui98    afhob := entry.afhob
64467402d75SLingrui98    lastBrNumOH := entry.lastBrNumOH
645c2ad24ebSLingrui98    histPtr := entry.histPtr
64609c6f1ddSLingrui98    rasSp := entry.rasSp
64709c6f1ddSLingrui98    rasTop := entry.rasEntry
64809c6f1ddSLingrui98    this
64909c6f1ddSLingrui98  }
65009c6f1ddSLingrui98
651c2ad24ebSLingrui98  override def display(cond: Bool) = {
65209c6f1ddSLingrui98    XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n")
65309c6f1ddSLingrui98    XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n")
65409c6f1ddSLingrui98    XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n")
65509c6f1ddSLingrui98    super.display(cond)
65609c6f1ddSLingrui98    XSDebug(cond, p"--------------------------------------------\n")
65709c6f1ddSLingrui98  }
65809c6f1ddSLingrui98}
65909c6f1ddSLingrui98
66009c6f1ddSLingrui98class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst {
66109c6f1ddSLingrui98  // override def toPrintable: Printable = {
66209c6f1ddSLingrui98  //   p"-----------BranchPredictionRedirect----------- " +
66309c6f1ddSLingrui98  //     p"-----------cfiUpdate----------- " +
66409c6f1ddSLingrui98  //     p"[pc] ${Hexadecimal(cfiUpdate.pc)} " +
66509c6f1ddSLingrui98  //     p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " +
66609c6f1ddSLingrui98  //     p"[target] ${Hexadecimal(cfiUpdate.target)} " +
66709c6f1ddSLingrui98  //     p"------------------------------- " +
6689aca92b9SYinan Xu  //     p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " +
66909c6f1ddSLingrui98  //     p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " +
67009c6f1ddSLingrui98  //     p"[ftqOffset] ${ftqOffset} " +
67109c6f1ddSLingrui98  //     p"[level] ${level}, [interrupt] ${interrupt} " +
67209c6f1ddSLingrui98  //     p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " +
67309c6f1ddSLingrui98  //     p"[stFtqOffset] ${stFtqOffset} " +
67409c6f1ddSLingrui98  //     p"\n"
67509c6f1ddSLingrui98
67609c6f1ddSLingrui98  // }
67709c6f1ddSLingrui98
67809c6f1ddSLingrui98  def display(cond: Bool): Unit = {
67909c6f1ddSLingrui98    XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n")
68009c6f1ddSLingrui98    XSDebug(cond, p"-----------cfiUpdate----------- \n")
68109c6f1ddSLingrui98    XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n")
682c2ad24ebSLingrui98    // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n")
68309c6f1ddSLingrui98    XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n")
68409c6f1ddSLingrui98    XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n")
68509c6f1ddSLingrui98    XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n")
68609c6f1ddSLingrui98    XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n")
68709c6f1ddSLingrui98    XSDebug(cond, p"------------------------------- \n")
6889aca92b9SYinan Xu    XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n")
68909c6f1ddSLingrui98    XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n")
69009c6f1ddSLingrui98    XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n")
69109c6f1ddSLingrui98    XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n")
69209c6f1ddSLingrui98    XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n")
69309c6f1ddSLingrui98    XSDebug(cond, p"---------------------------------------------- \n")
69409c6f1ddSLingrui98  }
69509c6f1ddSLingrui98}
696