xref: /XiangShan/src/main/scala/xiangshan/backend/regfile/Regfile.scala (revision 4daa5bf3c3f27e7fd090866d52405b21e107eb8d)
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 org.chipsalliance.cde.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 srcType: UInt = Input(UInt(3.W))
40
41  def readInt: Boolean = IntRegSrcDataSet.contains(rfReadDataCfg)
42  def readFp : Boolean = FpRegSrcDataSet .contains(rfReadDataCfg)
43  def readVec: Boolean = VecRegSrcDataSet.contains(rfReadDataCfg)
44  def readVf : Boolean = VfRegSrcDataSet .contains(rfReadDataCfg)
45}
46
47class RfWritePortWithConfig(val rfWriteDataCfg: DataConfig, addrWidth: Int) extends Bundle {
48  val wen = Input(Bool())
49  val addr = Input(UInt(addrWidth.W))
50  val data = Input(UInt(rfWriteDataCfg.dataWidth.W))
51  val intWen = Input(Bool())
52  val fpWen = Input(Bool())
53  val vecWen = Input(Bool())
54  def writeInt: Boolean = rfWriteDataCfg.isInstanceOf[IntData]
55  def writeFp : Boolean = rfWriteDataCfg.isInstanceOf[FpData]
56  def writeVec: Boolean = rfWriteDataCfg.isInstanceOf[VecData]
57}
58
59class Regfile
60(
61  name: String,
62  numPregs: Int,
63  numReadPorts: Int,
64  numWritePorts: Int,
65  hasZero: Boolean,
66  len: Int,
67  width: Int,
68  bankNum: Int = 1,
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  require(Seq(1, 2, 4).contains(bankNum), "bankNum must be 1 or 2 or 4")
80  for (r <- io.readPorts) {
81    if (bankNum == 1) {
82      r.data := mem(RegNext(r.addr))
83    }
84    else {
85      val banks = (0 until bankNum).map { case i =>
86        mem.zipWithIndex.filter{ case (m, index) => (index % bankNum) == i }.map(_._1)
87      }
88      val bankWidth = bankNum.U.getWidth - 1
89      val hitBankWire = VecInit((0 until bankNum).map { case i => r.addr(bankWidth - 1, 0) === i.U })
90      val hitBankReg = Reg(Vec(bankNum, Bool()))
91      hitBankReg := hitBankWire
92      val banksRdata = Wire(Vec(bankNum, UInt(len.W)))
93      for (i <- 0 until bankNum) {
94        banksRdata(i) := RegEnable(VecInit(banks(i))(r.addr(r.addr.getWidth - 1, bankWidth)), hitBankWire(i))
95      }
96      r.data := Mux1H(hitBankReg, banksRdata)
97    }
98  }
99  val writePorts = io.writePorts
100  for (i <- writePorts.indices) {
101    if (i < writePorts.size-1) {
102      val hasSameWrite = writePorts.drop(i + 1).map(w => w.wen && w.addr === writePorts(i).addr && writePorts(i).wen).reduce(_ || _)
103      assert(!hasSameWrite, "RegFile two or more writePorts write same addr")
104    }
105  }
106  for (i <- mem.indices) {
107    if (hasZero && i == 0) {
108      mem(i) := 0.U
109    }
110    else {
111      val wenOH = VecInit(io.writePorts.map(w => w.wen && w.addr === i.U))
112      val wData = Mux1H(wenOH, io.writePorts.map(_.data))
113      when(wenOH.asUInt.orR) {
114        mem(i) := wData
115      }
116    }
117  }
118
119  for (rport <- io.debug_rports) {
120    val zero_rdata = Mux(rport.addr === 0.U, 0.U, mem(rport.addr))
121    rport.data := (if (hasZero) zero_rdata else mem(rport.addr))
122  }
123}
124
125object Regfile {
126  // non-return version
127  def apply(
128    name         : String,
129    numEntries   : Int,
130    raddr        : Seq[UInt],
131    rdata        : Vec[UInt],
132    wen          : Seq[Bool],
133    waddr        : Seq[UInt],
134    wdata        : Seq[UInt],
135    hasZero      : Boolean,
136    withReset    : Boolean = false,
137    bankNum      : Int = 1,
138    debugReadAddr: Option[Seq[UInt]],
139    debugReadData: Option[Vec[UInt]],
140  )(implicit p: Parameters): Unit = {
141    val numReadPorts = raddr.length
142    val numWritePorts = wen.length
143    require(wen.length == waddr.length)
144    require(wen.length == wdata.length)
145    val dataBits = wdata.map(_.getWidth).min
146    require(wdata.map(_.getWidth).min == wdata.map(_.getWidth).max, s"dataBits != $dataBits")
147    val addrBits = waddr.map(_.getWidth).min
148    require(waddr.map(_.getWidth).min == waddr.map(_.getWidth).max, s"addrBits != $addrBits")
149
150    val regfile = Module(new Regfile(name, numEntries, numReadPorts, numWritePorts, hasZero, dataBits, addrBits, bankNum))
151    rdata := regfile.io.readPorts.zip(raddr).map { case (rport, addr) =>
152      rport.addr := addr
153      rport.data
154    }
155
156    regfile.io.writePorts.zip(wen).zip(waddr).zip(wdata).foreach{ case (((wport, en), addr), data) =>
157      wport.wen := en
158      wport.addr := addr
159      wport.data := data
160    }
161    if (withReset) {
162      val numResetCycles = math.ceil(numEntries / numWritePorts).toInt
163      val resetCounter = RegInit(numResetCycles.U)
164      val resetWaddr = RegInit(VecInit((0 until numWritePorts).map(_.U(log2Up(numEntries + 1).W))))
165      val inReset = resetCounter =/= 0.U
166      when (inReset) {
167        resetCounter := resetCounter - 1.U
168        resetWaddr := VecInit(resetWaddr.map(_ + numWritePorts.U))
169      }
170      when (!inReset) {
171        resetWaddr.map(_ := 0.U)
172      }
173      for ((wport, i) <- regfile.io.writePorts.zipWithIndex) {
174        wport.wen := inReset || wen(i)
175        wport.addr := Mux(inReset, resetWaddr(i), waddr(i))
176        wport.data := wdata(i)
177      }
178    }
179
180    require(debugReadAddr.nonEmpty == debugReadData.nonEmpty, "Both debug addr and data bundles should be empty or not")
181    regfile.io.debug_rports := DontCare
182    if (debugReadAddr.nonEmpty && debugReadData.nonEmpty) {
183      debugReadData.get := VecInit(regfile.io.debug_rports.zip(debugReadAddr.get).map { case (rport, addr) =>
184        rport.addr := addr
185        rport.data
186      })
187    }
188  }
189}
190
191object IntRegFile {
192  // non-return version
193  def apply(
194    name         : String,
195    numEntries   : Int,
196    raddr        : Seq[UInt],
197    rdata        : Vec[UInt],
198    wen          : Seq[Bool],
199    waddr        : Seq[UInt],
200    wdata        : Seq[UInt],
201    debugReadAddr: Option[Seq[UInt]],
202    debugReadData: Option[Vec[UInt]],
203    withReset    : Boolean = false,
204    bankNum      : Int,
205  )(implicit p: Parameters): Unit = {
206    Regfile(
207      name, numEntries, raddr, rdata, wen, waddr, wdata,
208      hasZero = true, withReset, bankNum, debugReadAddr, debugReadData)
209  }
210}
211
212object FpRegFile {
213  // non-return version
214  def apply(
215             name         : String,
216             numEntries   : Int,
217             raddr        : Seq[UInt],
218             rdata        : Vec[UInt],
219             wen          : Seq[Bool],
220             waddr        : Seq[UInt],
221             wdata        : Seq[UInt],
222             debugReadAddr: Option[Seq[UInt]],
223             debugReadData: Option[Vec[UInt]],
224             withReset    : Boolean = false,
225             bankNum      : Int,
226           )(implicit p: Parameters): Unit = {
227    Regfile(
228      name, numEntries, raddr, rdata, wen, waddr, wdata,
229      hasZero = false, withReset, bankNum, debugReadAddr, debugReadData)
230  }
231}
232
233object VfRegFile {
234  // non-return version
235  def apply(
236    name         : String,
237    numEntries   : Int,
238    splitNum     : Int,
239    raddr        : Seq[UInt],
240    rdata        : Vec[UInt],
241    wen          : Seq[Seq[Bool]],
242    waddr        : Seq[UInt],
243    wdata        : Seq[UInt],
244    debugReadAddr: Option[Seq[UInt]],
245    debugReadData: Option[Vec[UInt]],
246    withReset    : Boolean = false,
247  )(implicit p: Parameters): Unit = {
248    require(splitNum >= 1, "splitNum should be no less than 1")
249    require(splitNum == wen.length, "splitNum should be equal to length of wen vec")
250    if (splitNum == 1) {
251      Regfile(
252        name, numEntries, raddr, rdata, wen.head, waddr, wdata,
253        hasZero = false, withReset, bankNum = 1, debugReadAddr, debugReadData)
254    } else {
255      val dataWidth = 64
256      val numReadPorts = raddr.length
257      require(splitNum > 1 && wdata.head.getWidth == dataWidth * splitNum)
258      val wdataVec = Wire(Vec(splitNum, Vec(wdata.length, UInt(dataWidth.W))))
259      val rdataVec = Wire(Vec(splitNum, Vec(raddr.length, UInt(dataWidth.W))))
260      val debugRDataVec: Option[Vec[Vec[UInt]]] = debugReadData.map(x => Wire(Vec(splitNum, Vec(x.length, UInt(dataWidth.W)))))
261      for (i <- 0 until splitNum) {
262        wdataVec(i) := wdata.map(_ ((i + 1) * dataWidth - 1, i * dataWidth))
263        Regfile(
264          name + s"Part${i}", numEntries, raddr, rdataVec(i), wen(i), waddr, wdataVec(i),
265          hasZero = false, withReset, bankNum = 1, debugReadAddr, debugRDataVec.map(_(i))
266        )
267      }
268      for (i <- 0 until rdata.length) {
269        rdata(i) := Cat(rdataVec.map(_ (i)).reverse)
270      }
271      if (debugReadData.nonEmpty) {
272        for (i <- 0 until debugReadData.get.length) {
273          debugReadData.get(i) := Cat(debugRDataVec.get.map(_ (i)).reverse)
274        }
275      }
276    }
277  }
278}