xref: /XiangShan/src/main/scala/xiangshan/backend/rob/ExceptionGen.scala (revision bb2f3f51dd67f6e16e0cc1ffe43368c9fc7e4aef)
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.backend.rob
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import difftest._
23import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
24import utility._
25import utils._
26import xiangshan._
27import xiangshan.backend.BackendParams
28import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput}
29import xiangshan.backend.fu.{FuConfig, FuType}
30import xiangshan.frontend.FtqPtr
31import xiangshan.mem.{LqPtr, LsqEnqIO, SqPtr}
32import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput}
33import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfo, LsTopdownInfo}
34import xiangshan.backend.fu.vector.Bundles.VType
35import xiangshan.backend.rename.SnapshotGenerator
36
37class ExceptionGen(params: BackendParams)(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
38  val io = IO(new Bundle {
39    val redirect = Input(Valid(new Redirect))
40    val flush = Input(Bool())
41    val enq = Vec(RenameWidth, Flipped(ValidIO(new RobExceptionInfo)))
42    // csr + load + store + varith + vload + vstore
43    val wb = Vec(params.numException, Flipped(ValidIO(new RobExceptionInfo)))
44    val out = ValidIO(new RobExceptionInfo)
45    val state = ValidIO(new RobExceptionInfo)
46  })
47
48  val wbExuParams = params.allExuParams.filter(_.exceptionOut.nonEmpty)
49
50  def getOldest(valid: Seq[Bool], bits: Seq[RobExceptionInfo]): RobExceptionInfo = {
51    def getOldest_recursion(valid: Seq[Bool], bits: Seq[RobExceptionInfo]): (Seq[Bool], Seq[RobExceptionInfo]) = {
52      assert(valid.length == bits.length)
53      if (valid.length == 1) {
54        (valid, bits)
55      } else if (valid.length == 2) {
56        val res = Seq.fill(2)(Wire(ValidIO(chiselTypeOf(bits(0)))))
57        for (i <- res.indices) {
58          res(i).valid := valid(i)
59          res(i).bits := bits(i)
60        }
61        val oldest = Mux(!valid(1) || valid(0) && isAfter(bits(1).robIdx, bits(0).robIdx), res(0), res(1))
62        (Seq(oldest.valid), Seq(oldest.bits))
63      } else {
64        val left = getOldest_recursion(valid.take(valid.length / 2), bits.take(valid.length / 2))
65        val right = getOldest_recursion(valid.drop(valid.length / 2), bits.drop(valid.length / 2))
66        getOldest_recursion(left._1 ++ right._1, left._2 ++ right._2)
67      }
68    }
69    getOldest_recursion(valid, bits)._2.head
70  }
71
72
73  val currentValid = RegInit(false.B)
74  val current = Reg(new RobExceptionInfo)
75
76  // orR the exceptionVec
77  val lastCycleFlush = RegNext(io.flush)
78  val in_enq_valid = VecInit(io.enq.map(e => e.valid && e.bits.has_exception && !lastCycleFlush))
79
80  // s0: compare wb in 6 groups
81  val csr_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(t => t.isCsr).nonEmpty).map(_._1)
82  val load_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(_.fuType == FuType.ldu).nonEmpty).map(_._1)
83  val store_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(t => t.isSta || t.fuType == FuType.mou).nonEmpty).map(_._1)
84  val varith_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(_.isVecArith).nonEmpty).map(_._1)
85  val vls_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.exists(x => FuType.FuTypeOrR(x.fuType, FuType.vecMem))).map(_._1)
86
87  val writebacks = Seq(csr_wb, load_wb, store_wb, varith_wb, vls_wb)
88  val in_wb_valids = writebacks.map(_.map(w => w.valid && w.bits.has_exception && !lastCycleFlush))
89  val wb_valid = in_wb_valids.zip(writebacks).map { case (valid, wb) =>
90    valid.zip(wb.map(_.bits)).map { case (v, bits) => v && !(bits.robIdx.needFlush(io.redirect) || io.flush) }.reduce(_ || _)
91  }
92  val wb_bits = in_wb_valids.zip(writebacks).map { case (valid, wb) => getOldest(valid, wb.map(_.bits))}
93
94  val s0_out_valid = wb_valid.map(x => RegNext(x))
95  val s0_out_bits = wb_bits.zip(wb_valid).map{ case(b, v) => RegEnable(b, v)}
96
97  // s1: compare last six and current flush
98  val s1_valid = VecInit(s0_out_valid.zip(s0_out_bits).map{ case (v, b) => v && !(b.robIdx.needFlush(io.redirect) || io.flush) })
99  val s1_out_bits = RegEnable(getOldest(s0_out_valid, s0_out_bits), s1_valid.asUInt.orR)
100  val s1_out_valid = RegNext(s1_valid.asUInt.orR)
101
102  val enq_valid = RegNext(in_enq_valid.asUInt.orR && !io.redirect.valid && !io.flush)
103  val enq_bits = RegEnable(ParallelPriorityMux(in_enq_valid, io.enq.map(_.bits)), in_enq_valid.asUInt.orR && !io.redirect.valid && !io.flush)
104
105  // s2: compare the input exception with the current one
106  // priorities:
107  // (1) system reset
108  // (2) current is valid: flush, remain, merge, update
109  // (3) current is not valid: s1 or enq
110  val current_flush = current.robIdx.needFlush(io.redirect) || io.flush
111  val s1_flush = s1_out_bits.robIdx.needFlush(io.redirect) || io.flush
112  when (currentValid) {
113    when (current_flush) {
114      currentValid := Mux(s1_flush, false.B, s1_out_valid)
115    }
116    when (s1_out_valid && !s1_flush) {
117      when (isAfter(current.robIdx, s1_out_bits.robIdx)) {
118        current := s1_out_bits
119      }.elsewhen (current.robIdx === s1_out_bits.robIdx) {
120        current.exceptionVec := (s1_out_bits.exceptionVec.asUInt | current.exceptionVec.asUInt).asTypeOf(ExceptionVec())
121        current.flushPipe := s1_out_bits.flushPipe || current.flushPipe
122        current.replayInst := s1_out_bits.replayInst || current.replayInst
123        current.singleStep := s1_out_bits.singleStep || current.singleStep
124        current.trigger := (s1_out_bits.trigger.asUInt | current.trigger.asUInt).asTypeOf(new TriggerCf)
125      }
126    }
127  }.elsewhen (s1_out_valid && !s1_flush) {
128    currentValid := true.B
129    current := s1_out_bits
130  }.elsewhen (enq_valid && !(io.redirect.valid || io.flush)) {
131    currentValid := true.B
132    current := enq_bits
133  }
134
135  io.out.valid   := s1_out_valid || enq_valid && enq_bits.can_writeback
136  io.out.bits    := Mux(s1_out_valid, s1_out_bits, enq_bits)
137  io.state.valid := currentValid
138  io.state.bits  := current
139
140}