xref: /XiangShan/src/main/scala/xiangshan/backend/fu/Vsetu.scala (revision e6ac7fe1c52621343e26686048729fbfcc617b25)
1edace9bfSxiwenx/***************************************************************************************
2edace9bfSxiwenx * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3edace9bfSxiwenx * Copyright (c) 2020-2021 Peng Cheng Laboratory
4edace9bfSxiwenx *
5edace9bfSxiwenx * XiangShan is licensed under Mulan PSL v2.
6edace9bfSxiwenx * You can use this software according to the terms and conditions of the Mulan PSL v2.
7edace9bfSxiwenx * You may obtain a copy of Mulan PSL v2 at:
8edace9bfSxiwenx *          http://license.coscl.org.cn/MulanPSL2
9edace9bfSxiwenx *
10edace9bfSxiwenx * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11edace9bfSxiwenx * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12edace9bfSxiwenx * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13edace9bfSxiwenx *
14edace9bfSxiwenx * See the Mulan PSL v2 for more details.
15edace9bfSxiwenx ***************************************************************************************/
16edace9bfSxiwenx
17edace9bfSxiwenxpackage xiangshan.backend.fu
18edace9bfSxiwenx
1983ba63b3SXuan Huimport org.chipsalliance.cde.config.Parameters
20edace9bfSxiwenximport chisel3._
21edace9bfSxiwenximport chisel3.util._
223b739f49SXuan Huimport xiangshan._
234c8a449fSZiyue Zhangimport xiangshan.backend.fu.vector.Bundles.{VConfig, VType, Vl, VSew, VLmul, VsetVType}
24edace9bfSxiwenx
25a32c56f4SXuan Huclass VsetModuleIO(implicit p: Parameters) extends XSBundle {
26a32c56f4SXuan Hu  private val vlWidth = p(XSCoreParamsKey).vlWidth
27a32c56f4SXuan Hu
28a32c56f4SXuan Hu  val in = Input(new Bundle {
29a8db15d8Sfdy    val avl   : UInt = UInt(XLEN.W)
304c8a449fSZiyue Zhang    val vtype : VsetVType = VsetVType()
31a32c56f4SXuan Hu    val func  : UInt = FuOpType()
32edace9bfSxiwenx  })
33edace9bfSxiwenx
34a32c56f4SXuan Hu  val out = Output(new Bundle {
35a32c56f4SXuan Hu    val vconfig: VConfig = VConfig()
36b6279fc6SZiyue Zhang    val vlmax  : UInt = UInt(vlWidth.W)
37a32c56f4SXuan Hu  })
38edace9bfSxiwenx
39a32c56f4SXuan Hu  // test bundle for internal state
40a32c56f4SXuan Hu  val testOut = Output(new Bundle {
41a32c56f4SXuan Hu    val log2Vlmax : UInt = UInt(3.W)
42a32c56f4SXuan Hu    val vlmax     : UInt = UInt(vlWidth.W)
43a32c56f4SXuan Hu  })
44a32c56f4SXuan Hu}
45edace9bfSxiwenx
46a32c56f4SXuan Huclass VsetModule(implicit p: Parameters) extends XSModule {
47a32c56f4SXuan Hu  val io = IO(new VsetModuleIO)
48d91483a6Sfdy
49a32c56f4SXuan Hu  private val avl   = io.in.avl
50a32c56f4SXuan Hu  private val func  = io.in.func
51a32c56f4SXuan Hu  private val vtype = io.in.vtype
52a32c56f4SXuan Hu
53a32c56f4SXuan Hu  private val outVConfig = io.out.vconfig
54a32c56f4SXuan Hu
55a32c56f4SXuan Hu  private val vlWidth = p(XSCoreParamsKey).vlWidth
56a32c56f4SXuan Hu
57a32c56f4SXuan Hu  private val isSetVlmax = VSETOpType.isSetVlmax(func)
58a32c56f4SXuan Hu  private val isVsetivli = VSETOpType.isVsetivli(func)
59a32c56f4SXuan Hu
60a32c56f4SXuan Hu  private val vlmul: UInt = vtype.vlmul
61a32c56f4SXuan Hu  private val vsew : UInt = vtype.vsew
62a32c56f4SXuan Hu
63d91483a6Sfdy  private val vl = WireInit(0.U(XLEN.W))
64edace9bfSxiwenx
65a8db15d8Sfdy  // EncodedLMUL = log(LMUL)
66a8db15d8Sfdy  // EncodedSEW  = log(SEW) - 3
67a32c56f4SXuan Hu  //        VLMAX  = VLEN * LMUL / SEW
68a8db15d8Sfdy  // => log(VLMAX) = log(VLEN * LMUL / SEW)
69a8db15d8Sfdy  // => log(VLMAX) = log(VLEN) + log(LMUL) - log(SEW)
70a8db15d8Sfdy  // =>     VLMAX  = 1 << log(VLMAX)
71a8db15d8Sfdy  //               = 1 << (log(VLEN) + log(LMUL) - log(SEW))
72a32c56f4SXuan Hu
73edace9bfSxiwenx  // vlen =  128
74a32c56f4SXuan Hu  private val log2Vlen = log2Up(VLEN)
75a32c56f4SXuan Hu  println(s"[VsetModule] log2Vlen: $log2Vlen")
76a32c56f4SXuan Hu  println(s"[VsetModule] vlWidth: $vlWidth")
77a8db15d8Sfdy
78a8db15d8Sfdy  private val log2Vlmul = vlmul
794c8a449fSZiyue Zhang  // use 2 bits vsew to store vsew
804c8a449fSZiyue Zhang  private val log2Vsew = vsew(VSew.width - 1, 0) +& "b011".U
81a32c56f4SXuan Hu
82a32c56f4SXuan Hu  // vlen = 128, lmul = 8, sew = 8, log2Vlen = 7,
83a8db15d8Sfdy  // vlmul = b011, vsew = 0, 7 + 3 - (0 + 3) = 7
84a32c56f4SXuan Hu  // vlen = 128, lmul = 2, sew = 16
85a8db15d8Sfdy  // vlmul = b001, vsew = 1, 7 + 1 - (1 + 3) = 4
86a8db15d8Sfdy  private val log2Vlmax: UInt = log2Vlen.U(3.W) + log2Vlmul - log2Vsew
87a8db15d8Sfdy  private val vlmax = (1.U(vlWidth.W) << log2Vlmax).asUInt
88edace9bfSxiwenx
89d91483a6Sfdy  private val normalVL = Mux(avl > vlmax, vlmax, avl)
90edace9bfSxiwenx
91931544a3SXuan Hu  vl := Mux(isVsetivli, normalVL, Mux(isSetVlmax, vlmax, normalVL))
92a32c56f4SXuan Hu
93a8db15d8Sfdy  private val log2Elen = log2Up(ELEN)
94a8db15d8Sfdy  private val log2VsewMax = Mux(log2Vlmul(2), log2Elen.U + log2Vlmul, log2Elen.U)
95a8db15d8Sfdy
964c8a449fSZiyue Zhang  private val sewIllegal = VSew.isReserved(vsew) || (log2Vsew > log2VsewMax)
974c8a449fSZiyue Zhang  private val lmulIllegal = VLmul.isReserved(vlmul)
98*e6ac7fe1SZiyue Zhang  private val vtypeIllegal = vtype.reserved.orR
99a8db15d8Sfdy
100*e6ac7fe1SZiyue Zhang  private val illegal = lmulIllegal | sewIllegal | vtypeIllegal | vtype.illegal
101a8db15d8Sfdy
102a8db15d8Sfdy  outVConfig.vl := Mux(illegal, 0.U, vl)
103a8db15d8Sfdy  outVConfig.vtype.illegal := illegal
104a8db15d8Sfdy  outVConfig.vtype.vta := Mux(illegal, 0.U, vtype.vta)
105a8db15d8Sfdy  outVConfig.vtype.vma := Mux(illegal, 0.U, vtype.vma)
106a8db15d8Sfdy  outVConfig.vtype.vlmul := Mux(illegal, 0.U, vtype.vlmul)
107a8db15d8Sfdy  outVConfig.vtype.vsew := Mux(illegal, 0.U, vtype.vsew)
108a32c56f4SXuan Hu
1097994c930Ssinsanction  private val log2VlenDivVsew = log2Vlen.U(3.W) - log2Vsew
1107994c930Ssinsanction  private val vlenDivVsew = 1.U(vlWidth.W) << log2VlenDivVsew
1117994c930Ssinsanction  io.out.vlmax := Mux(vlmax >= vlenDivVsew, vlmax, vlenDivVsew)
112b6279fc6SZiyue Zhang
113a32c56f4SXuan Hu  io.testOut.vlmax := vlmax
114a32c56f4SXuan Hu  io.testOut.log2Vlmax := log2Vlmax
115edace9bfSxiwenx}
116