系列: RISC V Hardware
verilog
40 行
· 更新於 2026-07-04
regfile.v
RISC_V_Hardware/verilog/rtl/regfile.v
// ============================================================
// regfile.v -- 32 x 32-bit register file, x0 hardwired to 0
// 2 async read ports, 1 sync write port.
// Optional write-through bypass (BYPASS=1): a read of the
// register being written in the same cycle returns the NEW
// value. Required by the 5-stage pipeline (producer in WB,
// consumer reading in ID). Must stay 0 for the single-cycle
// core, where it would create a combinational loop.
// ============================================================
`timescale 1ns/1ps
module regfile #(
parameter BYPASS = 0
)(
input wire clk,
input wire we,
input wire [4:0] ra1,
input wire [4:0] ra2,
input wire [4:0] wa,
input wire [31:0] wd,
output wire [31:0] rd1,
output wire [31:0] rd2
);
reg [31:0] regs [0:31];
integer i;
initial begin
for (i = 0; i < 32; i = i + 1) regs[i] = 32'b0;
end
always @(posedge clk) begin
if (we && (wa != 5'd0))
regs[wa] <= wd;
end
wire bypass1 = (BYPASS != 0) && we && (wa != 5'd0) && (wa == ra1);
wire bypass2 = (BYPASS != 0) && we && (wa != 5'd0) && (wa == ra2);
assign rd1 = (ra1 == 5'd0) ? 32'b0 : (bypass1 ? wd : regs[ra1]);
assign rd2 = (ra2 == 5'd0) ? 32'b0 : (bypass2 ? wd : regs[ra2]);
endmodule
相關文章
RISC V Hardware
bash
更新於 2026-07-04
build.sh
build.sh — bash source code from the RISC V Hardware learning materials (RISC_V_Hardware/build.sh).
閱讀文章 →
RISC V Hardware
latex
更新於 2026-07-04
style.tex
style.tex — latex source code from the RISC V Hardware learning materials (RISC_V_Hardware/latex/common/style.tex).
閱讀文章 →
RISC V Hardware
verilog
更新於 2026-07-04
alu.v
alu.v — verilog source code from the RISC V Hardware learning materials (RISC_V_Hardware/verilog/rtl/alu.v).
閱讀文章 →
RISC V Hardware
verilog
更新於 2026-07-04
branch_unit.v
branch_unit.v — verilog source code from the RISC V Hardware learning materials (RISC_V_Hardware/verilog/rtl/branch_unit.v).
閱讀文章 →
RISC V Hardware
verilog
更新於 2026-07-04
control.v
control.v — verilog source code from the RISC V Hardware learning materials (RISC_V_Hardware/verilog/rtl/control.v).
閱讀文章 →
RISC V Hardware
verilog
更新於 2026-07-04
dmem.v
dmem.v — verilog source code from the RISC V Hardware learning materials (RISC_V_Hardware/verilog/rtl/dmem.v).
閱讀文章 →