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