xref: /XiangShan/src/main/scala/xiangshan/mem/vector/VecCommon.scala (revision 315e1323fd6f1542589678395105b3e9e207e3db)
1/***************************************************************************************
2  * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3  * Copyright (c) 2020-2021 Peng Cheng Laboratory
4  *
5  * XiangShan is licensed under Mulan PSL v2.
6  * You can use this software according to the terms and conditions of the Mulan PSL v2.
7  * You may obtain a copy of Mulan PSL v2 at:
8  *          http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  *
14  * See the Mulan PSL v2 for more details.
15  ***************************************************************************************/
16
17package xiangshan.mem
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import utils._
23import utility._
24import xiangshan._
25import xiangshan.backend.rob.RobPtr
26import xiangshan.backend.Bundles._
27import xiangshan.backend.fu.FuType
28
29/**
30  * Common used parameters or functions in vlsu
31  */
32trait VLSUConstants {
33  val VLEN = 128
34  //for pack unit-stride flow
35  val AlignedNum = 4 // 1/2/4/8
36  def VLENB = VLEN/8
37  def vOffsetBits = log2Up(VLENB) // bits-width to index offset inside a vector reg
38  lazy val vlmBindexBits = 8 //will be overrided later
39  lazy val vsmBindexBits = 8 // will be overrided later
40
41  def alignTypes = 5 // eew/sew = 1/2/4/8, last indicate 128 bit element
42  def alignTypeBits = log2Up(alignTypes)
43  def maxMUL = 8
44  def maxFields = 8
45  /**
46    * In the most extreme cases like a segment indexed instruction, eew=64, emul=8, sew=8, lmul=1,
47    * and nf=8, each data reg is mapped with 8 index regs and there are 8 data regs in total,
48    * each for a field. Therefore an instruction can be divided into 64 uops at most.
49    */
50  def maxUopNum = maxMUL * maxFields // 64
51  def maxFlowNum = 16
52  def maxElemNum = maxMUL * maxFlowNum // 128
53  // def uopIdxBits = log2Up(maxUopNum) // to index uop inside an robIdx
54  def elemIdxBits = log2Up(maxElemNum) + 1 // to index which element in an instruction
55  def flowIdxBits = log2Up(maxFlowNum) + 1 // to index which flow in a uop
56  def fieldBits = log2Up(maxFields) + 1 // 4-bits to indicate 1~8
57
58  def ewBits = 3 // bits-width of EEW/SEW
59  def mulBits = 3 // bits-width of emul/lmul
60
61  def getSlice(data: UInt, i: Int, alignBits: Int): UInt = {
62    require(data.getWidth >= (i+1) * alignBits)
63    data((i+1) * alignBits - 1, i * alignBits)
64  }
65
66  def getByte(data: UInt, i: Int = 0) = getSlice(data, i, 8)
67  def getHalfWord(data: UInt, i: Int = 0) = getSlice(data, i, 16)
68  def getWord(data: UInt, i: Int = 0) = getSlice(data, i, 32)
69  def getDoubleWord(data: UInt, i: Int = 0) = getSlice(data, i, 64)
70  def getDoubleDoubleWord(data: UInt, i: Int = 0) = getSlice(data, i, 128)
71}
72
73trait HasVLSUParameters extends HasXSParameter with VLSUConstants {
74  override val VLEN = coreParams.VLEN
75  override lazy val vlmBindexBits = log2Up(coreParams.VlMergeBufferSize)
76  override lazy val vsmBindexBits = log2Up(coreParams.VsMergeBufferSize)
77  def isUnitStride(instType: UInt) = instType(1, 0) === "b00".U
78  def isStrided(instType: UInt) = instType(1, 0) === "b10".U
79  def isIndexed(instType: UInt) = instType(0) === "b1".U
80  def isNotIndexed(instType: UInt) = instType(0) === "b0".U
81  def isSegment(instType: UInt) = instType(2) === "b1".U
82  def is128Bit(alignedType: UInt) = alignedType(2) === "b1".U
83
84  def mergeDataWithMask(oldData: UInt, newData: UInt, mask: UInt): Vec[UInt] = {
85    require(oldData.getWidth == newData.getWidth)
86    require(oldData.getWidth == mask.getWidth * 8)
87    VecInit(mask.asBools.zipWithIndex.map { case (en, i) =>
88      Mux(en, getByte(newData, i), getByte(oldData, i))
89    })
90  }
91
92  // def asBytes(data: UInt) = {
93  //   require(data.getWidth % 8 == 0)
94  //   (0 until data.getWidth/8).map(i => getByte(data, i))
95  // }
96
97  def mergeDataWithElemIdx(
98    oldData: UInt,
99    newData: Seq[UInt],
100    alignedType: UInt,
101    elemIdx: Seq[UInt],
102    valids: Seq[Bool]
103  ): UInt = {
104    require(newData.length == elemIdx.length)
105    require(newData.length == valids.length)
106    LookupTree(alignedType, List(
107      "b00".U -> VecInit(elemIdx.map(e => UIntToOH(e(3, 0)).asBools).transpose.zipWithIndex.map { case (selVec, i) =>
108        ParallelPosteriorityMux(
109          true.B +: selVec.zip(valids).map(x => x._1 && x._2),
110          getByte(oldData, i) +: newData.map(getByte(_))
111        )}).asUInt,
112      "b01".U -> VecInit(elemIdx.map(e => UIntToOH(e(2, 0)).asBools).transpose.zipWithIndex.map { case (selVec, i) =>
113        ParallelPosteriorityMux(
114          true.B +: selVec.zip(valids).map(x => x._1 && x._2),
115          getHalfWord(oldData, i) +: newData.map(getHalfWord(_))
116        )}).asUInt,
117      "b10".U -> VecInit(elemIdx.map(e => UIntToOH(e(1, 0)).asBools).transpose.zipWithIndex.map { case (selVec, i) =>
118        ParallelPosteriorityMux(
119          true.B +: selVec.zip(valids).map(x => x._1 && x._2),
120          getWord(oldData, i) +: newData.map(getWord(_))
121        )}).asUInt,
122      "b11".U -> VecInit(elemIdx.map(e => UIntToOH(e(0)).asBools).transpose.zipWithIndex.map { case (selVec, i) =>
123        ParallelPosteriorityMux(
124          true.B +: selVec.zip(valids).map(x => x._1 && x._2),
125          getDoubleWord(oldData, i) +: newData.map(getDoubleWord(_))
126        )}).asUInt
127    ))
128  }
129
130  def mergeDataWithElemIdx(oldData: UInt, newData: UInt, alignedType: UInt, elemIdx: UInt): UInt = {
131    mergeDataWithElemIdx(oldData, Seq(newData), alignedType, Seq(elemIdx), Seq(true.B))
132  }
133  /**
134    * for merge 128-bits data of unit-stride
135    */
136  object mergeDataByoffset{
137    def apply(oldData: UInt, newData: Seq[UInt], mask: Seq[UInt], offset: Seq[UInt], valids: Seq[Bool]): UInt = {
138      require(newData.length == valids.length)
139      require(newData.length == offset.length)
140      // if (i>offset[k] && mask[k][i]==1 && valid[k]) -> newData, else -> oldData
141      val selVec = (mask zip offset).map{case (m,e) =>
142        ((~UIntToMask(e, VLENB)).asBools.zip(m.asBools).map(x=> x._1 && x._2))}.transpose // vector(3,16)
143
144      VecInit(selVec.zipWithIndex.map{ case (selV, i) => // selV: vector(3,1), 0=<i<16
145        ParallelPosteriorityMux(
146          true.B +: selV.zip(valids).map(x => x._1 && x._2),
147          getByte(oldData, i) +: newData.map(getByte(_, i))
148        )}).asUInt
149    }
150  }
151  def mergeDataByoffset(oldData: UInt, newData: UInt, mask: UInt, offset: UInt): UInt = {
152    mergeDataByoffset(oldData, Seq(newData), Seq(mask), Seq(offset), Seq(true.B))
153  }
154}
155abstract class VLSUModule(implicit p: Parameters) extends XSModule
156  with HasVLSUParameters
157  with HasCircularQueuePtrHelper
158abstract class VLSUBundle(implicit p: Parameters) extends XSBundle
159  with HasVLSUParameters
160
161class VLSUBundleWithMicroOp(implicit p: Parameters) extends VLSUBundle {
162  val uop = new DynInst
163}
164
165class OnlyVecExuOutput(implicit p: Parameters) extends VLSUBundle {
166  val isvec = Bool()
167  val vecdata = UInt(VLEN.W)
168  val mask = UInt(VLENB.W)
169  // val rob_idx_valid = Vec(2, Bool())
170  // val inner_idx = Vec(2, UInt(3.W))
171  // val rob_idx = Vec(2, new RobPtr)
172  // val offset = Vec(2, UInt(4.W))
173  val reg_offset = UInt(vOffsetBits.W)
174  val vecActive = Bool() // 1: vector active element, 0: vector not active element
175  val is_first_ele = Bool()
176  val elemIdx = UInt(elemIdxBits.W) // element index
177  val elemIdxInsideVd = UInt(elemIdxBits.W) // element index in scope of vd
178  // val uopQueuePtr = new VluopPtr
179  // val flowPtr = new VlflowPtr
180}
181
182class VecExuOutput(implicit p: Parameters) extends MemExuOutput with HasVLSUParameters {
183  val vec = new OnlyVecExuOutput
184  val alignedType       = UInt(alignTypeBits.W)
185   // feedback
186  val vecFeedback       = Bool()
187}
188
189// class VecStoreExuOutput(implicit p: Parameters) extends MemExuOutput with HasVLSUParameters {
190//   val elemIdx = UInt(elemIdxBits.W)
191//   val uopQueuePtr = new VsUopPtr
192//   val fieldIdx = UInt(fieldBits.W)
193//   val segmentIdx = UInt(elemIdxBits.W)
194//   val vaddr = UInt(VAddrBits.W)
195//   // pack
196//   val isPackage         = Bool()
197//   val packageNum        = UInt((log2Up(VLENB) + 1).W)
198//   val originAlignedType = UInt(alignTypeBits.W)
199//   val alignedType       = UInt(alignTypeBits.W)
200// }
201
202class VecUopBundle(implicit p: Parameters) extends VLSUBundleWithMicroOp {
203  val flowMask       = UInt(VLENB.W) // each bit for a flow
204  val byteMask       = UInt(VLENB.W) // each bit for a byte
205  val data           = UInt(VLEN.W)
206  // val fof            = Bool() // fof is only used for vector loads
207  val excp_eew_index = UInt(elemIdxBits.W)
208  // val exceptionVec   = ExceptionVec() // uop has exceptionVec
209  val baseAddr = UInt(VAddrBits.W)
210  val stride = UInt(VLEN.W)
211  val flow_counter = UInt(flowIdxBits.W)
212
213  // instruction decode result
214  val flowNum = UInt(flowIdxBits.W) // # of flows in a uop
215  // val flowNumLog2 = UInt(log2Up(flowIdxBits).W) // log2(flowNum), for better timing of multiplication
216  val nfields = UInt(fieldBits.W) // NFIELDS
217  val vm = Bool() // whether vector masking is enabled
218  val usWholeReg = Bool() // unit-stride, whole register load
219  val usMaskReg = Bool() // unit-stride, masked store/load
220  val eew = UInt(ewBits.W) // size of memory elements
221  val sew = UInt(ewBits.W)
222  val emul = UInt(mulBits.W)
223  val lmul = UInt(mulBits.W)
224  val vlmax = UInt(elemIdxBits.W)
225  val instType = UInt(3.W)
226  val vd_last_uop = Bool()
227  val vd_first_uop = Bool()
228}
229
230class VecFlowBundle(implicit p: Parameters) extends VLSUBundleWithMicroOp {
231  val vaddr             = UInt(VAddrBits.W)
232  val mask              = UInt(VLENB.W)
233  val alignedType       = UInt(alignTypeBits.W)
234  val vecActive         = Bool()
235  val elemIdx           = UInt(elemIdxBits.W)
236  val is_first_ele      = Bool()
237
238  // pack
239  val isPackage         = Bool()
240  val packageNum        = UInt((log2Up(VLENB) + 1).W)
241  val originAlignedType = UInt(alignTypeBits.W)
242}
243
244class VecMemExuOutput(isVector: Boolean = false)(implicit p: Parameters) extends VLSUBundle{
245  val output = new MemExuOutput(isVector)
246  val vecFeedback = Bool()
247  val mmio = Bool()
248  val usSecondInv = Bool()
249  val elemIdx = UInt(elemIdxBits.W)
250  val alignedType = UInt(alignTypeBits.W)
251  val mbIndex     = UInt(vsmBindexBits.W)
252}
253
254object MulNum {
255  def apply (mul: UInt): UInt = { //mul means emul or lmul
256    (LookupTree(mul,List(
257      "b101".U -> 1.U , // 1/8
258      "b110".U -> 1.U , // 1/4
259      "b111".U -> 1.U , // 1/2
260      "b000".U -> 1.U , // 1
261      "b001".U -> 2.U , // 2
262      "b010".U -> 4.U , // 4
263      "b011".U -> 8.U   // 8
264    )))}
265}
266/**
267  * when emul is greater than or equal to 1, this means the entire register needs to be written;
268  * otherwise, only write the specified number of bytes */
269object MulDataSize {
270  def apply (mul: UInt): UInt = { //mul means emul or lmul
271    (LookupTree(mul,List(
272      "b101".U -> 2.U  , // 1/8
273      "b110".U -> 4.U  , // 1/4
274      "b111".U -> 8.U  , // 1/2
275      "b000".U -> 16.U , // 1
276      "b001".U -> 16.U , // 2
277      "b010".U -> 16.U , // 4
278      "b011".U -> 16.U   // 8
279    )))}
280}
281
282object OneRegNum {
283  def apply (eew: UInt): UInt = { //mul means emul or lmul
284    (LookupTree(eew,List(
285      "b000".U -> 16.U , // 1
286      "b101".U -> 8.U , // 2
287      "b110".U -> 4.U , // 4
288      "b111".U -> 2.U   // 8
289    )))}
290}
291
292//index inst read data byte
293object SewDataSize {
294  def apply (sew: UInt): UInt = {
295    (LookupTree(sew,List(
296      "b000".U -> 1.U , // 1
297      "b001".U -> 2.U , // 2
298      "b010".U -> 4.U , // 4
299      "b011".U -> 8.U   // 8
300    )))}
301}
302
303// strided inst read data byte
304object EewDataSize {
305  def apply (eew: UInt): UInt = {
306    (LookupTree(eew,List(
307      "b000".U -> 1.U , // 1
308      "b101".U -> 2.U , // 2
309      "b110".U -> 4.U , // 4
310      "b111".U -> 8.U   // 8
311    )))}
312}
313
314object loadDataSize {
315  def apply (instType: UInt, emul: UInt, eew: UInt, sew: UInt): UInt = {
316    (LookupTree(instType,List(
317      "b000".U ->  MulDataSize(emul), // unit-stride
318      "b010".U ->  EewDataSize(eew)  , // strided
319      "b001".U ->  SewDataSize(sew)  , // indexed-unordered
320      "b011".U ->  SewDataSize(sew)  , // indexed-ordered
321      "b100".U ->  EewDataSize(eew)  , // segment unit-stride
322      "b110".U ->  EewDataSize(eew)  , // segment strided
323      "b101".U ->  SewDataSize(sew)  , // segment indexed-unordered
324      "b111".U ->  SewDataSize(sew)    // segment indexed-ordered
325    )))}
326}
327
328object storeDataSize {
329  def apply (instType: UInt, eew: UInt, sew: UInt): UInt = {
330    (LookupTree(instType,List(
331      "b000".U ->  EewDataSize(eew)  , // unit-stride, do not use
332      "b010".U ->  EewDataSize(eew)  , // strided
333      "b001".U ->  SewDataSize(sew)  , // indexed-unordered
334      "b011".U ->  SewDataSize(sew)  , // indexed-ordered
335      "b100".U ->  EewDataSize(eew)  , // segment unit-stride
336      "b110".U ->  EewDataSize(eew)  , // segment strided
337      "b101".U ->  SewDataSize(sew)  , // segment indexed-unordered
338      "b111".U ->  SewDataSize(sew)    // segment indexed-ordered
339    )))}
340}
341
342object GenVecStoreMask {
343  def apply (instType: UInt, eew: UInt, sew: UInt): UInt = {
344    val mask = Wire(UInt(16.W))
345    mask := UIntToOH(storeDataSize(instType = instType, eew = eew, sew = sew)) - 1.U
346    mask
347  }
348}
349
350/**
351  * these are used to obtain immediate addresses for  index instruction */
352object EewEq8 {
353  def apply(index:UInt, flow_inner_idx: UInt): UInt = {
354    (LookupTree(flow_inner_idx,List(
355      0.U  -> index(7 ,0   ),
356      1.U  -> index(15,8   ),
357      2.U  -> index(23,16  ),
358      3.U  -> index(31,24  ),
359      4.U  -> index(39,32  ),
360      5.U  -> index(47,40  ),
361      6.U  -> index(55,48  ),
362      7.U  -> index(63,56  ),
363      8.U  -> index(71,64  ),
364      9.U  -> index(79,72  ),
365      10.U -> index(87,80  ),
366      11.U -> index(95,88  ),
367      12.U -> index(103,96 ),
368      13.U -> index(111,104),
369      14.U -> index(119,112),
370      15.U -> index(127,120)
371    )))}
372}
373
374object EewEq16 {
375  def apply(index: UInt, flow_inner_idx: UInt): UInt = {
376    (LookupTree(flow_inner_idx, List(
377      0.U -> index(15, 0),
378      1.U -> index(31, 16),
379      2.U -> index(47, 32),
380      3.U -> index(63, 48),
381      4.U -> index(79, 64),
382      5.U -> index(95, 80),
383      6.U -> index(111, 96),
384      7.U -> index(127, 112)
385    )))}
386}
387
388object EewEq32 {
389  def apply(index: UInt, flow_inner_idx: UInt): UInt = {
390    (LookupTree(flow_inner_idx, List(
391      0.U -> index(31, 0),
392      1.U -> index(63, 32),
393      2.U -> index(95, 64),
394      3.U -> index(127, 96)
395    )))}
396}
397
398object EewEq64 {
399  def apply (index: UInt, flow_inner_idx: UInt): UInt = {
400    (LookupTree(flow_inner_idx, List(
401      0.U -> index(63, 0),
402      1.U -> index(127, 64)
403    )))}
404}
405
406object IndexAddr {
407  def apply (index: UInt, flow_inner_idx: UInt, eew: UInt): UInt = {
408    (LookupTree(eew,List(
409      "b000".U -> EewEq8 (index = index, flow_inner_idx = flow_inner_idx ), // Imm is 1 Byte // TODO: index maybe cross register
410      "b101".U -> EewEq16(index = index, flow_inner_idx = flow_inner_idx ), // Imm is 2 Byte
411      "b110".U -> EewEq32(index = index, flow_inner_idx = flow_inner_idx ), // Imm is 4 Byte
412      "b111".U -> EewEq64(index = index, flow_inner_idx = flow_inner_idx )  // Imm is 8 Byte
413    )))}
414}
415
416object Log2Num {
417  def apply (num: UInt): UInt = {
418    (LookupTree(num,List(
419      16.U -> 4.U,
420      8.U  -> 3.U,
421      4.U  -> 2.U,
422      2.U  -> 1.U,
423      1.U  -> 0.U
424    )))}
425}
426
427object GenUopIdxInField {
428  def apply (instType: UInt, emul: UInt, lmul: UInt, uopIdx: UInt): UInt = {
429    val isIndexed = instType(0)
430    val mulInField = Mux(
431      isIndexed,
432      Mux(lmul.asSInt > emul.asSInt, lmul, emul),
433      emul
434    )
435    LookupTree(mulInField, List(
436      "b101".U -> 0.U,
437      "b110".U -> 0.U,
438      "b111".U -> 0.U,
439      "b000".U -> 0.U,
440      "b001".U -> uopIdx(0),
441      "b010".U -> uopIdx(1, 0),
442      "b011".U -> uopIdx(2, 0)
443    ))
444  }
445}
446
447//eew decode
448object EewLog2 extends VLSUConstants {
449  // def apply (eew: UInt): UInt = {
450  //   (LookupTree(eew,List(
451  //     "b000".U -> "b000".U , // 1
452  //     "b101".U -> "b001".U , // 2
453  //     "b110".U -> "b010".U , // 4
454  //     "b111".U -> "b011".U   // 8
455  //   )))}
456  def apply(eew: UInt): UInt = ZeroExt(eew(1, 0), ewBits)
457}
458
459/**
460  * unit-stride instructions don't use this method;
461  * other instructions generate realFlowNum by EmulDataSize >> eew(1,0),
462  * EmulDataSize means the number of bytes that need to be written to the register,
463  * eew(1,0) means the number of bytes written at once*/
464object GenRealFlowNum {
465  def apply (instType: UInt, emul: UInt, lmul: UInt, eew: UInt, sew: UInt): UInt = {
466    (LookupTree(instType,List(
467      "b000".U ->  (MulDataSize(emul) >> eew(1,0)).asUInt, // store use, load do not use
468      "b010".U ->  (MulDataSize(emul) >> eew(1,0)).asUInt, // strided
469      "b001".U ->  Mux(emul.asSInt > lmul.asSInt, (MulDataSize(emul) >> eew(1,0)).asUInt, (MulDataSize(lmul) >> sew(1,0)).asUInt), // indexed-unordered
470      "b011".U ->  Mux(emul.asSInt > lmul.asSInt, (MulDataSize(emul) >> eew(1,0)).asUInt, (MulDataSize(lmul) >> sew(1,0)).asUInt), // indexed-ordered
471      "b100".U ->  (MulDataSize(emul) >> eew(1,0)).asUInt, // segment unit-stride
472      "b110".U ->  (MulDataSize(emul) >> eew(1,0)).asUInt, // segment strided
473      "b101".U ->  Mux(emul.asSInt > lmul.asSInt, (MulDataSize(emul) >> eew(1,0)).asUInt, (MulDataSize(lmul) >> sew(1,0)).asUInt), // segment indexed-unordered
474      "b111".U ->  Mux(emul.asSInt > lmul.asSInt, (MulDataSize(emul) >> eew(1,0)).asUInt, (MulDataSize(lmul) >> sew(1,0)).asUInt)  // segment indexed-ordered
475    )))}
476}
477
478/**
479  * GenRealFlowLog2 = Log2(GenRealFlowNum)
480  */
481object GenRealFlowLog2 extends VLSUConstants {
482  def apply(instType: UInt, emul: UInt, lmul: UInt, eew: UInt, sew: UInt): UInt = {
483    val emulLog2 = Mux(emul.asSInt >= 0.S, 0.U, emul)
484    val lmulLog2 = Mux(lmul.asSInt >= 0.S, 0.U, lmul)
485    val eewRealFlowLog2 = emulLog2 + log2Up(VLENB).U - eew(1, 0)
486    val sewRealFlowLog2 = lmulLog2 + log2Up(VLENB).U - sew(1, 0)
487    (LookupTree(instType, List(
488      "b000".U -> eewRealFlowLog2, // unit-stride
489      "b010".U -> eewRealFlowLog2, // strided
490      "b001".U -> Mux(emul.asSInt > lmul.asSInt, eewRealFlowLog2, sewRealFlowLog2), // indexed-unordered
491      "b011".U -> Mux(emul.asSInt > lmul.asSInt, eewRealFlowLog2, sewRealFlowLog2), // indexed-ordered
492      "b100".U -> eewRealFlowLog2, // segment unit-stride
493      "b110".U -> eewRealFlowLog2, // segment strided
494      "b101".U -> Mux(emul.asSInt > lmul.asSInt, eewRealFlowLog2, sewRealFlowLog2), // segment indexed-unordered
495      "b111".U -> Mux(emul.asSInt > lmul.asSInt, eewRealFlowLog2, sewRealFlowLog2), // segment indexed-ordered
496    )))
497  }
498}
499
500/**
501  * GenElemIdx generals an element index within an instruction, given a certain uopIdx and a known flowIdx
502  * inside the uop.
503  */
504object GenElemIdx extends VLSUConstants {
505  def apply(instType: UInt, emul: UInt, lmul: UInt, eew: UInt, sew: UInt,
506    uopIdx: UInt, flowIdx: UInt): UInt = {
507    val isIndexed = instType(0).asBool
508    val eewUopFlowsLog2 = Mux(emul.asSInt > 0.S, 0.U, emul) + log2Up(VLENB).U - eew(1, 0)
509    val sewUopFlowsLog2 = Mux(lmul.asSInt > 0.S, 0.U, lmul) + log2Up(VLENB).U - sew(1, 0)
510    val uopFlowsLog2 = Mux(
511      isIndexed,
512      Mux(emul.asSInt > lmul.asSInt, eewUopFlowsLog2, sewUopFlowsLog2),
513      eewUopFlowsLog2
514    )
515    LookupTree(uopFlowsLog2, List(
516      0.U -> uopIdx,
517      1.U -> uopIdx ## flowIdx(0),
518      2.U -> uopIdx ## flowIdx(1, 0),
519      3.U -> uopIdx ## flowIdx(2, 0),
520      4.U -> uopIdx ## flowIdx(3, 0)
521    ))
522  }
523}
524
525/**
526  * GenVLMAX calculates VLMAX, which equals MUL * ew
527  */
528object GenVLMAXLog2 extends VLSUConstants {
529  def apply(lmul: UInt, sew: UInt): UInt = lmul + log2Up(VLENB).U - sew
530}
531object GenVLMAX {
532  def apply(lmul: UInt, sew: UInt): UInt = 1.U << GenVLMAXLog2(lmul, sew)
533}
534
535object GenUSWholeRegVL extends VLSUConstants {
536  def apply(nfields: UInt, eew: UInt): UInt = {
537    LookupTree(eew(1, 0), List(
538      "b00".U -> (nfields << (log2Up(VLENB) - 0)),
539      "b01".U -> (nfields << (log2Up(VLENB) - 1)),
540      "b10".U -> (nfields << (log2Up(VLENB) - 2)),
541      "b11".U -> (nfields << (log2Up(VLENB) - 3))
542    ))
543  }
544}
545object GenUSWholeEmul extends VLSUConstants{
546  def apply(nf: UInt): UInt={
547    LookupTree(nf,List(
548      "b000".U -> "b000".U(mulBits.W),
549      "b001".U -> "b001".U(mulBits.W),
550      "b011".U -> "b010".U(mulBits.W),
551      "b111".U -> "b011".U(mulBits.W)
552    ))
553  }
554}
555
556
557object GenUSMaskRegVL extends VLSUConstants {
558  def apply(vl: UInt): UInt = {
559    Mux(vl(2,0) === 0.U , (vl >> 3.U), ((vl >> 3.U) + 1.U))
560  }
561}
562
563object GenUopByteMask {
564  def apply(flowMask: UInt, alignedType: UInt): UInt = {
565    LookupTree(alignedType, List(
566      "b000".U -> flowMask,
567      "b001".U -> FillInterleaved(2, flowMask),
568      "b010".U -> FillInterleaved(4, flowMask),
569      "b011".U -> FillInterleaved(8, flowMask),
570      "b100".U -> FillInterleaved(16, flowMask)
571    ))
572  }
573}
574
575object GenVdIdxInField extends VLSUConstants {
576  def apply(instType: UInt, emul: UInt, lmul: UInt, uopIdx: UInt): UInt = {
577    val vdIdx = Wire(UInt(log2Up(maxMUL).W))
578    when (instType(1,0) === "b00".U || instType(1,0) === "b10".U || lmul.asSInt > emul.asSInt) {
579      // Unit-stride or Strided, or indexed with lmul >= emul
580      vdIdx := uopIdx
581    }.otherwise {
582      // Indexed with lmul <= emul
583      val multiple = emul - lmul
584      val uopIdxWidth = uopIdx.getWidth
585      vdIdx := LookupTree(multiple, List(
586        0.U -> uopIdx,
587        1.U -> (uopIdx >> 1),
588        2.U -> (uopIdx >> 2),
589        3.U -> (uopIdx >> 3)
590      ))
591    }
592    vdIdx
593  }
594}
595/**
596* Use start and vl to generate flow activative mask
597* mod = true fill 0
598* mod = false fill 1
599*/
600object GenFlowMask extends VLSUConstants {
601  def apply(elementMask: UInt, start: UInt, vl: UInt , mod: Boolean): UInt = {
602    val startMask = ~UIntToMask(start, VLEN)
603    val vlMask = UIntToMask(vl, VLEN)
604    val maskVlStart = vlMask & startMask
605    if(mod){
606      elementMask & maskVlStart
607    }
608    else{
609      (~elementMask).asUInt & maskVlStart
610    }
611  }
612}
613
614object CheckAligned extends VLSUConstants {
615  def apply(addr: UInt): UInt = {
616    val aligned_16 = (addr(0) === 0.U) // 16-bit
617    val aligned_32 = (addr(1,0) === 0.U) // 32-bit
618    val aligned_64 = (addr(2,0) === 0.U) // 64-bit
619    val aligned_128 = (addr(3,0) === 0.U) // 128-bit
620    Cat(true.B, aligned_16, aligned_32, aligned_64, aligned_128)
621  }
622}
623
624/**
625  search if mask have continue 'len' bit '1'
626  mask: source mask
627  len: search length
628*/
629object GenPackMask{
630  def leadX(mask: Seq[Bool], len: Int): Bool = {
631    if(len == 1){
632      mask.head
633    }
634    else{
635      leadX(mask.drop(1),len-1) & mask.head
636    }
637  }
638  def leadOneVec(shiftMask: Seq[Bool]): UInt = {
639    // max is 64-bit, so the max num of flow to pack is 8
640
641    val lead1 = leadX(shiftMask, 1) // continue 1 bit
642    val lead2 = leadX(shiftMask, 2) // continue 2 bit
643    val lead4 = leadX(shiftMask, 4) // continue 4 bit
644    val lead8 = leadX(shiftMask, 8) // continue 8 bit
645    val lead16 = leadX(shiftMask, 16) // continue 16 bit
646    Cat(lead1, lead2, lead4, lead8, lead16)
647  }
648
649  def apply(shiftMask: UInt) = {
650    // pack mask
651    val packMask = leadOneVec(shiftMask.asBools)
652    packMask
653  }
654}
655/**
656PackEnable = (LeadXVec >> eew) & alignedVec, where the 0th bit represents the ability to merge into a 64 bit flow, the second bit represents the ability to merge into a 32 bit flow, and so on.
657
658example:
659  addr = 0x0, activeMask = b00011100101111, flowIdx = 0, eew = 0(8-bit)
660
661  step 0 : addrAlignedVec = (1, 1, 1, 1) elemIdxAligned = (1, 1, 1, 1)
662  step 1 : activePackVec = (1, 1, 1, 0), inactivePackVec = (0, 0, 0, 0)
663  step 2 : activePackEnable = (1, 1, 1, 0), inactivePackVec = (0, 0, 0, 0)
664
665  we can package 4 8-bit activative flows into a 32-bit flow.
666*/
667object GenPackVec extends VLSUConstants{
668  def apply(addr: UInt, shiftMask: UInt, eew: UInt, elemIdx: UInt): UInt = {
669    val addrAlignedVec = CheckAligned(addr)
670    val elemIdxAligned = CheckAligned(elemIdx)
671    val packMask = GenPackMask(shiftMask)
672    // generate packVec
673    val packVec = addrAlignedVec & elemIdxAligned & (packMask.asUInt >> eew)
674
675    packVec
676  }
677}
678
679object GenPackAlignedType extends VLSUConstants{
680  def apply(packVec: UInt): UInt = {
681    val packAlignedType = PriorityMux(Seq(
682      packVec(0) -> "b100".U,
683      packVec(1) -> "b011".U,
684      packVec(2) -> "b010".U,
685      packVec(3) -> "b001".U,
686      packVec(4) -> "b000".U
687    ))
688    packAlignedType
689  }
690}
691
692object GenPackNum extends VLSUConstants{
693  def apply(alignedType: UInt, packAlignedType: UInt): UInt = {
694    (1.U << (packAlignedType - alignedType)).asUInt
695  }
696}
697
698object genVWmask128 {
699  def apply(addr: UInt, sizeEncode: UInt): UInt = {
700    (LookupTree(sizeEncode, List(
701      "b000".U -> 0x1.U, //0001 << addr(2:0)
702      "b001".U -> 0x3.U, //0011
703      "b010".U -> 0xf.U, //1111
704      "b011".U -> 0xff.U, //11111111
705      "b100".U -> 0xffff.U //1111111111111111
706    )) << addr(3, 0)).asUInt
707  }
708}
709/*
710* only use in max length is 128
711*/
712object genVWdata {
713  def apply(data: UInt, sizeEncode: UInt): UInt = {
714    LookupTree(sizeEncode, List(
715      "b000".U -> Fill(16, data(7, 0)),
716      "b001".U -> Fill(8, data(15, 0)),
717      "b010".U -> Fill(4, data(31, 0)),
718      "b011".U -> Fill(2, data(63,0)),
719      "b100".U -> data(127,0)
720    ))
721  }
722}
723
724object genUSSplitAddr{
725  def apply(addr: UInt, index: UInt): UInt = {
726    val tmpAddr = Cat(addr(38, 4), 0.U(4.W))
727    val nextCacheline = tmpAddr + 16.U
728    LookupTree(index, List(
729      0.U -> tmpAddr,
730      1.U -> nextCacheline
731    ))
732  }
733}
734
735object genUSSplitMask{
736  def apply(mask: UInt, index: UInt, addrOffset: UInt): UInt = {
737    val tmpMask = Cat(0.U(16.W),mask) << addrOffset // 32-bits
738    LookupTree(index, List(
739      0.U -> tmpMask(15, 0),
740      1.U -> tmpMask(31, 16),
741    ))
742  }
743}
744
745object genUSSplitData{
746  def apply(data: UInt, index: UInt, addrOffset: UInt): UInt = {
747    val tmpData = WireInit(0.U(256.W))
748    val lookupTable = (0 until 16).map{case i =>
749      if(i == 0){
750        i.U -> Cat(0.U(128.W), data)
751      }else{
752        i.U -> Cat(0.U(((16-i)*8).W), data, 0.U((i*8).W))
753      }
754    }
755    tmpData := LookupTree(addrOffset, lookupTable).asUInt
756
757    LookupTree(index, List(
758      0.U -> tmpData(127, 0),
759      1.U -> tmpData(255, 128)
760    ))
761  }
762}
763/**
764  * generate offset in Vd of flows, only used in Unit-Stride
765  * */
766object genVdOffset{
767  def apply(offset: UInt, index: UInt): UInt = {
768    LookupTree(index, List(
769      0.U -> 0.U,
770      1.U -> ((~offset).asUInt + 1.U)
771    ))
772  }
773}
774
775object GenVSData extends VLSUConstants {
776  def apply(data: UInt, elemIdx: UInt, alignedType: UInt): UInt = {
777    LookupTree(alignedType, List(
778      "b000".U -> ZeroExt(LookupTree(elemIdx(3, 0), List.tabulate(VLEN/8)(i => i.U -> getByte(data, i))), VLEN),
779      "b001".U -> ZeroExt(LookupTree(elemIdx(2, 0), List.tabulate(VLEN/16)(i => i.U -> getHalfWord(data, i))), VLEN),
780      "b010".U -> ZeroExt(LookupTree(elemIdx(1, 0), List.tabulate(VLEN/32)(i => i.U -> getWord(data, i))), VLEN),
781      "b011".U -> ZeroExt(LookupTree(elemIdx(0), List.tabulate(VLEN/64)(i => i.U -> getDoubleWord(data, i))), VLEN),
782      "b100".U -> data // if have wider element, it will broken
783    ))
784  }
785}