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