xref: /XiangShan/src/main/scala/xiangshan/backend/decode/VecExceptionGen.scala (revision e565f15a743f12b0e2d506852d1328cf0e6529aa)
1567f0269Ssinsanctionpackage xiangshan.backend.decode
2567f0269Ssinsanction
383ba63b3SXuan Huimport org.chipsalliance.cde.config.Parameters
4567f0269Ssinsanctionimport chisel3._
5567f0269Ssinsanctionimport chisel3.util._
6567f0269Ssinsanctionimport freechips.rocketchip.rocket.Instructions._
7567f0269Ssinsanctionimport freechips.rocketchip.util.uintToBitPat
8567f0269Ssinsanctionimport utility._
9567f0269Ssinsanctionimport utils._
10567f0269Ssinsanctionimport xiangshan._
11567f0269Ssinsanctionimport xiangshan.backend.Bundles.{DecodedInst, DynInst, StaticInst}
12567f0269Ssinsanctionimport xiangshan.backend.fu.FuType
13567f0269Ssinsanctionimport xiangshan.backend.fu.vector.Bundles._
1464523a1dSZiyue Zhangimport xiangshan.backend.decode.isa.bitfield.{InstVType, XSInstBitFields, OPCODE7Bit}
1564523a1dSZiyue Zhangimport xiangshan.backend.decode.Zvbb._
16567f0269Ssinsanction
17567f0269Ssinsanctionobject RegNumNotAlign {
18567f0269Ssinsanction  def apply(reg: UInt, emul: UInt): Bool = {
19567f0269Ssinsanction    emul === "b101".U && reg(0) =/= 0.U || emul === "b110".U && reg(1, 0) =/= 0.U || emul === "b111".U && reg(2, 0) =/= 0.U
20567f0269Ssinsanction  }
21567f0269Ssinsanction}
22567f0269Ssinsanction
23567f0269Ssinsanctionobject NFtoLmul {
24567f0269Ssinsanction  def apply(nf: UInt): UInt = {
25567f0269Ssinsanction    LookupTree(nf, List(
26567f0269Ssinsanction      "b000".U -> 4.U,
27567f0269Ssinsanction      "b001".U -> 5.U,
28567f0269Ssinsanction      "b011".U -> 6.U,
29567f0269Ssinsanction      "b111".U -> 7.U
30567f0269Ssinsanction    ))
31567f0269Ssinsanction  }
32567f0269Ssinsanction}
33567f0269Ssinsanction
34567f0269Ssinsanctionobject LmultoRegNum {
35567f0269Ssinsanction  def apply(lmul: UInt): UInt = {
36567f0269Ssinsanction    val numPow = Mux(lmul(2).asBool, lmul(1, 0), 0.U(2.W))
37567f0269Ssinsanction    val regNum = 1.U << numPow
38567f0269Ssinsanction    regNum
39567f0269Ssinsanction  }
40567f0269Ssinsanction}
41567f0269Ssinsanction
42567f0269Ssinsanctionclass VecExceptionGen(implicit p: Parameters) extends XSModule{
43567f0269Ssinsanction  val io = IO(new Bundle(){
44567f0269Ssinsanction    val inst = Input(UInt(32.W))
45567f0269Ssinsanction    val decodedInst = Input(new DecodedInst)
46567f0269Ssinsanction    val vtype = Input(new VType)
475110577fSZiyue Zhang    val vstart = Input(Vl())
48567f0269Ssinsanction
49567f0269Ssinsanction    val illegalInst = Output(Bool())
50567f0269Ssinsanction  })
51567f0269Ssinsanction
52567f0269Ssinsanction  private val inst: XSInstBitFields = io.inst.asTypeOf(new XSInstBitFields)
53c5f1351bSXuan Hu  private val isVArithMem = FuType.isVArithMem(io.decodedInst.fuType)
545110577fSZiyue Zhang  private val isVArith = FuType.isVArith(io.decodedInst.fuType)
55c5f1351bSXuan Hu  private val isVset = FuType.isVset(io.decodedInst.fuType)
56567f0269Ssinsanction
57567f0269Ssinsanction  private val SEW = io.vtype.vsew(1, 0)
58567f0269Ssinsanction  private val LMUL = Cat(~io.vtype.vlmul(2), io.vtype.vlmul(1, 0))
59567f0269Ssinsanction
60567f0269Ssinsanction  private val lsStrideInst = Seq(
61567f0269Ssinsanction    VLE8_V, VLE16_V, VLE32_V, VLE64_V, VSE8_V, VSE16_V, VSE32_V, VSE64_V,
62567f0269Ssinsanction    VLSE8_V, VLSE16_V, VLSE32_V, VLSE64_V, VSSE8_V, VSSE16_V, VSSE32_V, VSSE64_V,
63567f0269Ssinsanction    VLE8FF_V, VLE16FF_V, VLE32FF_V, VLE64FF_V
64567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
65567f0269Ssinsanction
66567f0269Ssinsanction  private val lsMaskInst = Seq(
67567f0269Ssinsanction    VLM_V, VSM_V
68567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
69567f0269Ssinsanction
70567f0269Ssinsanction  private val lsIndexInst = Seq(
71567f0269Ssinsanction    VLUXEI8_V, VLUXEI16_V, VLUXEI32_V, VLUXEI64_V, VLOXEI8_V, VLOXEI16_V, VLOXEI32_V, VLOXEI64_V,
72567f0269Ssinsanction    VSUXEI8_V, VSUXEI16_V, VSUXEI32_V, VSUXEI64_V, VSOXEI8_V, VSOXEI16_V, VSOXEI32_V, VSOXEI64_V
73567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
74567f0269Ssinsanction
75567f0269Ssinsanction  private val lsWholeInst = Seq(
76567f0269Ssinsanction    VL1RE8_V, VL1RE16_V, VL1RE32_V, VL1RE64_V,
77567f0269Ssinsanction    VL2RE8_V, VL2RE16_V, VL2RE32_V, VL2RE64_V,
78567f0269Ssinsanction    VL4RE8_V, VL4RE16_V, VL4RE32_V, VL4RE64_V,
79567f0269Ssinsanction    VL8RE8_V, VL8RE16_V, VL8RE32_V, VL8RE64_V,
80567f0269Ssinsanction    VS1R_V, VS2R_V, VS4R_V, VS8R_V
81567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
82567f0269Ssinsanction
83567f0269Ssinsanction  private val vdWideningInst = Seq(
84567f0269Ssinsanction    //int
85567f0269Ssinsanction    VWADD_VV, VWADD_VX, VWADD_WV, VWADD_WX, VWADDU_VV, VWADDU_VX, VWADDU_WV, VWADDU_WX,
86567f0269Ssinsanction    VWMACC_VV, VWMACC_VX, VWMACCSU_VV, VWMACCSU_VX, VWMACCU_VV, VWMACCU_VX, VWMACCUS_VX,
87567f0269Ssinsanction    VWMUL_VV, VWMUL_VX, VWMULSU_VV, VWMULSU_VX, VWMULU_VV, VWMULU_VX,
88567f0269Ssinsanction    VWSUB_VV, VWSUB_VX, VWSUB_WV, VWSUB_WX, VWSUBU_VV, VWSUBU_VX, VWSUBU_WV, VWSUBU_WX,
89567f0269Ssinsanction    //fp
90567f0269Ssinsanction    VFWADD_VF, VFWADD_VV, VFWADD_WF, VFWADD_WV, VFWSUB_VF, VFWSUB_VV, VFWSUB_WF, VFWSUB_WV,
91567f0269Ssinsanction    VFWMUL_VF, VFWMUL_VV,
92567f0269Ssinsanction    VFWMACC_VF, VFWMACC_VV, VFWMSAC_VF, VFWMSAC_VV, VFWNMACC_VF, VFWNMACC_VV, VFWNMSAC_VF, VFWNMSAC_VV,
9364523a1dSZiyue Zhang    VFWCVT_F_F_V, VFWCVT_F_X_V, VFWCVT_F_XU_V, VFWCVT_RTZ_X_F_V, VFWCVT_RTZ_XU_F_V, VFWCVT_X_F_V, VFWCVT_XU_F_V,
9464523a1dSZiyue Zhang    // zvbb
9564523a1dSZiyue Zhang    VWSLL_VV, VWSLL_VX, VWSLL_VI,
96567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
97567f0269Ssinsanction
98567f0269Ssinsanction  private val vs2WideningInst = Seq(
99567f0269Ssinsanction    //int
100567f0269Ssinsanction    VWADD_WV, VWADD_WX, VWADDU_WV, VWADDU_WX,
101567f0269Ssinsanction    VWSUB_WV, VWSUB_WX, VWSUBU_WV, VWSUBU_WX,
102567f0269Ssinsanction    //fp
103567f0269Ssinsanction    VFWADD_WF, VFWADD_WV, VFWSUB_WF, VFWSUB_WV
104567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
105567f0269Ssinsanction
106567f0269Ssinsanction  private val narrowingInst = Seq(
107567f0269Ssinsanction    //int
108567f0269Ssinsanction    VNCLIP_WI, VNCLIP_WV, VNCLIP_WX, VNCLIPU_WI, VNCLIPU_WV, VNCLIPU_WX,
109567f0269Ssinsanction    VNSRA_WI, VNSRA_WV, VNSRA_WX, VNSRL_WI, VNSRL_WV, VNSRL_WX,
110567f0269Ssinsanction    //fp
111567f0269Ssinsanction    VFNCVT_F_F_W, VFNCVT_F_X_W, VFNCVT_F_XU_W, VFNCVT_ROD_F_F_W, VFNCVT_RTZ_X_F_W, VFNCVT_RTZ_XU_F_W, VFNCVT_X_F_W, VFNCVT_XU_F_W
112567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
113567f0269Ssinsanction
114567f0269Ssinsanction  private val intExtInst = Seq(
115567f0269Ssinsanction    VSEXT_VF2, VSEXT_VF4, VSEXT_VF8, VZEXT_VF2, VZEXT_VF4, VZEXT_VF8
116567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
117567f0269Ssinsanction
118567f0269Ssinsanction  private val acsbInst = Seq(
119567f0269Ssinsanction    VMADC_VI, VMADC_VIM, VMADC_VV, VMADC_VVM, VMADC_VX, VMADC_VXM,
120567f0269Ssinsanction    VMSBC_VV, VMSBC_VVM, VMSBC_VX, VMSBC_VXM
121567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
122567f0269Ssinsanction
123567f0269Ssinsanction  private val cmpInst = Seq(
124567f0269Ssinsanction    //int
125567f0269Ssinsanction    VMSEQ_VI, VMSEQ_VV, VMSEQ_VX,
126567f0269Ssinsanction    VMSGT_VI, VMSGT_VX, VMSGTU_VI, VMSGTU_VX,
127567f0269Ssinsanction    VMSLE_VI, VMSLE_VV, VMSLE_VX, VMSLEU_VI, VMSLEU_VV, VMSLEU_VX,
128567f0269Ssinsanction    VMSLT_VV, VMSLT_VX, VMSLTU_VV, VMSLTU_VX,
129567f0269Ssinsanction    VMSNE_VI, VMSNE_VV, VMSNE_VX,
130567f0269Ssinsanction    //fp
131567f0269Ssinsanction    VMFEQ_VF, VMFEQ_VV, VMFNE_VF, VMFNE_VV,
132567f0269Ssinsanction    VMFGE_VF, VMFGT_VF, VMFLE_VF, VMFLE_VV, VMFLT_VF, VMFLT_VV
133567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
134567f0269Ssinsanction
135567f0269Ssinsanction  private val redInst = Seq(
136567f0269Ssinsanction    VREDAND_VS, VREDMAX_VS, VREDMAXU_VS, VREDMIN_VS, VREDMINU_VS, VREDOR_VS, VREDSUM_VS, VREDXOR_VS,
137567f0269Ssinsanction    VFREDMAX_VS, VFREDMIN_VS, VFREDOSUM_VS, VFREDUSUM_VS
138567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
139567f0269Ssinsanction
140567f0269Ssinsanction  private val redWideningInst = Seq(
141567f0269Ssinsanction    VWREDSUM_VS, VWREDSUMU_VS,
142567f0269Ssinsanction    VFWREDOSUM_VS, VFWREDUSUM_VS
143567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
144567f0269Ssinsanction
145567f0269Ssinsanction  private val maskLogicalInst = Seq(
146567f0269Ssinsanction    VMAND_MM, VMNAND_MM, VMANDN_MM, VMXOR_MM, VMOR_MM, VMNOR_MM, VMORN_MM, VMXNOR_MM
147567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
148567f0269Ssinsanction
149567f0269Ssinsanction  private val maskArithmeticInst = Seq(
150567f0269Ssinsanction    VCPOP_M, VFIRST_M, VMSBF_M, VMSIF_M, VMSOF_M
151567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _) || maskLogicalInst
152567f0269Ssinsanction
153567f0269Ssinsanction  private val maskIndexInst = Seq(
154567f0269Ssinsanction    VIOTA_M, VID_V
155567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
156567f0269Ssinsanction
157567f0269Ssinsanction  private val vmvSingleInst = Seq(
158567f0269Ssinsanction    VMV_X_S, VMV_S_X, VFMV_F_S, VFMV_S_F
159567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
160567f0269Ssinsanction
161567f0269Ssinsanction  private val vmvWholeInst = Seq(
162567f0269Ssinsanction    VMV1R_V, VMV2R_V, VMV4R_V, VMV8R_V
163567f0269Ssinsanction  ).map(_ === inst.ALL).reduce(_ || _)
164567f0269Ssinsanction
165567f0269Ssinsanction  private val vrgather16 = VRGATHEREI16_VV === inst.ALL
166567f0269Ssinsanction  private val vcompress = VCOMPRESS_VM === inst.ALL
167567f0269Ssinsanction  private val intExt2 = Seq(VSEXT_VF2, VZEXT_VF2).map(_ === inst.ALL).reduce(_ || _)
168567f0269Ssinsanction  private val intExt4 = Seq(VSEXT_VF4, VZEXT_VF4).map(_ === inst.ALL).reduce(_ || _)
169567f0269Ssinsanction  private val intExt8 = Seq(VSEXT_VF8, VZEXT_VF8).map(_ === inst.ALL).reduce(_ || _)
170567f0269Ssinsanction
1711e1ca94aSZiyue Zhang  private val notDependVtypeInst = Seq(VSETVLI, VSETIVLI, VSETVL).map(_ === inst.ALL).reduce(_ || _) || lsWholeInst
172567f0269Ssinsanction
173567f0269Ssinsanction
174567f0269Ssinsanction  // 1. inst Illegal
175567f0269Ssinsanction  private val instIllegal = maskLogicalInst && inst.VM === 0.U
176567f0269Ssinsanction
177567f0269Ssinsanction  // 2. vill Illegal
178c5f1351bSXuan Hu  private val villIllegal = io.vtype.illegal && isVArithMem && !notDependVtypeInst
179567f0269Ssinsanction
180567f0269Ssinsanction  // 3. EEW Illegal
181*e565f15aSHeiHuDie  private val doubleFpInst = Seq(
182*e565f15aSHeiHuDie    VFWCVT_F_X_V, VFWCVT_F_XU_V, VFNCVT_RTZ_X_F_W, VFNCVT_RTZ_XU_F_W, VFNCVT_X_F_W, VFNCVT_XU_F_W
183*e565f15aSHeiHuDie  ).map(_ === inst.ALL).reduce(_ || _)
184614d2bc6SHeiHuDie
18564523a1dSZiyue Zhang  // funct3 of OPFVV is 001, funct3 of OPFVF is 101
18664523a1dSZiyue Zhang  private val isFp = (inst.FUNCT3 === BitPat("b?01")) && (inst.OPCODE7Bit === OPCODE7Bit.VECTOR_ARITH)
187*e565f15aSHeiHuDie  private val fpEewIllegal = isFp && ((SEW === 0.U) && !doubleFpInst)
188567f0269Ssinsanction
189567f0269Ssinsanction  private val intExtEewIllegal = intExt2 && SEW === 0.U ||
190567f0269Ssinsanction                                 intExt4 && SEW <= 1.U ||
191567f0269Ssinsanction                                 intExt8 && SEW <= 2.U
192567f0269Ssinsanction
193567f0269Ssinsanction  private val wnEewIllegal = (vdWideningInst || narrowingInst || redWideningInst) && SEW === 3.U
194567f0269Ssinsanction
195567f0269Ssinsanction  private val eewIllegal = fpEewIllegal || intExtEewIllegal || wnEewIllegal
196567f0269Ssinsanction
197567f0269Ssinsanction  // 4. EMUL Illegal
198567f0269Ssinsanction  private val lsEmulIllegal = (lsStrideInst || lsIndexInst) && (LMUL +& inst.WIDTH(1, 0) < SEW +& 1.U || LMUL +& inst.WIDTH(1, 0) > SEW +& 7.U)
199567f0269Ssinsanction
200567f0269Ssinsanction  private val intExtEmulIllegal = intExt2 && LMUL === 1.U ||
201567f0269Ssinsanction                                  intExt4 && LMUL <= 2.U ||
202567f0269Ssinsanction                                  intExt8 && LMUL <= 3.U
203567f0269Ssinsanction
2045cac1ae7SZiyue Zhang  private val wnEmulIllegal = (vdWideningInst || narrowingInst) && LMUL === 7.U
205567f0269Ssinsanction
206567f0269Ssinsanction  private val gather16EmulIllegal = vrgather16 && (LMUL < SEW || LMUL > SEW +& 6.U)
207567f0269Ssinsanction
208567f0269Ssinsanction  private val NFIELDS = inst.NF +& 1.U
209567f0269Ssinsanction  private val segEmul = Mux(lsIndexInst, LMUL, LMUL +& inst.WIDTH(1, 0) - SEW)
210567f0269Ssinsanction  private val emulNumPow = Mux(segEmul(2), segEmul(1, 0), 0.U(2.W))
211567f0269Ssinsanction  private val segRegNum = NFIELDS << emulNumPow
212567f0269Ssinsanction  private val segRegMax = inst.VD +& segRegNum
213567f0269Ssinsanction
214567f0269Ssinsanction  private val lsSegIllegal = (lsStrideInst || lsIndexInst) && inst.NF =/= 0.U && (segRegNum > 8.U || segRegMax > 32.U)
215567f0269Ssinsanction
216567f0269Ssinsanction  private val emulIllegal = lsEmulIllegal || intExtEmulIllegal || wnEmulIllegal || gather16EmulIllegal || lsSegIllegal
217567f0269Ssinsanction
218567f0269Ssinsanction  // 5. Reg Number Align
219567f0269Ssinsanction  private val vs1IsMask = maskArithmeticInst || vcompress
2201e1ca94aSZiyue Zhang  private val vs1IsSingleElem = redInst || redWideningInst
221567f0269Ssinsanction  private val vs1Eew = Mux(vrgather16, "b01".U, SEW)
222567f0269Ssinsanction  private val vs1Emul = Mux(vs1IsMask || vs1IsSingleElem, "b100".U, Mux(vrgather16, LMUL +& 1.U - SEW, LMUL))
223567f0269Ssinsanction  private val vs1NotAlign = SrcType.isVp(io.decodedInst.srcType(0)) && RegNumNotAlign(inst.VS1, vs1Emul)
224567f0269Ssinsanction
225567f0269Ssinsanction  private val vs2IsMask = maskArithmeticInst || maskIndexInst
2261e1ca94aSZiyue Zhang  private val vs2IsSingleElem = vmvSingleInst
227567f0269Ssinsanction  private val vs2EewSel = Cat(lsIndexInst, (vs2WideningInst || narrowingInst || redWideningInst), intExt2, intExt4, intExt8)
228567f0269Ssinsanction  private val vs2Eew = LookupTreeDefault(vs2EewSel, SEW, List(
229567f0269Ssinsanction    "b10000".U  -> inst.WIDTH(1, 0),
230567f0269Ssinsanction    "b01000".U  -> (SEW + 1.U),
231567f0269Ssinsanction    "b00100".U  -> (SEW - 1.U),
232567f0269Ssinsanction    "b00010".U  -> (SEW - 2.U),
233567f0269Ssinsanction    "b00001".U  -> (SEW - 3.U)
234567f0269Ssinsanction  ))
235567f0269Ssinsanction  private val vs2EmulSel = Cat((vs2IsMask || vs2IsSingleElem), (vs2WideningInst || narrowingInst), vmvWholeInst, (intExtInst || lsIndexInst))
236567f0269Ssinsanction  private val vs2Emul = LookupTreeDefault(vs2EmulSel, LMUL, List(
237567f0269Ssinsanction    "b1000".U  -> "b100".U,
238567f0269Ssinsanction    "b0100".U  -> (LMUL + 1.U),
2390d9b3dabSZiyue Zhang    "b0010".U  -> NFtoLmul(inst.IMM5_OPIVI(2, 0)),
240567f0269Ssinsanction    "b0001".U  -> (LMUL +& vs2Eew - SEW)
241567f0269Ssinsanction  ))
242567f0269Ssinsanction  private val vs2NotAlign = SrcType.isVp(io.decodedInst.srcType(1)) && RegNumNotAlign(inst.VS2, vs2Emul)
243567f0269Ssinsanction
244567f0269Ssinsanction  private val vdIsMask = lsMaskInst || acsbInst || cmpInst || maskArithmeticInst
245567f0269Ssinsanction  private val vdIsSingleElem = redInst || redWideningInst || vmvSingleInst
246567f0269Ssinsanction  private val vdEew = Mux(lsStrideInst, inst.WIDTH(1, 0), Mux(vdWideningInst || redWideningInst, SEW + 1.U, SEW))
2470d9b3dabSZiyue Zhang  private val vdEmulSel = Cat((vdIsMask || vdIsSingleElem), vdWideningInst, vmvWholeInst, lsWholeInst, lsStrideInst)
248567f0269Ssinsanction  private val vdEmul = LookupTreeDefault(vdEmulSel, LMUL, List(
2490d9b3dabSZiyue Zhang    "b10000".U  -> "b100".U,
2500d9b3dabSZiyue Zhang    "b01000".U  -> (LMUL + 1.U),
2510d9b3dabSZiyue Zhang    "b00100".U  -> NFtoLmul(inst.IMM5_OPIVI(2, 0)),
2520d9b3dabSZiyue Zhang    "b00010".U  -> NFtoLmul(inst.NF),
2530d9b3dabSZiyue Zhang    "b00001".U  -> (LMUL +& vdEew - SEW)
254567f0269Ssinsanction  ))
255567f0269Ssinsanction  private val vdNotAlign = (SrcType.isVp(io.decodedInst.srcType(2)) || io.decodedInst.vecWen) && RegNumNotAlign(inst.VD, vdEmul)
256567f0269Ssinsanction
257c5f1351bSXuan Hu  private val regNumIllegal = isVArithMem && (vs1NotAlign || vs2NotAlign || vdNotAlign)
258567f0269Ssinsanction
259567f0269Ssinsanction  // 6. v0 Overlap
260567f0269Ssinsanction  private val v0AllowOverlap = (vdIsMask || vdIsSingleElem) && !Seq(VMSBF_M, VMSIF_M, VMSOF_M).map(_ === inst.ALL).reduce(_ || _)
261c5f1351bSXuan Hu  private val v0Overlap = isVArithMem && io.decodedInst.vecWen && inst.VM === 0.U && inst.VD === 0.U && !v0AllowOverlap
262567f0269Ssinsanction
263567f0269Ssinsanction  // 7. Src Reg Overlap
264567f0269Ssinsanction  private val vs1RegLo = inst.VS1
265567f0269Ssinsanction  private val vs1RegHi = inst.VS1 +& LmultoRegNum(vs1Emul) - 1.U
266567f0269Ssinsanction  private val vs2RegLo = inst.VS2
267567f0269Ssinsanction  private val vs2RegHi = inst.VS2 +& LmultoRegNum(vs2Emul) - 1.U
268567f0269Ssinsanction  private val vdRegLo = inst.VD
269567f0269Ssinsanction  private val vdRegHi = Mux(lsStrideInst || lsIndexInst, segRegMax - 1.U, inst.VD + LmultoRegNum(vdEmul) - 1.U)
270567f0269Ssinsanction
271567f0269Ssinsanction  private val notAllowOverlapInst = lsIndexInst && inst.NF =/= 0.U || Seq(VMSBF_M, VMSIF_M, VMSOF_M, VIOTA_M,
272567f0269Ssinsanction                                    VSLIDEUP_VX, VSLIDEUP_VI, VSLIDE1UP_VX, VFSLIDE1UP_VF, VRGATHER_VV, VRGATHEREI16_VV, VRGATHER_VX, VRGATHER_VI, VCOMPRESS_VM).map(_ === inst.ALL).reduce(_ || _)
273567f0269Ssinsanction
2745110577fSZiyue Zhang  // 8. vstart Illegal
2755110577fSZiyue Zhang  private val vstartIllegal = isVArith && (io.vstart =/= 0.U)
2765110577fSZiyue Zhang
277567f0269Ssinsanction  //vs1
278567f0269Ssinsanction  private val vs1vdRegNotOverlap = vs1RegHi < vdRegLo || vdRegHi < vs1RegLo
279567f0269Ssinsanction  private val vs1Constraint1 = vs1IsMask && vdIsMask || !vs1IsMask && !vdIsMask && vs1Eew === vdEew
280567f0269Ssinsanction  private val vs1Constraint2 = (vdIsMask && !vs1IsMask || !vs1IsMask && !vdIsMask && vs1Eew > vdEew) && vdRegLo === vs1RegLo && vdRegHi <= vs1RegHi
281567f0269Ssinsanction  private val vs1Constraint3 = (!vdIsMask && vs1IsMask || !vs1IsMask && !vdIsMask && vs1Eew < vdEew) && vs1Emul >= "b100".U && vdRegHi === vs1RegHi && vdRegLo <= vs1RegLo
282567f0269Ssinsanction  private val vs1AllowOverlap = (vs1Constraint1 || vs1Constraint2 || vs1Constraint3 || vdIsSingleElem) && !notAllowOverlapInst
283567f0269Ssinsanction  private val vs1vdOverlap = (SrcType.isVp(io.decodedInst.srcType(0)) && io.decodedInst.vecWen) && !vs1vdRegNotOverlap && !vs1AllowOverlap
284567f0269Ssinsanction  //vs2
285567f0269Ssinsanction  private val vs2vdRegNotOverlap = vs2RegHi < vdRegLo || vdRegHi < vs2RegLo
286567f0269Ssinsanction  private val vs2Constraint1 = vs2IsMask && vdIsMask || !vs2IsMask && !vdIsMask && vs2Eew === vdEew
287567f0269Ssinsanction  private val vs2Constraint2 = (vdIsMask && !vs2IsMask || !vs2IsMask && !vdIsMask && vs2Eew > vdEew) && vdRegLo === vs2RegLo && vdRegHi <= vs2RegHi
288567f0269Ssinsanction  private val vs2Constraint3 = (!vdIsMask && vs2IsMask || !vs2IsMask && !vdIsMask && vs2Eew < vdEew) && vs2Emul >= "b100".U && vdRegHi === vs2RegHi && vdRegLo <= vs2RegLo
289567f0269Ssinsanction  private val vs2AllowOverlap = (vs2Constraint1 || vs2Constraint2 || vs2Constraint3 || vdIsSingleElem) && !notAllowOverlapInst
290567f0269Ssinsanction  private val vs2vdOverlap = (SrcType.isVp(io.decodedInst.srcType(1)) && io.decodedInst.vecWen) && !vs2vdRegNotOverlap && !vs2AllowOverlap
291567f0269Ssinsanction
292567f0269Ssinsanction  private val regOverlapIllegal = v0Overlap || vs1vdOverlap || vs2vdOverlap
293567f0269Ssinsanction
2945110577fSZiyue Zhang  io.illegalInst := instIllegal || villIllegal || eewIllegal || emulIllegal || regNumIllegal || regOverlapIllegal || vstartIllegal
2957d9a777aSXuan Hu  dontTouch(instIllegal)
2967d9a777aSXuan Hu  dontTouch(villIllegal)
2977d9a777aSXuan Hu  dontTouch(eewIllegal)
2987d9a777aSXuan Hu  dontTouch(emulIllegal)
2997d9a777aSXuan Hu  dontTouch(regNumIllegal)
3007d9a777aSXuan Hu  dontTouch(regOverlapIllegal)
3017d9a777aSXuan Hu  dontTouch(notDependVtypeInst)
3025110577fSZiyue Zhang  dontTouch(vstartIllegal)
303567f0269Ssinsanction}