xref: /XiangShan/src/main/scala/xiangshan/backend/regfile/Regfile.scala (revision 44dead2f439412d45b4e0e5d4477308a0097c505)
1package xiangshan.backend.regfile
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6
7class RfReadPort extends XSBundle {
8  val addr = Input(UInt(PhyRegIdxWidth.W))
9  val data = Output(UInt(XLEN.W))
10}
11
12class RfWritePort extends XSBundle {
13  val wen = Input(Bool())
14  val addr = Input(UInt(PhyRegIdxWidth.W))
15  val data = Input(UInt(XLEN.W))
16}
17
18class Regfile
19(
20  numReadPorts: Int,
21  numWirtePorts: Int,
22  hasZero: Boolean
23) extends XSModule {
24  val io = IO(new Bundle() {
25    val readPorts = Vec(numReadPorts, new RfReadPort)
26    val writePorts = Vec(numWirtePorts, new RfWritePort)
27  })
28
29  val mem = Mem(NRPhyRegs, UInt(XLEN.W))
30
31  for(r <- io.readPorts){
32    val addr_reg = RegNext(r.addr)
33    r.data := {if(hasZero) Mux(addr_reg===0.U, 0.U, mem(addr_reg)) else mem(addr_reg)}
34  }
35
36  for(w <- io.writePorts){
37    when(w.wen){
38      mem(w.addr) := w.data
39    }
40  }
41
42  if (!env.FPGAPlatform) {
43    val debugArchRat = WireInit(VecInit(Seq.fill(32)(0.U(PhyRegIdxWidth.W))))
44    ExcitingUtils.addSink(
45      debugArchRat,
46      if(hasZero) "DEBUG_INI_ARCH_RAT" else "DEBUG_FP_ARCH_RAT",
47      ExcitingUtils.Debug
48    )
49
50    val debugArchReg = WireInit(VecInit(debugArchRat.zipWithIndex.map(
51      x => if(hasZero && x._2==0) 0.U else mem(x._1)
52    )))
53    ExcitingUtils.addSource(
54      debugArchReg,
55      if(hasZero) "DEBUG_INT_ARCH_REG" else "DEBUG_FP_ARCH_REG",
56      ExcitingUtils.Debug
57    )
58  }
59
60}
61