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.mem 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utils._ 23import utility._ 24import xiangshan._ 25import xiangshan.backend.rob.RobPtr 26import xiangshan.backend.Bundles._ 27import xiangshan.backend.fu.FuType 28import xiangshan.backend.fu.vector.Bundles.VEew 29 30/** 31 * Common used parameters or functions in vlsu 32 */ 33trait VLSUConstants { 34 val VLEN = 128 35 //for pack unit-stride flow 36 val AlignedNum = 4 // 1/2/4/8 37 def VLENB = VLEN/8 38 def vOffsetBits = log2Up(VLENB) // bits-width to index offset inside a vector reg 39 lazy val vlmBindexBits = 8 //will be overrided later 40 lazy val vsmBindexBits = 8 // will be overrided later 41 42 def alignTypes = 5 // eew/sew = 1/2/4/8, last indicate 128 bit element 43 def alignTypeBits = log2Up(alignTypes) 44 def maxMUL = 8 45 def maxFields = 8 46 /** 47 * In the most extreme cases like a segment indexed instruction, eew=64, emul=8, sew=8, lmul=1, 48 * and nf=8, each data reg is mapped with 8 index regs and there are 8 data regs in total, 49 * each for a field. Therefore an instruction can be divided into 64 uops at most. 50 */ 51 def maxUopNum = maxMUL * maxFields // 64 52 def maxFlowNum = 16 53 def maxElemNum = maxMUL * maxFlowNum // 128 54 // def uopIdxBits = log2Up(maxUopNum) // to index uop inside an robIdx 55 def elemIdxBits = log2Up(maxElemNum) + 1 // to index which element in an instruction 56 def flowIdxBits = log2Up(maxFlowNum) + 1 // to index which flow in a uop 57 def fieldBits = log2Up(maxFields) + 1 // 4-bits to indicate 1~8 58 59 def ewBits = 3 // bits-width of EEW/SEW 60 def mulBits = 3 // bits-width of emul/lmul 61 62 def getSlice(data: UInt, i: Int, alignBits: Int): UInt = { 63 require(data.getWidth >= (i+1) * alignBits) 64 data((i+1) * alignBits - 1, i * alignBits) 65 } 66 def getNoAlignedSlice(data: UInt, i: Int, alignBits: Int): UInt = { 67 data(i * 8 + alignBits - 1, i * 8) 68 } 69 70 def getByte(data: UInt, i: Int = 0) = getSlice(data, i, 8) 71 def getHalfWord(data: UInt, i: Int = 0) = getSlice(data, i, 16) 72 def getWord(data: UInt, i: Int = 0) = getSlice(data, i, 32) 73 def getDoubleWord(data: UInt, i: Int = 0) = getSlice(data, i, 64) 74 def getDoubleDoubleWord(data: UInt, i: Int = 0) = getSlice(data, i, 128) 75} 76 77trait HasVLSUParameters extends HasXSParameter with VLSUConstants { 78 override val VLEN = coreParams.VLEN 79 override lazy val vlmBindexBits = log2Up(coreParams.VlMergeBufferSize) 80 override lazy val vsmBindexBits = log2Up(coreParams.VsMergeBufferSize) 81 lazy val maxMemByteNum = 16 // Maximum bytes for a single memory access 82 /** 83 * get addr aligned low bits 84 * @param addr Address to be check 85 * @param width Width for checking alignment 86 */ 87 def getCheckAddrLowBits(addr: UInt, width: Int): UInt = addr(log2Up(width) - 1, 0) 88 def getOverflowBit(in: UInt, width: Int): UInt = in(log2Up(width)) 89 def isUnitStride(instType: UInt) = instType(1, 0) === "b00".U 90 def isStrided(instType: UInt) = instType(1, 0) === "b10".U 91 def isIndexed(instType: UInt) = instType(0) === "b1".U 92 def isNotIndexed(instType: UInt) = instType(0) === "b0".U 93 def isSegment(instType: UInt) = instType(2) === "b1".U 94 def is128Bit(alignedType: UInt) = alignedType(2) === "b1".U 95 96 def mergeDataWithMask(oldData: UInt, newData: UInt, mask: UInt): Vec[UInt] = { 97 require(oldData.getWidth == newData.getWidth) 98 require(oldData.getWidth == mask.getWidth * 8) 99 VecInit(mask.asBools.zipWithIndex.map { case (en, i) => 100 Mux(en, getByte(newData, i), getByte(oldData, i)) 101 }) 102 } 103 104 // def asBytes(data: UInt) = { 105 // require(data.getWidth % 8 == 0) 106 // (0 until data.getWidth/8).map(i => getByte(data, i)) 107 // } 108 109 def mergeDataWithElemIdx( 110 oldData: UInt, 111 newData: Seq[UInt], 112 alignedType: UInt, 113 elemIdx: Seq[UInt], 114 valids: Seq[Bool] 115 ): UInt = { 116 require(newData.length == elemIdx.length) 117 require(newData.length == valids.length) 118 LookupTree(alignedType, List( 119 "b00".U -> VecInit(elemIdx.map(e => UIntToOH(e(3, 0)).asBools).transpose.zipWithIndex.map { case (selVec, i) => 120 ParallelPosteriorityMux( 121 true.B +: selVec.zip(valids).map(x => x._1 && x._2), 122 getByte(oldData, i) +: newData.map(getByte(_)) 123 )}).asUInt, 124 "b01".U -> VecInit(elemIdx.map(e => UIntToOH(e(2, 0)).asBools).transpose.zipWithIndex.map { case (selVec, i) => 125 ParallelPosteriorityMux( 126 true.B +: selVec.zip(valids).map(x => x._1 && x._2), 127 getHalfWord(oldData, i) +: newData.map(getHalfWord(_)) 128 )}).asUInt, 129 "b10".U -> VecInit(elemIdx.map(e => UIntToOH(e(1, 0)).asBools).transpose.zipWithIndex.map { case (selVec, i) => 130 ParallelPosteriorityMux( 131 true.B +: selVec.zip(valids).map(x => x._1 && x._2), 132 getWord(oldData, i) +: newData.map(getWord(_)) 133 )}).asUInt, 134 "b11".U -> VecInit(elemIdx.map(e => UIntToOH(e(0)).asBools).transpose.zipWithIndex.map { case (selVec, i) => 135 ParallelPosteriorityMux( 136 true.B +: selVec.zip(valids).map(x => x._1 && x._2), 137 getDoubleWord(oldData, i) +: newData.map(getDoubleWord(_)) 138 )}).asUInt 139 )) 140 } 141 142 def mergeDataWithElemIdx(oldData: UInt, newData: UInt, alignedType: UInt, elemIdx: UInt): UInt = { 143 mergeDataWithElemIdx(oldData, Seq(newData), alignedType, Seq(elemIdx), Seq(true.B)) 144 } 145 /** 146 * for merge 128-bits data of unit-stride 147 */ 148 object mergeDataByByte{ 149 def apply(oldData: UInt, newData: UInt, mask: UInt): UInt = { 150 val selVec = Seq(mask).map(_.asBools).transpose 151 VecInit(selVec.zipWithIndex.map{ case (selV, i) => 152 ParallelPosteriorityMux( 153 true.B +: selV.map(x => x), 154 getByte(oldData, i) +: Seq(getByte(newData, i)) 155 )}).asUInt 156 } 157 } 158 159 /** 160 * for merge Unit-Stride data to 256-bits 161 * merge 128-bits data to 256-bits 162 * if have 3 port, 163 * if is port0, it is 6 to 1 Multiplexer -> (128'b0, data) or (data, 128'b0) or (data, port2data) or (port2data, data) or (data, port3data) or (port3data, data) 164 * if is port1, it is 4 to 1 Multiplexer -> (128'b0, data) or (data, 128'b0) or (data, port3data) or (port3data, data) 165 * if is port3, it is 2 to 1 Multiplexer -> (128'b0, data) or (data, 128'b0) 166 * 167 */ 168 object mergeDataByIndex{ 169 def apply(data: Seq[UInt], mask: Seq[UInt], index: UInt, valids: Seq[Bool]): (UInt, UInt) = { 170 require(data.length == valids.length) 171 require(data.length == mask.length) 172 val muxLength = data.length 173 val selDataMatrix = Wire(Vec(muxLength, Vec(2, UInt((VLEN * 2).W)))) // 3 * 2 * 256 174 val selMaskMatrix = Wire(Vec(muxLength, Vec(2, UInt((VLENB * 2).W)))) // 3 * 2 * 16 175 dontTouch(selDataMatrix) 176 dontTouch(selMaskMatrix) 177 for(i <- 0 until muxLength){ 178 if(i == 0){ 179 selDataMatrix(i)(0) := Cat(0.U(VLEN.W), data(i)) 180 selDataMatrix(i)(1) := Cat(data(i), 0.U(VLEN.W)) 181 selMaskMatrix(i)(0) := Cat(0.U(VLENB.W), mask(i)) 182 selMaskMatrix(i)(1) := Cat(mask(i), 0.U(VLENB.W)) 183 } 184 else{ 185 selDataMatrix(i)(0) := Cat(data(i), data(0)) 186 selDataMatrix(i)(1) := Cat(data(0), data(i)) 187 selMaskMatrix(i)(0) := Cat(mask(i), mask(0)) 188 selMaskMatrix(i)(1) := Cat(mask(0), mask(i)) 189 } 190 } 191 val selIdxVec = (0 until muxLength).map(_.U) 192 val selIdx = PriorityMux(valids.reverse, selIdxVec.reverse) 193 194 val selData = Mux(index === 0.U, 195 selDataMatrix(selIdx)(0), 196 selDataMatrix(selIdx)(1)) 197 val selMask = Mux(index === 0.U, 198 selMaskMatrix(selIdx)(0), 199 selMaskMatrix(selIdx)(1)) 200 (selData, selMask) 201 } 202 } 203 def mergeDataByIndex(data: UInt, mask: UInt, index: UInt): (UInt, UInt) = { 204 mergeDataByIndex(Seq(data), Seq(mask), index, Seq(true.B)) 205 } 206} 207abstract class VLSUModule(implicit p: Parameters) extends XSModule 208 with HasVLSUParameters 209 with HasCircularQueuePtrHelper 210abstract class VLSUBundle(implicit p: Parameters) extends XSBundle 211 with HasVLSUParameters 212 213class VLSUBundleWithMicroOp(implicit p: Parameters) extends VLSUBundle { 214 val uop = new DynInst 215} 216 217class OnlyVecExuOutput(implicit p: Parameters) extends VLSUBundle { 218 val isvec = Bool() 219 val vecdata = UInt(VLEN.W) 220 val mask = UInt(VLENB.W) 221 // val rob_idx_valid = Vec(2, Bool()) 222 // val inner_idx = Vec(2, UInt(3.W)) 223 // val rob_idx = Vec(2, new RobPtr) 224 // val offset = Vec(2, UInt(4.W)) 225 val reg_offset = UInt(vOffsetBits.W) 226 val vecActive = Bool() // 1: vector active element, 0: vector not active element 227 val is_first_ele = Bool() 228 val elemIdx = UInt(elemIdxBits.W) // element index 229 val elemIdxInsideVd = UInt(elemIdxBits.W) // element index in scope of vd 230 // val uopQueuePtr = new VluopPtr 231 // val flowPtr = new VlflowPtr 232} 233 234class VecExuOutput(implicit p: Parameters) extends MemExuOutput with HasVLSUParameters { 235 val vec = new OnlyVecExuOutput 236 val alignedType = UInt(alignTypeBits.W) 237 // feedback 238 val vecFeedback = Bool() 239} 240 241class VecUopBundle(implicit p: Parameters) extends VLSUBundleWithMicroOp { 242 val flowMask = UInt(VLENB.W) // each bit for a flow 243 val byteMask = UInt(VLENB.W) // each bit for a byte 244 val data = UInt(VLEN.W) 245 // val fof = Bool() // fof is only used for vector loads 246 val excp_eew_index = UInt(elemIdxBits.W) 247 // val exceptionVec = ExceptionVec() // uop has exceptionVec 248 val baseAddr = UInt(VAddrBits.W) 249 val stride = UInt(VLEN.W) 250 val flow_counter = UInt(flowIdxBits.W) 251 252 // instruction decode result 253 val flowNum = UInt(flowIdxBits.W) // # of flows in a uop 254 // val flowNumLog2 = UInt(log2Up(flowIdxBits).W) // log2(flowNum), for better timing of multiplication 255 val nfields = UInt(fieldBits.W) // NFIELDS 256 val vm = Bool() // whether vector masking is enabled 257 val usWholeReg = Bool() // unit-stride, whole register load 258 val usMaskReg = Bool() // unit-stride, masked store/load 259 val eew = VEew() // size of memory elements 260 val sew = UInt(ewBits.W) 261 val emul = UInt(mulBits.W) 262 val lmul = UInt(mulBits.W) 263 val vlmax = UInt(elemIdxBits.W) 264 val instType = UInt(3.W) 265 val vd_last_uop = Bool() 266 val vd_first_uop = Bool() 267} 268 269class VecFlowBundle(implicit p: Parameters) extends VLSUBundleWithMicroOp { 270 val vaddr = UInt(VAddrBits.W) 271 val mask = UInt(VLENB.W) 272 val alignedType = UInt(alignTypeBits.W) 273 val vecActive = Bool() 274 val elemIdx = UInt(elemIdxBits.W) 275 val is_first_ele = Bool() 276 277 // pack 278 val isPackage = Bool() 279 val packageNum = UInt((log2Up(VLENB) + 1).W) 280 val originAlignedType = UInt(alignTypeBits.W) 281} 282 283class VecMemExuOutput(isVector: Boolean = false)(implicit p: Parameters) extends VLSUBundle{ 284 val output = new MemExuOutput(isVector) 285 val vecFeedback = Bool() 286 val mmio = Bool() 287 val usSecondInv = Bool() 288 val elemIdx = UInt(elemIdxBits.W) 289 val alignedType = UInt(alignTypeBits.W) 290 val mbIndex = UInt(vsmBindexBits.W) 291 val mask = UInt(VLENB.W) 292 val vaddr = UInt(XLEN.W) 293 val gpaddr = UInt(GPAddrBits.W) 294 val isForVSnonLeafPTE = Bool() 295} 296 297object MulNum { 298 def apply (mul: UInt): UInt = { //mul means emul or lmul 299 (LookupTree(mul,List( 300 "b101".U -> 1.U , // 1/8 301 "b110".U -> 1.U , // 1/4 302 "b111".U -> 1.U , // 1/2 303 "b000".U -> 1.U , // 1 304 "b001".U -> 2.U , // 2 305 "b010".U -> 4.U , // 4 306 "b011".U -> 8.U // 8 307 )))} 308} 309/** 310 * when emul is greater than or equal to 1, this means the entire register needs to be written; 311 * otherwise, only write the specified number of bytes */ 312object MulDataSize { 313 def apply (mul: UInt): UInt = { //mul means emul or lmul 314 (LookupTree(mul,List( 315 "b101".U -> 2.U , // 1/8 316 "b110".U -> 4.U , // 1/4 317 "b111".U -> 8.U , // 1/2 318 "b000".U -> 16.U , // 1 319 "b001".U -> 16.U , // 2 320 "b010".U -> 16.U , // 4 321 "b011".U -> 16.U // 8 322 )))} 323} 324 325object OneRegNum { 326 def apply (eew: UInt): UInt = { //mul means emul or lmul 327 require(eew.getWidth == 2, "The eew width must be 2.") 328 (LookupTree(eew, List( 329 "b00".U -> 16.U , // 1 330 "b01".U -> 8.U , // 2 331 "b10".U -> 4.U , // 4 332 "b11".U -> 2.U // 8 333 )))} 334} 335 336//index inst read data byte 337object SewDataSize { 338 def apply (sew: UInt): UInt = { 339 (LookupTree(sew,List( 340 "b000".U -> 1.U , // 1 341 "b001".U -> 2.U , // 2 342 "b010".U -> 4.U , // 4 343 "b011".U -> 8.U // 8 344 )))} 345} 346 347// strided inst read data byte 348object EewDataSize { 349 def apply (eew: UInt): UInt = { 350 require(eew.getWidth == 2, "The eew width must be 2.") 351 (LookupTree(eew, List( 352 "b00".U -> 1.U , // 1 353 "b01".U -> 2.U , // 2 354 "b10".U -> 4.U , // 4 355 "b11".U -> 8.U // 8 356 )))} 357} 358 359object loadDataSize { 360 def apply (instType: UInt, emul: UInt, eew: UInt, sew: UInt): UInt = { 361 (LookupTree(instType,List( 362 "b000".U -> MulDataSize(emul), // unit-stride 363 "b010".U -> EewDataSize(eew) , // strided 364 "b001".U -> SewDataSize(sew) , // indexed-unordered 365 "b011".U -> SewDataSize(sew) , // indexed-ordered 366 "b100".U -> EewDataSize(eew) , // segment unit-stride 367 "b110".U -> EewDataSize(eew) , // segment strided 368 "b101".U -> SewDataSize(sew) , // segment indexed-unordered 369 "b111".U -> SewDataSize(sew) // segment indexed-ordered 370 )))} 371} 372 373object storeDataSize { 374 def apply (instType: UInt, eew: UInt, sew: UInt): UInt = { 375 (LookupTree(instType,List( 376 "b000".U -> EewDataSize(eew) , // unit-stride, do not use 377 "b010".U -> EewDataSize(eew) , // strided 378 "b001".U -> SewDataSize(sew) , // indexed-unordered 379 "b011".U -> SewDataSize(sew) , // indexed-ordered 380 "b100".U -> EewDataSize(eew) , // segment unit-stride 381 "b110".U -> EewDataSize(eew) , // segment strided 382 "b101".U -> SewDataSize(sew) , // segment indexed-unordered 383 "b111".U -> SewDataSize(sew) // segment indexed-ordered 384 )))} 385} 386 387/** 388 * these are used to obtain immediate addresses for index instruction */ 389object EewEq8 { 390 def apply(index:UInt, flow_inner_idx: UInt): UInt = { 391 (LookupTree(flow_inner_idx,List( 392 0.U -> index(7 ,0 ), 393 1.U -> index(15,8 ), 394 2.U -> index(23,16 ), 395 3.U -> index(31,24 ), 396 4.U -> index(39,32 ), 397 5.U -> index(47,40 ), 398 6.U -> index(55,48 ), 399 7.U -> index(63,56 ), 400 8.U -> index(71,64 ), 401 9.U -> index(79,72 ), 402 10.U -> index(87,80 ), 403 11.U -> index(95,88 ), 404 12.U -> index(103,96 ), 405 13.U -> index(111,104), 406 14.U -> index(119,112), 407 15.U -> index(127,120) 408 )))} 409} 410 411object EewEq16 { 412 def apply(index: UInt, flow_inner_idx: UInt): UInt = { 413 (LookupTree(flow_inner_idx, List( 414 0.U -> index(15, 0), 415 1.U -> index(31, 16), 416 2.U -> index(47, 32), 417 3.U -> index(63, 48), 418 4.U -> index(79, 64), 419 5.U -> index(95, 80), 420 6.U -> index(111, 96), 421 7.U -> index(127, 112) 422 )))} 423} 424 425object EewEq32 { 426 def apply(index: UInt, flow_inner_idx: UInt): UInt = { 427 (LookupTree(flow_inner_idx, List( 428 0.U -> index(31, 0), 429 1.U -> index(63, 32), 430 2.U -> index(95, 64), 431 3.U -> index(127, 96) 432 )))} 433} 434 435object EewEq64 { 436 def apply (index: UInt, flow_inner_idx: UInt): UInt = { 437 (LookupTree(flow_inner_idx, List( 438 0.U -> index(63, 0), 439 1.U -> index(127, 64) 440 )))} 441} 442 443object IndexAddr { 444 def apply (index: UInt, flow_inner_idx: UInt, eew: UInt): UInt = { 445 require(eew.getWidth == 2, "The eew width must be 2.") 446 (LookupTree(eew, List( 447 "b00".U -> EewEq8 (index = index, flow_inner_idx = flow_inner_idx ), // Imm is 1 Byte // TODO: index maybe cross register 448 "b01".U -> EewEq16(index = index, flow_inner_idx = flow_inner_idx ), // Imm is 2 Byte 449 "b10".U -> EewEq32(index = index, flow_inner_idx = flow_inner_idx ), // Imm is 4 Byte 450 "b11".U -> EewEq64(index = index, flow_inner_idx = flow_inner_idx ) // Imm is 8 Byte 451 )))} 452} 453 454object Log2Num { 455 def apply (num: UInt): UInt = { 456 (LookupTree(num,List( 457 16.U -> 4.U, 458 8.U -> 3.U, 459 4.U -> 2.U, 460 2.U -> 1.U, 461 1.U -> 0.U 462 )))} 463} 464 465object GenUopIdxInField { 466 /** 467 * Used in normal vector instruction 468 * */ 469 def apply (instType: UInt, emul: UInt, lmul: UInt, uopIdx: UInt): UInt = { 470 val isIndexed = instType(0) 471 val mulInField = Mux( 472 isIndexed, 473 Mux(lmul.asSInt > emul.asSInt, lmul, emul), 474 emul 475 ) 476 LookupTree(mulInField, List( 477 "b101".U -> 0.U, 478 "b110".U -> 0.U, 479 "b111".U -> 0.U, 480 "b000".U -> 0.U, 481 "b001".U -> uopIdx(0), 482 "b010".U -> uopIdx(1, 0), 483 "b011".U -> uopIdx(2, 0) 484 )) 485 } 486 /** 487 * Only used in segment instruction. 488 * */ 489 def apply (select: UInt, uopIdx: UInt): UInt = { 490 LookupTree(select, List( 491 "b101".U -> 0.U, 492 "b110".U -> 0.U, 493 "b111".U -> 0.U, 494 "b000".U -> 0.U, 495 "b001".U -> uopIdx(0), 496 "b010".U -> uopIdx(1, 0), 497 "b011".U -> uopIdx(2, 0) 498 )) 499 } 500} 501 502//eew decode 503object EewLog2 extends VLSUConstants { 504 // def apply (eew: UInt): UInt = { 505 // (LookupTree(eew,List( 506 // "b000".U -> "b000".U , // 1 507 // "b101".U -> "b001".U , // 2 508 // "b110".U -> "b010".U , // 4 509 // "b111".U -> "b011".U // 8 510 // )))} 511 def apply(eew: UInt): UInt = { 512 require(eew.getWidth == 2, "The eew width must be 2.") 513 ZeroExt(eew, ewBits) 514 } 515} 516 517object GenRealFlowNum { 518 /** 519 * unit-stride instructions don't use this method; 520 * other instructions generate realFlowNum by EmulDataSize >> eew, 521 * EmulDataSize means the number of bytes that need to be written to the register, 522 * eew means the number of bytes written at once. 523 * 524 * @param instType As the name implies. 525 * @param emul As the name implies. 526 * @param lmul As the name implies. 527 * @param eew As the name implies. 528 * @param sew As the name implies. 529 * @param isSegment Only modules related to segment need to be set to true. 530 * @return FlowNum of instruction. 531 * 532 */ 533 def apply (instType: UInt, emul: UInt, lmul: UInt, eew: UInt, sew: UInt, isSegment: Boolean = false): UInt = { 534 require(instType.getWidth == 3, "The instType width must be 3, (isSegment, mop)") 535 require(eew.getWidth == 2, "The eew width must be 2.") 536 // Because the new segmentunit is needed. But the previous implementation is retained for the time being in case of emergency. 537 val segmentIndexFlowNum = if (isSegment) (MulDataSize(lmul) >> sew(1,0)).asUInt 538 else Mux(emul.asSInt > lmul.asSInt, (MulDataSize(emul) >> eew).asUInt, (MulDataSize(lmul) >> sew(1,0)).asUInt) 539 (LookupTree(instType,List( 540 "b000".U -> (MulDataSize(emul) >> eew).asUInt, // store use, load do not use 541 "b010".U -> (MulDataSize(emul) >> eew).asUInt, // strided 542 "b001".U -> Mux(emul.asSInt > lmul.asSInt, (MulDataSize(emul) >> eew).asUInt, (MulDataSize(lmul) >> sew(1,0)).asUInt), // indexed-unordered 543 "b011".U -> Mux(emul.asSInt > lmul.asSInt, (MulDataSize(emul) >> eew).asUInt, (MulDataSize(lmul) >> sew(1,0)).asUInt), // indexed-ordered 544 "b100".U -> (MulDataSize(emul) >> eew).asUInt, // segment unit-stride 545 "b110".U -> (MulDataSize(emul) >> eew).asUInt, // segment strided 546 "b101".U -> segmentIndexFlowNum, // segment indexed-unordered 547 "b111".U -> segmentIndexFlowNum // segment indexed-ordered 548 )))} 549} 550 551object GenRealFlowLog2 extends VLSUConstants { 552 /** 553 * GenRealFlowLog2 = Log2(GenRealFlowNum) 554 * 555 * @param instType As the name implies. 556 * @param emul As the name implies. 557 * @param lmul As the name implies. 558 * @param eew As the name implies. 559 * @param sew As the name implies. 560 * @param isSegment Only modules related to segment need to be set to true. 561 * @return FlowNumLog2 of instruction. 562 */ 563 def apply(instType: UInt, emul: UInt, lmul: UInt, eew: UInt, sew: UInt, isSegment: Boolean = false): UInt = { 564 require(instType.getWidth == 3, "The instType width must be 3, (isSegment, mop)") 565 require(eew.getWidth == 2, "The eew width must be 2.") 566 val emulLog2 = Mux(emul.asSInt >= 0.S, 0.U, emul) 567 val lmulLog2 = Mux(lmul.asSInt >= 0.S, 0.U, lmul) 568 val eewRealFlowLog2 = emulLog2 + log2Up(VLENB).U - eew 569 val sewRealFlowLog2 = lmulLog2 + log2Up(VLENB).U - sew(1, 0) 570 // Because the new segmentunit is needed. But the previous implementation is retained for the time being in case of emergency. 571 val segmentIndexFlowLog2 = if (isSegment) sewRealFlowLog2 else Mux(emul.asSInt > lmul.asSInt, eewRealFlowLog2, sewRealFlowLog2) 572 (LookupTree(instType, List( 573 "b000".U -> eewRealFlowLog2, // unit-stride 574 "b010".U -> eewRealFlowLog2, // strided 575 "b001".U -> Mux(emul.asSInt > lmul.asSInt, eewRealFlowLog2, sewRealFlowLog2), // indexed-unordered 576 "b011".U -> Mux(emul.asSInt > lmul.asSInt, eewRealFlowLog2, sewRealFlowLog2), // indexed-ordered 577 "b100".U -> eewRealFlowLog2, // segment unit-stride 578 "b110".U -> eewRealFlowLog2, // segment strided 579 "b101".U -> segmentIndexFlowLog2, // segment indexed-unordered 580 "b111".U -> segmentIndexFlowLog2, // segment indexed-ordered 581 ))) 582 } 583} 584 585/** 586 * GenElemIdx generals an element index within an instruction, given a certain uopIdx and a known flowIdx 587 * inside the uop. 588 */ 589object GenElemIdx extends VLSUConstants { 590 def apply(instType: UInt, emul: UInt, lmul: UInt, eew: UInt, sew: UInt, 591 uopIdx: UInt, flowIdx: UInt): UInt = { 592 require(eew.getWidth == 2, "The eew width must be 2.") 593 val isIndexed = instType(0).asBool 594 val eewUopFlowsLog2 = Mux(emul.asSInt > 0.S, 0.U, emul) + log2Up(VLENB).U - eew 595 val sewUopFlowsLog2 = Mux(lmul.asSInt > 0.S, 0.U, lmul) + log2Up(VLENB).U - sew(1, 0) 596 val uopFlowsLog2 = Mux( 597 isIndexed, 598 Mux(emul.asSInt > lmul.asSInt, eewUopFlowsLog2, sewUopFlowsLog2), 599 eewUopFlowsLog2 600 ) 601 LookupTree(uopFlowsLog2, List( 602 0.U -> uopIdx, 603 1.U -> uopIdx ## flowIdx(0), 604 2.U -> uopIdx ## flowIdx(1, 0), 605 3.U -> uopIdx ## flowIdx(2, 0), 606 4.U -> uopIdx ## flowIdx(3, 0) 607 )) 608 } 609} 610 611/** 612 * GenVLMAX calculates VLMAX, which equals MUL * ew 613 */ 614object GenVLMAXLog2 extends VLSUConstants { 615 def apply(lmul: UInt, sew: UInt): UInt = lmul + log2Up(VLENB).U - sew 616} 617object GenVLMAX { 618 def apply(lmul: UInt, sew: UInt): UInt = 1.U << GenVLMAXLog2(lmul, sew) 619} 620/** 621 * generate mask base on vlmax 622 * example: vlmax = b100, max = b011 623 * */ 624object GenVlMaxMask{ 625 def apply(vlmax: UInt, length: Int): UInt = (vlmax - 1.U)(length-1, 0) 626} 627 628object GenUSWholeRegVL extends VLSUConstants { 629 def apply(nfields: UInt, eew: UInt): UInt = { 630 require(eew.getWidth == 2, "The eew width must be 2.") 631 LookupTree(eew, List( 632 "b00".U -> (nfields << (log2Up(VLENB) - 0)), 633 "b01".U -> (nfields << (log2Up(VLENB) - 1)), 634 "b10".U -> (nfields << (log2Up(VLENB) - 2)), 635 "b11".U -> (nfields << (log2Up(VLENB) - 3)) 636 )) 637 } 638} 639object GenUSWholeEmul extends VLSUConstants{ 640 def apply(nf: UInt): UInt={ 641 LookupTree(nf,List( 642 "b000".U -> "b000".U(mulBits.W), 643 "b001".U -> "b001".U(mulBits.W), 644 "b011".U -> "b010".U(mulBits.W), 645 "b111".U -> "b011".U(mulBits.W) 646 )) 647 } 648} 649 650 651object GenUSMaskRegVL extends VLSUConstants { 652 def apply(vl: UInt): UInt = { 653 Mux(vl(2,0) === 0.U , (vl >> 3.U), ((vl >> 3.U) + 1.U)) 654 } 655} 656 657object GenUopByteMask { 658 def apply(flowMask: UInt, alignedType: UInt): UInt = { 659 LookupTree(alignedType, List( 660 "b000".U -> flowMask, 661 "b001".U -> FillInterleaved(2, flowMask), 662 "b010".U -> FillInterleaved(4, flowMask), 663 "b011".U -> FillInterleaved(8, flowMask), 664 "b100".U -> FillInterleaved(16, flowMask) 665 )) 666 } 667} 668 669object GenVdIdxInField extends VLSUConstants { 670 def apply(instType: UInt, emul: UInt, lmul: UInt, uopIdx: UInt): UInt = { 671 val vdIdx = Wire(UInt(log2Up(maxMUL).W)) 672 when (instType(1,0) === "b00".U || instType(1,0) === "b10".U || lmul.asSInt > emul.asSInt) { 673 // Unit-stride or Strided, or indexed with lmul >= emul 674 vdIdx := uopIdx 675 }.otherwise { 676 // Indexed with lmul <= emul 677 val multiple = emul - lmul 678 val uopIdxWidth = uopIdx.getWidth 679 vdIdx := LookupTree(multiple, List( 680 0.U -> uopIdx, 681 1.U -> (uopIdx >> 1), 682 2.U -> (uopIdx >> 2), 683 3.U -> (uopIdx >> 3) 684 )) 685 } 686 vdIdx 687 } 688} 689/** 690* Use start and vl to generate flow activative mask 691* mod = true fill 0 692* mod = false fill 1 693*/ 694object GenFlowMask extends VLSUConstants { 695 def apply(elementMask: UInt, start: UInt, vl: UInt , mod: Boolean): UInt = { 696 val startMask = ~UIntToMask(start, VLEN) 697 val vlMask = UIntToMask(vl, VLEN) 698 val maskVlStart = vlMask & startMask 699 if(mod){ 700 elementMask & maskVlStart 701 } 702 else{ 703 (~elementMask).asUInt & maskVlStart 704 } 705 } 706} 707 708object genVWmask128 { 709 def apply(addr: UInt, sizeEncode: UInt): UInt = { 710 (LookupTree(sizeEncode, List( 711 "b000".U -> 0x1.U, //0001 << addr(2:0) 712 "b001".U -> 0x3.U, //0011 713 "b010".U -> 0xf.U, //1111 714 "b011".U -> 0xff.U, //11111111 715 "b100".U -> 0xffff.U //1111111111111111 716 )) << addr(3, 0)).asUInt 717 } 718} 719/* 720* only use in max length is 128 721*/ 722object genVWdata { 723 def apply(data: UInt, sizeEncode: UInt): UInt = { 724 LookupTree(sizeEncode, List( 725 "b000".U -> Fill(16, data(7, 0)), 726 "b001".U -> Fill(8, data(15, 0)), 727 "b010".U -> Fill(4, data(31, 0)), 728 "b011".U -> Fill(2, data(63,0)), 729 "b100".U -> data(127,0) 730 )) 731 } 732} 733 734object genUSSplitAddr{ 735 def apply(addr: UInt, index: UInt, width: Int): UInt = { 736 val tmpAddr = Cat(addr(width - 1, 4), 0.U(4.W)) 737 val nextCacheline = tmpAddr + 16.U 738 LookupTree(index, List( 739 0.U -> tmpAddr, 740 1.U -> nextCacheline 741 )) 742 } 743} 744 745object genUSSplitMask{ 746 def apply(mask: UInt, index: UInt): UInt = { 747 require(mask.getWidth == 32) // need to be 32-bits 748 LookupTree(index, List( 749 0.U -> mask(15, 0), 750 1.U -> mask(31, 16), 751 )) 752 } 753} 754 755object genUSSplitData{ 756 def apply(data: UInt, index: UInt, addrOffset: UInt): UInt = { 757 val tmpData = WireInit(0.U(256.W)) 758 val lookupTable = (0 until 16).map{case i => 759 if(i == 0){ 760 i.U -> Cat(0.U(128.W), data) 761 }else{ 762 i.U -> Cat(0.U(((16-i)*8).W), data, 0.U((i*8).W)) 763 } 764 } 765 tmpData := LookupTree(addrOffset, lookupTable).asUInt 766 767 LookupTree(index, List( 768 0.U -> tmpData(127, 0), 769 1.U -> tmpData(255, 128) 770 )) 771 } 772} 773 774object genVSData extends VLSUConstants { 775 def apply(data: UInt, elemIdx: UInt, alignedType: UInt): UInt = { 776 LookupTree(alignedType, List( 777 "b000".U -> ZeroExt(LookupTree(elemIdx(3, 0), List.tabulate(VLEN/8)(i => i.U -> getByte(data, i))), VLEN), 778 "b001".U -> ZeroExt(LookupTree(elemIdx(2, 0), List.tabulate(VLEN/16)(i => i.U -> getHalfWord(data, i))), VLEN), 779 "b010".U -> ZeroExt(LookupTree(elemIdx(1, 0), List.tabulate(VLEN/32)(i => i.U -> getWord(data, i))), VLEN), 780 "b011".U -> ZeroExt(LookupTree(elemIdx(0), List.tabulate(VLEN/64)(i => i.U -> getDoubleWord(data, i))), VLEN), 781 "b100".U -> data // if have wider element, it will broken 782 )) 783 } 784} 785 786// TODO: more elegant 787object genVStride extends VLSUConstants { 788 def apply(uopIdx: UInt, stride: UInt): UInt = { 789 LookupTree(uopIdx, List( 790 0.U -> 0.U, 791 1.U -> stride, 792 2.U -> (stride << 1), 793 3.U -> ((stride << 1).asUInt + stride), 794 4.U -> (stride << 2), 795 5.U -> ((stride << 2).asUInt + stride), 796 6.U -> ((stride << 2).asUInt + (stride << 1)), 797 7.U -> ((stride << 2).asUInt + (stride << 1) + stride) 798 )) 799 } 800} 801/** 802 * generate uopOffset, not used in segment instruction 803 * */ 804object genVUopOffset extends VLSUConstants { 805 def apply(instType: UInt, isfof: Bool, uopidx: UInt, nf: UInt, eew: UInt, stride: UInt, alignedType: UInt): UInt = { 806 val uopInsidefield = (uopidx >> nf).asUInt // when nf == 0, is uopidx 807 808 val fofVUopOffset = (LookupTree(instType,List( 809 "b000".U -> ( genVStride(uopInsidefield, stride) << (log2Up(VLENB).U - eew) ) , // unit-stride fof 810 "b100".U -> ( genVStride(uopInsidefield, stride) << (log2Up(VLENB).U - eew) ) , // segment unit-stride fof 811 ))).asUInt 812 813 val otherVUopOffset = (LookupTree(instType,List( 814 "b000".U -> ( uopInsidefield << alignedType ) , // unit-stride 815 "b010".U -> ( genVStride(uopInsidefield, stride) << (log2Up(VLENB).U - eew) ) , // strided 816 "b001".U -> ( 0.U ) , // indexed-unordered 817 "b011".U -> ( 0.U ) , // indexed-ordered 818 "b100".U -> ( uopInsidefield << alignedType ) , // segment unit-stride 819 "b110".U -> ( genVStride(uopInsidefield, stride) << (log2Up(VLENB).U - eew) ) , // segment strided 820 "b101".U -> ( 0.U ) , // segment indexed-unordered 821 "b111".U -> ( 0.U ) // segment indexed-ordered 822 ))).asUInt 823 824 Mux(isfof, fofVUopOffset, otherVUopOffset) 825 } 826} 827 828 829 830object genVFirstUnmask extends VLSUConstants { 831 /** 832 * Find the lowest unmasked number of bits. 833 * example: 834 * mask = 16'b1111_1111_1110_0000 835 * return 5 836 * @param mask 16bits of mask. 837 * @return lowest unmasked number of bits. 838 */ 839 def apply(mask: UInt): UInt = { 840 require(mask.getWidth == 16, "The mask width must be 16") 841 val select = (0 until 16).zip(mask.asBools).map{case (i, v) => 842 (v, i.U) 843 } 844 PriorityMuxDefault(select, 0.U) 845 } 846 847 def apply(mask: UInt, regOffset: UInt): UInt = { 848 require(mask.getWidth == 16, "The mask width must be 16") 849 val realMask = (mask >> regOffset).asUInt 850 val select = (0 until 16).zip(realMask.asBools).map{case (i, v) => 851 (v, i.U) 852 } 853 PriorityMuxDefault(select, 0.U) 854 } 855} 856 857class skidBufferConnect[T <: Data](gen: T) extends Module { 858 val io = IO(new Bundle() { 859 val in = Flipped(DecoupledIO(gen.cloneType)) 860 val flush = Input(Bool()) 861 val out = DecoupledIO(gen.cloneType) 862 }) 863 864 skidBuffer.connect(io.in, io.out, io.flush) 865} 866 867object skidBuffer{ 868 /* 869 * Skid Buffer used to break timing path of ready 870 * */ 871 def connect[T <: Data]( 872 in: DecoupledIO[T], 873 out: DecoupledIO[T], 874 flush: Bool 875 ): T = { 876 val empty :: skid :: Nil = Enum(2) 877 val state = RegInit(empty) 878 val stateNext = WireInit(empty) 879 val dataBuffer = RegEnable(in.bits, (!out.ready && in.fire)) 880 881 when(state === empty){ 882 stateNext := Mux(!out.ready && in.fire && !flush, skid, empty) 883 }.elsewhen(state === skid){ 884 stateNext := Mux(out.ready || flush, empty, skid) 885 } 886 state := stateNext 887 888 in.ready := state === empty 889 out.bits := Mux(state === skid, dataBuffer, in.bits) 890 out.valid := in.valid || (state === skid) 891 892 dataBuffer 893 } 894 def apply[T <: Data]( 895 in: DecoupledIO[T], 896 out: DecoupledIO[T], 897 flush: Bool, 898 moduleName: String 899 ): Unit = { 900 val buffer = Module(new skidBufferConnect(in.bits)) 901 buffer.suggestName(moduleName) 902 buffer.io.in <> in 903 buffer.io.flush := flush 904 out <> buffer.io.out 905 } 906} 907 908