xref: /XiangShan/src/main/scala/xiangshan/frontend/FrontendBundle.scala (revision 50780602f4b92c9abaa470881d254139c155bace)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16package xiangshan.frontend
17
18import chipsalliance.rocketchip.config.Parameters
19import chisel3._
20import chisel3.util._
21import chisel3.experimental.chiselName
22import xiangshan._
23import xiangshan.frontend.icache._
24import utils._
25import scala.math._
26
27@chiselName
28class FetchRequestBundle(implicit p: Parameters) extends XSBundle with HasICacheParameters {
29
30  //fast path: Timing critical
31  val startAddr       = UInt(VAddrBits.W)
32  val nextlineStart   = UInt(VAddrBits.W)
33  val nextStartAddr   = UInt(VAddrBits.W)
34  //slow path
35  val ftqIdx          = new FtqPtr
36  val ftqOffset       = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))
37
38  def crossCacheline =  startAddr(blockOffBits - 1) === 1.U
39
40  def fromFtqPcBundle(b: Ftq_RF_Components) = {
41    this.startAddr := b.startAddr
42    this.nextlineStart := b.nextLineAddr
43    when (b.fallThruError) {
44      val nextBlockHigherTemp = Mux(startAddr(log2Ceil(PredictWidth)+instOffsetBits), b.startAddr, b.nextLineAddr)
45      val nextBlockHigher = nextBlockHigherTemp(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1)
46      this.nextStartAddr :=
47        Cat(nextBlockHigher,
48          startAddr(log2Ceil(PredictWidth)+instOffsetBits) ^ 1.U(1.W),
49          startAddr(log2Ceil(PredictWidth)+instOffsetBits-1, instOffsetBits),
50          0.U(instOffsetBits.W)
51        )
52    }
53    this
54  }
55  override def toPrintable: Printable = {
56    p"[start] ${Hexadecimal(startAddr)} [next] ${Hexadecimal(nextlineStart)}" +
57      p"[tgt] ${Hexadecimal(nextStartAddr)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" +
58      p" offset: ${ftqOffset.bits}\n"
59  }
60}
61
62class FtqICacheInfo(implicit p: Parameters)extends XSBundle with HasICacheParameters{
63  val startAddr           = UInt(VAddrBits.W)
64  val nextlineStart       = UInt(VAddrBits.W)
65  def crossCacheline =  startAddr(blockOffBits - 1) === 1.U
66}
67
68class IFUICacheIO(implicit p: Parameters)extends XSBundle with HasICacheParameters{
69  val icacheReady       = Output(Bool())
70  val resp              = Vec(PortNumber, ValidIO(new ICacheMainPipeResp))
71}
72
73class FtqToICacheRequestBundle(implicit p: Parameters)extends XSBundle with HasICacheParameters{
74  val pcMemRead           = new FtqICacheInfo
75  val bpuBypassWrite      = Vec(4, new FtqICacheInfo)
76  val bypassSelect        = Bool()
77  def fromFtqPcBundle(b: Ftq_RF_Components) = {
78    this.pcMemRead.startAddr := b.startAddr
79    this.pcMemRead.nextlineStart := b.nextLineAddr
80    this
81  }
82}
83
84
85class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle {
86  val pc           = Vec(PredictWidth, UInt(VAddrBits.W))
87  val pd           = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode
88  val ftqIdx       = new FtqPtr
89  val ftqOffset    = UInt(log2Ceil(PredictWidth).W)
90  val misOffset    = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))
91  val cfiOffset    = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))
92  val target       = UInt(VAddrBits.W)
93  val jalTarget    = UInt(VAddrBits.W)
94  val instrRange   = Vec(PredictWidth, Bool())
95}
96
97// Ftq send req to Prefetch
98class PrefetchRequest(implicit p:Parameters) extends XSBundle {
99  val target          = UInt(VAddrBits.W)
100}
101
102class FtqPrefechBundle(implicit p:Parameters) extends XSBundle {
103  val req = DecoupledIO(new PrefetchRequest)
104}
105
106class FetchToIBuffer(implicit p: Parameters) extends XSBundle {
107  val instrs    = Vec(PredictWidth, UInt(32.W))
108  val valid     = UInt(PredictWidth.W)
109  val enqEnable = UInt(PredictWidth.W)
110  val pd        = Vec(PredictWidth, new PreDecodeInfo)
111  val pc        = Vec(PredictWidth, UInt(VAddrBits.W))
112  val foldpc    = Vec(PredictWidth, UInt(MemPredPCWidth.W))
113  val ftqPtr       = new FtqPtr
114  val ftqOffset    = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
115  val ipf          = Vec(PredictWidth, Bool())
116  val acf          = Vec(PredictWidth, Bool())
117  val crossPageIPFFix = Vec(PredictWidth, Bool())
118  val triggered    = Vec(PredictWidth, new TriggerCf)
119}
120
121// class BitWiseUInt(val width: Int, val init: UInt) extends Module {
122//   val io = IO(new Bundle {
123//     val set
124//   })
125// }
126// Move from BPU
127abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst {
128  def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory
129}
130
131class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory {
132  val predHist = UInt(HistoryLength.W)
133
134  def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = {
135    val g = Wire(new ShiftingGlobalHistory)
136    g.predHist := (hist << shift) | taken
137    g
138  }
139
140  def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = {
141    require(br_valids.length == numBr)
142    require(real_taken_mask.length == numBr)
143    val last_valid_idx = PriorityMux(
144      br_valids.reverse :+ true.B,
145      (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W))
146    )
147    val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask)
148    val smaller = Mux(last_valid_idx < first_taken_idx,
149      last_valid_idx,
150      first_taken_idx
151    )
152    val shift = smaller
153    val taken = real_taken_mask.reduce(_||_)
154    update(shift, taken, this.predHist)
155  }
156
157  // static read
158  def read(n: Int): Bool = predHist.asBools()(n)
159
160  final def === (that: ShiftingGlobalHistory): Bool = {
161    predHist === that.predHist
162  }
163
164  final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that)
165}
166
167// circular global history pointer
168class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr](
169  p => p(XSCoreParamsKey).HistoryLength
170){
171}
172
173object CGHPtr {
174  def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = {
175    val ptr = Wire(new CGHPtr)
176    ptr.flag := f
177    ptr.value := v
178    ptr
179  }
180  def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = {
181    apply(!ptr.flag, ptr.value)
182  }
183}
184
185class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory {
186  val buffer = Vec(HistoryLength, Bool())
187  type HistPtr = UInt
188  def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = {
189    this
190  }
191}
192
193class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters)
194  extends XSBundle with HasBPUConst {
195  require(compLen >= 1)
196  require(len > 0)
197  // require(folded_len <= len)
198  require(compLen >= max_update_num)
199  val folded_hist = UInt(compLen.W)
200
201  def need_oldest_bits = len > compLen
202  def info = (len, compLen)
203  def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1)
204  def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen)
205  def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0)
206  def oldest_bit_start = oldest_bit_pos_in_folded.head
207
208  def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = {
209    // TODO: wrap inc for histPtr value
210    oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value))
211  }
212
213  def circular_shift_left(src: UInt, shamt: Int) = {
214    val srcLen = src.getWidth
215    val src_doubled = Cat(src, src)
216    val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt)
217    shifted
218  }
219
220  // slow path, read bits from ghr
221  def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = {
222    val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr))
223    update(oldest_bits, num, taken)
224  }
225
226
227  // fast path, use pre-read oldest bits
228  def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = {
229    // do xors for several bitsets at specified bits
230    def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = {
231      val res = Wire(Vec(len, Bool()))
232      // println(f"num bitsets: ${bitsets.length}")
233      // println(f"bitsets $bitsets")
234      val resArr = Array.fill(len)(List[Bool]())
235      for (bs <- bitsets) {
236        for ((n, b) <- bs) {
237          resArr(n) = b :: resArr(n)
238        }
239      }
240      // println(f"${resArr.mkString}")
241      // println(f"histLen: ${this.len}, foldedLen: $folded_len")
242      for (i <- 0 until len) {
243        // println(f"bit[$i], ${resArr(i).mkString}")
244        if (resArr(i).length > 2) {
245          println(f"[warning] update logic of foldest history has two or more levels of xor gates! " +
246            f"histlen:${this.len}, compLen:$compLen, at bit $i")
247        }
248        if (resArr(i).length == 0) {
249          println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen")
250        }
251        res(i) := resArr(i).foldLeft(false.B)(_^_)
252      }
253      res.asUInt
254    }
255
256    val new_folded_hist = if (need_oldest_bits) {
257      val oldest_bits = ob
258      require(oldest_bits.length == max_update_num)
259      // mask off bits that do not update
260      val oldest_bits_masked = oldest_bits.zipWithIndex.map{
261        case (ob, i) => ob && (i < num).B
262      }
263      // if a bit does not wrap around, it should not be xored when it exits
264      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)))
265
266      // println(f"old bits pos ${oldest_bits_set.map(_._1)}")
267
268      // only the last bit could be 1, as we have at most one taken branch at a time
269      val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt
270      // if a bit does not wrap around, newest bits should not be xored onto it either
271      val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i)))
272
273      // println(f"new bits set ${newest_bits_set.map(_._1)}")
274      //
275      val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{
276        case (fb, i) => fb && !(num >= (len-i)).B
277      })
278      val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i)))
279
280      // do xor then shift
281      val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set))
282      circular_shift_left(xored, num)
283    } else {
284      // histLen too short to wrap around
285      ((folded_hist << num) | taken)(compLen-1,0)
286    }
287
288    val fh = WireInit(this)
289    fh.folded_hist := new_folded_hist
290    fh
291  }
292}
293
294class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle {
295  val bits = Vec(max_update_num*2, Bool())
296  // def info = (len, compLen)
297  def getRealOb(brNumOH: UInt): Vec[Bool] = {
298    val ob = Wire(Vec(max_update_num, Bool()))
299    for (i <- 0 until max_update_num) {
300      ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1))
301    }
302    ob
303  }
304}
305
306class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst {
307  val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1}
308    .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates
309  require(gen.toSet.toList.equals(gen))
310  def getObWithInfo(info: Tuple2[Int, Int]) = {
311    val selected = afhob.filter(_.len == info._1)
312    require(selected.length == 1)
313    selected(0)
314  }
315  def read(ghv: Vec[Bool], ptr: CGHPtr) = {
316    val hisLens = afhob.map(_.len)
317    val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates
318    val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value)))
319    for (ob <- afhob) {
320      for (i <- 0 until numBr*2) {
321        val pos = ob.len - i - 1
322        val bit_found = bitsWithInfo.filter(_._1 == pos).toList
323        require(bit_found.length == 1)
324        ob.bits(i) := bit_found(0)._2
325      }
326    }
327  }
328}
329
330class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst {
331  val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)})
332  // println(gen.mkString)
333  require(gen.toSet.toList.equals(gen))
334  def getHistWithInfo(info: Tuple2[Int, Int]) = {
335    val selected = hist.filter(_.info.equals(info))
336    require(selected.length == 1)
337    selected(0)
338  }
339  def autoConnectFrom(that: AllFoldedHistories) = {
340    require(this.hist.length <= that.hist.length)
341    for (h <- this.hist) {
342      h := that.getHistWithInfo(h.info)
343    }
344  }
345  def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = {
346    val res = WireInit(this)
347    for (i <- 0 until this.hist.length) {
348      res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken)
349    }
350    res
351  }
352  def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = {
353    val res = WireInit(this)
354    for (i <- 0 until this.hist.length) {
355      val fh = this.hist(i)
356      if (fh.need_oldest_bits) {
357        val info = fh.info
358        val selectedAfhob = afhob.getObWithInfo(info)
359        val ob = selectedAfhob.getRealOb(lastBrNumOH)
360        res.hist(i) := this.hist(i).update(ob, shift, taken)
361      } else {
362        val dumb = Wire(Vec(numBr, Bool())) // not needed
363        dumb := DontCare
364        res.hist(i) := this.hist(i).update(dumb, shift, taken)
365      }
366    }
367    res
368  }
369
370  def display(cond: Bool) = {
371    for (h <- hist) {
372      XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n")
373    }
374  }
375}
376
377class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{
378  def tagBits = VAddrBits - idxBits - instOffsetBits
379
380  val tag = UInt(tagBits.W)
381  val idx = UInt(idxBits.W)
382  val offset = UInt(instOffsetBits.W)
383
384  def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this)
385  def getTag(x: UInt) = fromUInt(x).tag
386  def getIdx(x: UInt) = fromUInt(x).idx
387  def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U
388  def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x)
389}
390
391trait BasicPrediction extends HasXSParameter {
392  def cfiIndex: ValidUndirectioned[UInt]
393  def target(pc: UInt): UInt
394  def lastBrPosOH: Vec[Bool]
395  def brTaken: Bool
396  def shouldShiftVec: Vec[Bool]
397  def fallThruError: Bool
398}
399class MinimalBranchPrediction(implicit p: Parameters) extends NewMicroBTBEntry with BasicPrediction {
400  val valid = Bool()
401  def cfiIndex = {
402    val res = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
403    res.valid := taken && valid
404    res.bits := cfiOffset | Fill(res.bits.getWidth, !valid)
405    res
406  }
407  def target(pc: UInt) = nextAddr
408  def lastBrPosOH: Vec[Bool] = VecInit(brNumOH.asBools())
409  def brTaken = takenOnBr
410  def shouldShiftVec: Vec[Bool] = VecInit((0 until numBr).map(i => lastBrPosOH.drop(i+1).reduce(_||_)))
411  def fallThruError: Bool = false.B // we do this check on the following stages
412
413  def fromMicroBTBEntry(valid: Bool, entry: NewMicroBTBEntry, pc: UInt) = {
414    this.valid := valid
415    this.nextAddr := Mux(valid, entry.nextAddr, pc + (FetchWidth*4).U)
416    this.cfiOffset := entry.cfiOffset | Fill(cfiOffset.getWidth, !valid)
417    this.taken := entry.taken && valid
418    this.takenOnBr := entry.takenOnBr && valid
419    this.brNumOH := Mux(valid, entry.brNumOH, 1.U((numBr+1).W))
420  }
421}
422@chiselName
423class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction {
424  val br_taken_mask = Vec(numBr, Bool())
425
426  val slot_valids = Vec(totalSlot, Bool())
427
428  val targets = Vec(totalSlot, UInt(VAddrBits.W))
429  val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors
430  val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W))
431  val fallThroughAddr = UInt(VAddrBits.W)
432  val fallThroughErr = Bool()
433
434  val is_jal = Bool()
435  val is_jalr = Bool()
436  val is_call = Bool()
437  val is_ret = Bool()
438  val last_may_be_rvi_call = Bool()
439  val is_br_sharing = Bool()
440
441  // val call_is_rvc = Bool()
442  val hit = Bool()
443
444  def br_slot_valids = slot_valids.init
445  def tail_slot_valid = slot_valids.last
446
447  def br_valids = {
448    VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing))
449  }
450
451  def taken_mask_on_slot = {
452    VecInit(
453      (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ (
454        tail_slot_valid && (
455          is_br_sharing && br_taken_mask.last || !is_br_sharing
456        )
457      )
458    )
459  }
460
461  def real_slot_taken_mask(): Vec[Bool] = {
462    VecInit(taken_mask_on_slot.map(_ && hit))
463  }
464
465  // len numBr
466  def real_br_taken_mask(): Vec[Bool] = {
467    VecInit(
468      taken_mask_on_slot.map(_ && hit).init :+
469      (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit)
470    )
471  }
472
473  // the vec indicating if ghr should shift on each branch
474  def shouldShiftVec =
475    VecInit(br_valids.zipWithIndex.map{ case (v, i) =>
476      v && !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B)})
477
478  def lastBrPosOH =
479    VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry
480      (0 until numBr).map(i =>
481        br_valids(i) &&
482        !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it
483        (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it
484        hit
485      )
486    )
487
488  def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_)
489
490  def target(pc: UInt): UInt = {
491    val targetVec = targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U)
492    val tm = taken_mask_on_slot
493    val selVecOH =
494      tm.zipWithIndex.map{ case (t, i) => !tm.take(i).fold(false.B)(_||_) && t && hit} :+
495      (!tm.asUInt.orR && hit) :+ !hit
496    Mux1H(selVecOH, targetVec)
497  }
498
499  def fallThruError: Bool = hit && fallThroughErr
500
501  def hit_taken_on_jmp =
502    !real_slot_taken_mask().init.reduce(_||_) &&
503    real_slot_taken_mask().last && !is_br_sharing
504  def hit_taken_on_call = hit_taken_on_jmp && is_call
505  def hit_taken_on_ret  = hit_taken_on_jmp && is_ret
506  def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr
507
508  def cfiIndex = {
509    val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)))
510    cfiIndex.valid := real_slot_taken_mask().asUInt.orR
511    // when no takens, set cfiIndex to PredictWidth-1
512    cfiIndex.bits :=
513      ParallelPriorityMux(real_slot_taken_mask(), offsets) |
514      Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt)
515    cfiIndex
516  }
517
518  def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr)
519
520  def fromFtbEntry(entry: FTBEntry, pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = {
521    slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid
522    targets := entry.getTargetVec(pc)
523    jalr_target := targets.last
524    offsets := entry.getOffsetVec
525    is_jal := entry.tailSlot.valid && entry.isJal
526    is_jalr := entry.tailSlot.valid && entry.isJalr
527    is_call := entry.tailSlot.valid && entry.isCall
528    is_ret := entry.tailSlot.valid && entry.isRet
529    last_may_be_rvi_call := entry.last_may_be_rvi_call
530    is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing
531
532    val startLower        = Cat(0.U(1.W),    pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits))
533    val endLowerwithCarry = Cat(entry.carry, entry.pftAddr)
534    fallThroughErr := startLower >= endLowerwithCarry
535    fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc))
536  }
537
538  def display(cond: Bool): Unit = {
539    XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n")
540  }
541}
542
543@chiselName
544class BranchPredictionBundle(implicit p: Parameters) extends XSBundle
545  with HasBPUConst with BPUUtils {
546  // def full_pred_info[T <: Data](x: T) = if (is_minimal) None else Some(x)
547  val pc = UInt(VAddrBits.W)
548
549  val valid = Bool()
550
551  val hasRedirect = Bool()
552  val ftq_idx = new FtqPtr
553  // val hit = Bool()
554  val is_minimal = Bool()
555  val minimal_pred = new MinimalBranchPrediction
556  val full_pred = new FullBranchPrediction
557
558
559  val folded_hist = new AllFoldedHistories(foldedGHistInfos)
560  val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)
561  val lastBrNumOH = UInt((numBr+1).W)
562  val histPtr = new CGHPtr
563  val rasSp = UInt(log2Ceil(RasSize).W)
564  val rasTop = new RASEntry
565  // val specCnt = Vec(numBr, UInt(10.W))
566  // val meta = UInt(MaxMetaLength.W)
567
568  val ftb_entry = new FTBEntry()
569
570  def target(pc: UInt) = Mux(is_minimal, minimal_pred.target(pc),     full_pred.target(pc))
571  def cfiIndex         = Mux(is_minimal, minimal_pred.cfiIndex,       full_pred.cfiIndex)
572  def lastBrPosOH      = Mux(is_minimal, minimal_pred.lastBrPosOH,    full_pred.lastBrPosOH)
573  def brTaken          = Mux(is_minimal, minimal_pred.brTaken,        full_pred.brTaken)
574  def shouldShiftVec   = Mux(is_minimal, minimal_pred.shouldShiftVec, full_pred.shouldShiftVec)
575  def fallThruError    = Mux(is_minimal, minimal_pred.fallThruError,  full_pred.fallThruError)
576
577  def getTarget = target(pc)
578  def taken = cfiIndex.valid
579
580  def display(cond: Bool): Unit = {
581    XSDebug(cond, p"[pc] ${Hexadecimal(pc)}\n")
582    folded_hist.display(cond)
583    full_pred.display(cond)
584    ftb_entry.display(cond)
585  }
586}
587
588@chiselName
589class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst {
590  // val valids = Vec(3, Bool())
591  val s1 = new BranchPredictionBundle
592  val s2 = new BranchPredictionBundle
593  val s3 = new BranchPredictionBundle
594
595  def selectedResp ={
596    val res =
597      PriorityMux(Seq(
598        ((s3.valid && s3.hasRedirect) -> s3),
599        ((s2.valid && s2.hasRedirect) -> s2),
600        (s1.valid -> s1)
601      ))
602    // println("is minimal: ", res.is_minimal)
603    res
604  }
605  def selectedRespIdx =
606    PriorityMux(Seq(
607      ((s3.valid && s3.hasRedirect) -> BP_S3),
608      ((s2.valid && s2.hasRedirect) -> BP_S2),
609      (s1.valid -> BP_S1)
610    ))
611  def lastStage = s3
612}
613
614class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp with HasBPUConst {
615  val meta = UInt(MaxMetaLength.W)
616}
617
618object BpuToFtqBundle {
619  def apply(resp: BranchPredictionResp)(implicit p: Parameters): BpuToFtqBundle = {
620    val e = Wire(new BpuToFtqBundle())
621    e.s1 := resp.s1
622    e.s2 := resp.s2
623    e.s3 := resp.s3
624
625    e.meta := DontCare
626    e
627  }
628}
629
630class BranchPredictionUpdate(implicit p: Parameters) extends BranchPredictionBundle with HasBPUConst {
631  val mispred_mask = Vec(numBr+1, Bool())
632  val pred_hit = Bool()
633  val false_hit = Bool()
634  val new_br_insert_pos = Vec(numBr, Bool())
635  val old_entry = Bool()
636  val meta = UInt(MaxMetaLength.W)
637  val full_target = UInt(VAddrBits.W)
638  val from_stage = UInt(2.W)
639  val ghist = UInt(HistoryLength.W)
640
641  def fromFtqRedirectSram(entry: Ftq_Redirect_SRAMEntry) = {
642    folded_hist := entry.folded_hist
643    afhob := entry.afhob
644    lastBrNumOH := entry.lastBrNumOH
645    histPtr := entry.histPtr
646    rasSp := entry.rasSp
647    rasTop := entry.rasEntry
648    this
649  }
650
651  override def display(cond: Bool) = {
652    XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n")
653    XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n")
654    XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n")
655    super.display(cond)
656    XSDebug(cond, p"--------------------------------------------\n")
657  }
658}
659
660class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst {
661  // override def toPrintable: Printable = {
662  //   p"-----------BranchPredictionRedirect----------- " +
663  //     p"-----------cfiUpdate----------- " +
664  //     p"[pc] ${Hexadecimal(cfiUpdate.pc)} " +
665  //     p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " +
666  //     p"[target] ${Hexadecimal(cfiUpdate.target)} " +
667  //     p"------------------------------- " +
668  //     p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " +
669  //     p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " +
670  //     p"[ftqOffset] ${ftqOffset} " +
671  //     p"[level] ${level}, [interrupt] ${interrupt} " +
672  //     p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " +
673  //     p"[stFtqOffset] ${stFtqOffset} " +
674  //     p"\n"
675
676  // }
677
678  def display(cond: Bool): Unit = {
679    XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n")
680    XSDebug(cond, p"-----------cfiUpdate----------- \n")
681    XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n")
682    // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n")
683    XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n")
684    XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n")
685    XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n")
686    XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n")
687    XSDebug(cond, p"------------------------------- \n")
688    XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n")
689    XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n")
690    XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n")
691    XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n")
692    XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n")
693    XSDebug(cond, p"---------------------------------------------- \n")
694  }
695}
696