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 protected lazy 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 := vdIdx 78 79 private val activeEn = maskTailGen.io.out.activeEn 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 activeEn(i) -> vdVecByte(i), 92 agnosticEn(i) -> byte1s, 93 )) 94 } 95 96 // the result of mask-generating inst 97 private val maxVdIdx = 8 98 private val meaningfulBitsSeq = Seq(16, 8, 4, 2) 99 private val allPossibleResBit = Wire(Vec(4, Vec(maxVdIdx, UInt(vlen.W)))) 100 private val catData = Mux(info.ta, ~0.U(vlen.W), oldVd) 101 102 for (sew <- 0 to 3) { 103 if (sew == 0) { 104 allPossibleResBit(sew)(maxVdIdx - 1) := Cat(vd(meaningfulBitsSeq(sew) - 1, 0), 105 oldVd(meaningfulBitsSeq(sew) * (maxVdIdx - 1) - 1, 0)) 106 } else { 107 allPossibleResBit(sew)(maxVdIdx - 1) := Cat(catData(vlen - 1, meaningfulBitsSeq(sew) * maxVdIdx), 108 vd(meaningfulBitsSeq(sew) - 1, 0), oldVd(meaningfulBitsSeq(sew) * (maxVdIdx - 1) - 1, 0)) 109 } 110 for (i <- 1 until maxVdIdx - 1) { 111 allPossibleResBit(sew)(i) := Cat(catData(vlen - 1, meaningfulBitsSeq(sew) * (i + 1)), 112 vd(meaningfulBitsSeq(sew) - 1, 0), oldVd(meaningfulBitsSeq(sew) * i - 1, 0)) 113 } 114 allPossibleResBit(sew)(0) := Cat(catData(vlen - 1, meaningfulBitsSeq(sew)), vd(meaningfulBitsSeq(sew) - 1, 0)) 115 } 116 117 private val resVecBit = allPossibleResBit(info.eew)(vdIdx) 118 119 io.out.vd := MuxCase(resVecByte.asUInt, Seq( 120 info.dstMask -> resVecBit.asUInt, 121 )) 122 io.out.active := activeEn 123 io.out.illegal := false.B // (info.vl > vlMaxForAssert) && info.valid 124 125 io.debugOnly.vstartMapVdIdx := vstartMapVdIdx 126 io.debugOnly.vlMapVdIdx := vlMapVdIdx 127 io.debugOnly.begin := maskTailGen.io.in.begin 128 io.debugOnly.end := maskTailGen.io.in.end 129 io.debugOnly.activeEn := activeEn 130 io.debugOnly.agnosticEn := agnosticEn 131 def elemIdxMapVdIdx(elemIdx: UInt) = { 132 require(elemIdx.getWidth >= log2Up(vlen)) 133 // 3 = log2(8) 134 Mux1H(eewOH, Seq.tabulate(eewOH.getWidth)(x => elemIdx(byteWidth - x + 3, byteWidth - x))) 135 } 136 137 def elemIdxMapUElemIdx(elemIdx: UInt) = { 138 Mux1H(eewOH, Seq.tabulate(eewOH.getWidth)(x => elemIdx(byteWidth - x - 1, 0))) 139 } 140} 141 142class VldMgu(vlen: Int)(implicit p: Parameters) extends Mgu(vlen) { 143 override lazy val maskUsed = in.mask(15, 0) 144} 145 146class MguIO(vlen: Int)(implicit p: Parameters) extends Bundle { 147 val in = new Bundle { 148 val vd = Input(UInt(vlen.W)) 149 val oldVd = Input(UInt(vlen.W)) 150 val mask = Input(UInt(vlen.W)) 151 val info = Input(new VecInfo) 152 val isIndexedVls = Input(Bool()) 153 } 154 val out = new Bundle { 155 val vd = Output(UInt(vlen.W)) 156 val active = Output(UInt((vlen / 8).W)) 157 val illegal = Output(Bool()) 158 } 159 val debugOnly = Output(new Bundle { 160 val vstartMapVdIdx = UInt() 161 val vlMapVdIdx = UInt() 162 val begin = UInt() 163 val end = UInt() 164 val activeEn = UInt() 165 val agnosticEn = UInt() 166 }) 167} 168 169class VecInfo(implicit p: Parameters) extends Bundle { 170 val ta = Bool() 171 val ma = Bool() 172 val vl = Vl() 173 val vstart = Vl() 174 val eew = VSew() 175 val vsew = VSew() 176 val vdIdx = UInt(3.W) // 0~7 177 val vlmul = UInt(3.W) 178 val valid = Bool() 179 val narrow = Bool() 180 val dstMask = Bool() 181} 182 183object VerilogMgu extends App { 184 println("Generating the Mgu hardware") 185 val (config, firrtlOpts, firtoolOpts) = ArgParser.parse(args) 186 val p = config.alterPartial({case XSCoreParamsKey => config(XSTileKey).head}) 187 188 emitVerilog(new Mgu(128)(p), Array("--target-dir", "build/vifu", "--full-stacktrace")) 189} 190 191class MguTest extends AnyFlatSpec with ChiselScalatestTester with Matchers { 192 193 val defaultConfig = (new DefaultConfig).alterPartial({ 194 case XSCoreParamsKey => XSCoreParameters() 195 }) 196 197 println("test start") 198 199 behavior of "Mgu" 200 it should "run" in { 201 test(new Mgu(128)(defaultConfig)).withAnnotations(Seq(VerilatorBackendAnnotation)) { 202 m: Mgu => 203 m.io.in.vd.poke("h8765_4321_8765_4321_8765_4321_8765_4321".U) 204 m.io.in.oldVd.poke("h7777_7777_7777_7777_7777_7777_7777_7777".U) 205 m.io.in.mask.poke("h0000_0000_0000_0000_0000_0000_ffff_0000".U) 206 m.io.in.info.ta.poke(true.B) 207 m.io.in.info.ma.poke(false.B) 208 m.io.in.info.vl.poke((16 + 7).U) 209 m.io.in.info.vstart.poke((16 + 2).U) 210 m.io.in.info.eew.poke(VSew.e8) 211 m.io.in.info.vdIdx.poke(1.U) 212 213 println("out.vd: " + m.io.out.vd.peek().litValue.toString(16)) 214 println("debugOnly.vstartMapVdIdx: " + m.io.debugOnly.vstartMapVdIdx.peek().litValue.toString(16)) 215 println("debugOnly.vlMapVdIdx: " + m.io.debugOnly.vlMapVdIdx.peek().litValue.toString(16)) 216 println("debugOnly.begin: " + m.io.debugOnly.begin.peek().litValue) 217 println("debugOnly.end: " + m.io.debugOnly.end.peek().litValue) 218 println("debugOnly.activeEn: " + m.io.debugOnly.activeEn.peek().litValue.toString(2)) 219 println("debugOnly.agnosticEn: " + m.io.debugOnly.agnosticEn.peek().litValue.toString(2)) 220 } 221 println("test done") 222 } 223}