xref: /XiangShan/src/main/scala/xiangshan/package.scala (revision 3802dba502b91d813c1e563035b876c4e6288166)
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    = "b0".U
25    def rdy     = "b1".U
26    // def specRdy = "b10".U // speculative ready, for future use
27    def apply() = UInt(1.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          = "b0110".U
37    def mul          = "b0100".U
38    def div          = "b0101".U
39    def fence        = "b0011".U
40
41    def fmac         = "b1000".U
42    def fmisc        = "b1011".U
43    def fDivSqrt     = "b1010".U
44
45    def ldu          = "b1100".U
46    def stu          = "b1101".U
47    def mou          = "b1111".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 isLoadStore(fuType: UInt) = isMemExu(fuType) && !fuType(1)
56    def isStoreExu(fuType: UInt) = isMemExu(fuType) && fuType(0)
57    def isAMO(fuType: UInt) = fuType(1)
58
59    def jmpCanAccept(fuType: UInt) = !fuType(2)
60    def mduCanAccept(fuType: UInt) = fuType(2) && !fuType(1)
61    def aluCanAccept(fuType: UInt) = fuType(2) && fuType(1)
62
63    def fmacCanAccept(fuType: UInt) = !fuType(1)
64    def fmiscCanAccept(fuType: UInt) = fuType(1)
65
66    def loadCanAccept(fuType: UInt) = !fuType(0)
67    def storeCanAccept(fuType: UInt) = fuType(0)
68
69    def storeIsAMO(fuType: UInt) = fuType(1)
70
71    val functionNameMap = Map(
72      jmp.litValue() -> "jmp",
73      i2f.litValue() -> "int to float",
74      csr.litValue() -> "csr",
75      alu.litValue() -> "alu",
76      mul.litValue() -> "mul",
77      div.litValue() -> "div",
78      fence.litValue() -> "fence",
79      fmac.litValue() -> "fmac",
80      fmisc.litValue() -> "fmisc",
81      fDivSqrt.litValue() -> "fdiv/fsqrt",
82      ldu.litValue() -> "load",
83      stu.litValue() -> "store"
84    )
85
86  }
87
88  object FuOpType extends HasXSParameter {
89    def apply() = UInt(exuParameters.FuOpWidth.W)
90  }
91
92  object BTBtype {
93    def B = "b00".U  // branch
94    def J = "b01".U  // jump
95    def I = "b10".U  // indirect
96    def R = "b11".U  // return
97
98    def apply() = UInt(2.W)
99  }
100
101  object CommitType {
102    def NORMAL = "b00".U  // int/fp
103    def BRANCH = "b01".U  // branch
104    def LOAD   = "b10".U  // load
105    def STORE  = "b11".U  // store
106
107    def apply() = UInt(2.W)
108    def isLoadStore(commitType: UInt) = commitType(1)
109    def lsInstIsStore(commitType: UInt) = commitType(0)
110    def isStore(commitType: UInt) = isLoadStore(commitType) && lsInstIsStore(commitType)
111    def isBranch(commitType: UInt) = commitType(0) && !commitType(1)
112  }
113
114  object RedirectLevel {
115    def flushAfter = "b00".U
116    def flush      = "b01".U
117    def flushAll   = "b10".U
118    def exception  = "b11".U
119
120    def apply() = UInt(2.W)
121    def isUnconditional(level: UInt) = level(1)
122    def flushItself(level: UInt) = level(0)
123    def isException(level: UInt) = level(1) && level(0)
124  }
125
126  object ExceptionVec {
127    def apply() = Vec(16, Bool())
128  }
129
130  object PMAMode {
131    def R = "b1".U << 0 //readable
132    def W = "b1".U << 1 //writeable
133    def X = "b1".U << 2 //executable
134    def I = "b1".U << 3 //cacheable: icache
135    def D = "b1".U << 4 //cacheable: dcache
136    def S = "b1".U << 5 //enable speculative access
137    def A = "b1".U << 6 //enable atomic operation, A imply R & W
138    def C = "b1".U << 7 //if it is cacheable is configable
139    def Reserved = "b0".U
140
141    def apply() = UInt(7.W)
142
143    def read(mode: UInt) = mode(0)
144    def write(mode: UInt) = mode(1)
145    def execute(mode: UInt) = mode(2)
146    def icache(mode: UInt) = mode(3)
147    def dcache(mode: UInt) = mode(4)
148    def speculate(mode: UInt) = mode(5)
149    def atomic(mode: UInt) = mode(6)
150    def configable_cache(mode: UInt) = mode(7)
151
152    def strToMode(s: String) = {
153      var result = 0.U << 8
154      if (s.toUpperCase.indexOf("R") >= 0) result = result + R
155      if (s.toUpperCase.indexOf("W") >= 0) result = result + W
156      if (s.toUpperCase.indexOf("X") >= 0) result = result + X
157      if (s.toUpperCase.indexOf("I") >= 0) result = result + I
158      if (s.toUpperCase.indexOf("D") >= 0) result = result + D
159      if (s.toUpperCase.indexOf("S") >= 0) result = result + S
160      if (s.toUpperCase.indexOf("A") >= 0) result = result + A
161      if (s.toUpperCase.indexOf("C") >= 0) result = result + C
162      result
163    }
164  }
165}
166