xref: /XiangShan/src/main/scala/xiangshan/package.scala (revision 2199a01c65d5a7bf503c4b40771336a50a6f1122)
1import chisel3._
2import chisel3.util._
3
4package object xiangshan {
5  object SrcType {
6    def reg = "b00".U
7    def pc  = "b01".U
8    def imm = "b01".U
9    def fp  = "b10".U
10
11    def DC = imm // Don't Care
12
13    def isReg(srcType: UInt) = srcType===reg
14    def isPc(srcType: UInt) = srcType===pc
15    def isImm(srcType: UInt) = srcType===imm
16    def isFp(srcType: UInt) = srcType===fp
17    def isPcImm(srcType: UInt) = isPc(srcType) || isImm(srcType)
18    def isRegFp(srcType: UInt) = isReg(srcType) || isFp(srcType)
19
20    def apply() = UInt(2.W)
21  }
22
23  object SrcState {
24    def busy    = "b00".U
25    def rdy     = "b01".U
26    def specRdy = "b10".U // speculative ready, for future use
27    def apply() = UInt(2.W)
28  }
29
30  object FuType extends HasXSParameter {
31    def num           = exuParameters.NRFuType
32
33    def jmp          = "b0000".U
34    def i2f          = "b0001".U
35    def csr          = "b0010".U
36    def alu          = "b0011".U
37    def mul          = "b0100".U
38    def div          = "b0101".U
39    def fence        = "b0110".U
40
41    def fmac         = "b1000".U
42    def fmisc        = "b1001".U
43    def fDivSqrt     = "b1010".U
44
45    def ldu          = "b1100".U
46    def stu          = "b1101".U
47    def mou          = "b1110".U // for amo, lr, sc, fence
48
49    def apply() = UInt(log2Up(num).W)
50
51    def isIntExu(fuType: UInt) = !fuType(3)
52    def isJumpExu(fuType: UInt) = fuType === jmp
53    def isFpExu(fuType: UInt) = fuType(3, 2) === "b10".U
54    def isMemExu(fuType: UInt) = fuType(3, 2) === "b11".U
55    def isLoadExu(fuType: UInt) = fuType === ldu || fuType === mou
56    def isStoreExu(fuType: UInt) = fuType === stu
57
58    val functionNameMap = Map(
59      jmp.litValue() -> "jmp",
60      i2f.litValue() -> "int to float",
61      csr.litValue() -> "csr",
62      alu.litValue() -> "alu",
63      mul.litValue() -> "mul",
64      div.litValue() -> "div",
65      fence.litValue() -> "fence",
66      fmac.litValue() -> "fmac",
67      fmisc.litValue() -> "fmisc",
68      fDivSqrt.litValue() -> "fdiv/fsqrt",
69      ldu.litValue() -> "load",
70      stu.litValue() -> "store"
71    )
72
73  }
74
75  object FuOpType extends HasXSParameter {
76    def apply() = UInt(exuParameters.FuOpWidth.W)
77  }
78
79  object BTBtype {
80    def B = "b00".U  // branch
81    def J = "b01".U  // jump
82    def I = "b10".U  // indirect
83    def R = "b11".U  // return
84
85    def apply() = UInt(2.W)
86  }
87
88  object CommitType {
89    def NORMAL = "b00".U  // int/fp
90    def BRANCH = "b01".U  // branch
91    def LOAD   = "b10".U  // load
92    def STORE  = "b11".U  // store
93
94    def apply() = UInt(2.W)
95    def isLoadStore(commitType: UInt) = commitType(1)
96    def lsInstIsStore(commitType: UInt) = commitType(0)
97    def isStore(commitType: UInt) = isLoadStore(commitType) && lsInstIsStore(commitType)
98    def isBranch(commitType: UInt) = commitType(0) && !commitType(1)
99  }
100
101  object RedirectLevel {
102    def flushAfter = "b00".U
103    def flush      = "b01".U
104    def flushAll   = "b10".U
105    def exception  = "b11".U
106
107    def apply() = UInt(2.W)
108    def isUnconditional(level: UInt) = level(1)
109    def flushItself(level: UInt) = level(0)
110    def isException(level: UInt) = level(1) && level(0)
111  }
112
113  object ExceptionVec {
114    def apply() = Vec(16, Bool())
115  }
116
117  object PMAMode {
118    def R = "b1".U << 0 //readable
119    def W = "b1".U << 1 //writeable
120    def X = "b1".U << 2 //executable
121    def I = "b1".U << 3 //cacheable: icache
122    def D = "b1".U << 4 //cacheable: dcache
123    def S = "b1".U << 5 //enable speculative access
124    def A = "b1".U << 6 //enable atomic operation, A imply R & W
125    def C = "b1".U << 7 //if it is cacheable is configable
126    def Reserved = "b0".U
127
128    def apply() = UInt(7.W)
129
130    def read(mode: UInt) = mode(0)
131    def write(mode: UInt) = mode(1)
132    def execute(mode: UInt) = mode(2)
133    def icache(mode: UInt) = mode(3)
134    def dcache(mode: UInt) = mode(4)
135    def speculate(mode: UInt) = mode(5)
136    def atomic(mode: UInt) = mode(6)
137    def configable_cache(mode: UInt) = mode(7)
138
139    def strToMode(s: String) = {
140      var result = 0.U << 8
141      if (s.toUpperCase.indexOf("R") >= 0) result = result + R
142      if (s.toUpperCase.indexOf("W") >= 0) result = result + W
143      if (s.toUpperCase.indexOf("X") >= 0) result = result + X
144      if (s.toUpperCase.indexOf("I") >= 0) result = result + I
145      if (s.toUpperCase.indexOf("D") >= 0) result = result + D
146      if (s.toUpperCase.indexOf("S") >= 0) result = result + S
147      if (s.toUpperCase.indexOf("A") >= 0) result = result + A
148      if (s.toUpperCase.indexOf("C") >= 0) result = result + C
149      result
150    }
151  }
152}
153