1c6d43980SLemover/*************************************************************************************** 2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 4c6d43980SLemover* 5c6d43980SLemover* XiangShan is licensed under Mulan PSL v2. 6c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 7c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at: 8c6d43980SLemover* http://license.coscl.org.cn/MulanPSL2 9c6d43980SLemover* 10c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13c6d43980SLemover* 14c6d43980SLemover* See the Mulan PSL v2 for more details. 15c6d43980SLemover***************************************************************************************/ 16c6d43980SLemover 175844fcf0SLinJiaweipackage xiangshan.backend.regfile 185844fcf0SLinJiawei 192225d46eSJiawei Linimport chipsalliance.rocketchip.config.Parameters 205844fcf0SLinJiaweiimport chisel3._ 21*510ae4eeSJiuyang Liuimport chisel3.experimental.ExtModule 225844fcf0SLinJiaweiimport chisel3.util._ 235844fcf0SLinJiaweiimport xiangshan._ 245844fcf0SLinJiawei 252225d46eSJiawei Linclass RfReadPort(len: Int)(implicit p: Parameters) extends XSBundle { 265844fcf0SLinJiawei val addr = Input(UInt(PhyRegIdxWidth.W)) 27ebd10a1fSYinan Xu val data = Output(UInt(len.W)) 28ebd10a1fSYinan Xu override def cloneType: RfReadPort.this.type = 29ebd10a1fSYinan Xu new RfReadPort(len).asInstanceOf[this.type] 305844fcf0SLinJiawei} 315844fcf0SLinJiawei 322225d46eSJiawei Linclass RfWritePort(len: Int)(implicit p: Parameters) extends XSBundle { 335844fcf0SLinJiawei val wen = Input(Bool()) 345844fcf0SLinJiawei val addr = Input(UInt(PhyRegIdxWidth.W)) 35ebd10a1fSYinan Xu val data = Input(UInt(len.W)) 36ebd10a1fSYinan Xu override def cloneType: RfWritePort.this.type = 37ebd10a1fSYinan Xu new RfWritePort(len).asInstanceOf[this.type] 385844fcf0SLinJiawei} 395844fcf0SLinJiawei 405844fcf0SLinJiaweiclass Regfile 415844fcf0SLinJiawei( 425844fcf0SLinJiawei numReadPorts: Int, 435844fcf0SLinJiawei numWirtePorts: Int, 449684eb4fSLinJiawei hasZero: Boolean, 459684eb4fSLinJiawei len: Int 462225d46eSJiawei Lin)(implicit p: Parameters) extends XSModule { 475844fcf0SLinJiawei val io = IO(new Bundle() { 48ebd10a1fSYinan Xu val readPorts = Vec(numReadPorts, new RfReadPort(len)) 49ebd10a1fSYinan Xu val writePorts = Vec(numWirtePorts, new RfWritePort(len)) 502225d46eSJiawei Lin val debug_rports = Vec(32, new RfReadPort(len)) 515844fcf0SLinJiawei }) 520c701001SLinJiawei 5305f23f57SWilliam Wang println("Regfile: size:" + NRPhyRegs + " read: " + numReadPorts + " write: " + numWirtePorts) 5405f23f57SWilliam Wang 55ebd10a1fSYinan Xu val useBlackBox = false 56ebd10a1fSYinan Xu if (!useBlackBox) { 57fc8a3b3fSljw val mem = Reg(Vec(NRPhyRegs, UInt(len.W))) 580c701001SLinJiawei for (r <- io.readPorts) { 59b441ea13SYikeZhou val rdata = if (hasZero) Mux(r.addr === 0.U, 0.U, mem(r.addr)) else mem(r.addr) 60b441ea13SYikeZhou r.data := RegNext(rdata) 610c701001SLinJiawei } 620c701001SLinJiawei for (w <- io.writePorts) { 630c701001SLinJiawei when(w.wen) { 640c701001SLinJiawei mem(w.addr) := w.data 650c701001SLinJiawei } 660c701001SLinJiawei } 676624015fSLinJiawei 682225d46eSJiawei Lin for (rport <- io.debug_rports) { 692225d46eSJiawei Lin val zero_rdata = Mux(rport.addr === 0.U, 0.U, mem(rport.addr)) 702225d46eSJiawei Lin rport.data := (if (hasZero) zero_rdata else mem(rport.addr)) 71adb5df20SYinan Xu } 72adb5df20SYinan Xu when (reset.asBool()) { 73adb5df20SYinan Xu mem.map(_ := 0.U) 74a165bd69Swangkaifan } 75067dba72SLinJiawei } else { 76067dba72SLinJiawei 77af5cf0d1SYinan Xu val regfile = Module(new regfile_160x64_10w16r_sim) 78067dba72SLinJiawei 79*510ae4eeSJiuyang Liu regfile.clk := this.clock 80*510ae4eeSJiuyang Liu regfile.gpr := hasZero.B 81067dba72SLinJiawei 82*510ae4eeSJiuyang Liu regfile.wen0 := io.writePorts(0).wen 83*510ae4eeSJiuyang Liu regfile.waddr0 := io.writePorts(0).addr 84*510ae4eeSJiuyang Liu regfile.wdata0 := io.writePorts(0).data 85067dba72SLinJiawei 86*510ae4eeSJiuyang Liu regfile.wen1 := io.writePorts(1).wen 87*510ae4eeSJiuyang Liu regfile.waddr1 := io.writePorts(1).addr 88*510ae4eeSJiuyang Liu regfile.wdata1 := io.writePorts(1).data 89067dba72SLinJiawei 90*510ae4eeSJiuyang Liu regfile.wen2 := io.writePorts(2).wen 91*510ae4eeSJiuyang Liu regfile.waddr2 := io.writePorts(2).addr 92*510ae4eeSJiuyang Liu regfile.wdata2 := io.writePorts(2).data 93067dba72SLinJiawei 94*510ae4eeSJiuyang Liu regfile.wen3 := io.writePorts(3).wen 95*510ae4eeSJiuyang Liu regfile.waddr3 := io.writePorts(3).addr 96*510ae4eeSJiuyang Liu regfile.wdata3 := io.writePorts(3).data 97067dba72SLinJiawei 98*510ae4eeSJiuyang Liu regfile.wen4 := io.writePorts(4).wen 99*510ae4eeSJiuyang Liu regfile.waddr4 := io.writePorts(4).addr 100*510ae4eeSJiuyang Liu regfile.wdata4 := io.writePorts(4).data 101067dba72SLinJiawei 102*510ae4eeSJiuyang Liu regfile.wen5 := io.writePorts(5).wen 103*510ae4eeSJiuyang Liu regfile.waddr5 := io.writePorts(5).addr 104*510ae4eeSJiuyang Liu regfile.wdata5 := io.writePorts(5).data 105067dba72SLinJiawei 106*510ae4eeSJiuyang Liu regfile.wen6 := io.writePorts(6).wen 107*510ae4eeSJiuyang Liu regfile.waddr6 := io.writePorts(6).addr 108*510ae4eeSJiuyang Liu regfile.wdata6 := io.writePorts(6).data 109067dba72SLinJiawei 110*510ae4eeSJiuyang Liu regfile.wen7 := io.writePorts(7).wen 111*510ae4eeSJiuyang Liu regfile.waddr7 := io.writePorts(7).addr 112*510ae4eeSJiuyang Liu regfile.wdata7 := io.writePorts(7).data 113067dba72SLinJiawei 114*510ae4eeSJiuyang Liu regfile.wen8 := false.B //io.writePorts(8).wen 115*510ae4eeSJiuyang Liu regfile.waddr8 := DontCare //io.writePorts(8).addr 116*510ae4eeSJiuyang Liu regfile.wdata8 := DontCare //io.writePorts(8).data 117067dba72SLinJiawei 118*510ae4eeSJiuyang Liu regfile.wen9 := false.B //io.writePorts(9).wen 119*510ae4eeSJiuyang Liu regfile.waddr9 := DontCare //io.writePorts(9).addr 120*510ae4eeSJiuyang Liu regfile.wdata9 := DontCare //io.writePorts(9).data 121067dba72SLinJiawei 122067dba72SLinJiawei 123*510ae4eeSJiuyang Liu regfile.raddr0 := io.readPorts(0).addr 124*510ae4eeSJiuyang Liu regfile.raddr1 := io.readPorts(1).addr 125*510ae4eeSJiuyang Liu regfile.raddr2 := io.readPorts(2).addr 126*510ae4eeSJiuyang Liu regfile.raddr3 := io.readPorts(3).addr 127*510ae4eeSJiuyang Liu regfile.raddr4 := io.readPorts(4).addr 128*510ae4eeSJiuyang Liu regfile.raddr5 := io.readPorts(5).addr 129*510ae4eeSJiuyang Liu regfile.raddr6 := io.readPorts(6).addr 130*510ae4eeSJiuyang Liu regfile.raddr7 := io.readPorts(7).addr 131*510ae4eeSJiuyang Liu regfile.raddr8 := io.readPorts(8).addr 132*510ae4eeSJiuyang Liu regfile.raddr9 := io.readPorts(9).addr 133*510ae4eeSJiuyang Liu regfile.raddr10 := io.readPorts(10).addr 134*510ae4eeSJiuyang Liu regfile.raddr11 := io.readPorts(11).addr 135*510ae4eeSJiuyang Liu regfile.raddr12 := io.readPorts(12).addr 136*510ae4eeSJiuyang Liu regfile.raddr13 := io.readPorts(13).addr 137*510ae4eeSJiuyang Liu regfile.raddr14 := DontCare //io.readPorts(14).addr 138*510ae4eeSJiuyang Liu regfile.raddr15 := DontCare //io.readPorts(15).addr 139067dba72SLinJiawei 140*510ae4eeSJiuyang Liu io.readPorts(0).data := regfile.rdata0 141*510ae4eeSJiuyang Liu io.readPorts(1).data := regfile.rdata1 142*510ae4eeSJiuyang Liu io.readPorts(2).data := regfile.rdata2 143*510ae4eeSJiuyang Liu io.readPorts(3).data := regfile.rdata3 144*510ae4eeSJiuyang Liu io.readPorts(4).data := regfile.rdata4 145*510ae4eeSJiuyang Liu io.readPorts(5).data := regfile.rdata5 146*510ae4eeSJiuyang Liu io.readPorts(6).data := regfile.rdata6 147*510ae4eeSJiuyang Liu io.readPorts(7).data := regfile.rdata7 148*510ae4eeSJiuyang Liu io.readPorts(8).data := regfile.rdata8 149*510ae4eeSJiuyang Liu io.readPorts(9).data := regfile.rdata9 150*510ae4eeSJiuyang Liu io.readPorts(10).data := regfile.rdata10 151*510ae4eeSJiuyang Liu io.readPorts(11).data := regfile.rdata11 152*510ae4eeSJiuyang Liu io.readPorts(12).data := regfile.rdata12 153*510ae4eeSJiuyang Liu io.readPorts(13).data := regfile.rdata13 1542225d46eSJiawei Lin 1552225d46eSJiawei Lin io.debug_rports := DontCare 1565844fcf0SLinJiawei } 15744dead2fSZhangZifei 15844dead2fSZhangZifei} 159067dba72SLinJiawei 160*510ae4eeSJiuyang Liuclass regfile_160x64_10w16r_sim extends ExtModule with HasExtModuleResource { 161067dba72SLinJiawei 162*510ae4eeSJiuyang Liu 163*510ae4eeSJiuyang Liu val clk = IO(Input(Clock())) 164*510ae4eeSJiuyang Liu val gpr = IO(Input(Bool())) 165067dba72SLinJiawei 166067dba72SLinJiawei // write 167*510ae4eeSJiuyang Liu val wen0, wen1, wen2, wen3, wen4, wen5, wen6, wen7, wen8, wen9 = IO(Input(Bool())) 168*510ae4eeSJiuyang Liu val waddr0, waddr1, waddr2, waddr3, waddr4, waddr5, waddr6, waddr7, waddr8, waddr9 = IO(Input(UInt(8.W))) 169*510ae4eeSJiuyang Liu val wdata0, wdata1, wdata2, wdata3, wdata4, wdata5, wdata6, wdata7, wdata8, wdata9 = IO(Input(UInt(64.W))) 170067dba72SLinJiawei 171067dba72SLinJiawei // read 172*510ae4eeSJiuyang Liu val raddr0, raddr1, raddr2, raddr3, raddr4, raddr5, raddr6, raddr7 = IO(Input(UInt(8.W))) 173*510ae4eeSJiuyang Liu val raddr8, raddr9, raddr10, raddr11, raddr12, raddr13, raddr14, raddr15 = IO(Input(UInt(8.W))) 174*510ae4eeSJiuyang Liu val rdata0, rdata1, rdata2, rdata3, rdata4, rdata5, rdata6, rdata7 = IO(Output(UInt(64.W))) 175*510ae4eeSJiuyang Liu val rdata8, rdata9, rdata10, rdata11, rdata12, rdata13, rdata14, rdata15 = IO(Output(UInt(64.W))) 176067dba72SLinJiawei 177067dba72SLinJiawei val vsrc = "/vsrc/regfile_160x64_10w16r_sim.v" 178067dba72SLinJiawei println(s"Regfile: Using verilog source at: $vsrc") 179c21bff99SJiawei Lin addResource(vsrc) 180067dba72SLinJiawei 181067dba72SLinJiawei} 182067dba72SLinJiawei 183