Brq.scala 6.1 KB
Newer Older
1 2 3 4 5
package xiangshan.backend.brq

import chisel3._
import chisel3.util._
import xiangshan._
L
LinJiawei 已提交
6
import xiangshan.utils.{PriorityEncoderDefault, XSDebug, XSInfo}
7 8


L
LinJiawei 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
class BrqPtr extends XSBundle {

  val flag = Bool()
  val value = UInt(BrTagWidth.W)

  final def + (inc: Bool): BrqPtr = {
    Mux(inc && (value === (BrqSize-1).U),
      BrqPtr(!flag, 0.U),
      BrqPtr(flag, value + inc)
    )
  }

  final def === (that: BrqPtr): Bool = {
    (this.value===that.value) && (this.flag===that.flag)
  }

25 26
  // this.age < that.age
  final def < (that: BrqPtr): Bool = {
L
LinJiawei 已提交
27 28 29 30 31 32
    Mux(this.flag === that.flag,
      this.value > that.value,
      this.value < that.value
    )
  }

33
  def needBrFlush(redirectTag: BrqPtr): Bool = this < redirectTag
L
LinJiawei 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  def needFlush(redirect: Valid[Redirect]): Bool = {
    redirect.valid && (redirect.bits.isException || needBrFlush(redirect.bits.brTag))
  }

  override def toPrintable: Printable = p"f:$flag v:$value"

}

object BrqPtr {
  def apply(f: Bool, v: UInt): BrqPtr = {
    val ptr = Wire(new BrqPtr)
    ptr.flag := f
    ptr.value := v
    ptr
  }
}

L
LinJiawei 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
class BrqIO extends XSBundle{
  // interrupt/exception happen, flush Brq
  val roqRedirect = Input(Valid(new Redirect))
  // receive branch/jump calculated target
  val exuRedirect = Vec(exuConfig.AluCnt + exuConfig.BruCnt, Flipped(ValidIO(new ExuOutput)))
  // from decode, branch insts enq
  val enqReqs = Vec(DecodeWidth, Flipped(DecoupledIO(new CfCtrl)))
  // to decode
  val brTags = Output(Vec(DecodeWidth, new BrqPtr))
  // to roq
  val out = ValidIO(new ExuOutput)
  // misprediction, flush pipeline
  val redirect = Output(Valid(new Redirect))
}
66

L
LinJiawei 已提交
67
class Brq extends XSModule {
L
LinJiawei 已提交
68 69 70 71
  val io = IO(new BrqIO)

  def redirctWindowSize: Int = BrqSize/2
  require(redirctWindowSize <= BrqSize && redirctWindowSize > 0)
L
LinJiawei 已提交
72

73
  class BrqEntry extends Bundle {
L
LinJiawei 已提交
74
    val ptrFlag = Bool()
75
    val npc = UInt(VAddrBits.W)
L
LinJiawei 已提交
76
    val misPred = Bool()
L
LinJiawei 已提交
77
    val exuOut = new ExuOutput
78 79
  }

L
LinJiawei 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  val s_idle :: s_wb :: s_commited :: Nil = Enum(3)
  class StateQueueEntry extends Bundle{
    val state = UInt(s_idle.getWidth.W)

    def hasWrittenBack: Bool = state=/=s_idle
    def isCommited: Bool = state===s_commited
    def isWrittenBack: Bool = state===s_wb

    def update(s: UInt): Unit ={
      state := s
    }

    override def toPrintable: Printable = p"$state"
  }

95
  val brQueue = Reg(Vec(BrqSize, new BrqEntry))
L
LinJiawei 已提交
96 97 98
  val stateQueue = RegInit(VecInit(Seq.fill(BrqSize)(
    s_idle.asTypeOf(new StateQueueEntry)
  )))
L
LinJiawei 已提交
99

L
LinJiawei 已提交
100
  val headPtr, tailPtr = RegInit(BrqPtr(false.B, 0.U))
L
LinJiawei 已提交
101

L
LinJiawei 已提交
102 103
  def isEmpty(ptr1: BrqPtr, ptr2: BrqPtr): Bool = ptr1 === ptr2
  def isFull(ptr1: BrqPtr, ptr2: BrqPtr): Bool = (ptr1.flag=/=ptr2.flag) && (ptr1.value===ptr2.value)
L
LinJiawei 已提交
104 105 106


  // dequeue
L
LinJiawei 已提交
107
  val headIdx = headPtr.value
L
LinJiawei 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  var commitIdx = WireInit(headIdx)
  var prevWb = WireInit(
    (stateQueue(headIdx).isWrittenBack && !brQueue(headIdx).misPred) ||
    stateQueue(headIdx).isCommited
  )

  for(i <- 1 until redirctWindowSize){
    val idx = commitIdx + i.U
    val commitThis = prevWb && stateQueue(idx).isWrittenBack && brQueue(idx).misPred
    commitIdx = Mux(commitThis,
      idx,
      commitIdx
    )
    prevWb = prevWb && (
      (stateQueue(idx).isWrittenBack && !brQueue(idx).misPred) ||
      stateQueue(idx).isCommited
      )
  }

