xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision a4f9c77fe0247e53cee5689dc0fe621024ac89ed)
109c6f1ddSLingrui98/***************************************************************************************
209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory
409c6f1ddSLingrui98*
509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2.
609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2.
709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at:
809c6f1ddSLingrui98*          http://license.coscl.org.cn/MulanPSL2
909c6f1ddSLingrui98*
1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1309c6f1ddSLingrui98*
1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details.
1509c6f1ddSLingrui98***************************************************************************************/
1609c6f1ddSLingrui98
1709c6f1ddSLingrui98package xiangshan.frontend
188891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
1909c6f1ddSLingrui98import chisel3._
2009c6f1ddSLingrui98import chisel3.util._
2109c6f1ddSLingrui98import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
226ab6918fSYinan Xuimport utils._
233c02ee8fSwakafaimport utility._
2409c6f1ddSLingrui98import xiangshan._
25ee175d78SJayimport xiangshan.backend.fu.{PFEvent, PMP, PMPChecker,PMPReqBundle}
26ee175d78SJayimport xiangshan.cache.mmu._
271d8f4dcbSJayimport xiangshan.frontend.icache._
2809c6f1ddSLingrui98
2909c6f1ddSLingrui98
3009c6f1ddSLingrui98class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter {
3195e60e55STang Haojin  override def shouldBeInlined: Boolean = false
3209c6f1ddSLingrui98
3309c6f1ddSLingrui98  val instrUncache  = LazyModule(new InstrUncache())
3409c6f1ddSLingrui98  val icache        = LazyModule(new ICache())
3509c6f1ddSLingrui98
3609c6f1ddSLingrui98  lazy val module = new FrontendImp(this)
3709c6f1ddSLingrui98}
3809c6f1ddSLingrui98
3909c6f1ddSLingrui98
4009c6f1ddSLingrui98class FrontendImp (outer: Frontend) extends LazyModuleImp(outer)
4109c6f1ddSLingrui98  with HasXSParameter
421ca0e4f3SYinan Xu  with HasPerfEvents
4309c6f1ddSLingrui98{
4409c6f1ddSLingrui98  val io = IO(new Bundle() {
45f57f7f2aSYangyu Chen    val hartId = Input(UInt(hartIdLen.W))
46c4b44470SGuokai Chen    val reset_vector = Input(UInt(PAddrBits.W))
4709c6f1ddSLingrui98    val fencei = Input(Bool())
481a718038SHaoyuan Feng    val ptw = new TlbPtwIO()
4909c6f1ddSLingrui98    val backend = new FrontendToCtrlIO
5009c6f1ddSLingrui98    val sfence = Input(new SfenceBundle)
5109c6f1ddSLingrui98    val tlbCsr = Input(new TlbCsrBundle)
5209c6f1ddSLingrui98    val csrCtrl = Input(new CustomCSRCtrlIO)
53e19f7967SWilliam Wang    val csrUpdate = new DistributedCSRUpdateReq
5409c6f1ddSLingrui98    val error  = new L1CacheErrorInfo
5509c6f1ddSLingrui98    val frontendInfo = new Bundle {
5609c6f1ddSLingrui98      val ibufFull  = Output(Bool())
5709c6f1ddSLingrui98      val bpuInfo = new Bundle {
5809c6f1ddSLingrui98        val bpRight = Output(UInt(XLEN.W))
5909c6f1ddSLingrui98        val bpWrong = Output(UInt(XLEN.W))
6009c6f1ddSLingrui98      }
6109c6f1ddSLingrui98    }
6260ebee38STang Haojin    val debugTopDown = new Bundle {
6360ebee38STang Haojin      val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W)))
6460ebee38STang Haojin    }
6509c6f1ddSLingrui98  })
6609c6f1ddSLingrui98
6709c6f1ddSLingrui98  //decouped-frontend modules
681d8f4dcbSJay  val instrUncache = outer.instrUncache.module
691d8f4dcbSJay  val icache       = outer.icache.module
7009c6f1ddSLingrui98  val bpu     = Module(new Predictor)
7109c6f1ddSLingrui98  val ifu     = Module(new NewIFU)
7244c9c1deSEaston Man  val ibuffer =  Module(new IBuffer)
7309c6f1ddSLingrui98  val ftq = Module(new Ftq)
7409c6f1ddSLingrui98
75f1fe8698SLemover  val needFlush = RegNext(io.backend.toFtq.redirect.valid)
76d2b20d1aSTang Haojin  val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl)
77d2b20d1aSTang Haojin  val FlushMemVioRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio)
78d2b20d1aSTang Haojin  val FlushControlBTBMiss = Wire(Bool())
79d2b20d1aSTang Haojin  val FlushTAGEMiss = Wire(Bool())
80d2b20d1aSTang Haojin  val FlushSCMiss = Wire(Bool())
81d2b20d1aSTang Haojin  val FlushITTAGEMiss = Wire(Bool())
82d2b20d1aSTang Haojin  val FlushRASMiss = Wire(Bool())
83f1fe8698SLemover
846f688dacSYinan Xu  val tlbCsr = DelayN(io.tlbCsr, 2)
856f688dacSYinan Xu  val csrCtrl = DelayN(io.csrCtrl, 2)
86fa9f9690SLemover  val sfence = RegNext(RegNext(io.sfence))
8772951335SLi Qianruo
8872951335SLi Qianruo  // trigger
896f688dacSYinan Xu  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
906f688dacSYinan Xu  val triggerEn = csrCtrl.trigger_enable
9172951335SLi Qianruo  ifu.io.csrTriggerEnable := VecInit(triggerEn(0), triggerEn(1), triggerEn(6), triggerEn(8))
9272951335SLi Qianruo
936ee06c7aSSteve Gou  // bpu ctrl
946ee06c7aSSteve Gou  bpu.io.ctrl := csrCtrl.bp_ctrl
95c20095f4SChen Xi  bpu.io.reset_vector := RegNext(io.reset_vector)
966ee06c7aSSteve Gou
97b6982e83SLemover// pmp
980c26d810Sguohongyu  val prefetchPipeNum = ICacheParameters().prefetchPipeNum
99b6982e83SLemover  val pmp = Module(new PMP())
10034f9624dSguohongyu  val pmp_check = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io))
1016f688dacSYinan Xu  pmp.io.distribute_csr := csrCtrl.distribute_csr
10234f9624dSguohongyu  val pmp_req_vec     = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle())))
1030c26d810Sguohongyu  (0 until 2 + prefetchPipeNum).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req)
1040c26d810Sguohongyu  pmp_req_vec.last <> ifu.io.pmp.req
105ee175d78SJay
106b6982e83SLemover  for (i <- pmp_check.indices) {
107ee175d78SJay    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
108b6982e83SLemover  }
1090c26d810Sguohongyu  (0 until 2 + prefetchPipeNum).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp)
1100c26d810Sguohongyu  ifu.io.pmp.resp <> pmp_check.last.resp
111ee175d78SJay
11234f9624dSguohongyu  val itlb = Module(new TLB(coreParams.itlbPortNum, nRespDups = 1,
113cb6e5d3cSssszwic    Seq(false, false) ++ Seq.fill(prefetchPipeNum)(false) ++ Seq(true), itlbParams))
1140c26d810Sguohongyu  itlb.io.requestor.take(2 + prefetchPipeNum) zip icache.io.itlb foreach {case (a,b) => a <> b}
1150c26d810Sguohongyu  itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
116254e4960SHaoyuan Feng  itlb.io.hartId := io.hartId
1171a718038SHaoyuan Feng  itlb.io.base_connect(sfence, tlbCsr)
118f1fe8698SLemover  itlb.io.flushPipe.map(_ := needFlush)
119*a4f9c77fSpeixiaokun  itlb.io.redirect := DontCare // itlb has flushpipe, don't need redirect signal
12009c6f1ddSLingrui98
1211a718038SHaoyuan Feng  val itlb_ptw = Wire(new VectorTlbPtwIO(coreParams.itlbPortNum))
1221a718038SHaoyuan Feng  itlb_ptw.connect(itlb.io.ptw)
1231a718038SHaoyuan Feng  val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay, itlb_ptw, sfence, tlbCsr, l2tlbParams.ifilterSize)
124cd2ff98bShappy-lx  val itlbRepeater2 = PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, io.ptw, sfence, tlbCsr)
1251a718038SHaoyuan Feng
1267052722fSJay  icache.io.prefetch <> ftq.io.toPrefetch
127efcb3cd3SJinYue
12809c6f1ddSLingrui98
12909c6f1ddSLingrui98  //IFU-Ftq
13009c6f1ddSLingrui98  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
131c5c5edaeSJenius  ftq.io.toIfu.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
132c5c5edaeSJenius
13309c6f1ddSLingrui98  ftq.io.fromIfu          <> ifu.io.ftqInter.toFtq
13409c6f1ddSLingrui98  bpu.io.ftq_to_bpu       <> ftq.io.toBpu
13509c6f1ddSLingrui98  ftq.io.fromBpu          <> bpu.io.bpu_to_ftq
1361d1e6d4dSJenius
1371d1e6d4dSJenius  ftq.io.mmioCommitRead   <> ifu.io.mmioCommitRead
13809c6f1ddSLingrui98  //IFU-ICache
139c5c5edaeSJenius
140c5c5edaeSJenius  icache.io.fetch.req <> ftq.io.toICache.req
141c5c5edaeSJenius  ftq.io.toICache.req.ready :=  ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
142c5c5edaeSJenius
143c5c5edaeSJenius  ifu.io.icacheInter.resp <>    icache.io.fetch.resp
14450780602SJenius  ifu.io.icacheInter.icacheReady :=  icache.io.toIFU
145d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss
146d2b20d1aSTang Haojin  ifu.io.icacheInter.topdownItlbMiss := icache.io.fetch.topdownItlbMiss
1471d8f4dcbSJay  icache.io.stop := ifu.io.icacheStop
14809c6f1ddSLingrui98
1491d8f4dcbSJay  ifu.io.icachePerfInfo := icache.io.perfInfo
1501d8f4dcbSJay
151330aad7fSGuokai Chen  icache.io.csr.distribute_csr <> DontCare
152330aad7fSGuokai Chen  io.csrUpdate := DontCare
153e19f7967SWilliam Wang
154ecccf78fSJay  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
155ecccf78fSJay  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
156ecccf78fSJay
1572a6078bfSguohongyu  icache.io.fencei := io.fencei
1582a6078bfSguohongyu
15909c6f1ddSLingrui98  //IFU-Ibuffer
16009c6f1ddSLingrui98  ifu.io.toIbuffer    <> ibuffer.io.in
16109c6f1ddSLingrui98
16209c6f1ddSLingrui98  ftq.io.fromBackend <> io.backend.toFtq
16309c6f1ddSLingrui98  io.backend.fromFtq <> ftq.io.toBackend
16409c6f1ddSLingrui98  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
16509c6f1ddSLingrui98
1665359309bSGuokai Chen  val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components))
1675359309bSGuokai Chen  when (ftq.io.toBackend.pc_mem_wen) {
1685359309bSGuokai Chen    checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata
1695359309bSGuokai Chen  }
1705359309bSGuokai Chen
1715359309bSGuokai Chen  val checkTargetIdx = Wire(Vec(DecodeWidth, UInt(log2Up(FtqSize).W)))
1725359309bSGuokai Chen  val checkTarget = Wire(Vec(DecodeWidth, UInt(VAddrBits.W)))
1735359309bSGuokai Chen
1745359309bSGuokai Chen  for (i <- 0 until DecodeWidth) {
1755359309bSGuokai Chen    checkTargetIdx(i) := ibuffer.io.out(i).bits.ftqPtr.value
1765359309bSGuokai Chen    checkTarget(i) := Mux(ftq.io.toBackend.newest_entry_ptr.value === checkTargetIdx(i),
1775359309bSGuokai Chen                        ftq.io.toBackend.newest_entry_target,
1785359309bSGuokai Chen                        checkPcMem(checkTargetIdx(i) + 1.U).startAddr)
1795359309bSGuokai Chen  }
1805359309bSGuokai Chen
1815359309bSGuokai Chen  // commented out for this br could be the last instruction in the fetch block
1825359309bSGuokai Chen  def checkNotTakenConsecutive = {
1835359309bSGuokai Chen    val prevNotTakenValid = RegInit(0.B)
1845359309bSGuokai Chen    val prevNotTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
1855359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
1865359309bSGuokai Chen      // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr
1875359309bSGuokai Chen      // for instrs that is the last, record and check next request
1885359309bSGuokai Chen      when (ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) {
1895359309bSGuokai Chen        when (ibuffer.io.out(i+1).fire) {
1905359309bSGuokai Chen          // not last br, check now
1915359309bSGuokai Chen          XSError(checkTargetIdx(i) =/= checkTargetIdx(i+1), "not-taken br should have same ftqPtr\n")
1925359309bSGuokai Chen        } .otherwise {
1935359309bSGuokai Chen          // last br, record its info
1945359309bSGuokai Chen          prevNotTakenValid := true.B
1955359309bSGuokai Chen          prevNotTakenFtqIdx := checkTargetIdx(i)
1965359309bSGuokai Chen        }
1975359309bSGuokai Chen      }
1985359309bSGuokai Chen    }
1995359309bSGuokai Chen    when (ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) {
2005359309bSGuokai Chen      // last instr is a br, record its info
2015359309bSGuokai Chen      prevNotTakenValid := true.B
2025359309bSGuokai Chen      prevNotTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
2035359309bSGuokai Chen    }
2045359309bSGuokai Chen    when (prevNotTakenValid && ibuffer.io.out(0).fire) {
2055359309bSGuokai Chen      XSError(prevNotTakenFtqIdx =/= checkTargetIdx(0), "not-taken br should have same ftqPtr\n")
2065359309bSGuokai Chen      prevNotTakenValid := false.B
2075359309bSGuokai Chen    }
2085359309bSGuokai Chen    when (needFlush) {
2095359309bSGuokai Chen      prevNotTakenValid := false.B
2105359309bSGuokai Chen    }
2115359309bSGuokai Chen  }
2125359309bSGuokai Chen
2135359309bSGuokai Chen  def checkTakenNotConsecutive = {
2145359309bSGuokai Chen    val prevTakenValid = RegInit(0.B)
2155359309bSGuokai Chen    val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
2165359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2175359309bSGuokai Chen      // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr
2185359309bSGuokai Chen      // for instrs that is the last, record and check next request
2195359309bSGuokai Chen      when (ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) {
2205359309bSGuokai Chen        when (ibuffer.io.out(i+1).fire) {
2215359309bSGuokai Chen          // not last br, check now
2225359309bSGuokai Chen          XSError(checkTargetIdx(i) + 1.U =/= checkTargetIdx(i+1), "taken br should have consecutive ftqPtr\n")
2235359309bSGuokai Chen        } .otherwise {
2245359309bSGuokai Chen          // last br, record its info
2255359309bSGuokai Chen          prevTakenValid := true.B
2265359309bSGuokai Chen          prevTakenFtqIdx := checkTargetIdx(i)
2275359309bSGuokai Chen        }
2285359309bSGuokai Chen      }
2295359309bSGuokai Chen    }
2305359309bSGuokai Chen    when (ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out(DecodeWidth - 1).bits.pred_taken) {
2315359309bSGuokai Chen      // last instr is a br, record its info
2325359309bSGuokai Chen      prevTakenValid := true.B
2335359309bSGuokai Chen      prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
2345359309bSGuokai Chen    }
2355359309bSGuokai Chen    when (prevTakenValid && ibuffer.io.out(0).fire) {
2365359309bSGuokai Chen      XSError(prevTakenFtqIdx + 1.U =/= checkTargetIdx(0), "taken br should have consecutive ftqPtr\n")
2375359309bSGuokai Chen      prevTakenValid := false.B
2385359309bSGuokai Chen    }
2395359309bSGuokai Chen    when (needFlush) {
2405359309bSGuokai Chen      prevTakenValid := false.B
2415359309bSGuokai Chen    }
2425359309bSGuokai Chen  }
2435359309bSGuokai Chen
2445359309bSGuokai Chen  def checkNotTakenPC = {
2455359309bSGuokai Chen    val prevNotTakenPC = Reg(UInt(VAddrBits.W))
2465359309bSGuokai Chen    val prevIsRVC = Reg(Bool())
2475359309bSGuokai Chen    val prevNotTakenValid = RegInit(0.B)
2485359309bSGuokai Chen
2495359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2505359309bSGuokai Chen      when (ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) {
2515359309bSGuokai Chen        when (ibuffer.io.out(i+1).fire) {
2525359309bSGuokai Chen          XSError(ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out(i+1).bits.pc, "not-taken br should have consecutive pc\n")
2535359309bSGuokai Chen        } .otherwise {
2545359309bSGuokai Chen          prevNotTakenValid := true.B
2555359309bSGuokai Chen          prevIsRVC := ibuffer.io.out(i).bits.pd.isRVC
2565359309bSGuokai Chen          prevNotTakenPC := ibuffer.io.out(i).bits.pc
2575359309bSGuokai Chen        }
2585359309bSGuokai Chen      }
2595359309bSGuokai Chen    }
2605359309bSGuokai Chen    when (ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out(DecodeWidth - 1).bits.pred_taken) {
2615359309bSGuokai Chen      prevNotTakenValid := true.B
2625359309bSGuokai Chen      prevIsRVC := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC
2635359309bSGuokai Chen      prevNotTakenPC := ibuffer.io.out(DecodeWidth - 1).bits.pc
2645359309bSGuokai Chen    }
2655359309bSGuokai Chen    when (prevNotTakenValid && ibuffer.io.out(0).fire) {
2665359309bSGuokai Chen      XSError(prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc, "not-taken br should have same pc\n")
2675359309bSGuokai Chen      prevNotTakenValid := false.B
2685359309bSGuokai Chen    }
2695359309bSGuokai Chen    when (needFlush) {
2705359309bSGuokai Chen      prevNotTakenValid := false.B
2715359309bSGuokai Chen    }
2725359309bSGuokai Chen  }
2735359309bSGuokai Chen
2745359309bSGuokai Chen  def checkTakenPC = {
2755359309bSGuokai Chen    val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
2765359309bSGuokai Chen    val prevTakenValid = RegInit(0.B)
2775359309bSGuokai Chen    val prevTakenTarget = Wire(UInt(VAddrBits.W))
2785359309bSGuokai Chen    prevTakenTarget := checkPcMem(prevTakenFtqIdx + 1.U).startAddr
2795359309bSGuokai Chen
2805359309bSGuokai Chen    for (i <- 0 until DecodeWidth - 1) {
2815359309bSGuokai Chen      when (ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) {
2825359309bSGuokai Chen        when (ibuffer.io.out(i+1).fire) {
2835359309bSGuokai Chen          XSError(checkTarget(i) =/= ibuffer.io.out(i+1).bits.pc, "taken instr should follow target pc\n")
2845359309bSGuokai Chen        } .otherwise {
2855359309bSGuokai Chen          prevTakenValid := true.B
2865359309bSGuokai Chen          prevTakenFtqIdx := checkTargetIdx(i)
2875359309bSGuokai Chen        }
2885359309bSGuokai Chen      }
2895359309bSGuokai Chen    }
2905359309bSGuokai Chen    when (ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out(DecodeWidth - 1).bits.pred_taken) {
2915359309bSGuokai Chen      prevTakenValid := true.B
2925359309bSGuokai Chen      prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
2935359309bSGuokai Chen    }
2945359309bSGuokai Chen    when (prevTakenValid && ibuffer.io.out(0).fire) {
2955359309bSGuokai Chen      XSError(prevTakenTarget =/= ibuffer.io.out(0).bits.pc, "taken instr should follow target pc\n")
2965359309bSGuokai Chen      prevTakenValid := false.B
2975359309bSGuokai Chen    }
2985359309bSGuokai Chen    when (needFlush) {
2995359309bSGuokai Chen      prevTakenValid := false.B
3005359309bSGuokai Chen    }
3015359309bSGuokai Chen  }
3025359309bSGuokai Chen
3035359309bSGuokai Chen  //checkNotTakenConsecutive
3045359309bSGuokai Chen  checkTakenNotConsecutive
3055359309bSGuokai Chen  checkTakenPC
3065359309bSGuokai Chen  checkNotTakenPC
3075359309bSGuokai Chen
308a37fbf10SJay  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
309a37fbf10SJay
31009c6f1ddSLingrui98  ibuffer.io.flush := needFlush
311d2b20d1aSTang Haojin  ibuffer.io.ControlRedirect := FlushControlRedirect
312d2b20d1aSTang Haojin  ibuffer.io.MemVioRedirect := FlushMemVioRedirect
313d2b20d1aSTang Haojin  ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss
314d2b20d1aSTang Haojin  ibuffer.io.TAGEMissBubble := FlushTAGEMiss
315d2b20d1aSTang Haojin  ibuffer.io.SCMissBubble := FlushSCMiss
316d2b20d1aSTang Haojin  ibuffer.io.ITTAGEMissBubble := FlushITTAGEMiss
317d2b20d1aSTang Haojin  ibuffer.io.RASMissBubble := FlushRASMiss
318d2b20d1aSTang Haojin
319d2b20d1aSTang Haojin  FlushControlBTBMiss := ftq.io.ControlBTBMissBubble
320d2b20d1aSTang Haojin  FlushTAGEMiss := ftq.io.TAGEMissBubble
321d2b20d1aSTang Haojin  FlushSCMiss := ftq.io.SCMissBubble
322d2b20d1aSTang Haojin  FlushITTAGEMiss := ftq.io.ITTAGEMissBubble
323d2b20d1aSTang Haojin  FlushRASMiss := ftq.io.RASMissBubble
324d2b20d1aSTang Haojin
32509c6f1ddSLingrui98  io.backend.cfVec <> ibuffer.io.out
326d2b20d1aSTang Haojin  io.backend.stallReason <> ibuffer.io.stallReason
32709c6f1ddSLingrui98
3280be662e4SJay  instrUncache.io.req   <> ifu.io.uncacheInter.toUncache
3290be662e4SJay  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
33058dbdfc2SJay  instrUncache.io.flush := false.B
33158dbdfc2SJay  io.error <> RegNext(RegNext(icache.io.error))
33209c6f1ddSLingrui98
33341cb8b61SJenius  icache.io.hartId := io.hartId
33441cb8b61SJenius
33560ebee38STang Haojin  itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr
33660ebee38STang Haojin
33709c6f1ddSLingrui98  val frontendBubble = PopCount((0 until DecodeWidth).map(i => io.backend.cfVec(i).ready && !ibuffer.io.out(i).valid))
33809c6f1ddSLingrui98  XSPerfAccumulate("FrontendBubble", frontendBubble)
33909c6f1ddSLingrui98  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
340cd365d4cSrvcoresjw
3411ca0e4f3SYinan Xu  // PFEvent
3421ca0e4f3SYinan Xu  val pfevent = Module(new PFEvent)
3431ca0e4f3SYinan Xu  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
3441ca0e4f3SYinan Xu  val csrevents = pfevent.io.hpmevent.take(8)
345cd365d4cSrvcoresjw
3469a128342SHaoyuan Feng  val perfFromUnits = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerfEvents)
3479a128342SHaoyuan Feng  val perfFromIO    = Seq()
3489a128342SHaoyuan Feng  val perfBlock     = Seq()
3499a128342SHaoyuan Feng  // let index = 0 be no event
3509a128342SHaoyuan Feng  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
3519a128342SHaoyuan Feng
3529a128342SHaoyuan Feng  if (printEventCoding) {
3539a128342SHaoyuan Feng    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
3549a128342SHaoyuan Feng      println("Frontend perfEvents Set", name, inc, i)
3559a128342SHaoyuan Feng    }
3569a128342SHaoyuan Feng  }
3579a128342SHaoyuan Feng
3589a128342SHaoyuan Feng  val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
3599a128342SHaoyuan Feng  override val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
3601ca0e4f3SYinan Xu  generatePerfEvent()
36109c6f1ddSLingrui98}
362