xref: /XiangShan/src/main/scala/xiangshan/frontend/IBuffer.scala (revision cf7d6b7a1a781c73aeb87de112de2e7fe5ea3b7c)
144c9c1deSEaston Man/***************************************************************************************
244c9c1deSEaston Man* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
344c9c1deSEaston Man* Copyright (c) 2020-2021 Peng Cheng Laboratory
444c9c1deSEaston Man*
544c9c1deSEaston Man* XiangShan is licensed under Mulan PSL v2.
644c9c1deSEaston Man* You can use this software according to the terms and conditions of the Mulan PSL v2.
744c9c1deSEaston Man* You may obtain a copy of Mulan PSL v2 at:
844c9c1deSEaston Man*          http://license.coscl.org.cn/MulanPSL2
944c9c1deSEaston Man*
1044c9c1deSEaston Man* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
1144c9c1deSEaston Man* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1244c9c1deSEaston Man* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1344c9c1deSEaston Man*
1444c9c1deSEaston Man* See the Mulan PSL v2 for more details.
1544c9c1deSEaston Man***************************************************************************************/
1644c9c1deSEaston Man
1744c9c1deSEaston Manpackage xiangshan.frontend
1844c9c1deSEaston Man
1944c9c1deSEaston Manimport chisel3._
2044c9c1deSEaston Manimport chisel3.util._
21*cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters
2244c9c1deSEaston Manimport utility._
23*cf7d6b7aSMuziimport utils._
24*cf7d6b7aSMuziimport xiangshan._
2544c9c1deSEaston Manimport xiangshan.ExceptionNO._
2644c9c1deSEaston Man
27*cf7d6b7aSMuziclass IBufPtr(implicit p: Parameters) extends CircularQueuePtr[IBufPtr](p => p(XSCoreParamsKey).IBufSize) {}
2844c9c1deSEaston Man
29*cf7d6b7aSMuziclass IBufInBankPtr(implicit p: Parameters) extends CircularQueuePtr[IBufInBankPtr](p =>
30*cf7d6b7aSMuzi      p(XSCoreParamsKey).IBufSize / p(XSCoreParamsKey).IBufNBank
31*cf7d6b7aSMuzi    ) {}
3244c9c1deSEaston Man
33*cf7d6b7aSMuziclass IBufBankPtr(implicit p: Parameters) extends CircularQueuePtr[IBufBankPtr](p => p(XSCoreParamsKey).IBufNBank) {}
3444c9c1deSEaston Man
3544c9c1deSEaston Manclass IBufferIO(implicit p: Parameters) extends XSBundle {
3644c9c1deSEaston Man  val flush                = Input(Bool())
3744c9c1deSEaston Man  val ControlRedirect      = Input(Bool())
3844c9c1deSEaston Man  val ControlBTBMissBubble = Input(Bool())
3944c9c1deSEaston Man  val TAGEMissBubble       = Input(Bool())
4044c9c1deSEaston Man  val SCMissBubble         = Input(Bool())
4144c9c1deSEaston Man  val ITTAGEMissBubble     = Input(Bool())
4244c9c1deSEaston Man  val RASMissBubble        = Input(Bool())
4344c9c1deSEaston Man  val MemVioRedirect       = Input(Bool())
4444c9c1deSEaston Man  val in                   = Flipped(DecoupledIO(new FetchToIBuffer))
4544c9c1deSEaston Man  val out                  = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
4644c9c1deSEaston Man  val full                 = Output(Bool())
4705cc2a4eSXuan Hu  val decodeCanAccept      = Input(Bool())
4844c9c1deSEaston Man  val stallReason          = new StallReasonIO(DecodeWidth)
4944c9c1deSEaston Man}
5044c9c1deSEaston Man
5144c9c1deSEaston Manclass IBufEntry(implicit p: Parameters) extends XSBundle {
5244c9c1deSEaston Man  val inst                 = UInt(32.W)
5344c9c1deSEaston Man  val pc                   = UInt(VAddrBits.W)
5444c9c1deSEaston Man  val foldpc               = UInt(MemPredPCWidth.W)
5544c9c1deSEaston Man  val pd                   = new PreDecodeInfo
5644c9c1deSEaston Man  val pred_taken           = Bool()
5744c9c1deSEaston Man  val ftqPtr               = new FtqPtr
5844c9c1deSEaston Man  val ftqOffset            = UInt(log2Ceil(PredictWidth).W)
5992c61038SXuan Hu  val exceptionType        = IBufferExceptionType()
60c1b28b66STang Haojin  val exceptionFromBackend = Bool()
617e0f64b0SGuanghui Cheng  val triggered            = TriggerAction()
62948e8159SEaston Man  val isLastInFtqEntry     = Bool()
6344c9c1deSEaston Man
6444c9c1deSEaston Man  def fromFetch(fetch: FetchToIBuffer, i: Int): IBufEntry = {
6544c9c1deSEaston Man    inst       := fetch.instrs(i)
6644c9c1deSEaston Man    pc         := fetch.pc(i)
6744c9c1deSEaston Man    foldpc     := fetch.foldpc(i)
6844c9c1deSEaston Man    pd         := fetch.pd(i)
6944c9c1deSEaston Man    pred_taken := fetch.ftqOffset(i).valid
7044c9c1deSEaston Man    ftqPtr     := fetch.ftqPtr
7144c9c1deSEaston Man    ftqOffset  := fetch.ftqOffset(i).bits
7292c61038SXuan Hu    exceptionType := IBufferExceptionType.cvtFromFetchExcpAndCrossPageAndRVCII(
7392c61038SXuan Hu      fetch.exceptionType(i),
7492c61038SXuan Hu      fetch.crossPageIPFFix(i),
75*cf7d6b7aSMuzi      fetch.illegalInstr(i)
7692c61038SXuan Hu    )
77c1b28b66STang Haojin    exceptionFromBackend := fetch.exceptionFromBackend(i)
7844c9c1deSEaston Man    triggered            := fetch.triggered(i)
79948e8159SEaston Man    isLastInFtqEntry     := fetch.isLastInFtqEntry(i)
8044c9c1deSEaston Man    this
8144c9c1deSEaston Man  }
8244c9c1deSEaston Man
8344c9c1deSEaston Man  def toCtrlFlow: CtrlFlow = {
8444c9c1deSEaston Man    val cf = Wire(new CtrlFlow)
8544c9c1deSEaston Man    cf.instr                             := inst
8644c9c1deSEaston Man    cf.pc                                := pc
8744c9c1deSEaston Man    cf.foldpc                            := foldpc
8844c9c1deSEaston Man    cf.exceptionVec                      := 0.U.asTypeOf(ExceptionVec())
8992c61038SXuan Hu    cf.exceptionVec(instrPageFault)      := IBufferExceptionType.isPF(this.exceptionType)
9092c61038SXuan Hu    cf.exceptionVec(instrGuestPageFault) := IBufferExceptionType.isGPF(this.exceptionType)
9192c61038SXuan Hu    cf.exceptionVec(instrAccessFault)    := IBufferExceptionType.isAF(this.exceptionType)
9292c61038SXuan Hu    cf.exceptionVec(EX_II)               := IBufferExceptionType.isRVCII(this.exceptionType)
93c1b28b66STang Haojin    cf.exceptionFromBackend              := exceptionFromBackend
9444c9c1deSEaston Man    cf.trigger                           := triggered
9544c9c1deSEaston Man    cf.pd                                := pd
9644c9c1deSEaston Man    cf.pred_taken                        := pred_taken
9792c61038SXuan Hu    cf.crossPageIPFFix                   := IBufferExceptionType.isCrossPage(this.exceptionType)
9844c9c1deSEaston Man    cf.storeSetHit                       := DontCare
9944c9c1deSEaston Man    cf.waitForRobIdx                     := DontCare
10044c9c1deSEaston Man    cf.loadWaitBit                       := DontCare
10144c9c1deSEaston Man    cf.loadWaitStrict                    := DontCare
10244c9c1deSEaston Man    cf.ssid                              := DontCare
10344c9c1deSEaston Man    cf.ftqPtr                            := ftqPtr
10444c9c1deSEaston Man    cf.ftqOffset                         := ftqOffset
105948e8159SEaston Man    cf.isLastInFtqEntry                  := isLastInFtqEntry
10644c9c1deSEaston Man    cf
10744c9c1deSEaston Man  }
10892c61038SXuan Hu
10992c61038SXuan Hu  object IBufferExceptionType extends NamedUInt(3) {
11092c61038SXuan Hu    def None        = "b000".U
11192c61038SXuan Hu    def NonCrossPF  = "b001".U
11292c61038SXuan Hu    def NonCrossGPF = "b010".U
11392c61038SXuan Hu    def NonCrossAF  = "b011".U
11492c61038SXuan Hu    // illegal instruction
11592c61038SXuan Hu    def rvcII    = "b100".U
11692c61038SXuan Hu    def CrossPF  = "b101".U
11792c61038SXuan Hu    def CrossGPF = "b110".U
11892c61038SXuan Hu    def CrossAF  = "b111".U
11992c61038SXuan Hu
12092c61038SXuan Hu    def cvtFromFetchExcpAndCrossPageAndRVCII(fetchExcp: UInt, crossPage: Bool, rvcIll: Bool): UInt = {
12192c61038SXuan Hu      require(
12292c61038SXuan Hu        fetchExcp.getWidth == ExceptionType.width,
12392c61038SXuan Hu        s"The width(${fetchExcp.getWidth}) of fetchExcp should be equal to " +
12492c61038SXuan Hu          s"the width(${ExceptionType.width}) of frontend.ExceptionType."
12592c61038SXuan Hu      )
126*cf7d6b7aSMuzi      MuxCase(
127*cf7d6b7aSMuzi        0.U,
128*cf7d6b7aSMuzi        Seq(
12992c61038SXuan Hu          crossPage     -> Cat(1.U(1.W), fetchExcp),
130f5b900a2SXuan Hu          fetchExcp.orR -> fetchExcp,
131*cf7d6b7aSMuzi          rvcIll        -> this.rvcII
132*cf7d6b7aSMuzi        )
133*cf7d6b7aSMuzi      )
13492c61038SXuan Hu    }
13592c61038SXuan Hu
13692c61038SXuan Hu    def isRVCII(uint: UInt): Bool = {
13792c61038SXuan Hu      this.checkInputWidth(uint)
13892c61038SXuan Hu      uint(2) && uint(1, 0) === 0.U
13992c61038SXuan Hu    }
14092c61038SXuan Hu
14192c61038SXuan Hu    def isCrossPage(uint: UInt): Bool = {
14292c61038SXuan Hu      this.checkInputWidth(uint)
14392c61038SXuan Hu      uint(2) && uint(1, 0) =/= 0.U
14492c61038SXuan Hu    }
14592c61038SXuan Hu
14692c61038SXuan Hu    def isPF(uint:  UInt): Bool = uint(1, 0) === this.NonCrossPF(1, 0)
14792c61038SXuan Hu    def isGPF(uint: UInt): Bool = uint(1, 0) === this.NonCrossGPF(1, 0)
14892c61038SXuan Hu    def isAF(uint:  UInt): Bool = uint(1, 0) === this.NonCrossAF(1, 0)
14992c61038SXuan Hu  }
15044c9c1deSEaston Man}
15144c9c1deSEaston Man
15244c9c1deSEaston Manclass IBuffer(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper with HasPerfEvents {
15344c9c1deSEaston Man  val io = IO(new IBufferIO)
15444c9c1deSEaston Man
15505cc2a4eSXuan Hu  // io alias
15605cc2a4eSXuan Hu  private val decodeCanAccept = io.decodeCanAccept
15705cc2a4eSXuan Hu
15844c9c1deSEaston Man  // Parameter Check
15944c9c1deSEaston Man  private val bankSize = IBufSize / IBufNBank
16044c9c1deSEaston Man  require(IBufSize % IBufNBank == 0, s"IBufNBank should divide IBufSize, IBufNBank: $IBufNBank, IBufSize: $IBufSize")
161*cf7d6b7aSMuzi  require(
162*cf7d6b7aSMuzi    IBufNBank >= DecodeWidth,
163*cf7d6b7aSMuzi    s"IBufNBank should be equal or larger than DecodeWidth, IBufNBank: $IBufNBank, DecodeWidth: $DecodeWidth"
164*cf7d6b7aSMuzi  )
16544c9c1deSEaston Man
16644c9c1deSEaston Man  // IBuffer is organized as raw registers
16744c9c1deSEaston Man  // This is due to IBuffer is a huge queue, read & write port logic should be precisely controlled
16844c9c1deSEaston Man  //                             . + + E E E - .
16944c9c1deSEaston Man  //                             . + + E E E - .
17044c9c1deSEaston Man  //                             . . + E E E - .
17144c9c1deSEaston Man  //                             . . + E E E E -
17244c9c1deSEaston Man  // As shown above, + means enqueue, - means dequeue, E is current content
17344c9c1deSEaston Man  // When dequeue, read port is organized like a banked FIFO
17444c9c1deSEaston Man  // Dequeue reads no more than 1 entry from each bank sequentially, this can be exploit to reduce area
17544c9c1deSEaston Man  // Enqueue writes cannot benefit from this characteristic unless use a SRAM
17644c9c1deSEaston Man  // For detail see Enqueue and Dequeue below
17744c9c1deSEaston Man  private val ibuf: Vec[IBufEntry] = RegInit(VecInit.fill(IBufSize)(0.U.asTypeOf(new IBufEntry)))
178*cf7d6b7aSMuzi  private val bankedIBufView: Vec[Vec[IBufEntry]] = VecInit.tabulate(IBufNBank)(bankID =>
179*cf7d6b7aSMuzi    VecInit.tabulate(bankSize)(inBankOffset => ibuf(bankID + inBankOffset * IBufNBank))
18044c9c1deSEaston Man  )
1818fae59bbSEaston Man
1828fae59bbSEaston Man  // Bypass wire
1838fae59bbSEaston Man  private val bypassEntries = WireDefault(VecInit.fill(DecodeWidth)(0.U.asTypeOf(Valid(new IBufEntry))))
1848fae59bbSEaston Man  // Normal read wire
1858fae59bbSEaston Man  private val deqEntries = WireDefault(VecInit.fill(DecodeWidth)(0.U.asTypeOf(Valid(new IBufEntry))))
1868fae59bbSEaston Man  // Output register
1878fae59bbSEaston Man  private val outputEntries = RegInit(VecInit.fill(DecodeWidth)(0.U.asTypeOf(Valid(new IBufEntry))))
188*cf7d6b7aSMuzi  private val outputEntriesValidNum =
189*cf7d6b7aSMuzi    PriorityMuxDefault(outputEntries.map(_.valid).zip(Seq.range(1, DecodeWidth).map(_.U)).reverse.toSeq, 0.U)
1908fae59bbSEaston Man
19144c9c1deSEaston Man  // Between Bank
19244c9c1deSEaston Man  private val deqBankPtrVec: Vec[IBufBankPtr] = RegInit(VecInit.tabulate(DecodeWidth)(_.U.asTypeOf(new IBufBankPtr)))
19344c9c1deSEaston Man  private val deqBankPtr:    IBufBankPtr      = deqBankPtrVec(0)
19405cc2a4eSXuan Hu  private val deqBankPtrVecNext = Wire(deqBankPtrVec.cloneType)
19544c9c1deSEaston Man  // Inside Bank
19644c9c1deSEaston Man  private val deqInBankPtr: Vec[IBufInBankPtr] = RegInit(VecInit.fill(IBufNBank)(0.U.asTypeOf(new IBufInBankPtr)))
19705cc2a4eSXuan Hu  private val deqInBankPtrNext = Wire(deqInBankPtr.cloneType)
19844c9c1deSEaston Man
19944c9c1deSEaston Man  val deqPtr     = RegInit(0.U.asTypeOf(new IBufPtr))
20005cc2a4eSXuan Hu  val deqPtrNext = Wire(deqPtr.cloneType)
20144c9c1deSEaston Man
20244c9c1deSEaston Man  val enqPtrVec = RegInit(VecInit.tabulate(PredictWidth)(_.U.asTypeOf(new IBufPtr)))
20344c9c1deSEaston Man  val enqPtr    = enqPtrVec(0)
20444c9c1deSEaston Man
2058fae59bbSEaston Man  val numTryEnq = WireDefault(0.U)
2068fae59bbSEaston Man  val numEnq    = Mux(io.in.fire, numTryEnq, 0.U)
2078fae59bbSEaston Man
2088506cfc0Sxiaofeibao  // empty and decode can accept insts
2098506cfc0Sxiaofeibao  val useBypass = enqPtr === deqPtr && decodeCanAccept
21005cc2a4eSXuan Hu
21105cc2a4eSXuan Hu  // The number of decode accepted insts.
21205cc2a4eSXuan Hu  // Since decode promises accepting insts in order, use priority encoder to simplify the accumulation.
213a5546049Sxiaofeibao  private val numOut = Wire(UInt(log2Ceil(DecodeWidth).W))
214a5546049Sxiaofeibao  private val numDeq = numOut
21505cc2a4eSXuan Hu
21605cc2a4eSXuan Hu  // counter current number of valid
21705cc2a4eSXuan Hu  val numValid         = distanceBetween(enqPtr, deqPtr)
21805cc2a4eSXuan Hu  val numValidAfterDeq = numValid - numDeq
21905cc2a4eSXuan Hu  // counter next number of valid
22005cc2a4eSXuan Hu  val numValidNext = numValid + numEnq - numDeq
22105cc2a4eSXuan Hu  val allowEnq     = RegInit(true.B)
22205cc2a4eSXuan Hu  val numFromFetch = Mux(io.in.valid, PopCount(io.in.bits.enqEnable), 0.U)
22305cc2a4eSXuan Hu
22405cc2a4eSXuan Hu  allowEnq := (IBufSize - PredictWidth).U >= numValidNext // Disable when almost full
22544c9c1deSEaston Man
2268fae59bbSEaston Man  val enqOffset = VecInit.tabulate(PredictWidth)(i => PopCount(io.in.bits.valid.asBools.take(i)))
2278fae59bbSEaston Man  val enqData   = VecInit.tabulate(PredictWidth)(i => Wire(new IBufEntry).fromFetch(io.in.bits, i))
2288fae59bbSEaston Man
229a5546049Sxiaofeibao  val outputEntriesIsNotFull = !outputEntries(DecodeWidth - 1).valid
230a5546049Sxiaofeibao  when(decodeCanAccept) {
231a5546049Sxiaofeibao    numOut := Mux(numValid >= DecodeWidth.U, DecodeWidth.U, numValid)
232a5546049Sxiaofeibao  }.elsewhen(outputEntriesIsNotFull) {
233a5546049Sxiaofeibao    numOut := Mux(numValid >= DecodeWidth.U - outputEntriesValidNum, DecodeWidth.U - outputEntriesValidNum, numValid)
234a5546049Sxiaofeibao  }.otherwise {
235a5546049Sxiaofeibao    numOut := 0.U
236a5546049Sxiaofeibao  }
237a5546049Sxiaofeibao  val numBypass = Wire(UInt(log2Ceil(DecodeWidth).W))
2388fae59bbSEaston Man  // when using bypass, bypassed entries do not enqueue
2398fae59bbSEaston Man  when(useBypass) {
2408fae59bbSEaston Man    when(numFromFetch >= DecodeWidth.U) {
2418fae59bbSEaston Man      numTryEnq := numFromFetch - DecodeWidth.U
242a5546049Sxiaofeibao      numBypass := DecodeWidth.U
2438fae59bbSEaston Man    }.otherwise {
2448fae59bbSEaston Man      numTryEnq := 0.U
245a5546049Sxiaofeibao      numBypass := numFromFetch
2468fae59bbSEaston Man    }
2478fae59bbSEaston Man  }.otherwise {
2488fae59bbSEaston Man    numTryEnq := numFromFetch
249a5546049Sxiaofeibao    numBypass := 0.U
2508fae59bbSEaston Man  }
2518fae59bbSEaston Man
2528fae59bbSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2538fae59bbSEaston Man  // Bypass
2548fae59bbSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2558fae59bbSEaston Man  bypassEntries.zipWithIndex.foreach {
2568fae59bbSEaston Man    case (entry, idx) =>
2578fae59bbSEaston Man      // Select
2588fae59bbSEaston Man      val validOH = Range(0, PredictWidth).map {
2598fae59bbSEaston Man        i =>
2608fae59bbSEaston Man          io.in.bits.valid(i) &&
2618fae59bbSEaston Man          io.in.bits.enqEnable(i) &&
2628fae59bbSEaston Man          enqOffset(i) === idx.asUInt
2638fae59bbSEaston Man      } // Should be OneHot
2648fae59bbSEaston Man      entry.valid := validOH.reduce(_ || _) && io.in.fire && !io.flush
2658fae59bbSEaston Man      entry.bits  := Mux1H(validOH, enqData)
2668fae59bbSEaston Man
2678fae59bbSEaston Man      // Debug Assertion
2689afa8a47STang Haojin      XSError(io.in.valid && PopCount(validOH) > 1.asUInt, "validOH is not OneHot")
2698fae59bbSEaston Man  }
2708fae59bbSEaston Man
2718fae59bbSEaston Man  // => Decode Output
2728fae59bbSEaston Man  // clean register output
2738fae59bbSEaston Man  io.out zip outputEntries foreach {
2748fae59bbSEaston Man    case (io, reg) =>
2758fae59bbSEaston Man      io.valid := reg.valid
2768fae59bbSEaston Man      io.bits  := reg.bits.toCtrlFlow
2778fae59bbSEaston Man  }
278a5546049Sxiaofeibao  (outputEntries zip bypassEntries).zipWithIndex.foreach {
279a5546049Sxiaofeibao    case ((out, bypass), i) =>
28005cc2a4eSXuan Hu      when(decodeCanAccept) {
28105cc2a4eSXuan Hu        when(useBypass && io.in.valid) {
2828fae59bbSEaston Man          out := bypass
283e778bb8aSxiaofeibao-xjtu        }.otherwise {
284a5546049Sxiaofeibao          out := deqEntries(i)
2858fae59bbSEaston Man        }
286a5546049Sxiaofeibao      }.elsewhen(outputEntriesIsNotFull) {
287a5546049Sxiaofeibao        out.valid := deqEntries(i).valid
288*cf7d6b7aSMuzi        out.bits := Mux(
289*cf7d6b7aSMuzi          i.U < outputEntriesValidNum,
290*cf7d6b7aSMuzi          out.bits,
291*cf7d6b7aSMuzi          VecInit(deqEntries.take(i + 1).map(_.bits))(i.U - outputEntriesValidNum)
292*cf7d6b7aSMuzi        )
2938fae59bbSEaston Man      }
2948fae59bbSEaston Man  }
2958fae59bbSEaston Man
29644c9c1deSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29744c9c1deSEaston Man  // Enqueue
29844c9c1deSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29944c9c1deSEaston Man  io.in.ready := allowEnq
30044c9c1deSEaston Man  // Data
30144c9c1deSEaston Man  ibuf.zipWithIndex.foreach {
30244c9c1deSEaston Man    case (entry, idx) => {
30344c9c1deSEaston Man      // Select
30444c9c1deSEaston Man      val validOH = Range(0, PredictWidth).map {
3058fae59bbSEaston Man        i =>
3068fae59bbSEaston Man          val useBypassMatch = enqOffset(i) >= DecodeWidth.U &&
3078fae59bbSEaston Man            enqPtrVec(enqOffset(i) - DecodeWidth.U).value === idx.asUInt
3088fae59bbSEaston Man          val normalMatch = enqPtrVec(enqOffset(i)).value === idx.asUInt
3098fae59bbSEaston Man          val m = Mux(useBypass, useBypassMatch, normalMatch) // when using bypass, bypassed entries do not enqueue
3108fae59bbSEaston Man
3118fae59bbSEaston Man          io.in.bits.valid(i) && io.in.bits.enqEnable(i) && m
31244c9c1deSEaston Man      } // Should be OneHot
31344c9c1deSEaston Man      val wen = validOH.reduce(_ || _) && io.in.fire && !io.flush
31444c9c1deSEaston Man
31544c9c1deSEaston Man      // Write port
31644c9c1deSEaston Man      // Each IBuffer entry has a PredictWidth -> 1 Mux
31744c9c1deSEaston Man      val writeEntry = Mux1H(validOH, enqData)
31844c9c1deSEaston Man      entry := Mux(wen, writeEntry, entry)
31944c9c1deSEaston Man
32044c9c1deSEaston Man      // Debug Assertion
32178c76c74STang Haojin      XSError(io.in.valid && PopCount(validOH) > 1.asUInt, "validOH is not OneHot")
32244c9c1deSEaston Man    }
32344c9c1deSEaston Man  }
32444c9c1deSEaston Man  // Pointer maintenance
32544c9c1deSEaston Man  when(io.in.fire && !io.flush) {
3268fae59bbSEaston Man    enqPtrVec := VecInit(enqPtrVec.map(_ + numTryEnq))
32744c9c1deSEaston Man  }
32844c9c1deSEaston Man
32944c9c1deSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
33044c9c1deSEaston Man  // Dequeue
33144c9c1deSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
332a5546049Sxiaofeibao  val outputEntriesValidNumNext = Wire(UInt(log2Ceil(DecodeWidth).W))
333a5546049Sxiaofeibao  XSError(outputEntriesValidNumNext > DecodeWidth.U, "Ibuffer: outputEntriesValidNumNext > DecodeWidth.U")
334a5546049Sxiaofeibao  val validVec = UIntToMask(outputEntriesValidNumNext(log2Ceil(DecodeWidth) - 1, 0), DecodeWidth)
335a5546049Sxiaofeibao  when(decodeCanAccept) {
336a5546049Sxiaofeibao    outputEntriesValidNumNext := Mux(useBypass, numBypass, numDeq)
337a5546049Sxiaofeibao  }.elsewhen(outputEntriesIsNotFull) {
338a5546049Sxiaofeibao    outputEntriesValidNumNext := outputEntriesValidNum + numDeq
339a5546049Sxiaofeibao  }.otherwise {
340a5546049Sxiaofeibao    outputEntriesValidNumNext := outputEntriesValidNum
341a5546049Sxiaofeibao  }
34244c9c1deSEaston Man  // Data
34344c9c1deSEaston Man  // Read port
34444c9c1deSEaston Man  // 2-stage, IBufNBank * (bankSize -> 1) + IBufNBank -> 1
34544c9c1deSEaston Man  // Should be better than IBufSize -> 1 in area, with no significant latency increase
346*cf7d6b7aSMuzi  private val readStage1: Vec[IBufEntry] =
347*cf7d6b7aSMuzi    VecInit.tabulate(IBufNBank)(bankID => Mux1H(UIntToOH(deqInBankPtr(bankID).value), bankedIBufView(bankID)))
34844c9c1deSEaston Man  for (i <- 0 until DecodeWidth) {
3498fae59bbSEaston Man    deqEntries(i).valid := validVec(i)
350a5546049Sxiaofeibao    deqEntries(i).bits  := Mux1H(UIntToOH(deqBankPtrVec(i).value), readStage1)
35144c9c1deSEaston Man  }
35244c9c1deSEaston Man  // Pointer maintenance
353ac3c9508SXuan Hu  deqBankPtrVecNext := VecInit(deqBankPtrVec.map(_ + numDeq))
354ac3c9508SXuan Hu  deqPtrNext        := deqPtr + numDeq
35505cc2a4eSXuan Hu  deqInBankPtrNext.zip(deqInBankPtr).zipWithIndex.foreach {
35605cc2a4eSXuan Hu    case ((ptrNext, ptr), idx) => {
35744c9c1deSEaston Man      // validVec[k] == bankValid[deqBankPtr + k]
35844c9c1deSEaston Man      // So bankValid[n] == validVec[n - deqBankPtr]
359*cf7d6b7aSMuzi      val validIdx = Mux(
360*cf7d6b7aSMuzi        idx.asUInt >= deqBankPtr.value,
36144c9c1deSEaston Man        idx.asUInt - deqBankPtr.value,
36244c9c1deSEaston Man        ((idx + IBufNBank).asUInt - deqBankPtr.value)(log2Ceil(IBufNBank) - 1, 0)
36345b8fd86SEaston Man      )(log2Ceil(DecodeWidth) - 1, 0)
364a5546049Sxiaofeibao      val bankAdvance = numOut > validIdx
36505cc2a4eSXuan Hu      ptrNext := Mux(bankAdvance, ptr + 1.U, ptr)
36644c9c1deSEaston Man    }
36744c9c1deSEaston Man  }
36844c9c1deSEaston Man
36944c9c1deSEaston Man  // Flush
37044c9c1deSEaston Man  when(io.flush) {
37144c9c1deSEaston Man    allowEnq      := true.B
37244c9c1deSEaston Man    enqPtrVec     := enqPtrVec.indices.map(_.U.asTypeOf(new IBufPtr))
37344c9c1deSEaston Man    deqBankPtrVec := deqBankPtrVec.indices.map(_.U.asTypeOf(new IBufBankPtr))
37444c9c1deSEaston Man    deqInBankPtr  := VecInit.fill(IBufNBank)(0.U.asTypeOf(new IBufInBankPtr))
37544c9c1deSEaston Man    deqPtr        := 0.U.asTypeOf(new IBufPtr())
3768fae59bbSEaston Man    outputEntries.foreach(_.valid := false.B)
37705cc2a4eSXuan Hu  }.otherwise {
37805cc2a4eSXuan Hu    deqPtr        := deqPtrNext
37905cc2a4eSXuan Hu    deqInBankPtr  := deqInBankPtrNext
38005cc2a4eSXuan Hu    deqBankPtrVec := deqBankPtrVecNext
38144c9c1deSEaston Man  }
38244c9c1deSEaston Man  io.full := !allowEnq
38344c9c1deSEaston Man
38444c9c1deSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38544c9c1deSEaston Man  // TopDown
38644c9c1deSEaston Man  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38744c9c1deSEaston Man  val topdown_stage = RegInit(0.U.asTypeOf(new FrontendTopDownBundle))
38844c9c1deSEaston Man  topdown_stage := io.in.bits.topdown_info
38944c9c1deSEaston Man  when(io.flush) {
39044c9c1deSEaston Man    when(io.ControlRedirect) {
39144c9c1deSEaston Man      when(io.ControlBTBMissBubble) {
39244c9c1deSEaston Man        topdown_stage.reasons(TopDownCounters.BTBMissBubble.id) := true.B
39344c9c1deSEaston Man      }.elsewhen(io.TAGEMissBubble) {
39444c9c1deSEaston Man        topdown_stage.reasons(TopDownCounters.TAGEMissBubble.id) := true.B
39544c9c1deSEaston Man      }.elsewhen(io.SCMissBubble) {
39644c9c1deSEaston Man        topdown_stage.reasons(TopDownCounters.SCMissBubble.id) := true.B
39744c9c1deSEaston Man      }.elsewhen(io.ITTAGEMissBubble) {
39844c9c1deSEaston Man        topdown_stage.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B
39944c9c1deSEaston Man      }.elsewhen(io.RASMissBubble) {
40044c9c1deSEaston Man        topdown_stage.reasons(TopDownCounters.RASMissBubble.id) := true.B
40144c9c1deSEaston Man      }
40244c9c1deSEaston Man    }.elsewhen(io.MemVioRedirect) {
40344c9c1deSEaston Man      topdown_stage.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B
40444c9c1deSEaston Man    }.otherwise {
40544c9c1deSEaston Man      topdown_stage.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B
40644c9c1deSEaston Man    }
40744c9c1deSEaston Man  }
40844c9c1deSEaston Man
40944c9c1deSEaston Man  val matchBubble   = Wire(UInt(log2Up(TopDownCounters.NumStallReasons.id).W))
41044c9c1deSEaston Man  val deqValidCount = PopCount(validVec.asBools)
41144c9c1deSEaston Man  val deqWasteCount = DecodeWidth.U - deqValidCount
41244c9c1deSEaston Man  matchBubble := (TopDownCounters.NumStallReasons.id - 1).U - PriorityEncoder(topdown_stage.reasons.reverse)
41344c9c1deSEaston Man
41444c9c1deSEaston Man  io.stallReason.reason.map(_ := 0.U)
41544c9c1deSEaston Man  for (i <- 0 until DecodeWidth) {
41644c9c1deSEaston Man    when(i.U < deqWasteCount) {
41744c9c1deSEaston Man      io.stallReason.reason(DecodeWidth - i - 1) := matchBubble
41844c9c1deSEaston Man    }
41944c9c1deSEaston Man  }
42044c9c1deSEaston Man
42144c9c1deSEaston Man  when(!(deqWasteCount === DecodeWidth.U || topdown_stage.reasons.asUInt.orR)) {
42244c9c1deSEaston Man    // should set reason for FetchFragmentationStall
42344c9c1deSEaston Man    // topdown_stage.reasons(TopDownCounters.FetchFragmentationStall.id) := true.B
42444c9c1deSEaston Man    for (i <- 0 until DecodeWidth) {
42544c9c1deSEaston Man      when(i.U < deqWasteCount) {
42644c9c1deSEaston Man        io.stallReason.reason(DecodeWidth - i - 1) := TopDownCounters.FetchFragBubble.id.U
42744c9c1deSEaston Man      }
42844c9c1deSEaston Man    }
42944c9c1deSEaston Man  }
43044c9c1deSEaston Man
43144c9c1deSEaston Man  when(io.stallReason.backReason.valid) {
43244c9c1deSEaston Man    io.stallReason.reason.map(_ := io.stallReason.backReason.bits)
43344c9c1deSEaston Man  }
43444c9c1deSEaston Man
43544c9c1deSEaston Man  // Debug info
43644c9c1deSEaston Man  XSError(
43744c9c1deSEaston Man    deqPtr.value =/= deqBankPtr.value + deqInBankPtr(deqBankPtr.value).value * IBufNBank.asUInt,
43844c9c1deSEaston Man    "Dequeue PTR mismatch"
43944c9c1deSEaston Man  )
44005cc2a4eSXuan Hu  XSError(isBefore(enqPtr, deqPtr) && !isFull(enqPtr, deqPtr), "\ndeqPtr is older than enqPtr!\n")
44105cc2a4eSXuan Hu
44244c9c1deSEaston Man  XSDebug(io.flush, "IBuffer Flushed\n")
44344c9c1deSEaston Man
44444c9c1deSEaston Man  when(io.in.fire) {
44544c9c1deSEaston Man    XSDebug("Enque:\n")
44644c9c1deSEaston Man    XSDebug(p"MASK=${Binary(io.in.bits.valid)}\n")
44744c9c1deSEaston Man    for (i <- 0 until PredictWidth) {
44844c9c1deSEaston Man      XSDebug(p"PC=${Hexadecimal(io.in.bits.pc(i))} ${Hexadecimal(io.in.bits.instrs(i))}\n")
44944c9c1deSEaston Man    }
45044c9c1deSEaston Man  }
45144c9c1deSEaston Man
45244c9c1deSEaston Man  for (i <- 0 until DecodeWidth) {
453*cf7d6b7aSMuzi    XSDebug(
454*cf7d6b7aSMuzi      io.out(i).fire,
45544c9c1deSEaston Man      p"deq: ${Hexadecimal(io.out(i).bits.instr)} PC=${Hexadecimal(io.out(i).bits.pc)}" +
45644c9c1deSEaston Man        p"v=${io.out(i).valid} r=${io.out(i).ready} " +
457*cf7d6b7aSMuzi        p"excpVec=${Binary(io.out(i).bits.exceptionVec.asUInt)} crossPageIPF=${io.out(i).bits.crossPageIPFFix}\n"
458*cf7d6b7aSMuzi    )
45944c9c1deSEaston Man  }
46044c9c1deSEaston Man
46105cc2a4eSXuan Hu  XSDebug(p"numValid: ${numValid}\n")
46244c9c1deSEaston Man  XSDebug(p"EnqNum: ${numEnq}\n")
46344c9c1deSEaston Man  XSDebug(p"DeqNum: ${numDeq}\n")
46444c9c1deSEaston Man
46544c9c1deSEaston Man  val afterInit  = RegInit(false.B)
46644c9c1deSEaston Man  val headBubble = RegInit(false.B)
467*cf7d6b7aSMuzi  when(io.in.fire)(afterInit := true.B)
46844c9c1deSEaston Man  when(io.flush) {
46944c9c1deSEaston Man    headBubble := true.B
47005cc2a4eSXuan Hu  }.elsewhen(numValid =/= 0.U) {
47144c9c1deSEaston Man    headBubble := false.B
47244c9c1deSEaston Man  }
47305cc2a4eSXuan Hu  val instrHungry = afterInit && (numValid === 0.U) && !headBubble
47444c9c1deSEaston Man
47505cc2a4eSXuan Hu  QueuePerf(IBufSize, numValid, !allowEnq)
47644c9c1deSEaston Man  XSPerfAccumulate("flush", io.flush)
47744c9c1deSEaston Man  XSPerfAccumulate("hungry", instrHungry)
47844c9c1deSEaston Man
47905cc2a4eSXuan Hu  val ibuffer_IDWidth_hvButNotFull = afterInit && (numValid =/= 0.U) && (numValid < DecodeWidth.U) && !headBubble
48044c9c1deSEaston Man  XSPerfAccumulate("ibuffer_IDWidth_hvButNotFull", ibuffer_IDWidth_hvButNotFull)
48127d10d0cSEaston Man
48227d10d0cSEaston Man  val FrontBubble = Mux(decodeCanAccept, DecodeWidth.U - numOut, 0.U)
48344c9c1deSEaston Man
48444c9c1deSEaston Man  val perfEvents = Seq(
48544c9c1deSEaston Man    ("IBuffer_Flushed  ", io.flush),
48644c9c1deSEaston Man    ("IBuffer_hungry   ", instrHungry),
48705cc2a4eSXuan Hu    ("IBuffer_1_4_valid", (numValid > (0 * (IBufSize / 4)).U) & (numValid < (1 * (IBufSize / 4)).U)),
48805cc2a4eSXuan Hu    ("IBuffer_2_4_valid", (numValid >= (1 * (IBufSize / 4)).U) & (numValid < (2 * (IBufSize / 4)).U)),
48905cc2a4eSXuan Hu    ("IBuffer_3_4_valid", (numValid >= (2 * (IBufSize / 4)).U) & (numValid < (3 * (IBufSize / 4)).U)),
49005cc2a4eSXuan Hu    ("IBuffer_4_4_valid", (numValid >= (3 * (IBufSize / 4)).U) & (numValid < (4 * (IBufSize / 4)).U)),
49105cc2a4eSXuan Hu    ("IBuffer_full     ", numValid.andR),
49227d10d0cSEaston Man    ("Front_Bubble     ", FrontBubble)
49344c9c1deSEaston Man  )
49444c9c1deSEaston Man  generatePerfEvent()
49544c9c1deSEaston Man}
496