Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TABLE_GEN := scripts/gen_control_table.py

# Stubs whose lint warnings are not enforced yet. Remove a module from this
# list in the PR that implements it.
LINT_SKIP := data_mem reg_file top
LINT_SKIP := data_mem top

.PHONY: all lint l1 table-check ci clean

Expand Down
44 changes: 25 additions & 19 deletions src/reg_file.sv
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@

module reg_file(
input logic [4:0] rs1,
input logic [4:0] rs2,
input logic [4:0] rd,
input logic [31:0] data_in,
input logic RegWrite, // 0: write disabled, 1: write enabled
input logic clk,
output logic [31:0] rs1_out,
output logic [31:0] rs2_out
);
/*
* Register File Module:
* The requirements for the register file are to support reads of rs1
* and rs2 in parallel, as well as supporting sequential writes to rd
* with data from data_in. RegWrite is a control signal that determines
* whether we want to write on a given clock edge (posedge), and clk is
* our clock signal which is shared across the core.
* Also recall that there are 32, 32-bit registers, one of which
* (register x0) is hard-wired to 32'b0.
*/
input logic [4:0] rs1,
input logic [4:0] rs2,
input logic [4:0] rd,
input logic [31:0] data_in,
input logic RegWrite,
input logic clk,
output logic [31:0] rs1_out,
output logic [31:0] rs2_out
);

// 32 registers, each 32 bits wide
logic [31:0] registers [0:31];

// Two asynchronous read ports
assign rs1_out = (rs1 == 5'd0) ? 32'b0 : registers[rs1];
assign rs2_out = (rs2 == 5'd0) ? 32'b0 : registers[rs2];

// Synchronous write on rising clock edge
always_ff @(posedge clk) begin
if (RegWrite && (rd != 5'd0)) begin
registers[rd] <= data_in;
end
end

endmodule
92 changes: 92 additions & 0 deletions testbench/level1_tb/reg_file_tb.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

`timescale 1ns/1ps

module reg_file_tb;

logic clk = 0;
logic [4:0] rs1, rs2, rd;
logic [31:0] data_in;
logic RegWrite;
logic [31:0] rs1_out, rs2_out;

reg_file dut (
.rs1(rs1),
.rs2(rs2),
.rd(rd),
.data_in(data_in),
.RegWrite(RegWrite),
.clk(clk),
.rs1_out(rs1_out),
.rs2_out(rs2_out)
);

always #5 clk = ~clk;

initial begin
rs1 = 0;
rs2 = 0;
rd = 0;
data_in = 0;
RegWrite = 0;

#1;
if (rs1_out !== 0 || rs2_out !== 0)
$fatal(1, "x0 read failed");

// Write to x5
@(negedge clk);
rd = 5;
data_in = 32'h12345678;
RegWrite = 1;

@(posedge clk);
#1;
rs1 = 5;
#1;
if (rs1_out !== 32'h12345678)
$fatal(1, "Write/read x5 failed");

// Write to x10
@(negedge clk);
rd = 10;
data_in = 32'hABCDEF12;

@(posedge clk);
#1;
rs1 = 5;
rs2 = 10;
#1;
if (rs1_out !== 32'h12345678 ||
rs2_out !== 32'hABCDEF12)
$fatal(1, "Dual read failed");

// Check that RegWrite disables writing
@(negedge clk);
RegWrite = 0;
rd = 5;
data_in = 32'hFFFFFFFF;

@(posedge clk);
#1;
if (rs1_out !== 32'h12345678)
$fatal(1, "Disabled write failed");

// Attempt to overwrite x0
@(negedge clk);
RegWrite = 1;
rd = 0;
data_in = 32'hFFFFFFFF;

@(posedge clk);
#1;
rs1 = 0;
rs2 = 0;
#1;
if (rs1_out !== 0 || rs2_out !== 0)
$fatal(1, "x0 protection failed");

$display("TEST PASSED: Register file basic tests completed");
$finish;
end

endmodule
Loading