  val commitIsHead = commitIdx===headIdx
  val deqValid = stateQueue(headIdx).hasWrittenBack && commitIsHead
  val commitValid = stateQueue(commitIdx).isWrittenBack
  val commitEntry = brQueue(commitIdx)


  XSDebug(p"headIdx:$headIdx commitIdx:$commitIdx\n")
  XSDebug(p"headPtr:$headPtr tailPtr:$tailPtr\n")
  XSDebug(stateQueue.map(_.toPrintable).reduce((a,b) => a+b)+"\n")
L
LinJiawei 已提交
136 137

  val headPtrNext = WireInit(headPtr + deqValid)
L
LinJiawei 已提交
138 139 140 141 142 143 144 145 146 147
  stateQueue(commitIdx).update(
    Mux(deqValid,
      s_idle,
      Mux(commitValid,
        s_commited,
        stateQueue(commitIdx).state
      )
    )
  )

L
LinJiawei 已提交
148
  headPtr := headPtrNext
L
LinJiawei 已提交
149 150 151 152 153 154 155
  io.redirect.valid := commitValid && commitEntry.misPred
  io.redirect.bits := commitEntry.exuOut.redirect
  io.out.valid := commitValid
  io.out.bits := commitEntry.exuOut
  XSInfo(io.out.valid,
    p"commit branch to roq, mispred:${io.redirect.valid} pc=${Hexadecimal(io.out.bits.uop.cf.pc)}\n"
  )
L
LinJiawei 已提交
156 157 158 159

  // branch insts enq
  var full = WireInit(isFull(headPtrNext, tailPtr))
  var tailPtrNext = WireInit(tailPtr)
L
LinJiawei 已提交
160
  for((enq, brTag) <- io.enqReqs.zip(io.brTags)){
L
LinJiawei 已提交
161
    enq.ready := !full
L
LinJiawei 已提交
162
    brTag := tailPtrNext
163
    // TODO: check rvc and use predict npc
L
LinJiawei 已提交
164 165 166 167
    when(enq.fire()){
      brQueue(tailPtrNext.value).npc := enq.bits.cf.pc + 4.U
      brQueue(tailPtrNext.value).ptrFlag := tailPtrNext.flag
    }
L
LinJiawei 已提交
168 169 170 171 172 173 174
    tailPtrNext = tailPtrNext + enq.fire()
    full = isFull(tailPtrNext, headPtrNext)
  }
  tailPtr := tailPtrNext

  // exu write back
  for(exuWb <- io.exuRedirect){
L
LinJiawei 已提交
175
    when(exuWb.valid){
L
LinJiawei 已提交
176 177 178 179 180 181 182 183
      val wbIdx = exuWb.bits.redirect.brTag.value
      XSInfo(
        p"exu write back: brTag:${exuWb.bits.redirect.brTag}" +
          p" pc=${Hexadecimal(exuWb.bits.uop.cf.pc)}\n"
      )
      stateQueue(wbIdx).update(s_wb)
      brQueue(wbIdx).exuOut := exuWb.bits
      brQueue(wbIdx).misPred := brQueue(wbIdx).npc =/= exuWb.bits.redirect.target
L
LinJiawei 已提交
184 185 186
    }
  }

L
LinJiawei 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200
  when(io.roqRedirect.valid){
    // exception
    stateQueue.foreach(_.update(s_idle))
    headPtr := BrqPtr(false.B, 0.U)
    tailPtr := BrqPtr(false.B, 0.U)
  }.elsewhen(io.redirect.valid){
    // misprediction
    stateQueue.zipWithIndex.foreach({case(s, i) =>
      val ptr = BrqPtr(brQueue(i).ptrFlag, i.U)
      when(ptr.needBrFlush(io.redirect.bits.brTag)){
        s.update(s_idle)
      }
    })
    tailPtr := io.redirect.bits.brTag + true.B
L
LinJiawei 已提交
201
  }
L
LinJiawei 已提交
202 203 204 205 206 207 208 209 210 211




  // Debug info
  val debug_roq_redirect = io.roqRedirect.valid
  val debug_brq_redirect = io.redirect.valid && !debug_roq_redirect
  val debug_normal_mode = !(debug_roq_redirect || debug_brq_redirect)

  for(i <- 0 until DecodeWidth){
L
LinJiawei 已提交
212
    XSDebug(
L
LinJiawei 已提交
213 214
      debug_normal_mode,
      p"enq v:${io.enqReqs(i).valid} rdy:${io.enqReqs(i).ready} pc:${Hexadecimal(io.enqReqs(i).bits.cf.pc)}" +
L
LinJiawei 已提交
215
        p" brTag:${io.brTags(i)}\n"
L
LinJiawei 已提交
216 217 218 219
    )
  }

  XSInfo(debug_roq_redirect, "roq redirect, flush brq\n")
L
LinJiawei 已提交
220
  XSInfo(debug_brq_redirect, p"brq redirect, target:${Hexadecimal(io.redirect.bits.target)} flptr:${io.redirect.bits.freelistAllocPtr}\n")
221
}