Rename.scala 7.5 KB
Newer Older
1 2 3 4 5
package xiangshan.backend.rename

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

8
class Rename extends XSModule {
9 10 11
  val io = IO(new Bundle() {
    val redirect = Flipped(ValidIO(new Redirect))
    val roqCommits = Vec(CommitWidth, Flipped(ValidIO(new RoqCommit)))
12 13
    val wbIntResults = Vec(NRWritePorts, Flipped(ValidIO(new ExuOutput)))
    val wbFpResults = Vec(NRWritePorts, Flipped(ValidIO(new ExuOutput)))
14 15
    val intRfReadAddr = Vec(NRReadPorts, Input(UInt(PhyRegIdxWidth.W)))
    val fpRfReadAddr = Vec(NRReadPorts, Input(UInt(PhyRegIdxWidth.W)))
16 17 18
    val intPregRdy = Vec(NRReadPorts, Output(Bool()))
    val fpPregRdy = Vec(NRReadPorts, Output(Bool()))
    // from decode buffer
19
    val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl)))
20
    // to dispatch1
21
    val out = Vec(RenameWidth, DecoupledIO(new MicroOp))
22
  })
23

L
LinJiawei 已提交
24 25
  val isWalk = ParallelOR(io.roqCommits.map(x => x.valid && x.bits.isWalk)).asBool()

L
LinJiawei 已提交
26
  val debug_exception = io.redirect.valid && io.redirect.bits.isException
L
LinJiawei 已提交
27
  val debug_walk = isWalk
L
LinJiawei 已提交
28 29 30 31
  val debug_norm = !(debug_exception || debug_walk)

  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
    XSInfo(
Y
Yinan Xu 已提交
32
      debug_norm && in.valid && in.ready,
L
LinJiawei 已提交
33
      p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " +
L
LinJiawei 已提交
34 35 36 37
        p"lsrc1:${in.bits.ctrl.lsrc1} -> psrc1:${out.bits.psrc1} " +
        p"lsrc2:${in.bits.ctrl.lsrc2} -> psrc2:${out.bits.psrc2} " +
        p"lsrc3:${in.bits.ctrl.lsrc3} -> psrc3:${out.bits.psrc3} " +
        p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " +
L
LinJiawei 已提交
38
        p"old_pdest:${out.bits.old_pdest} " +
L
LinJiawei 已提交
39
        p"out v:${out.valid} r:${out.ready}\n"
L
LinJiawei 已提交
40 41 42 43 44 45 46
    )
  }

  for((x,y) <- io.in.zip(io.out)){
    printRenameInfo(x, y)
  }

47 48 49 50 51
  val fpFreeList, intFreeList = Module(new FreeList).io
  val fpRat = Module(new RenameTable(float = true)).io
  val intRat = Module(new RenameTable(float = false)).io
  val fpBusyTable, intBusyTable = Module(new BusyTable).io

L
LinJiawei 已提交
52
  fpFreeList.redirect := io.redirect
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  intFreeList.redirect := io.redirect

  val flush = io.redirect.valid && io.redirect.bits.isException
  fpRat.flush := flush
  intRat.flush := flush
  fpBusyTable.flush := flush
  intBusyTable.flush := flush

  def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = {
    {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)}
  }

  val uops = Wire(Vec(RenameWidth, new MicroOp))

  uops.foreach( uop => {
Y
Yinan Xu 已提交
68 69
//    uop.brMask := DontCare
//    uop.brTag := DontCare
70 71 72 73 74 75
    uop.src1State := DontCare
    uop.src2State := DontCare
    uop.src3State := DontCare
    uop.roqIdx := DontCare
  })

L
LinJiawei 已提交
76
  var lastReady = WireInit(true.B)
