xref: /XiangShan/src/main/scala/xiangshan/backend/regfile/Regfile.scala (revision 39c59369af6e7d78fa72e13aae3735f1a6e98f5c)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.backend.regfile
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import xiangshan._
23import xiangshan.backend.datapath.DataConfig.{DataConfig, FpData, FpRegSrcDataSet, IntData, IntRegSrcDataSet, VecData, VecRegSrcDataSet, VfRegSrcDataSet}
24import xiangshan.backend.exu.ExeUnitParams
25
26class RfReadPort(dataWidth: Int, addrWidth: Int) extends Bundle {
27  val addr = Input(UInt(addrWidth.W))
28  val data = Output(UInt(dataWidth.W))
29}
30
31class RfWritePort(dataWidth: Int, addrWidth: Int) extends Bundle {
32  val wen = Input(Bool())
33  val addr = Input(UInt(addrWidth.W))
34  val data = Input(UInt(dataWidth.W))
35}
36
37class RfReadPortWithConfig(val rfReadDataCfg: DataConfig, addrWidth: Int) extends Bundle {
38  val addr: UInt = Input(UInt(addrWidth.W))
39  val data: UInt = Output(UInt(rfReadDataCfg.dataWidth.W))
40  val srcType: UInt = Input(UInt(3.W))
41
42  def readInt: Boolean = IntRegSrcDataSet.contains(rfReadDataCfg)
43  def readFp : Boolean = FpRegSrcDataSet .contains(rfReadDataCfg)
44  def readVec: Boolean = VecRegSrcDataSet.contains(rfReadDataCfg)
45  def readVf : Boolean = VfRegSrcDataSet .contains(rfReadDataCfg)
46}
47
48class RfWritePortWithConfig(val rfWriteDataCfg: DataConfig, addrWidth: Int) extends Bundle {
49  val wen = Input(Bool())
50  val addr = Input(UInt(addrWidth.W))
51  val data = Input(UInt(rfWriteDataCfg.dataWidth.W))
52  val intWen = Input(Bool())
53  val fpWen = Input(Bool())
54  val vecWen = Input(Bool())
55  def writeInt: Boolean = rfWriteDataCfg.isInstanceOf[IntData]
56  def writeFp : Boolean = rfWriteDataCfg.isInstanceOf[FpData]
57  def writeVec: Boolean = rfWriteDataCfg.isInstanceOf[VecData]
58}
59
60class Regfile
61(
62  name: String,
63  numPregs: Int,
64  numReadPorts: Int,
65  numWritePorts: Int,
66  hasZero: Boolean,
67  len: Int,
68  width: Int,
69) extends Module {
70  val io = IO(new Bundle() {
71    val readPorts = Vec(numReadPorts, new RfReadPort(len, width))
72    val writePorts = Vec(numWritePorts, new RfWritePort(len, width))
73    val debug_rports = Vec(65, new RfReadPort(len, width))
74  })
75
76  println(name + ": size:" + numPregs + " read: " + numReadPorts + " write: " + numWritePorts)
77
78  val mem = Reg(Vec(numPregs, UInt(len.W)))
79  for (r <- io.readPorts) {
80    val rdata = if (hasZero) Mux(r.addr === 0.U, 0.U, mem(r.addr)) else mem(r.addr)
81    r.data := RegNext(rdata)
82  }
83  for (w <- io.writePorts) {
84    when(w.wen) {
85      mem(w.addr) := w.data
86    }
87  }
88
89  for (rport <- io.debug_rports) {
90    val zero_rdata = Mux(rport.addr === 0.U, 0.U, mem(rport.addr))
91    rport.data := (if (hasZero) zero_rdata else mem(rport.addr))
92  }
93}
94
95object Regfile {
96  // non-return version
97  def apply(
98    name         : String,
99    numEntries   : Int,
100    raddr        : Seq[UInt],
101    rdata        : Vec[UInt],
102    wen          : Seq[Bool],
103    waddr        : Seq[UInt],
104    wdata        : Seq[UInt],
105    hasZero      : Boolean,
106    withReset    : Boolean = false,
107    debugReadAddr: Option[Seq[UInt]],
108    debugReadData: Option[Vec[UInt]],
109  )(implicit p: Parameters): Unit = {
110    val numReadPorts = raddr.length
111    val numWritePorts = wen.length
112    require(wen.length == waddr.length)
113    require(wen.length == wdata.length)
114    val dataBits = wdata.map(_.getWidth).min
115    require(wdata.map(_.getWidth).min == wdata.map(_.getWidth).max, s"dataBits != $dataBits")
116    val addrBits = waddr.map(_.getWidth).min
117    require(waddr.map(_.getWidth).min == waddr.map(_.getWidth).max, s"addrBits != $addrBits")
118
119    val regfile = Module(new Regfile(name, numEntries, numReadPorts, numWritePorts, hasZero, dataBits, addrBits))
120    rdata := regfile.io.readPorts.zip(raddr).map { case (rport, addr) =>
121      rport.addr := addr
122      rport.data
123    }
124
125    regfile.io.writePorts.zip(wen).zip(waddr).zip(wdata).foreach{ case (((wport, en), addr), data) =>
126      wport.wen := en
127      wport.addr := addr
128      wport.data := data
129    }
130    if (withReset) {
131      val numResetCycles = math.ceil(numEntries / numWritePorts).toInt
132      val resetCounter = RegInit(numResetCycles.U)
133      val resetWaddr = RegInit(VecInit((0 until numWritePorts).map(_.U(log2Up(numEntries + 1).W))))
134      val inReset = resetCounter =/= 0.U
135      when (inReset) {
136        resetCounter := resetCounter - 1.U
137        resetWaddr := VecInit(resetWaddr.map(_ + numWritePorts.U))
138      }
139      when (!inReset) {
140        resetWaddr.map(_ := 0.U)
141      }
142      for ((wport, i) <- regfile.io.writePorts.zipWithIndex) {
143        wport.wen := inReset || wen(i)
144        wport.addr := Mux(inReset, resetWaddr(i), waddr(i))
145        wport.data := wdata(i)
146      }
147    }
148
149    require(debugReadAddr.nonEmpty == debugReadData.nonEmpty, "Both debug addr and data bundles should be empty or not")
150    regfile.io.debug_rports := DontCare
151    if (debugReadAddr.nonEmpty && debugReadData.nonEmpty) {
152      debugReadData.get := VecInit(regfile.io.debug_rports.zip(debugReadAddr.get).map { case (rport, addr) =>
153        rport.addr := addr
154        rport.data
155      })
156    }
157  }
158}
159
160object IntRegFile {
161  // non-return version
162  def apply(
163    name         : String,
164    numEntries   : Int,
165    raddr        : Seq[UInt],
166    rdata        : Vec[UInt],
167    wen          : Seq[Bool],
168    waddr        : Seq[UInt],
169    wdata        : Seq[UInt],
170    debugReadAddr: Option[Seq[UInt]],
171    debugReadData: Option[Vec[UInt]],
172    withReset    : Boolean = false,
173  )(implicit p: Parameters): Unit = {
174    Regfile(
175      name, numEntries, raddr, rdata, wen, waddr, wdata,
176      hasZero = true, withReset, debugReadAddr, debugReadData)
177  }
178}
179
180object VfRegFile {
181  // non-return version
182  def apply(
183    name         : String,
184    numEntries   : Int,
185    splitNum     : Int,
186    raddr        : Seq[UInt],
187    rdata        : Vec[UInt],
188    wen          : Seq[Seq[Bool]],
189    waddr        : Seq[UInt],
190    wdata        : Seq[UInt],
191    debugReadAddr: Option[Seq[UInt]],
192    debugReadData: Option[Vec[UInt]],
193    withReset    : Boolean = false,
194  )(implicit p: Parameters): Unit = {
195    require(splitNum >= 1, "splitNum should be no less than 1")
196    require(splitNum == wen.length, "splitNum should be equal to length of wen vec")
197    if (splitNum == 1) {
198      Regfile(
199        name, numEntries, raddr, rdata, wen.head, waddr, wdata,
200        hasZero = false, withReset, debugReadAddr, debugReadData)
201    } else {
202      val dataWidth = 64
203      val numReadPorts = raddr.length
204      require(splitNum > 1 && wdata.head.getWidth == dataWidth * splitNum)
205      val wdataVec = Wire(Vec(splitNum, Vec(wdata.length, UInt(dataWidth.W))))
206      val rdataVec = Wire(Vec(splitNum, Vec(raddr.length, UInt(dataWidth.W))))
207      val debugRDataVec: Option[Vec[Vec[UInt]]] = debugReadData.map(x => Wire(Vec(splitNum, Vec(x.length, UInt(dataWidth.W)))))
208      for (i <- 0 until splitNum) {
209        wdataVec(i) := wdata.map(_ ((i + 1) * dataWidth - 1, i * dataWidth))
210        Regfile(
211          name + s"Part${i}", numEntries, raddr, rdataVec(i), wen(i), waddr, wdataVec(i),
212          hasZero = false, withReset, debugReadAddr, debugRDataVec.map(_(i))
213        )
214      }
215      for (i <- 0 until rdata.length) {
216        rdata(i) := Cat(rdataVec.map(_ (i)).reverse)
217      }
218      if (debugReadData.nonEmpty) {
219        for (i <- 0 until debugReadData.get.length) {
220          debugReadData.get(i) := Cat(debugRDataVec.get.map(_ (i)).reverse)
221        }
222      }
223    }
224  }
225}