xref: /XiangShan/src/main/scala/xiangshan/backend/fu/vector/VPUSubModule.scala (revision 8426028029cc2989d7cd536a9c72e84e43b1ffb9)
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.backend.fu.vector
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util.{Mux1H, _}
22import xiangshan.backend.fu.FunctionUnit
23import xiangshan.{SelImm, SrcType}
24import utility._
25
26abstract class VPUDataModule(len: Int = 128)(implicit p: Parameters) extends FunctionUnit(len: Int)
27{
28  val rm = IO(Input(UInt(3.W)))
29  val fflags = IO(Output(UInt(5.W)))
30  val vstart = IO(Input(UInt(XLEN.W)))
31  val vxrm = IO(Input(UInt(2.W)))
32  val vxsat = IO(Output(UInt(1.W)))
33  val needReverse = Wire(Bool())
34  val needClearMask = Wire(Bool())
35
36  // rename signal
37  val in = io.in.bits
38  val ctrl = in.uop.ctrl
39  val vtype = ctrl.vconfig.vtype
40
41  // for generate src1 and src2
42  val imm = VecInit(Seq.fill(VLEN/XLEN)(VecImmExtractor(ctrl.selImm, vtype.vsew, ctrl.imm))).asUInt
43  val _vs1 = Mux(SrcType.isImm(ctrl.srcType(0)), imm,
44             Mux(in.uop.ctrl.srcType(0) === SrcType.vp, io.in.bits.src(0), VecExtractor(vtype.vsew, io.in.bits.src(0))))
45  val _vs2 = in.src(1)
46  // generate src1 and src2
47  val vs1 = Mux(needReverse, _vs2, _vs1)
48  val vs2 = Mux(needReverse, _vs1, _vs2)
49  val mask = Mux(needClearMask, 0.U, in.src(3))
50
51  // connect io
52  io.out.bits.uop := DontCare
53  io.in.ready := DontCare
54  fflags := DontCare
55  vxsat := DontCare
56
57}
58
59
60abstract class VPUSubModule(len: Int = 128)(implicit p: Parameters) extends FunctionUnit(len: Int)
61{
62  val rm = IO(Input(UInt(3.W)))
63  val fflags = IO(Output(UInt(5.W)))
64  val vstart = IO(Input(UInt(XLEN.W)))
65  val vxrm = IO(Input(UInt(2.W)))
66  val vxsat = IO(Output(UInt(1.W)))
67
68  val dataModule: Seq[VPUDataModule]
69  val select : Seq[Bool]
70
71  def connectDataModule = {
72  // def some signal
73    val dataReg = Reg(io.out.bits.data.cloneType)
74    val dataWire = Wire(dataReg.cloneType)
75    val s_idle :: s_compute :: s_finish :: Nil = Enum(3)
76    val state = RegInit(s_idle)
77
78    val outValid = dataModule.map(_.io.out.valid).reduce(_||_)
79    val outFire = dataModule.map(_.io.out.fire()).reduce(_||_)
80  // reg input signal
81    val s0_uopReg = Reg(io.in.bits.uop.cloneType)
82    val s0_selectReg = Reg(VecInit(select).asUInt().cloneType)
83    val inHs = io.in.fire()
84    when(inHs && state === s_idle){
85        s0_uopReg := io.in.bits.uop
86        s0_selectReg := VecInit(select).asUInt()
87      }
88    dataReg := Mux(outValid, dataWire, dataReg)
89
90  // fsm
91    switch (state) {
92      is (s_idle) {
93        state := Mux(inHs, s_compute, s_idle)
94      }
95      is (s_compute) {
96        state := Mux(outValid, Mux(outFire, s_idle, s_finish),
97                              s_compute)
98      }
99      is (s_finish) {
100        state := Mux(io.out.fire(), s_idle, s_finish)
101      }
102    }
103
104  // connect
105    dataWire := Mux1H(s0_selectReg, dataModule.map(_.io.out.bits.data))
106    dataModule.zipWithIndex.foreach{ case(l, i) =>
107      l.io.in.bits <> io.in.bits
108      l.io.redirectIn := DontCare
109      l.rm := rm
110      l.vxrm := vxrm
111      l.vstart := vstart
112      l.io.in.valid := io.in.valid && state === s_idle && select(i)
113      l.io.out.ready := io.out.ready
114    }
115    vxsat := Mux1H(s0_selectReg, dataModule.map(_.vxsat))
116    fflags := Mux1H(s0_selectReg, dataModule.map(_.fflags))
117
118    io.out.bits.data :=  Mux(state === s_compute && outFire, dataWire, dataReg)
119    io.out.bits.uop := s0_uopReg
120    io.out.valid := state === s_compute && outValid || state === s_finish
121    io.in.ready := state === s_idle
122  }
123}
124
125
126object VecImmExtractor {
127  def Imm_OPIVIS(imm: UInt): UInt = {
128    SignExt(imm(4,0), 8)
129  }
130  def Imm_OPIVIU(imm: UInt): UInt = {
131    ZeroExt(imm(4,0), 8)
132  }
133
134  def imm_sew(sew: UInt, imm: UInt): UInt = {
135    val _imm = SignExt(imm(7,0), 64)
136    LookupTree(sew(1,0), List(
137      "b00".U -> VecInit(Seq.fill(8)(_imm(7,0))).asUInt,
138      "b01".U -> VecInit(Seq.fill(4)(_imm(15,0))).asUInt,
139      "b10".U -> VecInit(Seq.fill(2)(_imm(31,0))).asUInt,
140      "b11".U -> _imm(63,0),
141    ))
142  }
143
144  def apply(immType: UInt, sew: UInt, imm: UInt): UInt = {
145    val _imm = Mux(immType === SelImm.IMM_OPIVIS, Imm_OPIVIS(imm), Imm_OPIVIU(imm))
146    imm_sew(sew, _imm(7,0))
147  }
148}
149
150object VecExtractor{
151  def xf2v_sew(sew: UInt, xf:UInt): UInt = {
152    LookupTree(sew(1, 0), List(
153      "b00".U -> VecInit(Seq.fill(16)(xf(7, 0))).asUInt,
154      "b01".U -> VecInit(Seq.fill(8)(xf(15, 0))).asUInt,
155      "b10".U -> VecInit(Seq.fill(4)(xf(31, 0))).asUInt,
156      "b11".U -> VecInit(Seq.fill(2)(xf(63, 0))).asUInt,
157    ))
158  }
159
160  def apply(sew: UInt, xf: UInt): UInt = {
161    xf2v_sew(sew, xf)
162  }
163}