xref: /XiangShan/src/main/scala/xiangshan/backend/fu/vector/Bundles.scala (revision 887862dbb8debde8ab099befc426493834a69ee7)
1package xiangshan.backend.fu.vector
2
3import org.chipsalliance.cde.config.Parameters
4import chisel3._
5import chisel3.util._
6import xiangshan.XSBundle
7import xiangshan.XSCoreParamsKey
8import xiangshan.backend.decode.isa.bitfield.InstVType
9import xiangshan.backend.fu.VtypeStruct
10import _root_.utils.NamedUInt
11
12object Bundles {
13
14  /**
15    * vtype bundle, should not used as csr reg
16    */
17  class VType(implicit p: Parameters) extends Bundle {
18    val illegal = Bool()
19    val vma     = Bool()
20    val vta     = Bool()
21    val vsew    = VSew()
22    val vlmul   = VLmul()
23  }
24
25  /**
26    * vset module's vtype bundle, use 3 bits vsew to check if it is illegal
27    *
28    * we need to get 3 bits vsew in Vtype struct, then vset module can check if it is reserved.
29    * and we use 2 bits to store vsew in other places to save space
30    */
31  class VsetVType(implicit p: Parameters) extends XSBundle {
32    val illegal  = Bool()
33    val reserved = UInt((XLEN - 9).W)
34    val vma      = Bool()
35    val vta      = Bool()
36    val vsew     = VtypeVSew()
37    val vlmul    = VLmul()
38  }
39
40  object VType {
41    def apply()(implicit p: Parameters) : VType = {
42      new VType
43    }
44
45    def fromInstVType(instVType: InstVType)(implicit p: Parameters) : VType = {
46      val res = Wire(VType())
47      res.vma   := instVType.vma
48      res.vta   := instVType.vta
49      res.vsew  := instVType.vsew(VSew.width - 1, 0)
50      res.vlmul := instVType.vlmul
51      res.illegal := false.B // Todo: add illegal check function
52      res
53    }
54
55    def fromVtypeStruct(vtypeStruct: VtypeStruct)(implicit p: Parameters): VType = {
56      val res = Wire(VType())
57      res.illegal := vtypeStruct.vill
58      res.vma := vtypeStruct.vma
59      res.vta := vtypeStruct.vta
60      res.vsew := vtypeStruct.vsew(VSew.width - 1, 0)
61      res.vlmul := vtypeStruct.vlmul
62      res
63    }
64
65    def toVtypeStruct(vtype: VType)(implicit p: Parameters) : VtypeStruct = {
66      val res = WireInit(0.U.asTypeOf(new VtypeStruct))
67      res.vill := vtype.illegal
68      res.vma := vtype.vma
69      res.vta := vtype.vta
70      res.vsew := Cat(0.U(1.W), vtype.vsew)
71      res.vlmul := vtype.vlmul
72      res
73    }
74
75    def initVtype()(implicit p: Parameters) : VType = {
76      val res = Wire(VType())
77      res.illegal := true.B
78      res.vma := false.B
79      res.vta := false.B
80      res.vsew := 0.U
81      res.vlmul := 0.U
82      res
83    }
84  }
85
86  object VsetVType {
87    def apply()(implicit p: Parameters) : VsetVType = {
88      new VsetVType
89    }
90
91    def fromInstVType(instVType: InstVType)(implicit p: Parameters) : VsetVType = {
92      val res = Wire(VsetVType())
93      res.vma   := instVType.vma
94      res.vta   := instVType.vta
95      res.vsew  := instVType.vsew
96      res.vlmul := instVType.vlmul
97      res.illegal := false.B
98      res.reserved := instVType.reserved
99      res
100    }
101
102    def fromVtypeStruct(vtypeStruct: VtypeStruct)(implicit p: Parameters): VsetVType = {
103      val res = Wire(VsetVType())
104      res.illegal := vtypeStruct.vill
105      res.vma := vtypeStruct.vma
106      res.vta := vtypeStruct.vta
107      res.vsew := vtypeStruct.vsew
108      res.vlmul := vtypeStruct.vlmul
109      res.reserved := vtypeStruct.reserved
110      res
111    }
112  }
113
114  class VConfig(implicit p: Parameters) extends Bundle {
115    val vtype = new VType
116    val vl    = Vl()
117  }
118
119  object VConfig {
120    def apply()(implicit p: Parameters) : VConfig = {
121      new VConfig()
122    }
123  }
124
125  def mu: UInt = 0.U(1.W)
126  def ma: UInt = 1.U(1.W)
127  def tu: UInt = 0.U(1.W)
128  def ta: UInt = 1.U(1.W)
129
130  // modify the width when support more vector data width
131  object VSew extends NamedUInt(2) {
132    def e8  : UInt = "b000".U(width.W)
133    def e16 : UInt = "b001".U(width.W)
134    def e32 : UInt = "b010".U(width.W)
135    def e64 : UInt = "b011".U(width.W)
136
137    def reserved: BitPat = BitPat("b1??")
138
139    def isReserved(sew: UInt) : Bool = {
140      require(sew.getWidth >= 2 && sew.getWidth <= 3)
141      if (sew.getWidth == 3) {
142        sew === reserved
143      } else {
144        false.B
145      }
146    }
147  }
148
149  object VtypeVSew extends NamedUInt(3)
150
151  object VLmul extends NamedUInt(3) {
152    def m1  : UInt = "b000".U(width.W)
153    def m2  : UInt = "b001".U(width.W)
154    def m4  : UInt = "b010".U(width.W)
155    def m8  : UInt = "b011".U(width.W)
156    def mf2 : UInt = "b111".U(width.W)
157    def mf4 : UInt = "b110".U(width.W)
158    def mf8 : UInt = "b101".U(width.W)
159
160    def reserved: BitPat = BitPat("b100")
161
162    def isReserved(vlmul: UInt) : Bool = {
163      require(vlmul.getWidth == 3)
164      vlmul === reserved
165    }
166  }
167
168  object Vl {
169    def apply()(implicit p: Parameters): UInt = UInt(width.W)
170
171    def width(implicit p: Parameters) = p(XSCoreParamsKey).vlWidth
172  }
173
174  object Vstart {
175    def apply()(implicit p: Parameters): UInt = UInt(width.W)
176
177    def width(implicit p: Parameters) = p(XSCoreParamsKey).vlWidth - 1
178  }
179
180  object Vxsat extends NamedUInt(1)
181
182  object Vxrm extends NamedUInt(2)
183
184  object Nf extends NamedUInt(3)
185
186  object VEew extends NamedUInt(2)
187
188  object NumLsElem {
189    def apply()(implicit p: Parameters): UInt = UInt(width.W)
190
191    def width(implicit p: Parameters) = log2Up(p(XSCoreParamsKey).maxElemPerVreg) + 1
192  }
193
194  class Fpu extends Bundle{
195    val isFpToVecInst = Bool()
196    val isFP32Instr   = Bool()
197    val isFP64Instr   = Bool()
198    val isReduction   = Bool()
199    val isFoldTo1_2   = Bool()
200    val isFoldTo1_4   = Bool()
201    val isFoldTo1_8   = Bool()
202  }
203  object Fpu {
204    def apply() = new Fpu
205  }
206}
207