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

import chisel3._
import chisel3.util._
import xiangshan._
L
LinJiawei 已提交
6
import xiangshan.utils.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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  val debug_exception = io.redirect.valid && io.redirect.bits.isException
  val debug_walk = io.roqCommits.map(_.bits.isWalk).reduce(_ || _)
  val debug_norm = !(debug_exception || debug_walk)

  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
    XSInfo(
      debug_norm,
      p"pc:${Hexadecimal(in.bits.cf.pc)} v:${in.valid} rdy:${in.ready} " +
        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} " +
        p"old_pdest:${out.bits.old_pdest}\n"
    )
  }

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

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  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

  fpFreeList.redirect := io.redirect
  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 已提交
65 66
//    uop.brMask := DontCare
//    uop.brTag := DontCare
67 68 69 70 71 72 73
    uop.src1State := DontCare
    uop.src2State := DontCare
    uop.src3State := DontCare
    uop.roqIdx := DontCare
  })

  var last_can_alloc = WireInit(true.B)
Y
Yinan Xu 已提交
74
  for(i <- 0 until RenameWidth) {
75 76
    uops(i).cf := io.in(i).bits.cf
    uops(i).ctrl := io.in(i).bits.ctrl
Y
Yinan Xu 已提交
77 78
    uops(i).brMask := io.in(i).bits.brMask
    uops(i).brTag := io.in(i).bits.brTag
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    // alloc a new phy reg
    val needFpDest = io.in(i).valid && needDestReg(fp = true, io.in(i).bits)
    val needIntDest = io.in(i).valid && needDestReg(fp = false, io.in(i).bits)
    fpFreeList.allocReqs(i) := needFpDest && last_can_alloc && io.out(i).ready
    intFreeList.allocReqs(i) := needIntDest && last_can_alloc && io.out(i).ready
    val fpCanAlloc = fpFreeList.canAlloc(i)
    val intCanAlloc = intFreeList.canAlloc(i)
    val this_can_alloc = Mux(needIntDest, intCanAlloc, fpCanAlloc)
    io.in(i).ready := this_can_alloc
    last_can_alloc = last_can_alloc && this_can_alloc
    uops(i).pdest := Mux(needIntDest, intFreeList.pdests(i), fpFreeList.pdests(i))
    uops(i).freelistAllocPtr := Mux(needIntDest, intFreeList.allocPtrs(i), fpFreeList.allocPtrs(i))

    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 已提交
111 112 113 114 115
      XSInfo(walkWen,
        {if(fp) "fp" else "int "} + p"walk: pc:${Hexadecimal(uops(i).cf.pc)}" +
          p" ldst:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n"
      )

116 117 118 119
      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 已提交
120 121 122 123 124
      XSInfo(rat.archWritePorts(i).wen,
        {if(fp) "fp" else "int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" +
          p" pdest:${rat.archWritePorts(i).wdata}\n"
      )

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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
      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))
    uops(i).psrc2 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
    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
    for((wb, setPhyRegRdy) <- wbResults.zip(busyTable.wbPregs)){
      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
181
}