FreeList.scala 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package xiangshan.backend.rename

import chisel3._
import chisel3.util._
import xiangshan._

class FreeList extends XSModule {
  val io = IO(new Bundle() {
    val redirect = Flipped(ValidIO(new Redirect))

    // alloc new phy regs
    val allocReqs = Input(Vec(RenameWidth, Bool()))
    val pdests = Output(Vec(RenameWidth, UInt(PhyRegIdxWidth.W)))
    val allocPtrs = Output(Vec(RenameWidth, UInt(PhyRegIdxWidth.W)))
    val canAlloc = Output(Vec(RenameWidth, Bool()))

    // dealloc phy regs
    val deallocReqs = Input(Vec(CommitWidth, Bool()))
    val deallocPregs = Input(Vec(CommitWidth, UInt(PhyRegIdxWidth.W)))
  })

Y
Yinan Xu 已提交
22
  val freeList = RegInit(VecInit(Seq.tabulate(NRPhyRegs-1)(i => (i+1).U(PhyRegIdxWidth.W))))
23 24
  val headPtr = RegInit(0.U((PhyRegIdxWidth+1).W))
  val tailPtr = RegInit((1 << PhyRegIdxWidth).U((PhyRegIdxWidth+1).W))
25 26 27

  def ptrToIndex(ptr: UInt): UInt = ptr.tail(1)
  def isEmpty(ptr1: UInt, ptr2: UInt): Bool = ptr1 === ptr2
28 29 30 31 32

  // dealloc: commited instructions's 'old_pdest' enqueue
  var tailPtrNext = WireInit(tailPtr)
  for((deallocValid, deallocReg) <- io.deallocReqs.zip(io.deallocPregs)){
    when(deallocValid){
33
      freeList(ptrToIndex(tailPtrNext)) := deallocReg
34 35 36 37 38 39
    }
    tailPtrNext = tailPtrNext + deallocValid
  }
  tailPtr := tailPtrNext

  // allocate new pregs to rename instructions
40
  var empty = WireInit(isEmpty(headPtr, tailPtr))
41 42 43 44 45
  var headPtrNext = WireInit(headPtr)
  for(
    (((allocReq, canAlloc),pdest),allocPtr) <- io.allocReqs.zip(io.canAlloc).zip(io.pdests).zip(io.allocPtrs)
  ){
    canAlloc := !empty
46
    pdest := freeList(ptrToIndex(headPtrNext))
47 48
    allocPtr := headPtrNext
    headPtrNext = headPtrNext + (allocReq && canAlloc)
49
    empty = isEmpty(headPtrNext, tailPtr)
50 51 52 53 54 55 56 57
  }

  headPtr := Mux(io.redirect.valid,
    io.redirect.bits.freelistAllocPtr, // mispredict or exception happen
    headPtrNext
  )

}