xref: /XiangShan/src/main/scala/xiangshan/backend/fu/NewCSR/CSRPermitModule.scala (revision 25dc4a827ee27e3ccbaf02e8e5134872cba28fcd)
1package xiangshan.backend.fu.NewCSR
2
3import chisel3._
4import chisel3.util._
5import chisel3.util.experimental.decode.TruthTable
6import xiangshan.backend.fu.NewCSR.CSRBundles.PrivState
7
8class CSRPermitModule extends Module {
9  val io = IO(new CSRPermitIO)
10
11  val (wen, addr, privState) = (
12    io.in.wen,
13    io.in.addr,
14    io.in.privState
15  )
16
17  private val isRO = addr(11, 10) === "b11".U
18
19  private val accessTable = TruthTable(Seq(
20    //       V PRVM ADDR
21    BitPat("b?__00___00") -> BitPat.Y(), // HU/VU access U
22    BitPat("b?__00___??") -> BitPat.N(), // HU/VU access the others
23    BitPat("b1__01___00") -> BitPat.Y(), // VS access U
24    BitPat("b1__01___01") -> BitPat.Y(), // VS access S
25    BitPat("b1__01___??") -> BitPat.N(), // VS access the others
26    BitPat("b0__01___11") -> BitPat.N(), // HS access M
27    BitPat("b0__01___??") -> BitPat.Y(), // HS access the others
28    BitPat("b0__11___??") -> BitPat.Y(), // M  access any
29  ), BitPat.N())
30
31  private val privilegeLegal = chisel3.util.experimental.decode.decoder(
32    privState.V.asUInt ## privState.PRVM.asUInt ## addr(9, 8),
33    accessTable
34  ).asBool
35
36  private val rwLegal = !(isRO && wen)
37
38  io.out.legal := privilegeLegal && rwLegal
39}
40
41class CSRPermitIO extends Bundle {
42  val in = Input(new Bundle {
43    val wen  = Bool()
44    val addr = UInt(12.W)
45    val privState = new PrivState
46  })
47
48  val out = Output(new Bundle {
49    val legal = Bool()
50  })
51}
52