xref: /XiangShan/src/main/scala/xiangshan/backend/fu/NewCSR/TrapHandleModule.scala (revision dafddbf0ee612a40fe658c7c5a3c3d680a2ced81)
1package xiangshan.backend.fu.NewCSR
2
3import chisel3._
4import chisel3.util._
5import xiangshan.{ExceptionVec, TriggerCf}
6import CSRConfig._
7import xiangshan.backend.fu.NewCSR.CSRBundles.{CauseBundle, PrivState}
8import xiangshan.ExceptionNO
9import xiangshan.backend.fu.util.CSRConst
10
11class TrapHandleModule extends Module {
12  val io = IO(new TrapHandleIO)
13
14  private val trapInfo = io.in.trapInfo
15  private val privState = io.in.privState
16
17  private val hasTrap = trapInfo.valid
18  private val hasIR = hasTrap && trapInfo.bits.isInterrupt
19  private val hasEX = hasTrap && !trapInfo.bits.isInterrupt
20
21  private val trapVec = io.in.trapInfo.bits.trapVec
22  private val hasEXVec = Mux(hasEX, trapVec, 0.U)
23  private val hasIRVec = Mux(hasIR, trapVec, 0.U)
24
25  // Todo: support more interrupt and exception
26  private val exceptionNO = ExceptionNO.priorities.foldRight(0.U)((i: Int, sum: UInt) => Mux(trapVec(i), i.U, sum))
27  private val interruptNO = CSRConst.IntPriority.foldRight(0.U)((i: Int, sum: UInt) => Mux(trapVec(i), i.U, sum))
28
29  private val causeNO = Mux(hasIR, interruptNO, exceptionNO)
30
31  private val mdeleg = Mux(hasIR, io.in.mideleg.asUInt, io.in.medeleg.asUInt)
32  private val hdeleg = Mux(hasIR, io.in.hideleg.asUInt, io.in.hedeleg.asUInt)
33
34  private val handleTrapUnderHS = mdeleg(causeNO) && privState < PrivState.ModeM
35  private val handleTrapUnderVS = mdeleg(causeNO) && hdeleg(causeNO) && privState < PrivState.ModeHS
36
37  io.out.entryPrivState := MuxCase(default = PrivState.ModeM, mapping = Seq(
38    handleTrapUnderVS -> PrivState.ModeVS,
39    handleTrapUnderHS -> PrivState.ModeHS,
40  ))
41
42  io.out.causeNO.Interrupt := hasIR
43  io.out.causeNO.ExceptionCode := causeNO
44}
45
46class TrapHandleIO extends Bundle {
47  val in = Input(new Bundle {
48    val trapInfo = ValidIO(new Bundle {
49      val trapVec = UInt(64.W)
50      val isInterrupt = Bool()
51    })
52    val privState = new PrivState
53    val mideleg = new MidelegBundle
54    val medeleg = new MedelegBundle
55    val hideleg = new HidelegBundle
56    val hedeleg = new HedelegBundle
57  })
58
59  val out = new Bundle {
60    val entryPrivState = new PrivState
61    val causeNO = new CauseBundle
62  }
63}