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