Y
Yinan Xu 已提交
77
  for(i <- 0 until RenameWidth) {
78 79
    uops(i).cf := io.in(i).bits.cf
    uops(i).ctrl := io.in(i).bits.ctrl
Y
Yinan Xu 已提交
80
    uops(i).brTag := io.in(i).bits.brTag
81

L
LinJiawei 已提交
82 83
    val inValid = io.in(i).valid && !isWalk

84
    // alloc a new phy reg
L
LinJiawei 已提交
85 86
    val needFpDest = inValid && needDestReg(fp = true, io.in(i).bits)
    val needIntDest = inValid && needDestReg(fp = false, io.in(i).bits)
L
LinJiawei 已提交
87 88
    fpFreeList.allocReqs(i) := needFpDest && lastReady && io.out(i).ready
    intFreeList.allocReqs(i) := needIntDest && lastReady && io.out(i).ready
89 90
    val fpCanAlloc = fpFreeList.canAlloc(i)
    val intCanAlloc = intFreeList.canAlloc(i)
L
LinJiawei 已提交
91 92 93 94 95 96 97 98 99
    val this_can_alloc = Mux(
      needIntDest,
      intCanAlloc,
      Mux(
        needFpDest,
        fpCanAlloc,
        true.B
      )
    )
L
LinJiawei 已提交
100 101
    io.in(i).ready := lastReady && io.out(i).ready && this_can_alloc && !isWalk

L
LinJiawei 已提交
102 103 104 105 106 107
    // do checkpoints when a branch inst come
    for(fl <- Seq(fpFreeList, intFreeList)){
      fl.cpReqs(i).valid := inValid
      fl.cpReqs(i).bits := io.in(i).bits.brTag
    }

L
LinJiawei 已提交
108 109
    lastReady = io.in(i).ready

L
LinJiawei 已提交
110 111 112 113 114 115 116
    uops(i).pdest := Mux(needIntDest,
      intFreeList.pdests(i),
      Mux(
        uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen,
        0.U, fpFreeList.pdests(i)
      )
    )
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    io.out(i).valid := io.in(i).fire()
    io.out(i).bits := uops(i)

    // write rename table
    def writeRat(fp: Boolean) = {
      val rat = if(fp) fpRat else intRat
      val freeList = if(fp) fpFreeList else intFreeList
      val busyTable = if(fp) fpBusyTable else intBusyTable
      // speculative inst write
      val specWen = freeList.allocReqs(i) && freeList.canAlloc(i)
      // walk back write
      val commitDestValid = io.roqCommits(i).valid && needDestReg(fp, io.roqCommits(i).bits.uop)
      val walkWen = commitDestValid && io.roqCommits(i).bits.isWalk

      rat.specWritePorts(i).wen := specWen || walkWen
      rat.specWritePorts(i).addr := Mux(specWen, uops(i).ctrl.ldest, io.roqCommits(i).bits.uop.ctrl.ldest)
      rat.specWritePorts(i).wdata := Mux(specWen, freeList.pdests(i), io.roqCommits(i).bits.uop.old_pdest)

L
linjiawei 已提交
136 137 138
      busyTable.wbPregs(NRWritePorts + i).valid := walkWen
      busyTable.wbPregs(NRWritePorts + i).bits := io.roqCommits(i).bits.uop.pdest

L
LinJiawei 已提交
139
      XSInfo(walkWen,
L
linjiawei 已提交
140
        {if(fp) p"fp" else p"int "} + p"walk: pc:${Hexadecimal(io.roqCommits(i).bits.uop.cf.pc)}" +
L
LinJiawei 已提交
141 142 143
          p" ldst:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n"
      )

144 145 146 147
      rat.archWritePorts(i).wen := commitDestValid && !io.roqCommits(i).bits.isWalk
      rat.archWritePorts(i).addr := io.roqCommits(i).bits.uop.ctrl.ldest
      rat.archWritePorts(i).wdata := io.roqCommits(i).bits.uop.pdest

L
LinJiawei 已提交
148
      XSInfo(rat.archWritePorts(i).wen,
L
LinJiawei 已提交
149
        {if(fp) p"fp" else p"int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" +
L
LinJiawei 已提交
150 151 152
          p" pdest:${rat.archWritePorts(i).wdata}\n"
      )

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
      freeList.deallocReqs(i) := rat.archWritePorts(i).wen
      freeList.deallocPregs(i) := io.roqCommits(i).bits.uop.old_pdest

      // set phy reg status to busy
      busyTable.allocPregs(i).valid := specWen
      busyTable.allocPregs(i).bits := freeList.pdests(i)
    }

    writeRat(fp = false)
    writeRat(fp = true)

    // read rename table
    def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = {
      val rat = if(fp) fpRat else intRat
      val srcCnt = lsrcList.size
      val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W)))
      val old_pdest = Wire(UInt(PhyRegIdxWidth.W))
      for(k <- 0 until srcCnt+1){
        val rportIdx = i * (srcCnt+1) + k
        if(k != srcCnt){
          rat.readPorts(rportIdx).addr := lsrcList(k)
          psrcVec(k) := rat.readPorts(rportIdx).rdata
        } else {
          rat.readPorts(rportIdx).addr := ldest
          old_pdest := rat.readPorts(rportIdx).rdata
        }
      }
      (psrcVec, old_pdest)
    }
    val lsrcList = List(uops(i).ctrl.lsrc1, uops(i).ctrl.lsrc2, uops(i).ctrl.lsrc3)
    val ldest = uops(i).ctrl.ldest
    val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false)
    val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true)
    uops(i).psrc1 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0))
L
LinJiawei 已提交
187
    uops(i).psrc2 := Mux(uops(i).ctrl.src2Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
188 189 190 191 192 193 194 195
    uops(i).psrc3 := fpPhySrcVec(2)
    uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest)
  }


  def updateBusyTable(fp: Boolean) = {
    val wbResults = if(fp) io.wbFpResults else io.wbIntResults
    val busyTable = if(fp) fpBusyTable else intBusyTable
L
linjiawei 已提交
196
    for((wb, setPhyRegRdy) <- wbResults.zip(busyTable.wbPregs.take(NRWritePorts))){
197 198 199 200 201 202 203 204 205 206 207 208
      setPhyRegRdy.valid := wb.valid && needDestReg(fp, wb.bits.uop)
      setPhyRegRdy.bits := wb.bits.uop.pdest
    }
  }

  updateBusyTable(false)
  updateBusyTable(true)

  intBusyTable.rfReadAddr <> io.intRfReadAddr
  intBusyTable.pregRdy <> io.intPregRdy
  fpBusyTable.rfReadAddr <> io.fpRfReadAddr
  fpBusyTable.pregRdy <> io.fpPregRdy
209
}