xref: /XiangShan/src/main/scala/xiangshan/backend/fu/vector/Mgu.scala (revision b1e920234888fd3e5463ceb2a99c9bdca087f585)
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.vector
20
21import org.chipsalliance.cde.config.Parameters
22import chisel3._
23import chisel3.util._
24import chiseltest._
25import chiseltest.ChiselScalatestTester
26import org.scalatest.flatspec.AnyFlatSpec
27import org.scalatest.matchers.must.Matchers
28import top.{ArgParser, BaseConfig, DefaultConfig}
29import xiangshan._
30import xiangshan.backend.fu.vector.Bundles.{VSew, Vl}
31import xiangshan.backend.fu.vector.Utils.VecDataToMaskDataVec
32import yunsuan.vector._
33
34class Mgu(vlen: Int)(implicit p: Parameters) extends  Module {
35  private val numBytes = vlen / 8
36  private val byteWidth = log2Up(numBytes)
37
38  val io = IO(new MguIO(vlen))
39
40  val in = io.in
41  val out = io.out
42  val info = in.info
43  val vd = in.vd
44  val oldVd = in.oldVd
45  val narrow = io.in.info.narrow
46
47  private val vdIdx = Mux(narrow, info.vdIdx(2, 1), info.vdIdx)
48
49  private val maskTailGen = Module(new ByteMaskTailGen(vlen))
50
51  private val eewOH = SewOH(info.eew).oneHot
52
53  private val vstartMapVdIdx = elemIdxMapVdIdx(info.vstart)(2, 0) // 3bits 0~7
54  private val vlMapVdIdx = elemIdxMapVdIdx(info.vl)(3, 0)         // 4bits 0~8
55  private val uvlMax = numBytes.U >> info.eew
56  private val uvlMaxForAssert = numBytes.U >> info.vsew
57  private val vlMaxForAssert = Mux(io.in.info.vlmul(2), uvlMaxForAssert >> (-io.in.info.vlmul), uvlMaxForAssert << io.in.info.vlmul).asUInt
58
59  private val realEw = Mux(in.isIndexedVls, info.vsew, info.eew)
60  private val maskDataVec: Vec[UInt] = VecDataToMaskDataVec(in.mask, realEw)
61  private val maskUsed = maskDataVec(vdIdx)
62
63  maskTailGen.io.in.begin := info.vstart /*Mux1H(Seq(
64    (vstartMapVdIdx < vdIdx) -> 0.U,
65    (vstartMapVdIdx === vdIdx) -> elemIdxMapUElemIdx(info.vstart),
66    (vstartMapVdIdx > vdIdx) -> uvlMax,
67  ))*/
68  maskTailGen.io.in.end := info.vl /*Mux1H(Seq(
69    (vlMapVdIdx < vdIdx) -> 0.U,
70    (vlMapVdIdx === vdIdx) -> elemIdxMapUElemIdx(info.vl),
71    (vlMapVdIdx > vdIdx) -> uvlMax,
72  ))*/
73  maskTailGen.io.in.vma := info.ma
74  maskTailGen.io.in.vta := info.ta
75  maskTailGen.io.in.vsew := realEw
76  maskTailGen.io.in.maskUsed := maskUsed
77  maskTailGen.io.in.vdIdx := info.vdIdx
78
79  private val keepEn = maskTailGen.io.out.keepEn
80  private val agnosticEn = maskTailGen.io.out.agnosticEn
81
82  // the result of normal inst and narrow inst which does not need concat
83  private val byte1s: UInt = (~0.U(8.W)).asUInt
84
85  private val resVecByte = Wire(Vec(numBytes, UInt(8.W)))
86  private val vdVecByte = vd.asTypeOf(resVecByte)
87  private val oldVdVecByte = oldVd.asTypeOf(resVecByte)
88
89  for (i <- 0 until numBytes) {
90    resVecByte(i) := MuxCase(oldVdVecByte(i), Seq(
91      keepEn(i) -> vdVecByte(i),
92      agnosticEn(i) -> byte1s,
93    ))
94  }
95
96  // the result of narrow inst which needs concat
97  private val narrowNeedCat = info.vdIdx(0).asBool & narrow
98  private val narrowResCat = Cat(resVecByte.asUInt(vlen / 2 - 1, 0), oldVd(vlen / 2 - 1, 0))
99
100  // the result of mask-generating inst
101  private val maxVdIdx = 8
102  private val meaningfulBitsSeq = Seq(16, 8, 4, 2)
103  private val allPossibleResBit = Wire(Vec(4, Vec(maxVdIdx, UInt(vlen.W))))
104  private val catData = Mux(info.ta, ~0.U(vlen.W), oldVd)
105
106  for (sew <- 0 to 3) {
107    if (sew == 0) {
108      allPossibleResBit(sew)(maxVdIdx - 1) := Cat(vd(meaningfulBitsSeq(sew) - 1, 0),
109        oldVd(meaningfulBitsSeq(sew) * (maxVdIdx - 1) - 1, 0))
110    } else {
111      allPossibleResBit(sew)(maxVdIdx - 1) := Cat(catData(vlen - 1, meaningfulBitsSeq(sew) * maxVdIdx),
112        vd(meaningfulBitsSeq(sew) - 1, 0), oldVd(meaningfulBitsSeq(sew) * (maxVdIdx - 1) - 1, 0))
113    }
114    for (i <- 1 until maxVdIdx - 1) {
115      allPossibleResBit(sew)(i) := Cat(catData(vlen - 1, meaningfulBitsSeq(sew) * (i + 1)),
116        vd(meaningfulBitsSeq(sew) - 1, 0), oldVd(meaningfulBitsSeq(sew) * i - 1, 0))
117    }
118    allPossibleResBit(sew)(0) := Cat(catData(vlen - 1, meaningfulBitsSeq(sew)), vd(meaningfulBitsSeq(sew) - 1, 0))
119  }
120
121  private val resVecBit = allPossibleResBit(info.eew)(vdIdx)
122
123  io.out.vd := MuxCase(resVecByte.asUInt, Seq(
124    info.dstMask -> resVecBit.asUInt,
125    narrowNeedCat -> narrowResCat,
126  ))
127  io.out.keep := keepEn
128  io.out.illegal := (info.vl > vlMaxForAssert) && info.valid
129
130  io.debugOnly.vstartMapVdIdx := vstartMapVdIdx
131  io.debugOnly.vlMapVdIdx := vlMapVdIdx
132  io.debugOnly.begin := maskTailGen.io.in.begin
133  io.debugOnly.end := maskTailGen.io.in.end
134  io.debugOnly.keepEn := keepEn
135  io.debugOnly.agnosticEn := agnosticEn
136  def elemIdxMapVdIdx(elemIdx: UInt) = {
137    require(elemIdx.getWidth >= log2Up(vlen))
138    // 3 = log2(8)
139    Mux1H(eewOH, Seq.tabulate(eewOH.getWidth)(x => elemIdx(byteWidth - x + 3, byteWidth - x)))
140  }
141
142  def elemIdxMapUElemIdx(elemIdx: UInt) = {
143    Mux1H(eewOH, Seq.tabulate(eewOH.getWidth)(x => elemIdx(byteWidth - x - 1, 0)))
144  }
145}
146
147
148class MguIO(vlen: Int)(implicit p: Parameters) extends Bundle {
149  val in = new Bundle {
150    val vd = Input(UInt(vlen.W))
151    val oldVd = Input(UInt(vlen.W))
152    val mask = Input(UInt(vlen.W))
153    val info = Input(new VecInfo)
154    val isIndexedVls = Input(Bool())
155  }
156  val out = new Bundle {
157    val vd = Output(UInt(vlen.W))
158    val keep = Output(UInt((vlen / 8).W))
159    val illegal = Output(Bool())
160  }
161  val debugOnly = Output(new Bundle {
162    val vstartMapVdIdx = UInt()
163    val vlMapVdIdx = UInt()
164    val begin = UInt()
165    val end = UInt()
166    val keepEn = UInt()
167    val agnosticEn = UInt()
168  })
169}
170
171class VecInfo(implicit p: Parameters) extends Bundle {
172  val ta = Bool()
173  val ma = Bool()
174  val vl = Vl()
175  val vstart = Vl()
176  val eew = VSew()
177  val vsew = VSew()
178  val vdIdx = UInt(3.W) // 0~7
179  val vlmul = UInt(3.W)
180  val valid = Bool()
181  val narrow = Bool()
182  val dstMask = Bool()
183}
184
185object VerilogMgu extends App {
186  println("Generating the Mgu hardware")
187  val (config, firrtlOpts, firtoolOpts) = ArgParser.parse(args)
188  val p = config.alterPartial({case XSCoreParamsKey => config(XSTileKey).head})
189
190  emitVerilog(new Mgu(128)(p), Array("--target-dir", "build/vifu", "--full-stacktrace"))
191}
192
193class MguTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
194
195  val defaultConfig = (new DefaultConfig).alterPartial({
196    case XSCoreParamsKey => XSCoreParameters()
197  })
198
199  println("test start")
200
201  behavior of "Mgu"
202  it should "run" in {
203    test(new Mgu(128)(defaultConfig)).withAnnotations(Seq(VerilatorBackendAnnotation)) {
204      m: Mgu =>
205        m.io.in.vd.poke("h8765_4321_8765_4321_8765_4321_8765_4321".U)
206        m.io.in.oldVd.poke("h7777_7777_7777_7777_7777_7777_7777_7777".U)
207        m.io.in.mask.poke("h0000_0000_0000_0000_0000_0000_ffff_0000".U)
208        m.io.in.info.ta.poke(true.B)
209        m.io.in.info.ma.poke(false.B)
210        m.io.in.info.vl.poke((16 + 7).U)
211        m.io.in.info.vstart.poke((16 + 2).U)
212        m.io.in.info.eew.poke(VSew.e8)
213        m.io.in.info.vdIdx.poke(1.U)
214
215        println("out.vd: " + m.io.out.vd.peek().litValue.toString(16))
216        println("debugOnly.vstartMapVdIdx: " + m.io.debugOnly.vstartMapVdIdx.peek().litValue.toString(16))
217        println("debugOnly.vlMapVdIdx: "     + m.io.debugOnly.vlMapVdIdx.peek().litValue.toString(16))
218        println("debugOnly.begin: "          + m.io.debugOnly.begin.peek().litValue)
219        println("debugOnly.end: "            + m.io.debugOnly.end.peek().litValue)
220        println("debugOnly.keepEn: "         + m.io.debugOnly.keepEn.peek().litValue.toString(2))
221        println("debugOnly.agnosticEn: "     + m.io.debugOnly.agnosticEn.peek().litValue.toString(2))
222    }
223    println("test done")
224  }
225}