xref: /XiangShan/src/main/scala/xiangshan/backend/fu/wrapper/VIPU.scala (revision bb2f3f51dd67f6e16e0cc1ffe43368c9fc7e4aef)
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 */
17
18
19package xiangshan.backend.fu.wrapper
20
21import org.chipsalliance.cde.config.Parameters
22import chisel3._
23import chisel3.util._
24import freechips.rocketchip.rocket.DecodeLogic
25import utility._
26import xiangshan.{SelImm, SrcType, UopSplitType, XSCoreParamsKey, XSModule}
27import xiangshan.backend.fu.FuConfig
28import xiangshan.backend.fu.vector.Bundles.VSew
29import xiangshan.backend.fu.vector.utils.VecDataSplitModule
30import xiangshan.backend.fu.vector.{Mgu, Utils, VecPipedFuncUnit, VecSrcTypeModule}
31import xiangshan.SrcType
32import yunsuan.vector.alu.{VAluOpcode, VIAlu}
33import yunsuan.{OpType, VipuType}
34
35import scala.collection.Seq
36
37class VIAluDecodeResultBundle extends Bundle {
38  val opcode = UInt(6.W)
39  val srcType2 = UInt(3.W)
40  val srcType1 = UInt(3.W)
41  val vdType = UInt(3.W)
42}
43
44class VIAluDecoder (implicit p: Parameters) extends XSModule {
45  val io = IO(new Bundle{
46    val in = Input(new Bundle{
47      val fuOpType = UInt(8.W)
48      val sew = UInt(2.W)
49    })
50    val out = Output(new VIAluDecodeResultBundle)
51  })
52
53  // u 00 s 01 f 10 mask 1111
54  // Cat(1(1111),1(s)/0(u), 1(add1)/0(add0))
55  val uSew   = BitPat("b000")
56  val uSew2  = BitPat("b001")
57  val sSew   = BitPat("b010")
58  val sSew2  = BitPat("b011")
59  val mask = BitPat("b100".U(3.W))
60  val default = List(BitPat("b000000"),BitPat("b000"),BitPat("b000"),BitPat("b000"))
61  val decodeTable : Array[(BitPat, List[BitPat])] = Array(
62    BitPat(VipuType.vredsum_vs)   -> List(BitPat(VAluOpcode.vredsum), uSew, uSew, uSew),
63    BitPat(VipuType.vredmaxu_vs)  -> List(BitPat(VAluOpcode.vredmax), uSew, uSew, uSew),
64    BitPat(VipuType.vredmax_vs)   -> List(BitPat(VAluOpcode.vredmax), sSew, sSew, sSew),
65    BitPat(VipuType.vredminu_vs)  -> List(BitPat(VAluOpcode.vredmin), uSew, uSew, uSew),
66    BitPat(VipuType.vredmin_vs)   -> List(BitPat(VAluOpcode.vredmin), sSew, sSew, sSew),
67    BitPat(VipuType.vredand_vs)   -> List(BitPat(VAluOpcode.vredand), uSew, uSew, uSew),
68    BitPat(VipuType.vredor_vs)    -> List(BitPat(VAluOpcode.vredor), uSew, uSew, uSew),
69    BitPat(VipuType.vredxor_vs)   -> List(BitPat(VAluOpcode.vredxor), uSew, uSew, uSew),
70
71    BitPat(VipuType.vwredsumu_vs) -> List(BitPat(VAluOpcode.vredsum), uSew, uSew, uSew2),
72    BitPat(VipuType.vwredsum_vs)  -> List(BitPat(VAluOpcode.vredsum), sSew, sSew, sSew2),
73
74    BitPat(VipuType.vcpop_m)      -> List(BitPat(VAluOpcode.vcpop), mask, mask, uSew),
75    BitPat(VipuType.vfirst_m)     -> List(BitPat(VAluOpcode.vfirst), mask, mask, uSew),
76    BitPat(VipuType.vmsbf_m)      -> List(BitPat(VAluOpcode.vmsbf), mask, mask, mask),
77    BitPat(VipuType.vmsif_m)      -> List(BitPat(VAluOpcode.vmsif), mask, mask, mask),
78    BitPat(VipuType.vmsof_m)      -> List(BitPat(VAluOpcode.vmsof), mask, mask, mask),
79
80    BitPat(VipuType.viota_m)      -> List(BitPat(VAluOpcode.viota), uSew, uSew, uSew),
81    BitPat(VipuType.vid_v)        -> List(BitPat(VAluOpcode.vid), uSew, uSew, uSew),
82    BitPat(VipuType.vmv_x_s)      -> List(BitPat(VAluOpcode.vmvxs), uSew, uSew, uSew)
83  )
84  val decoder = DecodeLogic(io.in.fuOpType, default, decodeTable)
85  val outsig = Seq(io.out.opcode, io.out.srcType2, io.out.srcType1, io.out.vdType)
86  outsig.zip(decoder).foreach({case (s, d) => s := d})
87}
88
89class VIPU(cfg: FuConfig)(implicit p: Parameters) extends VecPipedFuncUnit(cfg) {
90  XSError(io.in.valid && io.in.bits.ctrl.fuOpType === VipuType.dummy, "VIPU OpType not supported")
91
92  // params alias
93  private val dataWidth = cfg.destDataBits
94  private val dataWidthOfDataModule = 64
95  private val numVecModule = dataWidth / dataWidthOfDataModule
96  private val needClearVs1 = (VipuType.vcpop_m === io.in.bits.ctrl.fuOpType && vuopIdx === 0.U) ||
97    (VipuType.viota_m === io.in.bits.ctrl.fuOpType && vuopIdx(log2Up(MaxUopSize)-1,1) === 0.U) ||
98    (VipuType.vid_v   === io.in.bits.ctrl.fuOpType && vuopIdx(log2Up(MaxUopSize)-1,1) === 0.U)    // dirty code TODO:  inset into IAlu
99  private val lmul = MuxLookup(vlmul, 1.U(4.W))(Array(
100    "b001".U -> 2.U,
101    "b010".U -> 4.U,
102    "b011".U -> 8.U
103  ))
104  private val needShiftVs1 = (VipuType.vwredsumu_vs === io.in.bits.ctrl.fuOpType || VipuType.vwredsum_vs === io.in.bits.ctrl.fuOpType) && vuopIdx < lmul
105
106  // modules
107  private val decoder = Module(new VIAluDecoder)
108  private val vialu   = Module(new VIAlu)
109
110  /**
111   * [[decoder]]'s in connection
112   */
113  decoder.io.in.fuOpType := fuOpType
114  decoder.io.in.sew      := vsew(1,0)
115
116  val typeop2 = decoder.io.out.srcType2
117  val typeop1 = decoder.io.out.srcType1
118  val typevd = decoder.io.out.vdType
119  val sew = decoder.io.in.sew
120
121  val srcTypeVs2 = Cat(0.U | typeop2(2) , typeop2(1) | typeop2(2) , Fill(2,typeop2(2)) | (sew + typeop2(0)) )
122  val srcTypeVs1 = Cat(0.U | typeop1(2) , typeop1(1) | typeop1(2) , Fill(2,typeop1(2)) | (sew + typeop1(0)) )
123  val vdType =  Cat(0.U | typevd(2) , typevd(1) | typevd(2) , Fill(2,typevd(2)) | (sew + typevd(0)))
124  /**
125   * [[vialu]]'s in connection
126   */
127  vialu.io match {
128    case subIO =>
129      subIO.in.valid            := io.in.valid
130      subIO.in.bits.opcode.op   := decoder.io.out.opcode
131      subIO.in.bits.info.vm     := vm
132      subIO.in.bits.info.ma     := vma
133      subIO.in.bits.info.ta     := vta
134      subIO.in.bits.info.vlmul  := vlmul
135      subIO.in.bits.info.vl     := srcVConfig.vl
136      subIO.in.bits.info.vstart := vstart
137      subIO.in.bits.info.uopIdx := vuopIdx
138      subIO.in.bits.info.vxrm   := vxrm
139      subIO.in.bits.srcType(0)  := srcTypeVs2
140      subIO.in.bits.srcType(1)  := srcTypeVs1
141      subIO.in.bits.vdType      := vdType
142      subIO.in.bits.vs1         := Mux1H(Seq(needClearVs1 -> 0.U,
143        needShiftVs1 -> ZeroExt(vs1(127,64), 128),
144        ((!needClearVs1) && (!needShiftVs1)) -> vs1))
145      subIO.in.bits.vs2         := vs2
146      subIO.in.bits.old_vd      := oldVd
147      subIO.in.bits.mask        := srcMask
148  }
149
150  io.out.bits.res.data := vialu.io.out.bits.vd
151}
152