xref: /XiangShan/src/main/scala/xiangshan/backend/rob/RobBundles.scala (revision f7063a43ab34da917ba6c670d21871314340c550)
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 ***************************************************************************************/
16
17package xiangshan.backend.rob
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3.{Mem, Mux, Vec, _}
21import chisel3.util._
22import difftest._
23import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
24import utility._
25import utils._
26import xiangshan._
27import xiangshan.backend.BackendParams
28import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput}
29import xiangshan.backend.fu.{FuConfig, FuType}
30import xiangshan.frontend.FtqPtr
31import xiangshan.mem.{LqPtr, LsqEnqIO, SqPtr}
32import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput}
33import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfo, LsTopdownInfo}
34import xiangshan.backend.fu.vector.Bundles.VType
35import xiangshan.backend.rename.SnapshotGenerator
36
37import scala.collection.immutable.Nil
38
39
40
41object RobBundles extends HasCircularQueuePtrHelper {
42
43  class RobEntryBundle(implicit p: Parameters) extends XSBundle {
44
45    // data begin
46    val vls = Bool()
47    // some instructions are not allowed to trigger interrupts
48    // They have side effects on the states of the processor before they write back
49    val interrupt_safe = Bool()
50    val fpWen = Bool()
51    val rfWen = Bool()
52    val wflags = Bool()
53    val dirtyVs = Bool()
54    val commitType = CommitType()
55    val ftqIdx = new FtqPtr
56    val ftqOffset = UInt(log2Up(PredictWidth).W)
57    val isRVC = Bool()
58    val isVset = Bool()
59    val isHls = Bool()
60    val instrSize = UInt(log2Ceil(RenameWidth + 1).W)
61    val loadWaitBit = Bool()    // for perfEvents
62    val eliminatedMove = Bool() // for perfEvents
63    // data end
64
65    // status begin
66    val valid = Bool()
67    val fflags = UInt(5.W)
68    val mmio = Bool()
69    // store will be commited if both sta & std have been writebacked
70    val stdWritebacked = Bool()
71    val vxsat = Bool()
72    val realDestSize = UInt(log2Up(MaxUopSize + 1).W)
73    val uopNum = UInt(log2Up(MaxUopSize + 1).W)
74    val commitTrigger = Bool()
75    // status end
76
77    // debug_begin
78    val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W))
79    val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W))
80    val debug_ldest = OptionWrapper(backendParams.debugEn, UInt(6.W))
81    val debug_pdest = OptionWrapper(backendParams.debugEn, UInt(PhyRegIdxWidth.W))
82    val debug_fuType = OptionWrapper(backendParams.debugEn, FuType())
83    // debug_end
84
85    def isWritebacked: Bool = !uopNum.orR && stdWritebacked
86    def isUopWritebacked: Bool = !uopNum.orR
87
88  }
89
90  class RobCommitEntryBundle(implicit p: Parameters) extends XSBundle {
91    val walk_v = Bool()
92    val commit_v = Bool()
93    val commit_w = Bool()
94    val realDestSize = UInt(log2Up(MaxUopSize + 1).W)
95    val interrupt_safe = Bool()
96    val wflags = Bool()
97    val fflags = UInt(5.W)
98    val vxsat = Bool()
99    val isRVC = Bool()
100    val isVset = Bool()
101    val isHls = Bool()
102    val commitType = CommitType()
103    val ftqIdx = new FtqPtr
104    val ftqOffset = UInt(log2Up(PredictWidth).W)
105    val instrSize = UInt(log2Ceil(RenameWidth + 1).W)
106    val fpWen = Bool()
107    val rfWen = Bool()
108    val loadWaitBit = Bool() // for perfEvents
109    val isMove = Bool()      // for perfEvents
110    // debug_begin
111    val debug_pc = OptionWrapper(backendParams.debugEn, UInt(VAddrBits.W))
112    val debug_instr = OptionWrapper(backendParams.debugEn, UInt(32.W))
113    val debug_ldest = OptionWrapper(backendParams.debugEn, UInt(6.W))
114    val debug_pdest = OptionWrapper(backendParams.debugEn, UInt(PhyRegIdxWidth.W))
115    val debug_fuType = OptionWrapper(backendParams.debugEn, FuType())
116    // debug_end
117    def dirtyFs = fpWen
118    val dirtyVs = Bool()
119  }
120
121  def connectEnq(robEntry: RobEntryBundle, robEnq: DynInst): Unit = {
122    robEntry.wflags := robEnq.wfflags
123    robEntry.commitType := robEnq.commitType
124    robEntry.ftqIdx := robEnq.ftqPtr
125    robEntry.ftqOffset := robEnq.ftqOffset
126    robEntry.isRVC := robEnq.preDecodeInfo.isRVC
127    robEntry.isVset := robEnq.isVset
128    robEntry.isHls := robEnq.isHls
129    robEntry.instrSize := robEnq.instrSize
130    robEntry.rfWen := robEnq.rfWen
131    robEntry.fpWen := robEnq.dirtyFs
132    robEntry.dirtyVs := robEnq.dirtyVs
133    robEntry.loadWaitBit := robEnq.loadWaitBit
134    robEntry.eliminatedMove := robEnq.eliminatedMove
135    robEntry.debug_pc.foreach(_ := robEnq.pc)
136    robEntry.debug_instr.foreach(_ := robEnq.instr)
137    robEntry.debug_ldest.foreach(_ := robEnq.ldest)
138    robEntry.debug_pdest.foreach(_ := robEnq.pdest)
139    robEntry.debug_fuType.foreach(_ := robEnq.fuType)
140  }
141
142  def connectCommitEntry(robCommitEntry: RobCommitEntryBundle, robEntry: RobEntryBundle): Unit = {
143    robCommitEntry.walk_v := robEntry.valid
144    robCommitEntry.commit_v := robEntry.valid
145    robCommitEntry.commit_w := (robEntry.uopNum === 0.U) && (robEntry.stdWritebacked === true.B)
146    robCommitEntry.realDestSize := robEntry.realDestSize
147    robCommitEntry.interrupt_safe := robEntry.interrupt_safe
148    robCommitEntry.rfWen := robEntry.rfWen
149    robCommitEntry.fpWen := robEntry.fpWen
150    robCommitEntry.fflags := robEntry.fflags
151    robCommitEntry.wflags := robEntry.wflags
152    robCommitEntry.vxsat := robEntry.vxsat
153    robCommitEntry.isRVC := robEntry.isRVC
154    robCommitEntry.isVset := robEntry.isVset
155    robCommitEntry.isHls := robEntry.isHls
156    robCommitEntry.ftqIdx := robEntry.ftqIdx
157    robCommitEntry.ftqOffset := robEntry.ftqOffset
158    robCommitEntry.commitType := robEntry.commitType
159    robCommitEntry.instrSize := robEntry.instrSize
160    robCommitEntry.loadWaitBit := robEntry.loadWaitBit
161    robCommitEntry.isMove := robEntry.eliminatedMove
162    robCommitEntry.dirtyVs := robEntry.dirtyVs
163    robCommitEntry.debug_pc.foreach(_ := robEntry.debug_pc.get)
164    robCommitEntry.debug_instr.foreach(_ := robEntry.debug_instr.get)
165    robCommitEntry.debug_ldest.foreach(_ := robEntry.debug_ldest.get)
166    robCommitEntry.debug_pdest.foreach(_ := robEntry.debug_pdest.get)
167    robCommitEntry.debug_fuType.foreach(_ := robEntry.debug_fuType.get)
168  }
169}
170
171import RobBundles._
172
173class RobPtr(entries: Int) extends CircularQueuePtr[RobPtr](
174  entries
175) with HasCircularQueuePtrHelper {
176
177  def this()(implicit p: Parameters) = this(p(XSCoreParamsKey).RobSize)
178
179  def needFlush(redirect: Valid[Redirect]): Bool = {
180    val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx
181    redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx))
182  }
183
184  def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR
185
186  def lineHeadPtr()(implicit p: Parameters): RobPtr = {
187    val CommitWidth = p(XSCoreParamsKey).CommitWidth
188    val out = Wire(new RobPtr)
189    out.flag := this.flag
190    out.value := Cat(this.value(this.PTR_WIDTH-1, log2Up(CommitWidth)), 0.U(log2Up(CommitWidth).W))
191    out
192  }
193
194}
195
196object RobPtr {
197  def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = {
198    val ptr = Wire(new RobPtr)
199    ptr.flag := f
200    ptr.value := v
201    ptr
202  }
203}
204
205class RobCSRIO(implicit p: Parameters) extends XSBundle {
206  val intrBitSet = Input(Bool())
207  val trapTarget = Input(UInt(VAddrBits.W))
208  val isXRet     = Input(Bool())
209  val wfiEvent   = Input(Bool())
210
211  val fflags     = Output(Valid(UInt(5.W)))
212  val vxsat      = Output(Valid(Bool()))
213  val vstart     = Output(Valid(UInt(XLEN.W)))
214  val dirty_fs   = Output(Bool())
215  val dirty_vs   = Output(Bool())
216  val perfinfo   = new Bundle {
217    val retiredInstr = Output(UInt(3.W))
218  }
219
220  val vcsrFlag   = Output(Bool())
221}
222
223class RobLsqIO(implicit p: Parameters) extends XSBundle {
224  val lcommit = Output(UInt(log2Up(CommitWidth + 1).W))
225  val scommit = Output(UInt(log2Up(CommitWidth + 1).W))
226  val pendingld = Output(Bool())
227  val pendingst = Output(Bool())
228  val commit = Output(Bool())
229  val pendingPtr = Output(new RobPtr)
230  val pendingPtrNext = Output(new RobPtr)
231
232  val mmio = Input(Vec(LoadPipelineWidth, Bool()))
233  // Todo: what's this?
234  val uop = Input(Vec(LoadPipelineWidth, new DynInst))
235}
236
237class RobEnqIO(implicit p: Parameters) extends XSBundle {
238  val canAccept = Output(Bool())
239  val isEmpty = Output(Bool())
240  // valid vector, for robIdx gen and walk
241  val needAlloc = Vec(RenameWidth, Input(Bool()))
242  val req = Vec(RenameWidth, Flipped(ValidIO(new DynInst)))
243  val resp = Vec(RenameWidth, Output(new RobPtr))
244}
245
246class RobCoreTopDownIO(implicit p: Parameters) extends XSBundle {
247  val robHeadVaddr = Valid(UInt(VAddrBits.W))
248  val robHeadPaddr = Valid(UInt(PAddrBits.W))
249}
250
251class RobDispatchTopDownIO extends Bundle {
252  val robTrueCommit = Output(UInt(64.W))
253  val robHeadLsIssue = Output(Bool())
254}
255
256class RobDebugRollingIO extends Bundle {
257  val robTrueCommit = Output(UInt(64.W))
258}
259
260class RobExceptionInfo(implicit p: Parameters) extends XSBundle {
261  // val valid = Bool()
262  val robIdx = new RobPtr
263  val ftqPtr = new FtqPtr
264  val ftqOffset = UInt(log2Up(PredictWidth).W)
265  val exceptionVec = ExceptionVec()
266  val flushPipe = Bool()
267  val isVset = Bool()
268  val replayInst = Bool() // redirect to that inst itself
269  val singleStep = Bool() // TODO add frontend hit beneath
270  val crossPageIPFFix = Bool()
271  val trigger = new TriggerCf
272  val vstartEn = Bool()
273  val vstart = UInt(XLEN.W)
274
275  def has_exception = exceptionVec.asUInt.orR || flushPipe || singleStep || replayInst || trigger.canFire
276  def not_commit = exceptionVec.asUInt.orR || singleStep || replayInst || trigger.canFire
277  // only exceptions are allowed to writeback when enqueue
278  def can_writeback = exceptionVec.asUInt.orR || singleStep || trigger.canFire
279}
280
281class RobFlushInfo(implicit p: Parameters) extends XSBundle {
282  val ftqIdx = new FtqPtr
283  val robIdx = new RobPtr
284  val ftqOffset = UInt(log2Up(PredictWidth).W)
285  val replayInst = Bool()
286}
287