xref: /XiangShan/src/main/scala/xiangshan/backend/regfile/Regfile.scala (revision f320e0f01bd645f0a3045a8a740e60dd770734a9)
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._
23
24class RfReadPort(len: Int)(implicit p: Parameters) extends XSBundle {
25  val addr = Input(UInt(PhyRegIdxWidth.W))
26  val data = Output(UInt(len.W))
27  override def cloneType: RfReadPort.this.type =
28    new RfReadPort(len).asInstanceOf[this.type]
29}
30
31class RfWritePort(len: Int)(implicit p: Parameters) extends XSBundle {
32  val wen = Input(Bool())
33  val addr = Input(UInt(PhyRegIdxWidth.W))
34  val data = Input(UInt(len.W))
35  override def cloneType: RfWritePort.this.type =
36    new RfWritePort(len).asInstanceOf[this.type]
37}
38
39class Regfile
40(
41  numReadPorts: Int,
42  numWirtePorts: Int,
43  hasZero: Boolean,
44  len: Int
45)(implicit p: Parameters) extends XSModule {
46  val io = IO(new Bundle() {
47    val readPorts = Vec(numReadPorts, new RfReadPort(len))
48    val writePorts = Vec(numWirtePorts, new RfWritePort(len))
49    val debug_rports = Vec(32, new RfReadPort(len))
50  })
51
52  println("Regfile: size:" + NRPhyRegs + " read: " + numReadPorts + "write: " + numWirtePorts)
53
54  val useBlackBox = false
55  if (!useBlackBox) {
56    val mem = Reg(Vec(NRPhyRegs, UInt(len.W)))
57    for (r <- io.readPorts) {
58      val rdata = if (hasZero) Mux(r.addr === 0.U, 0.U, mem(r.addr)) else mem(r.addr)
59      r.data := RegNext(rdata)
60    }
61    for (w <- io.writePorts) {
62      when(w.wen) {
63        mem(w.addr) := w.data
64      }
65    }
66
67    for (rport <- io.debug_rports) {
68      val zero_rdata = Mux(rport.addr === 0.U, 0.U, mem(rport.addr))
69      rport.data := (if (hasZero) zero_rdata else mem(rport.addr))
70    }
71  } else {
72
73    val regfile = Module(new regfile_160x64_10w16r_sim)
74
75    regfile.io.clk := this.clock
76    regfile.io.gpr := hasZero.B
77
78    regfile.io.wen0   := io.writePorts(0).wen
79    regfile.io.waddr0 := io.writePorts(0).addr
80    regfile.io.wdata0 := io.writePorts(0).data
81
82    regfile.io.wen1   := io.writePorts(1).wen
83    regfile.io.waddr1 := io.writePorts(1).addr
84    regfile.io.wdata1 := io.writePorts(1).data
85
86    regfile.io.wen2   := io.writePorts(2).wen
87    regfile.io.waddr2 := io.writePorts(2).addr
88    regfile.io.wdata2 := io.writePorts(2).data
89
90    regfile.io.wen3   := io.writePorts(3).wen
91    regfile.io.waddr3 := io.writePorts(3).addr
92    regfile.io.wdata3 := io.writePorts(3).data
93
94    regfile.io.wen4   := io.writePorts(4).wen
95    regfile.io.waddr4 := io.writePorts(4).addr
96    regfile.io.wdata4 := io.writePorts(4).data
97
98    regfile.io.wen5   := io.writePorts(5).wen
99    regfile.io.waddr5 := io.writePorts(5).addr
100    regfile.io.wdata5 := io.writePorts(5).data
101
102    regfile.io.wen6   := io.writePorts(6).wen
103    regfile.io.waddr6 := io.writePorts(6).addr
104    regfile.io.wdata6 := io.writePorts(6).data
105
106    regfile.io.wen7   := io.writePorts(7).wen
107    regfile.io.waddr7 := io.writePorts(7).addr
108    regfile.io.wdata7 := io.writePorts(7).data
109
110    regfile.io.wen8   := false.B   //io.writePorts(8).wen
111    regfile.io.waddr8 := DontCare  //io.writePorts(8).addr
112    regfile.io.wdata8 := DontCare  //io.writePorts(8).data
113
114    regfile.io.wen9   := false.B   //io.writePorts(9).wen
115    regfile.io.waddr9 := DontCare  //io.writePorts(9).addr
116    regfile.io.wdata9 := DontCare  //io.writePorts(9).data
117
118
119    regfile.io.raddr0  := io.readPorts(0).addr
120    regfile.io.raddr1  := io.readPorts(1).addr
121    regfile.io.raddr2  := io.readPorts(2).addr
122    regfile.io.raddr3  := io.readPorts(3).addr
123    regfile.io.raddr4  := io.readPorts(4).addr
124    regfile.io.raddr5  := io.readPorts(5).addr
125    regfile.io.raddr6  := io.readPorts(6).addr
126    regfile.io.raddr7  := io.readPorts(7).addr
127    regfile.io.raddr8  := io.readPorts(8).addr
128    regfile.io.raddr9  := io.readPorts(9).addr
129    regfile.io.raddr10 := io.readPorts(10).addr
130    regfile.io.raddr11 := io.readPorts(11).addr
131    regfile.io.raddr12 := io.readPorts(12).addr
132    regfile.io.raddr13 := io.readPorts(13).addr
133    regfile.io.raddr14 := DontCare //io.readPorts(14).addr
134    regfile.io.raddr15 := DontCare //io.readPorts(15).addr
135
136    io.readPorts(0).data := regfile.io.rdata0
137    io.readPorts(1).data := regfile.io.rdata1
138    io.readPorts(2).data := regfile.io.rdata2
139    io.readPorts(3).data := regfile.io.rdata3
140    io.readPorts(4).data := regfile.io.rdata4
141    io.readPorts(5).data := regfile.io.rdata5
142    io.readPorts(6).data := regfile.io.rdata6
143    io.readPorts(7).data := regfile.io.rdata7
144    io.readPorts(8).data := regfile.io.rdata8
145    io.readPorts(9).data := regfile.io.rdata9
146    io.readPorts(10).data := regfile.io.rdata10
147    io.readPorts(11).data := regfile.io.rdata11
148    io.readPorts(12).data := regfile.io.rdata12
149    io.readPorts(13).data := regfile.io.rdata13
150
151    io.debug_rports := DontCare
152  }
153
154}
155
156class regfile_160x64_10w16r_sim extends BlackBox with HasBlackBoxResource {
157
158  val io = IO(new Bundle{
159    val clk = Input(Clock())
160    val gpr = Input(Bool())
161
162    // write
163    val wen0, wen1, wen2, wen3, wen4, wen5, wen6, wen7, wen8, wen9 = Input(Bool())
164    val waddr0, waddr1, waddr2, waddr3, waddr4, waddr5, waddr6, waddr7, waddr8, waddr9 = Input(UInt(8.W))
165    val wdata0, wdata1, wdata2, wdata3, wdata4, wdata5, wdata6, wdata7, wdata8, wdata9 = Input(UInt(64.W))
166
167    // read
168    val raddr0, raddr1, raddr2, raddr3, raddr4, raddr5, raddr6, raddr7 = Input(UInt(8.W))
169    val raddr8, raddr9, raddr10, raddr11, raddr12, raddr13, raddr14, raddr15 = Input(UInt(8.W))
170    val rdata0, rdata1, rdata2, rdata3, rdata4, rdata5, rdata6, rdata7 = Output(UInt(64.W))
171    val rdata8, rdata9, rdata10, rdata11, rdata12, rdata13, rdata14, rdata15 = Output(UInt(64.W))
172  })
173
174  val vsrc = "/vsrc/regfile_160x64_10w16r_sim.v"
175  println(s"Regfile: Using verilog source at: $vsrc")
176  setResource(vsrc)
177
178}
179
180