xref: /XiangShan/src/main/scala/xiangshan/mem/vector/VSegmentUnit.scala (revision 46e9ee74b05bff567ec23c58a1dde7151176258f)
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.mem._
28import xiangshan.backend.fu.FuType
29import freechips.rocketchip.diplomacy.BufferParams
30import xiangshan.cache.mmu._
31import xiangshan.cache._
32import xiangshan.cache.wpu.ReplayCarry
33import xiangshan.backend.fu.util.SdtrigExt
34import xiangshan.ExceptionNO._
35import xiangshan.backend.fu.vector.Bundles.VConfig
36import xiangshan.backend.datapath.NewPipelineConnect
37import xiangshan.backend.fu.vector.Utils.VecDataToMaskDataVec
38
39class VSegmentBundle(implicit p: Parameters) extends VLSUBundle
40{
41  val baseVaddr        = UInt(XLEN.W)
42  val uop              = new DynInst
43  val paddr            = UInt(PAddrBits.W)
44  val mask             = UInt(VLEN.W)
45  val alignedType      = UInt(alignTypeBits.W)
46  val vl               = UInt(elemIdxBits.W)
47  val uopFlowNum       = UInt(elemIdxBits.W)
48  val uopFlowNumMask   = UInt(elemIdxBits.W)
49  // for exception
50  val vstart           = UInt(elemIdxBits.W)
51  val exceptionVaddr   = UInt(XLEN.W)
52  val exceptionGpaddr  = UInt(XLEN.W)
53  val exceptionIsForVSnonLeafPTE = Bool()
54  val exception_va     = Bool()
55  val exception_gpa    = Bool()
56  val exception_pa     = Bool()
57  val exceptionVstart  = UInt(elemIdxBits.W)
58  val exceptionVl      = UInt(elemIdxBits.W)
59  val isFof            = Bool()
60}
61
62// latch each uop's VecWen, pdest, v0Wen, uopIdx
63class VSegmentUop(implicit p: Parameters) extends VLSUBundle{
64  val uop              = new DynInst
65}
66
67class VSegmentUnit (implicit p: Parameters) extends VLSUModule
68  with HasDCacheParameters
69  with MemoryOpConstants
70  with SdtrigExt
71  with HasLoadHelper
72{
73  val io               = IO(new VSegmentUnitIO)
74
75  val maxSize          = VSegmentBufferSize
76
77  class VSegUPtr(implicit p: Parameters) extends CircularQueuePtr[VSegUPtr](maxSize){
78  }
79
80  object VSegUPtr {
81    def apply(f: Bool, v: UInt)(implicit p: Parameters): VSegUPtr = {
82      val ptr           = Wire(new VSegUPtr)
83      ptr.flag         := f
84      ptr.value        := v
85      ptr
86    }
87  }
88
89
90  /**
91  ********************************************************************************************************
92  *  Use an example to illustrate the working logic of a segmentunit:                                    *
93  *    For:                                                                                              *
94  *      lmul=2 sew=32 emul=2 eew=32  vl=16                                                              *
95  *    Then:                                                                                             *
96  *      Access memory in the order:                                                                     *
97  *        (V2,S0),(V4,S0),(V6,S0),(V8,S0),                                                              *
98  *        (V2,S1),(V4,S1),(V6,S1),(V8,S1),                                                              *
99  *        (V2,S2),(V4,S2),(V6,S2),(V8,S2),                                                              *
100  *        (V2,S3),(V4,S3),(V6,S3),(V8,S3),                                                              *
101  *        (V3,S4),(V5,S4),(V7,S4),(V9,S4),                                                              *
102  *        (V3,S5),(V5,S5),(V7,S5),(V9,S5),                                                              *
103  *        (V3,S6),(V5,S6),(V7,S6),(V9,S6),                                                              *
104  *        (V3,S7),(V5,S7),(V7,S7),(V9,S7),                                                              *
105  *                                                                                                      *
106  *                                                                                                      *
107  *    [[data]] saves the data generated by the access and corresponds to the register.                  *
108  *    [[splitPtr]] controls the destination register written to.                                        *
109  *                                                                                                      *
110  *    splitptr offset can be seen in [[splitPtrNext]] is assignment logic,                              *
111  *    which is mainly calculated in terms of [[fieldIdx]] and [[segmentIdx]]                            *
112  *    First access different fields of the same segment, and then visit different segments.             *
113  *    For the case of 'emul' greater than 1, such as the following example,                             *
114  *    although 'v2' and 'v3' are different vd and the same field, they are still different segments,    *
115  *    so they should be accessed sequentially.Just like the 'Access memory in the order' above.         *
116  *                                                                                                      *
117  *                         [[segmentIdx]]                                                               *
118  *                               |                                                                      *
119  *                               |                                                                      *
120  *                               V                                                                      *
121  *                                                                                                      *
122  *                               S0               S1                S2                 S3               *
123  *                      ----------------------------------------------------------------------------    *
124  *  [[splitPtr]]--> v2  |     field0     |      field0     |      field0     |      field0         |    *
125  *                      ----------------------------------------------------------------------------    *
126  *                               S4               S5                S6                 S7               *
127  *                      ----------------------------------------------------------------------------    *
128  *                  v3  |     field0     |      field0     |      field0     |      field0         |    *
129  *                      ----------------------------------------------------------------------------    *
130  *                               S0               S1                S2                 S3               *
131  *                      ----------------------------------------------------------------------------    *
132  *                  v4  |     field1     |      field1     |      field1     |      field1         |    *
133  *                      ----------------------------------------------------------------------------    *
134  *                               S4               S5                S6                 S7               *
135  *                      ----------------------------------------------------------------------------    *
136  *                  v5  |     field1     |      field1     |      field1     |      field1         |    *
137  *                      ----------------------------------------------------------------------------    *
138  *                               S0               S1                S2                 S3               *
139  *                      ----------------------------------------------------------------------------    *
140  *                  v6  |     field2     |      field2     |      field2     |      field2         |    *
141  *                      ----------------------------------------------------------------------------    *
142  *                               S4               S5                S6                 S7               *
143  *                      ----------------------------------------------------------------------------    *
144  *                  v7  |     field2     |      field2     |      field2     |      field2         |    *
145  *                      ----------------------------------------------------------------------------    *
146  *                               S0               S1                S2                 S3               *
147  *                      ----------------------------------------------------------------------------    *
148  *                  v8  |     field3     |      field3     |      field3     |      field3         |    *
149  *                      ----------------------------------------------------------------------------    *
150  *                               S4               S5                S6                 S7               *
151  *                      ----------------------------------------------------------------------------    *
152  *                  v9  |     field3     |      field3     |      field3     |      field3         |    *
153  *                      ----------------------------------------------------------------------------    *                                                                                    *
154  *                                                                                                      *                                                                                    *
155  *                                                                                                      *                                                                                    *
156  ********************************************************************************************************
157  **/
158
159
160  // buffer uop
161  val instMicroOp       = Reg(new VSegmentBundle)
162  val instMicroOpValid  = RegInit(false.B)
163  val data              = Reg(Vec(maxSize, UInt(VLEN.W)))
164  val uopq              = Reg(Vec(maxSize, new VSegmentUop))
165  val stride            = Reg(Vec(maxSize, UInt(VLEN.W)))
166  val allocated         = RegInit(VecInit(Seq.fill(maxSize)(false.B)))
167  val enqPtr            = RegInit(0.U.asTypeOf(new VSegUPtr))
168  val deqPtr            = RegInit(0.U.asTypeOf(new VSegUPtr))
169  val stridePtr         = WireInit(0.U.asTypeOf(new VSegUPtr)) // for select stride/index
170
171  val segmentIdx        = RegInit(0.U(elemIdxBits.W))
172  val fieldIdx          = RegInit(0.U(fieldBits.W))
173  val segmentOffset     = RegInit(0.U(XLEN.W))
174  val splitPtr          = RegInit(0.U.asTypeOf(new VSegUPtr)) // for select load/store data
175  val splitPtrNext      = WireInit(0.U.asTypeOf(new VSegUPtr))
176
177  val exception_va      = WireInit(false.B)
178  val exception_gpa     = WireInit(false.B)
179  val exception_pa      = WireInit(false.B)
180
181  val maxSegIdx         = instMicroOp.vl - 1.U
182  val maxNfields        = instMicroOp.uop.vpu.nf
183  val latchVaddr        = RegInit(0.U(VAddrBits.W))
184
185  XSError((segmentIdx > maxSegIdx) && instMicroOpValid, s"segmentIdx > vl, something error!\n")
186  XSError((fieldIdx > maxNfields) &&  instMicroOpValid, s"fieldIdx > nfields, something error!\n")
187
188  // MicroOp
189  val baseVaddr                       = instMicroOp.baseVaddr
190  val alignedType                     = instMicroOp.alignedType
191  val fuType                          = instMicroOp.uop.fuType
192  val mask                            = instMicroOp.mask
193  val exceptionVec                    = instMicroOp.uop.exceptionVec
194  val issueEew                        = instMicroOp.uop.vpu.veew
195  val issueLmul                       = instMicroOp.uop.vpu.vtype.vlmul
196  val issueSew                        = instMicroOp.uop.vpu.vtype.vsew
197  val issueEmul                       = EewLog2(issueEew) - issueSew + issueLmul
198  val elemIdxInVd                     = segmentIdx & instMicroOp.uopFlowNumMask
199  val issueInstType                   = Cat(true.B, instMicroOp.uop.fuOpType(6, 5)) // always segment instruction
200  val issueUopFlowNumLog2             = GenRealFlowLog2(issueInstType, issueEmul, issueLmul, issueEew, issueSew, true) // max element number log2 in vd
201  val issueVlMax                      = instMicroOp.uopFlowNum // max elementIdx in vd
202  val issueMaxIdxInIndex              = GenVLMAX(Mux(issueEmul.asSInt > 0.S, 0.U, issueEmul), issueEew(1, 0)) // index element index in index register
203  val issueMaxIdxInIndexMask          = GenVlMaxMask(issueMaxIdxInIndex, elemIdxBits)
204  val issueMaxIdxInIndexLog2          = GenVLMAXLog2(Mux(issueEmul.asSInt > 0.S, 0.U, issueEmul), issueEew(1, 0))
205  val issueIndexIdx                   = segmentIdx & issueMaxIdxInIndexMask
206  val segmentActive                   = (mask & UIntToOH(segmentIdx)).orR
207
208  // sbuffer write interface
209  val sbufferOut                      = Wire(Decoupled(new DCacheWordReqWithVaddrAndPfFlag))
210
211  // Segment instruction's FSM
212  /*
213  * s_idle: wait request
214  * s_flush_sbuffer_req: flush sbuffer
215  * s_wait_flush_sbuffer_resp: wait sbuffer empty
216  * s_tlb_req: request tlb
217  * s_wait_tlb_resp: wait tlb resp
218  * s_pm: check pmp
219  * s_cache_req: request cache
220  * s_cache_resp: wait cache resp
221  * s_latch_and_merge_data: for read data
222  * s_send_data: for send write data
223  * s_wait_to_sbuffer: Wait for data from the sbufferOut pipelayer to be sent to the sbuffer
224  * s_finish:
225  * */
226  val s_idle :: s_flush_sbuffer_req :: s_wait_flush_sbuffer_resp :: s_tlb_req :: s_wait_tlb_resp :: s_pm ::s_cache_req :: s_cache_resp :: s_latch_and_merge_data :: s_send_data :: s_wait_to_sbuffer :: s_finish :: Nil = Enum(12)
227  val state             = RegInit(s_idle)
228  val stateNext         = WireInit(s_idle)
229  val sbufferEmpty      = io.flush_sbuffer.empty
230
231  /**
232   * state update
233   */
234  state  := stateNext
235
236  /**
237   * state transfer
238   */
239  when(state === s_idle){
240    stateNext := Mux(isAfter(enqPtr, deqPtr), s_flush_sbuffer_req, s_idle)
241  }.elsewhen(state === s_flush_sbuffer_req){
242    stateNext := Mux(sbufferEmpty, s_tlb_req, s_wait_flush_sbuffer_resp) // if sbuffer is empty, go to query tlb
243
244  }.elsewhen(state === s_wait_flush_sbuffer_resp){
245    stateNext := Mux(sbufferEmpty, s_tlb_req, s_wait_flush_sbuffer_resp)
246
247  }.elsewhen(state === s_tlb_req){
248    stateNext := Mux(segmentActive, s_wait_tlb_resp, Mux(FuType.isVLoad(instMicroOp.uop.fuType), s_latch_and_merge_data, s_send_data))
249
250  }.elsewhen(state === s_wait_tlb_resp){
251    stateNext := Mux(io.dtlb.resp.fire,
252                      Mux(!io.dtlb.resp.bits.miss,
253                          s_pm,
254                          s_tlb_req),
255                      s_wait_tlb_resp)
256
257  }.elsewhen(state === s_pm){
258    /* if is vStore, send data to sbuffer, so don't need query dcache */
259    stateNext := Mux(exception_pa || exception_va || exception_gpa,
260                     s_finish,
261                     Mux(FuType.isVLoad(instMicroOp.uop.fuType), s_cache_req, s_send_data))
262
263  }.elsewhen(state === s_cache_req){
264    stateNext := Mux(io.rdcache.req.fire, s_cache_resp, s_cache_req)
265
266  }.elsewhen(state === s_cache_resp){
267    when(io.rdcache.resp.fire) {
268      when(io.rdcache.resp.bits.miss || io.rdcache.s2_bank_conflict) {
269        stateNext := s_cache_req
270      }.otherwise {
271        stateNext := Mux(FuType.isVLoad(instMicroOp.uop.fuType), s_latch_and_merge_data, s_send_data)
272      }
273    }.otherwise{
274      stateNext := s_cache_resp
275    }
276    /* if segment is inactive, don't need to wait access all of the field */
277  }.elsewhen(state === s_latch_and_merge_data) {
278    when((segmentIdx === maxSegIdx) && (fieldIdx === maxNfields) ||
279      ((segmentIdx === maxSegIdx) && !segmentActive)) {
280
281      stateNext := s_finish // segment instruction finish
282    }.otherwise {
283      stateNext := s_tlb_req // need continue
284    }
285    /* if segment is inactive, don't need to wait access all of the field */
286  }.elsewhen(state === s_send_data) { // when sbuffer accept data
287    when(!sbufferOut.fire && segmentActive) {
288      stateNext := s_send_data
289    }.elsewhen(segmentIdx === maxSegIdx && (fieldIdx === maxNfields && sbufferOut.fire || !segmentActive && io.sbuffer.valid && !io.sbuffer.ready)) {
290      stateNext := s_wait_to_sbuffer
291    }.elsewhen(segmentIdx === maxSegIdx && !segmentActive){
292      stateNext := s_finish // segment instruction finish
293    }.otherwise {
294      stateNext := s_tlb_req // need continue
295    }
296
297  }.elsewhen(state === s_wait_to_sbuffer){
298    stateNext := Mux(io.sbuffer.fire, s_finish, s_wait_to_sbuffer)
299
300  }.elsewhen(state === s_finish){ // writeback uop
301    stateNext := Mux(distanceBetween(enqPtr, deqPtr) === 0.U, s_idle, s_finish)
302
303  }.otherwise{
304    stateNext := s_idle
305    XSError(true.B, s"Unknown state!\n")
306  }
307
308  /*************************************************************************
309   *                            enqueue logic
310   *************************************************************************/
311  io.in.ready                         := true.B
312  val fuOpType                         = io.in.bits.uop.fuOpType
313  val vtype                            = io.in.bits.uop.vpu.vtype
314  val mop                              = fuOpType(6, 5)
315  val instType                         = Cat(true.B, mop)
316  val eew                              = io.in.bits.uop.vpu.veew
317  val sew                              = vtype.vsew
318  val lmul                             = vtype.vlmul
319  val emul                             = EewLog2(eew) - sew + lmul
320  val vl                               = instMicroOp.vl
321  val vm                               = instMicroOp.uop.vpu.vm
322  val vstart                           = instMicroOp.uop.vpu.vstart
323  val srcMask                          = GenFlowMask(Mux(vm, Fill(VLEN, 1.U(1.W)), io.in.bits.src_mask), vstart, vl, true)
324  // first uop enqueue, we need to latch microOp of segment instruction
325  when(io.in.fire && !instMicroOpValid){
326    // element number in a vd
327    // TODO Rewrite it in a more elegant way.
328    val uopFlowNum                    = ZeroExt(GenRealFlowNum(instType, emul, lmul, eew, sew, true), elemIdxBits)
329    instMicroOp.baseVaddr             := io.in.bits.src_rs1
330    instMicroOpValid                  := true.B // if is first uop
331    instMicroOp.alignedType           := Mux(isIndexed(instType), sew(1, 0), eew)
332    instMicroOp.uop                   := io.in.bits.uop
333    instMicroOp.mask                  := srcMask
334    instMicroOp.vstart                := 0.U
335    instMicroOp.uopFlowNum            := uopFlowNum
336    instMicroOp.uopFlowNumMask        := GenVlMaxMask(uopFlowNum, elemIdxBits) // for merge data
337    instMicroOp.vl                    := io.in.bits.src_vl.asTypeOf(VConfig()).vl
338    segmentOffset                     := 0.U
339    instMicroOp.isFof                 := (fuOpType === VlduType.vleff) && FuType.isVLoad(fuType)
340  }
341  // latch data
342  when(io.in.fire){
343    data(enqPtr.value)                := io.in.bits.src_vs3
344    stride(enqPtr.value)              := io.in.bits.src_stride
345    uopq(enqPtr.value).uop            := io.in.bits.uop
346  }
347
348  // update enqptr, only 1 port
349  when(io.in.fire){
350    enqPtr                            := enqPtr + 1.U
351  }
352
353  /*************************************************************************
354   *                            output logic
355   *************************************************************************/
356
357  val indexStride                     = IndexAddr( // index for indexed instruction
358                                                    index = stride(stridePtr.value),
359                                                    flow_inner_idx = issueIndexIdx,
360                                                    eew = issueEew
361                                                  )
362  val realSegmentOffset               = Mux(isIndexed(issueInstType),
363                                            indexStride,
364                                            segmentOffset)
365  val vaddr                           = baseVaddr + (fieldIdx << alignedType).asUInt + realSegmentOffset
366
367  //latch vaddr
368  when(state === s_tlb_req){
369    latchVaddr := vaddr(VAddrBits - 1, 0)
370  }
371  /**
372   * tlb req and tlb resq
373   */
374
375  // query DTLB IO Assign
376  io.dtlb.req                         := DontCare
377  io.dtlb.resp.ready                  := true.B
378  io.dtlb.req.valid                   := state === s_tlb_req && segmentActive
379  io.dtlb.req.bits.cmd                := Mux(FuType.isVLoad(fuType), TlbCmd.read, TlbCmd.write)
380  io.dtlb.req.bits.vaddr              := vaddr(VAddrBits - 1, 0)
381  io.dtlb.req.bits.fullva             := vaddr
382  io.dtlb.req.bits.checkfullva        := true.B
383  io.dtlb.req.bits.size               := instMicroOp.alignedType(2,0)
384  io.dtlb.req.bits.memidx.is_ld       := FuType.isVLoad(fuType)
385  io.dtlb.req.bits.memidx.is_st       := FuType.isVStore(fuType)
386  io.dtlb.req.bits.debug.robIdx       := instMicroOp.uop.robIdx
387  io.dtlb.req.bits.no_translate       := false.B
388  io.dtlb.req.bits.debug.pc           := instMicroOp.uop.pc
389  io.dtlb.req.bits.debug.isFirstIssue := DontCare
390  io.dtlb.req_kill                    := false.B
391
392  val canTriggerException              = segmentIdx === 0.U || !instMicroOp.isFof // only elementIdx = 0 or is not fof can trigger
393  // tlb resp
394  when(io.dtlb.resp.fire && state === s_wait_tlb_resp){
395      exceptionVec(storePageFault)      := io.dtlb.resp.bits.excp(0).pf.st && canTriggerException
396      exceptionVec(loadPageFault)       := io.dtlb.resp.bits.excp(0).pf.ld && canTriggerException
397      exceptionVec(storeGuestPageFault) := io.dtlb.resp.bits.excp(0).gpf.st && canTriggerException
398      exceptionVec(loadGuestPageFault)  := io.dtlb.resp.bits.excp(0).gpf.ld && canTriggerException
399      exceptionVec(storeAccessFault)    := io.dtlb.resp.bits.excp(0).af.st && canTriggerException
400      exceptionVec(loadAccessFault)     := io.dtlb.resp.bits.excp(0).af.ld && canTriggerException
401      when(!io.dtlb.resp.bits.miss){
402        instMicroOp.paddr             := io.dtlb.resp.bits.paddr(0)
403        instMicroOp.exceptionGpaddr   := io.dtlb.resp.bits.gpaddr(0)
404        instMicroOp.exceptionIsForVSnonLeafPTE  := io.dtlb.resp.bits.isForVSnonLeafPTE
405      }
406  }
407  // pmp
408  // NOTE: only handle load/store exception here, if other exception happens, don't send here
409  val pmp = WireInit(io.pmpResp)
410  when(state === s_pm) {
411    val addr_aligned = LookupTree(Mux(isIndexed(issueInstType), issueSew(1, 0), issueEew(1, 0)), List(
412      "b00".U   -> true.B,                   //b
413      "b01".U   -> (vaddr(0)    === 0.U), //h
414      "b10".U   -> (vaddr(1, 0) === 0.U), //w
415      "b11".U   -> (vaddr(2, 0) === 0.U)  //d
416    ))
417    val missAligned = !addr_aligned
418    exceptionVec(loadAddrMisaligned)  := missAligned && FuType.isVLoad(fuType)  && canTriggerException
419    exceptionVec(storeAddrMisaligned) := missAligned && FuType.isVStore(fuType) && canTriggerException
420
421    exception_va  := exceptionVec(storePageFault) || exceptionVec(loadPageFault) ||
422      exceptionVec(storeAccessFault) || exceptionVec(loadAccessFault) || (missAligned && canTriggerException)
423    exception_gpa := exceptionVec(storeGuestPageFault) || exceptionVec(loadGuestPageFault)
424    exception_pa  := (pmp.st || pmp.ld || pmp.mmio) && canTriggerException
425
426    instMicroOp.exception_pa  := exception_pa
427    instMicroOp.exception_va  := exception_va
428    instMicroOp.exception_gpa := exception_gpa
429    // update storeAccessFault bit. Currently, we don't support vector MMIO
430    exceptionVec(loadAccessFault)  := (exceptionVec(loadAccessFault) || pmp.ld || pmp.mmio) && canTriggerException
431    exceptionVec(storeAccessFault) := (exceptionVec(storeAccessFault) || pmp.st || pmp.mmio) && canTriggerException
432
433    when(exception_va || exception_gpa || exception_pa) {
434      when(canTriggerException) {
435        instMicroOp.exceptionVaddr  := vaddr
436        instMicroOp.exceptionVl     := segmentIdx // for exception
437        instMicroOp.exceptionVstart := segmentIdx // for exception
438      }.otherwise {
439        instMicroOp.exceptionVl     := segmentIdx
440      }
441    }
442  }
443
444  /**
445   * flush sbuffer IO Assign
446   */
447  io.flush_sbuffer.valid           := !sbufferEmpty && (state === s_flush_sbuffer_req)
448
449
450  /**
451   * merge data for load
452   */
453  val cacheData = LookupTree(latchVaddr(3,0), List(
454    "b0000".U -> io.rdcache.resp.bits.data_delayed(63,    0),
455    "b0001".U -> io.rdcache.resp.bits.data_delayed(63,    8),
456    "b0010".U -> io.rdcache.resp.bits.data_delayed(63,   16),
457    "b0011".U -> io.rdcache.resp.bits.data_delayed(63,   24),
458    "b0100".U -> io.rdcache.resp.bits.data_delayed(63,   32),
459    "b0101".U -> io.rdcache.resp.bits.data_delayed(63,   40),
460    "b0110".U -> io.rdcache.resp.bits.data_delayed(63,   48),
461    "b0111".U -> io.rdcache.resp.bits.data_delayed(63,   56),
462    "b1000".U -> io.rdcache.resp.bits.data_delayed(127,  64),
463    "b1001".U -> io.rdcache.resp.bits.data_delayed(127,  72),
464    "b1010".U -> io.rdcache.resp.bits.data_delayed(127,  80),
465    "b1011".U -> io.rdcache.resp.bits.data_delayed(127,  88),
466    "b1100".U -> io.rdcache.resp.bits.data_delayed(127,  96),
467    "b1101".U -> io.rdcache.resp.bits.data_delayed(127, 104),
468    "b1110".U -> io.rdcache.resp.bits.data_delayed(127, 112),
469    "b1111".U -> io.rdcache.resp.bits.data_delayed(127, 120)
470  ))
471  val pickData  = rdataVecHelper(alignedType(1,0), cacheData)
472  val mergedData = mergeDataWithElemIdx(
473    oldData = data(splitPtr.value),
474    newData = Seq(pickData),
475    alignedType = alignedType(1,0),
476    elemIdx = Seq(elemIdxInVd),
477    valids = Seq(true.B)
478  )
479  when(state === s_latch_and_merge_data && segmentActive){
480    data(splitPtr.value) := mergedData
481  }
482  /**
483   * split data for store
484   * */
485  val splitData = genVSData(
486    data = data(splitPtr.value),
487    elemIdx = elemIdxInVd,
488    alignedType = alignedType
489  )
490  val flowData  = genVWdata(splitData, alignedType) // TODO: connect vstd, pass vector data
491  val wmask     = genVWmask(latchVaddr, alignedType(1, 0)) & Fill(VLENB, segmentActive)
492
493  /**
494   * rdcache req, write request don't need to query dcache, because we write element to sbuffer
495   */
496  io.rdcache.req                    := DontCare
497  io.rdcache.req.valid              := state === s_cache_req && FuType.isVLoad(fuType)
498  io.rdcache.req.bits.cmd           := MemoryOpConstants.M_XRD
499  io.rdcache.req.bits.vaddr         := latchVaddr
500  io.rdcache.req.bits.mask          := mask
501  io.rdcache.req.bits.data          := flowData
502  io.rdcache.pf_source              := LOAD_SOURCE.U
503  io.rdcache.req.bits.id            := DontCare
504  io.rdcache.resp.ready             := true.B
505  io.rdcache.s1_paddr_dup_lsu       := instMicroOp.paddr
506  io.rdcache.s1_paddr_dup_dcache    := instMicroOp.paddr
507  io.rdcache.s1_kill                := false.B
508  io.rdcache.s1_kill_data_read      := false.B
509  io.rdcache.s2_kill                := false.B
510  if (env.FPGAPlatform){
511    io.rdcache.s0_pc                := DontCare
512    io.rdcache.s1_pc                := DontCare
513    io.rdcache.s2_pc                := DontCare
514  }else{
515    io.rdcache.s0_pc                := instMicroOp.uop.pc
516    io.rdcache.s1_pc                := instMicroOp.uop.pc
517    io.rdcache.s2_pc                := instMicroOp.uop.pc
518  }
519  io.rdcache.replacementUpdated     := false.B
520  io.rdcache.is128Req               := false.B
521
522
523  /**
524   * write data to sbuffer
525   * */
526  sbufferOut.bits                  := DontCare
527  sbufferOut.valid                 := state === s_send_data && segmentActive
528  sbufferOut.bits.vecValid         := state === s_send_data && segmentActive
529  sbufferOut.bits.mask             := wmask
530  sbufferOut.bits.data             := flowData
531  sbufferOut.bits.vaddr            := latchVaddr
532  sbufferOut.bits.cmd              := MemoryOpConstants.M_XWR
533  sbufferOut.bits.id               := DontCare
534  sbufferOut.bits.addr             := instMicroOp.paddr
535
536  NewPipelineConnect(
537    sbufferOut, io.sbuffer, io.sbuffer.fire,
538    false.B,
539    Option(s"VSegmentUnitPipelineConnect")
540  )
541
542  io.vecDifftestInfo.valid         := io.sbuffer.valid
543  io.vecDifftestInfo.bits          := uopq(deqPtr.value).uop
544
545  /**
546   * update ptr
547   * */
548  private val fieldActiveWirteFinish = sbufferOut.fire && segmentActive // writedata finish and is a active segment
549  XSError(sbufferOut.fire && !segmentActive, "Attempt write inactive segment to sbuffer, something wrong!\n")
550
551  private val segmentInactiveFinish = ((state === s_latch_and_merge_data) || (state === s_send_data)) && !segmentActive
552
553  val splitPtrOffset = Mux(
554    isIndexed(instType),
555    Mux(lmul.asSInt < 0.S, 1.U, (1.U << lmul).asUInt),
556    Mux(emul.asSInt < 0.S, 1.U, (1.U << emul).asUInt)
557  )
558  splitPtrNext :=
559    Mux(fieldIdx === maxNfields || !segmentActive, // if segment is active, need to complete this segment, otherwise jump to next segment
560      // segment finish, By shifting 'issueUopFlowNumLog2' to the right to ensure that emul != 1 can correctly generate lateral offset.
561     (deqPtr + ((segmentIdx +& 1.U) >> issueUopFlowNumLog2).asUInt),
562      // next field.
563     (splitPtr + splitPtrOffset)
564    )
565
566  dontTouch(issueUopFlowNumLog2)
567  dontTouch(issueEmul)
568  dontTouch(splitPtrNext)
569  dontTouch(stridePtr)
570  dontTouch(segmentActive)
571
572  // update splitPtr
573  when(state === s_latch_and_merge_data || (state === s_send_data && (fieldActiveWirteFinish || !segmentActive))){
574    splitPtr := splitPtrNext
575  }.elsewhen(io.in.fire && !instMicroOpValid){
576    splitPtr := deqPtr // initial splitPtr
577  }
578
579  // update stridePtr, only use in index
580  val strideOffset = Mux(isIndexed(issueInstType), segmentIdx >> issueMaxIdxInIndexLog2, 0.U)
581  stridePtr       := deqPtr + strideOffset
582
583  // update fieldIdx
584  when(io.in.fire && !instMicroOpValid){ // init
585    fieldIdx := 0.U
586  }.elsewhen(state === s_latch_and_merge_data && segmentActive ||
587            (state === s_send_data && fieldActiveWirteFinish)){ // only if segment is active
588
589    /* next segment, only if segment complete */
590    fieldIdx := Mux(fieldIdx === maxNfields, 0.U, fieldIdx + 1.U)
591  }.elsewhen(segmentInactiveFinish){ // segment is inactive, go to next segment
592    fieldIdx := 0.U
593  }
594  //update segmentIdx
595  when(io.in.fire && !instMicroOpValid){
596    segmentIdx := 0.U
597  }.elsewhen(fieldIdx === maxNfields && (state === s_latch_and_merge_data || (state === s_send_data && fieldActiveWirteFinish)) &&
598             segmentIdx =/= maxSegIdx){ // next segment, only if segment is active
599
600    segmentIdx := segmentIdx + 1.U
601  }.elsewhen(segmentInactiveFinish && segmentIdx =/= maxSegIdx){ // if segment is inactive, go to next segment
602    segmentIdx := segmentIdx + 1.U
603  }
604
605  //update segmentOffset
606  /* when segment is active or segment is inactive, increase segmentOffset */
607  when((fieldIdx === maxNfields && (state === s_latch_and_merge_data || (state === s_send_data && fieldActiveWirteFinish))) ||
608       segmentInactiveFinish){
609
610    segmentOffset := segmentOffset + Mux(isUnitStride(issueInstType), (maxNfields +& 1.U) << issueEew(1, 0), stride(stridePtr.value))
611  }
612
613  //update deqPtr
614  when((state === s_finish) && !isEmpty(enqPtr, deqPtr)){
615    deqPtr := deqPtr + 1.U
616  }
617
618  /*************************************************************************
619   *                            dequeue logic
620   *************************************************************************/
621  val vdIdxInField = GenUopIdxInField(Mux(isIndexed(instType), issueLmul, issueEmul), uopq(deqPtr.value).uop.vpu.vuopIdx)
622  /*select mask of vd, maybe remove in feature*/
623  val realEw        = Mux(isIndexed(issueInstType), issueSew(1, 0), issueEew(1, 0))
624  val maskDataVec: Vec[UInt] = VecDataToMaskDataVec(instMicroOp.mask, realEw)
625  val maskUsed      = maskDataVec(vdIdxInField)
626
627  when(stateNext === s_idle){
628    instMicroOpValid := false.B
629  }
630  // writeback to backend
631  val writebackOut                     = WireInit(io.uopwriteback.bits)
632  val writebackValid                   = (state === s_finish) && !isEmpty(enqPtr, deqPtr)
633  writebackOut.uop                    := uopq(deqPtr.value).uop
634  writebackOut.uop.vpu                := instMicroOp.uop.vpu
635  writebackOut.uop.exceptionVec       := instMicroOp.uop.exceptionVec
636  writebackOut.mask.get               := instMicroOp.mask
637  writebackOut.data                   := data(deqPtr.value)
638  writebackOut.vdIdx.get              := vdIdxInField
639  writebackOut.uop.vpu.vl             := instMicroOp.vl
640  writebackOut.uop.vpu.vstart         := instMicroOp.vstart
641  writebackOut.uop.vpu.vmask          := maskUsed
642  writebackOut.uop.vpu.vuopIdx        := uopq(deqPtr.value).uop.vpu.vuopIdx
643  writebackOut.debug                  := DontCare
644  writebackOut.vdIdxInField.get       := vdIdxInField
645  writebackOut.uop.robIdx             := instMicroOp.uop.robIdx
646  writebackOut.uop.fuOpType           := instMicroOp.uop.fuOpType
647
648  io.uopwriteback.valid               := RegNext(writebackValid)
649  io.uopwriteback.bits                := RegEnable(writebackOut, writebackValid)
650
651  dontTouch(writebackValid)
652
653  //to RS
654  val feedbackOut                      = WireInit(0.U.asTypeOf(io.feedback.bits))
655  val feedbackValid                    = state === s_finish && !isEmpty(enqPtr, deqPtr)
656  feedbackOut.hit                     := true.B
657  feedbackOut.robIdx                  := instMicroOp.uop.robIdx
658  feedbackOut.sourceType              := DontCare
659  feedbackOut.flushState              := DontCare
660  feedbackOut.dataInvalidSqIdx        := DontCare
661  feedbackOut.sqIdx                   := uopq(deqPtr.value).uop.sqIdx
662  feedbackOut.lqIdx                   := uopq(deqPtr.value).uop.lqIdx
663
664  io.feedback.valid                   := RegNext(feedbackValid)
665  io.feedback.bits                    := RegEnable(feedbackOut, feedbackValid)
666
667  dontTouch(feedbackValid)
668
669  // exception
670  io.exceptionInfo                    := DontCare
671  io.exceptionInfo.bits.robidx        := instMicroOp.uop.robIdx
672  io.exceptionInfo.bits.uopidx        := uopq(deqPtr.value).uop.vpu.vuopIdx
673  io.exceptionInfo.bits.vstart        := instMicroOp.exceptionVstart
674  io.exceptionInfo.bits.vaddr         := instMicroOp.exceptionVaddr
675  io.exceptionInfo.bits.gpaddr        := instMicroOp.exceptionGpaddr
676  io.exceptionInfo.bits.isForVSnonLeafPTE := instMicroOp.exceptionIsForVSnonLeafPTE
677  io.exceptionInfo.bits.vl            := instMicroOp.exceptionVl
678  io.exceptionInfo.valid              := (state === s_finish) && instMicroOp.uop.exceptionVec.asUInt.orR && !isEmpty(enqPtr, deqPtr)
679}
680
681