xref: /XiangShan/src/main/scala/xiangshan/backend/fu/wrapper/VIPU.scala (revision e3da8bad334fc71ba0d72f0607e2e93245ddaece)
1/****************************************************************************************
2 * Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
3 * Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
4 * Copyright (c) 2020-2021 Peng Cheng Laboratory
5 *
6 * XiangShan is licensed under Mulan PSL v2.
7 * You can use this software according to the terms and conditions of the Mulan PSL v2.
8 * You may obtain a copy of Mulan PSL v2 at:
9 *       http://license.coscl.org.cn/MulanPSL2
10 *
11 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14 *
15 * See the Mulan PSL v2 for more details.
16 ****************************************************************************************
17 */
18
19
20package xiangshan.backend.fu.wrapper
21
22import org.chipsalliance.cde.config.Parameters
23import chisel3._
24import chisel3.util._
25import freechips.rocketchip.rocket.DecodeLogic
26import utility._
27import xiangshan.{SelImm, SrcType, UopSplitType, XSCoreParamsKey, XSModule}
28import xiangshan.backend.fu.FuConfig
29import xiangshan.backend.fu.vector.Bundles.VSew
30import xiangshan.backend.fu.vector.utils.VecDataSplitModule
31import xiangshan.backend.fu.vector.{Mgu, Utils, VecPipedFuncUnit, VecSrcTypeModule}
32import xiangshan.SrcType
33import yunsuan.vector.alu.{VAluOpcode, VIAlu}
34import yunsuan.{OpType, VipuType}
35
36class VIAluDecodeResultBundle extends Bundle {
37  val opcode = UInt(6.W)
38  val srcType2 = UInt(3.W)
39  val srcType1 = UInt(3.W)
40  val vdType = UInt(3.W)
41}
42
43class VIAluDecoder (implicit p: Parameters) extends XSModule {
44  val io = IO(new Bundle{
45    val in = Input(new Bundle{
46      val fuOpType = UInt(8.W)
47      val sew = UInt(2.W)
48    })
49    val out = Output(new VIAluDecodeResultBundle)
50  })
51
52  // u 00 s 01 f 10 mask 1111
53  // Cat(1(1111),1(s)/0(u), 1(add1)/0(add0))
54  val uSew   = BitPat("b000")
55  val uSew2  = BitPat("b001")
56  val sSew   = BitPat("b010")
57  val sSew2  = BitPat("b011")
58  val mask = BitPat("b100".U(3.W))
59  val default = List(BitPat("b000000"),BitPat("b000"),BitPat("b000"),BitPat("b000"))
60  val decodeTable : Array[(BitPat, List[BitPat])] = Array(
61    BitPat(VipuType.vredsum_vs)   -> List(BitPat(VAluOpcode.vredsum), uSew, uSew, uSew),
62    BitPat(VipuType.vredmaxu_vs)  -> List(BitPat(VAluOpcode.vredmax), uSew, uSew, uSew),
63    BitPat(VipuType.vredmax_vs)   -> List(BitPat(VAluOpcode.vredmax), sSew, sSew, sSew),
64    BitPat(VipuType.vredminu_vs)  -> List(BitPat(VAluOpcode.vredmin), uSew, uSew, uSew),
65    BitPat(VipuType.vredmin_vs)   -> List(BitPat(VAluOpcode.vredmin), sSew, sSew, sSew),
66    BitPat(VipuType.vredand_vs)   -> List(BitPat(VAluOpcode.vredand), uSew, uSew, uSew),
67    BitPat(VipuType.vredor_vs)    -> List(BitPat(VAluOpcode.vredor), uSew, uSew, uSew),
68    BitPat(VipuType.vredxor_vs)   -> List(BitPat(VAluOpcode.vredxor), uSew, uSew, uSew),
69
70    BitPat(VipuType.vwredsumu_vs) -> List(BitPat(VAluOpcode.vredsum), uSew, uSew, uSew2),
71    BitPat(VipuType.vwredsum_vs)  -> List(BitPat(VAluOpcode.vredsum), sSew, sSew, sSew2),
72
73    BitPat(VipuType.vcpop_m)      -> List(BitPat(VAluOpcode.vcpop), mask, mask, uSew),
74    BitPat(VipuType.vfirst_m)     -> List(BitPat(VAluOpcode.vfirst), mask, mask, uSew),
75    BitPat(VipuType.vmsbf_m)      -> List(BitPat(VAluOpcode.vmsbf), mask, mask, mask),
76    BitPat(VipuType.vmsif_m)      -> List(BitPat(VAluOpcode.vmsif), mask, mask, mask),
77    BitPat(VipuType.vmsof_m)      -> List(BitPat(VAluOpcode.vmsof), mask, mask, mask),
78
79    BitPat(VipuType.viota_m)      -> List(BitPat(VAluOpcode.viota), uSew, uSew, uSew),
80    BitPat(VipuType.vid_v)        -> List(BitPat(VAluOpcode.vid), uSew, uSew, uSew),
81    BitPat(VipuType.vmv_x_s)      -> List(BitPat(VAluOpcode.vmvxs), uSew, uSew, uSew)
82  )
83  val decoder = DecodeLogic(io.in.fuOpType, default, decodeTable)
84  val outsig = Seq(io.out.opcode, io.out.srcType2, io.out.srcType1, io.out.vdType)
85  outsig.zip(decoder).foreach({case (s, d) => s := d})
86}
87
88class VIPU(cfg: FuConfig)(implicit p: Parameters) extends VecPipedFuncUnit(cfg) {
89  XSError(io.in.valid && io.in.bits.ctrl.fuOpType === VipuType.dummy, "VIPU OpType not supported")
90
91  // params alias
92  private val dataWidth = cfg.destDataBits
93  private val dataWidthOfDataModule = 64
94  private val numVecModule = dataWidth / dataWidthOfDataModule
95  private val needClearVs1 = (VipuType.vcpop_m === io.in.bits.ctrl.fuOpType && vuopIdx === 0.U) ||
96    (VipuType.viota_m === io.in.bits.ctrl.fuOpType && vuopIdx(log2Up(MaxUopSize)-1,1) === 0.U) ||
97    (VipuType.vid_v   === io.in.bits.ctrl.fuOpType && vuopIdx(log2Up(MaxUopSize)-1,1) === 0.U)    // dirty code TODO:  inset into IAlu
98  private val lmul = MuxLookup(vlmul, 1.U(4.W))(Seq(
99    "b001".U -> 2.U,
100    "b010".U -> 4.U,
101    "b011".U -> 8.U
102  ))
103  private val needShiftVs1 = (VipuType.vwredsumu_vs === io.in.bits.ctrl.fuOpType || VipuType.vwredsum_vs === io.in.bits.ctrl.fuOpType) && vuopIdx < lmul
104
105  // modules
106  private val decoder = Module(new VIAluDecoder)
107  private val vialu   = Module(new VIAlu)
108
109  /**
110   * [[decoder]]'s in connection
111   */
112  decoder.io.in.fuOpType := fuOpType
113  decoder.io.in.sew      := vsew(1,0)
114
115  val typeop2 = decoder.io.out.srcType2
116  val typeop1 = decoder.io.out.srcType1
117  val typevd = decoder.io.out.vdType
118  val sew = decoder.io.in.sew
119
120  val srcTypeVs2 = Cat(0.U | typeop2(2) , typeop2(1) | typeop2(2) , Fill(2,typeop2(2)) | (sew + typeop2(0)) )
121  val srcTypeVs1 = Cat(0.U | typeop1(2) , typeop1(1) | typeop1(2) , Fill(2,typeop1(2)) | (sew + typeop1(0)) )
122  val vdType =  Cat(0.U | typevd(2) , typevd(1) | typevd(2) , Fill(2,typevd(2)) | (sew + typevd(0)))
123  /**
124   * [[vialu]]'s in connection
125   */
126  vialu.io match {
127    case subIO =>
128      subIO.in.valid            := io.in.valid
129      subIO.in.bits.opcode.op   := decoder.io.out.opcode
130      subIO.in.bits.info.vm     := vm
131      subIO.in.bits.info.ma     := vma
132      subIO.in.bits.info.ta     := vta
133      subIO.in.bits.info.vlmul  := vlmul
134      subIO.in.bits.info.vl     := srcVConfig.vl
135      subIO.in.bits.info.vstart := vstart
136      subIO.in.bits.info.uopIdx := vuopIdx
137      subIO.in.bits.info.vxrm   := vxrm
138      subIO.in.bits.srcType(0)  := srcTypeVs2
139      subIO.in.bits.srcType(1)  := srcTypeVs1
140      subIO.in.bits.vdType      := vdType
141      subIO.in.bits.vs1         := Mux1H(Seq(needClearVs1 -> 0.U,
142        needShiftVs1 -> ZeroExt(vs1(127,64), 128),
143        ((!needClearVs1) && (!needShiftVs1)) -> vs1))
144      subIO.in.bits.vs2         := vs2
145      subIO.in.bits.old_vd      := oldVd
146      subIO.in.bits.mask        := srcMask
147  }
148
149  io.out.bits.res.data := vialu.io.out.bits.vd
150}
151