aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rv64cpu/rtl/core/decoder.sv2
-rw-r--r--rv64cpu/rtl/core/ex_mem_reg.sv195
-rw-r--r--rv64cpu/rtl/core/ex_stage.sv28
-rw-r--r--rv64cpu/rtl/core/id_ex_reg.sv35
-rw-r--r--rv64cpu/rtl/core/id_stage.sv6
-rw-r--r--rv64cpu/rtl/core/mem_stage.sv214
-rw-r--r--rv64cpu/rtl/core/mem_wb_reg.sv132
-rw-r--r--rv64cpu/tb/CMakeLists.txt27
-rw-r--r--rv64cpu/tb/unit/tb_ex_mem_reg.cc182
-rw-r--r--rv64cpu/tb/unit/tb_id_ex_reg.cc10
-rw-r--r--rv64cpu/tb/unit/tb_id_stage.cc4
-rw-r--r--rv64cpu/tb/unit/tb_mem_stage.cc210
-rw-r--r--rv64cpu/tb/unit/tb_mem_wb_reg.cc165
13 files changed, 1190 insertions, 20 deletions
diff --git a/rv64cpu/rtl/core/decoder.sv b/rv64cpu/rtl/core/decoder.sv
index 585a018..c948646 100644
--- a/rv64cpu/rtl/core/decoder.sv
+++ b/rv64cpu/rtl/core/decoder.sv
@@ -80,7 +80,7 @@ module decoder
assign opcode = instr[6:0];
assign rd_addr = instr[11:7];
assign funct3 = instr[14:12];
- assign rs1_addr = instr[19:15];
+ assign rs1_addr = (opcode == OPCODE_LUI) ? 5'b00000 : instr[19:15];
assign rs2_addr = instr[24:20];
assign funct7 = instr[31:25];
diff --git a/rv64cpu/rtl/core/ex_mem_reg.sv b/rv64cpu/rtl/core/ex_mem_reg.sv
new file mode 100644
index 0000000..2cc5e9e
--- /dev/null
+++ b/rv64cpu/rtl/core/ex_mem_reg.sv
@@ -0,0 +1,195 @@
+// Richard CPU
+// Copyright (C) 2026 Richard Qin
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// File path: rtl/core/ex_mem_reg.sv
+// Description: Execute to Memory Pipeline Register
+// Latches results from the EX stage and bypassed control signals from ID/EX.
+// Supports stall and flush operations.
+
+module ex_mem_reg
+ import rv64_pkg::*;
+(
+ input logic clk,
+ input logic rst,
+
+ // Pipeline Control
+ input logic stall_en,
+ input logic flush_en,
+
+ // ----------------------
+ // Inputs from EX Stage (computed results)
+ input logic [XLEN-1:0] ex_alu_result,
+ input logic ex_branch_taken,
+ input logic [XLEN-1:0] ex_branch_target,
+
+ // ----------------------
+ // Bypassed Inputs from ID/EX Register
+
+ // Data/Operands
+ input logic [XLEN-1:0] ex_rs1_rdata, // Needed for CSRRW/RS/RC
+ input logic [XLEN-1:0] ex_rs2_rdata, // Needed for store data
+ input logic [4:0] ex_rs1_addr, // Serves as zimm for CSRRWI/SI/CI
+ input logic [XLEN-1:0] ex_pc, // PC passed along for exceptions/CSRs
+ input logic [XLEN-1:0] ex_imm, // Imm passed for CSR writes
+
+ // Memory Control
+ input logic ex_is_load,
+ input logic ex_is_store,
+ input logic [2:0] ex_mem_size,
+
+ // Writeback Control
+ input logic ex_reg_write_en,
+ input logic [4:0] ex_rd_addr,
+ input logic [1:0] ex_wb_sel,
+
+ // System / Exception Control
+ input logic ex_is_csr,
+ input logic [2:0] ex_csr_op,
+ input logic ex_illegal_instr,
+ input logic ex_is_ecall,
+ input logic ex_is_ebreak,
+ input logic ex_is_mret,
+ input logic ex_is_sret,
+ input logic ex_is_wfi,
+ input logic ex_is_sfence_vma,
+ input logic ex_is_fence,
+ input logic ex_is_fence_i,
+
+
+ // ----------------------
+ // Outputs to MEM Stage
+
+ // Data/Operands
+ output logic [XLEN-1:0] mem_alu_result,
+ output logic mem_branch_taken,
+ output logic [XLEN-1:0] mem_branch_target,
+ output logic [XLEN-1:0] mem_rs1_rdata,
+ output logic [XLEN-1:0] mem_rs2_rdata,
+ output logic [4:0] mem_rs1_addr,
+ output logic [XLEN-1:0] mem_pc,
+ output logic [XLEN-1:0] mem_imm,
+
+ // Memory Control
+ output logic mem_is_load,
+ output logic mem_is_store,
+ output logic [2:0] mem_mem_size,
+
+ // Writeback Control
+ output logic mem_reg_write_en,
+ output logic [4:0] mem_rd_addr,
+ output logic [1:0] mem_wb_sel,
+
+ // System / Exception Control
+ output logic mem_is_csr,
+ output logic [2:0] mem_csr_op,
+ output logic mem_illegal_instr,
+ output logic mem_is_ecall,
+ output logic mem_is_ebreak,
+ output logic mem_is_mret,
+ output logic mem_is_sret,
+ output logic mem_is_wfi,
+ output logic mem_is_sfence_vma,
+ output logic mem_is_fence,
+ output logic mem_is_fence_i
+);
+
+ always_ff @(posedge clk) begin
+ if (rst) begin
+ mem_alu_result <= '0;
+ mem_branch_taken <= '0;
+ mem_branch_target <= '0;
+ mem_rs1_rdata <= '0;
+ mem_rs2_rdata <= '0;
+ mem_rs1_addr <= '0;
+ mem_pc <= '0;
+ mem_imm <= '0;
+
+ // Critical control signals must be zeroed out
+ mem_is_load <= '0;
+ mem_is_store <= '0;
+ mem_mem_size <= '0;
+ mem_reg_write_en <= '0;
+ mem_rd_addr <= '0;
+ mem_wb_sel <= '0;
+
+ // Exceptions and system control
+ mem_is_csr <= '0;
+ mem_csr_op <= '0;
+ mem_illegal_instr <= '0;
+ mem_is_ecall <= '0;
+ mem_is_ebreak <= '0;
+ mem_is_mret <= '0;
+ mem_is_sret <= '0;
+ mem_is_wfi <= '0;
+ mem_is_sfence_vma <= '0;
+ mem_is_fence <= '0;
+ mem_is_fence_i <= '0;
+
+ end else if (flush_en) begin
+ // Critical control signals must be zeroed out
+ mem_branch_taken <= '0;
+ mem_is_load <= '0;
+ mem_is_store <= '0;
+ mem_mem_size <= '0;
+ mem_reg_write_en <= '0;
+ mem_rd_addr <= '0;
+ mem_wb_sel <= '0;
+
+ // Exceptions and system control
+ mem_is_csr <= '0;
+ mem_csr_op <= '0;
+ mem_illegal_instr <= '0;
+ mem_is_ecall <= '0;
+ mem_is_ebreak <= '0;
+ mem_is_mret <= '0;
+ mem_is_sret <= '0;
+ mem_is_wfi <= '0;
+ mem_is_sfence_vma <= '0;
+ mem_is_fence <= '0;
+ mem_is_fence_i <= '0;
+ end else if (~stall_en) begin
+ mem_alu_result <= ex_alu_result;
+ mem_branch_taken <= ex_branch_taken;
+ mem_branch_target <= ex_branch_target;
+ mem_rs1_rdata <= ex_rs1_rdata;
+ mem_rs2_rdata <= ex_rs2_rdata;
+ mem_rs1_addr <= ex_rs1_addr;
+ mem_pc <= ex_pc;
+ mem_imm <= ex_imm;
+
+ mem_is_load <= ex_is_load;
+ mem_is_store <= ex_is_store;
+ mem_mem_size <= ex_mem_size;
+ mem_reg_write_en <= ex_reg_write_en;
+ mem_rd_addr <= ex_rd_addr;
+ mem_wb_sel <= ex_wb_sel;
+
+ mem_is_csr <= ex_is_csr;
+ mem_csr_op <= ex_csr_op;
+ mem_illegal_instr <= ex_illegal_instr;
+ mem_is_ecall <= ex_is_ecall;
+ mem_is_ebreak <= ex_is_ebreak;
+ mem_is_mret <= ex_is_mret;
+ mem_is_sret <= ex_is_sret;
+ mem_is_wfi <= ex_is_wfi;
+ mem_is_sfence_vma <= ex_is_sfence_vma;
+ mem_is_fence <= ex_is_fence;
+ mem_is_fence_i <= ex_is_fence_i;
+ end
+ // If stall_en && !flush_en, maintain current state
+ end
+
+endmodule
diff --git a/rv64cpu/rtl/core/ex_stage.sv b/rv64cpu/rtl/core/ex_stage.sv
index 0991b50..48d4833 100644
--- a/rv64cpu/rtl/core/ex_stage.sv
+++ b/rv64cpu/rtl/core/ex_stage.sv
@@ -42,17 +42,17 @@ module ex_stage
output logic [XLEN-1:0] ex_branch_target
);
- logic [XLEN-1:0] alumux_a;
- logic [XLEN-1:0] alumux_b;
+ logic [XLEN-1:0] alu_mux_a;
+ logic [XLEN-1:0] alu_mux_b;
logic [XLEN-1:0] alu_operand_a;
logic [XLEN-1:0] alu_operand_b;
logic [XLEN-1:0] alu_raw_result;
- logic bu_branch_taken;
+ logic branch_unit_taken;
// -------------
// ALU Input Multiplexers (MUX)
- assign alumux_a = (ex_alu_src1_sel == 1'b1) ? ex_pc : ex_rs1_rdata;
- assign alumux_b = (ex_alu_src2_sel == 1'b1) ? ex_imm : ex_rs2_rdata;
+ assign alu_mux_a = (ex_alu_src1_sel == 1'b1) ? ex_pc : ex_rs1_rdata;
+ assign alu_mux_b = (ex_alu_src2_sel == 1'b1) ? ex_imm : ex_rs2_rdata;
// -------------
// RV64 Word Operation (32-bit) Pre-conditioning
@@ -62,21 +62,21 @@ module ex_stage
if (ex_is_word_op) begin
// Shift operations require specific 32-bit extension forms
if (ex_alu_op == ALU_SRA)
- alu_operand_a = { {32{alumux_a[31]}}, alumux_a[31:0] }; // Sign extend for arithmetic shift
+ alu_operand_a = { {32{alu_mux_a[31]}}, alu_mux_a[31:0] }; // Sign extend for arithmetic shift
else if (ex_alu_op == ALU_SRL)
- alu_operand_a = { 32'b0, alumux_a[31:0] }; // Zero extend for logical shift
+ alu_operand_a = { 32'b0, alu_mux_a[31:0] }; // Zero extend for logical shift
else
- alu_operand_a = alumux_a; // ADDW/SUBW/SLLW don't care about upper bits
+ alu_operand_a = alu_mux_a; // ADDW/SUBW/SLLW don't care about upper bits
// RV32 shift amount is strictly 5 bits (0-31), ignoring bit 5 which might be set in 64-bit registers
if (ex_alu_op == ALU_SLL || ex_alu_op == ALU_SRL || ex_alu_op == ALU_SRA)
- alu_operand_b = { 59'b0, alumux_b[4:0] };
+ alu_operand_b = { 59'b0, alu_mux_b[4:0] };
else
- alu_operand_b = alumux_b;
+ alu_operand_b = alu_mux_b;
end else begin
// 64-bit operations pass through unchanged
- alu_operand_a = alumux_a;
- alu_operand_b = alumux_b;
+ alu_operand_a = alu_mux_a;
+ alu_operand_b = alu_mux_b;
end
end
@@ -96,7 +96,7 @@ module ex_stage
.branch_op (ex_branch_op),
.operand_a (ex_rs1_rdata),
.operand_b (ex_rs2_rdata),
- .branch_taken (bu_branch_taken)
+ .branch_taken (branch_unit_taken)
);
// -------------
@@ -109,6 +109,6 @@ module ex_stage
// Branch Target Calculation
assign ex_branch_target = alu_raw_result & ~64'b1;
- assign ex_branch_taken = ex_is_jump | (ex_is_branch & bu_branch_taken);
+ assign ex_branch_taken = ex_is_jump | (ex_is_branch & branch_unit_taken);
endmodule
diff --git a/rv64cpu/rtl/core/id_ex_reg.sv b/rv64cpu/rtl/core/id_ex_reg.sv
index b8a3bd7..e88ce17 100644
--- a/rv64cpu/rtl/core/id_ex_reg.sv
+++ b/rv64cpu/rtl/core/id_ex_reg.sv
@@ -36,6 +36,8 @@ module id_ex_reg
input logic [XLEN-1:0] id_pc,
input logic [XLEN-1:0] id_rs1_rdata,
input logic [XLEN-1:0] id_rs2_rdata,
+ input logic [4:0] id_rs1_addr,
+ input logic [4:0] id_rs2_addr,
input logic [XLEN-1:0] id_imm,
// Execution Control
@@ -75,6 +77,8 @@ module id_ex_reg
output logic [XLEN-1:0] ex_pc,
output logic [XLEN-1:0] ex_rs1_rdata,
output logic [XLEN-1:0] ex_rs2_rdata,
+ output logic [4:0] ex_rs1_addr,
+ output logic [4:0] ex_rs2_addr,
output logic [XLEN-1:0] ex_imm,
// Execution Control
@@ -109,10 +113,12 @@ module id_ex_reg
);
always_ff @(posedge clk) begin
- if (rst || flush_en) begin
+ if (rst) begin
ex_pc <= '0;
ex_rs1_rdata <= '0;
ex_rs2_rdata <= '0;
+ ex_rs1_addr <= '0;
+ ex_rs2_addr <= '0;
ex_imm <= '0;
ex_alu_op <= ALU_ADD; // NOP is effectively ADD x0, x0, 0
ex_alu_src1_sel <= '0;
@@ -138,10 +144,37 @@ module id_ex_reg
ex_is_sfence_vma <= '0;
ex_is_fence <= '0;
ex_is_fence_i <= '0;
+ end else if (flush_en) begin
+ ex_alu_op <= ALU_ADD; // NOP is effectively ADD x0, x0, 0
+ ex_alu_src1_sel <= '0;
+ ex_alu_src2_sel <= '0;
+ ex_is_word_op <= '0;
+ ex_is_branch <= '0;
+ ex_branch_op <= BRANCH_EQ;
+ ex_is_jump <= '0;
+ ex_is_load <= '0;
+ ex_is_store <= '0;
+ ex_mem_size <= '0;
+ ex_reg_write_en <= '0; // VERY IMPORTANT: Do not write to regfile on NOP
+ ex_rd_addr <= '0;
+ ex_wb_sel <= '0;
+ ex_is_csr <= '0;
+ ex_csr_op <= '0;
+ ex_illegal_instr <= '0;
+ ex_is_ecall <= '0;
+ ex_is_ebreak <= '0;
+ ex_is_mret <= '0;
+ ex_is_sret <= '0;
+ ex_is_wfi <= '0;
+ ex_is_sfence_vma <= '0;
+ ex_is_fence <= '0;
+ ex_is_fence_i <= '0;
end else if (~stall_en) begin
ex_pc <= id_pc;
ex_rs1_rdata <= id_rs1_rdata;
ex_rs2_rdata <= id_rs2_rdata;
+ ex_rs1_addr <= id_rs1_addr;
+ ex_rs2_addr <= id_rs2_addr;
ex_imm <= id_imm;
ex_alu_op <= id_alu_op;
ex_alu_src1_sel <= id_alu_src1_sel;
diff --git a/rv64cpu/rtl/core/id_stage.sv b/rv64cpu/rtl/core/id_stage.sv
index 5e22662..27d3daf 100644
--- a/rv64cpu/rtl/core/id_stage.sv
+++ b/rv64cpu/rtl/core/id_stage.sv
@@ -38,6 +38,8 @@ module id_stage
output logic [XLEN-1:0] id_pc, // Pass-through PC for branch/jump calculation
output logic [XLEN-1:0] id_rs1_rdata, // Source register 1 data
output logic [XLEN-1:0] id_rs2_rdata, // Source register 2 data
+ output logic [4:0] id_rs1_addr, // Rs1 index (for Forwarding & CSR zimm)
+ output logic [4:0] id_rs2_addr, // Rs2 index (for Forwarding)
output logic [XLEN-1:0] id_imm, // Sign-extended generic immediate
// Execution Control (ALU & Branch)
@@ -74,8 +76,10 @@ module id_stage
logic [4:0] rs1_addr;
logic [4:0] rs2_addr;
- // Pass-through PC directly to next stage
+ // Pass-through PC and reg indices directly to next stage
assign id_pc = if_pc;
+ assign id_rs1_addr = rs1_addr;
+ assign id_rs2_addr = rs2_addr;
// Instruction Decoder Instantiation
diff --git a/rv64cpu/rtl/core/mem_stage.sv b/rv64cpu/rtl/core/mem_stage.sv
new file mode 100644
index 0000000..a63a456
--- /dev/null
+++ b/rv64cpu/rtl/core/mem_stage.sv
@@ -0,0 +1,214 @@
+// Richard CPU
+// Copyright (C) 2026 Richard Qin
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// File path: rtl/core/mem_stage.sv
+// Description: Memory Stage
+// Latches results from the EX stage and bypassed control signals from ID/EX.
+// Supports stall and flush operations.
+
+module mem_stage
+ import rv64_pkg::*;
+(
+ // Inputs from EX/MEM pipeline register
+ input logic [XLEN-1:0] mem_alu_result, // Used as memory address or ALU result
+ input logic mem_branch_taken,
+ input logic [XLEN-1:0] mem_branch_target,
+ input logic [XLEN-1:0] mem_rs1_rdata,
+ input logic [XLEN-1:0] mem_rs2_rdata, // Store data
+ input logic [4:0] mem_rs1_addr,
+ input logic [XLEN-1:0] mem_pc,
+ input logic [XLEN-1:0] mem_imm,
+
+ // Memory Control
+ input logic mem_is_load,
+ input logic mem_is_store,
+ input logic [2:0] mem_mem_size, // LB/LH/LW/LD/LBU/LHU/LWU
+
+ // Writeback Control
+ input logic mem_reg_write_en,
+ input logic [4:0] mem_rd_addr,
+ input logic [1:0] mem_wb_sel,
+
+ // Exceptions and System (bypassed through or generated)
+ input logic mem_is_csr,
+ input logic [2:0] mem_csr_op,
+ input logic mem_illegal_instr,
+ input logic mem_is_ecall,
+ input logic mem_is_ebreak,
+ input logic mem_is_mret,
+ input logic mem_is_sret,
+ input logic mem_is_wfi,
+ input logic mem_is_sfence_vma,
+ input logic mem_is_fence,
+ input logic mem_is_fence_i,
+
+ // Data Memory Interface (to Data Cache / Memory Controller)
+ output logic dmem_read_req,
+ output logic dmem_write_req,
+ output logic [XLEN-1:0] dmem_addr,
+ output logic [XLEN-1:0] dmem_wdata,
+ output logic [7:0] dmem_wstrb, // Write strobe depending on size
+ input logic dmem_req_ready, // Wait signal from D-Cache
+ input logic dmem_rsp_valid, // Data valid from D-Cache
+ input logic [XLEN-1:0] dmem_rdata,
+
+ // Outputs to MEM/WB Pipeline Register
+ output logic [XLEN-1:0] mem_wb_alu_result,
+ output logic [XLEN-1:0] mem_wb_mem_rdata,
+ output logic [XLEN-1:0] mem_wb_pc,
+
+ output logic mem_wb_reg_write_en,
+ output logic [4:0] mem_wb_rd_addr,
+ output logic [1:0] mem_wb_wb_sel,
+
+ output logic mem_wb_is_csr,
+ output logic [2:0] mem_wb_csr_op,
+ output logic [XLEN-1:0] mem_wb_rs1_rdata,
+ output logic [4:0] mem_wb_rs1_addr,
+ output logic [XLEN-1:0] mem_wb_imm,
+
+ output logic mem_wb_illegal_instr,
+ output logic mem_wb_is_ecall,
+ output logic mem_wb_is_ebreak,
+ output logic mem_wb_is_mret,
+ output logic mem_wb_is_sret,
+
+ // Control to Hazard Unit / Stall Logic
+ output logic mem_stall_req // Stall pipeline if D-Cache doesn't respond
+);
+
+ logic [XLEN-1:0] load_data_aligned;
+ logic [XLEN-1:0] store_data_aligned;
+ logic [7:0] write_strobe;
+
+ // --------------------------
+ // Memory Request Logic
+ assign dmem_read_req = mem_is_load;
+ assign dmem_write_req = mem_is_store;
+ assign dmem_addr = mem_alu_result; // ALU result is the computed address
+
+ // --------------------------
+ // Stall generation: Stall if memory request is not yet ready or valid
+ // For a simple synchronous SRAM, this might be 0, but for cache it will depend on hits/misses
+ // --------------------------
+ always_comb begin
+ if ((mem_is_load || mem_is_store) && (!dmem_req_ready || (mem_is_load && !dmem_rsp_valid))) begin
+ mem_stall_req = 1'b1;
+ end else begin
+ mem_stall_req = 1'b0;
+ end
+ end
+
+ // --------------------------
+ // Store Data Alignment and Strobe Generation
+ // --------------------------
+ always_comb begin
+ logic [2:0] align_offset;
+ store_data_aligned = '0;
+ write_strobe = 8'b0000_0000;
+
+ // Mem address lower 3 bits determine the alignment within the 64-bit word
+ align_offset = mem_alu_result[2:0];
+
+ if (mem_is_store) begin
+ case (mem_mem_size)
+ // SB: Store Byte
+ 3'b000: begin
+ store_data_aligned = {56'b0, mem_rs2_rdata[7:0]} << (align_offset * 8);
+ write_strobe = 8'b0000_0001 << align_offset;
+ end
+ // SH: Store Halfword
+ 3'b001: begin
+ store_data_aligned = {48'b0, mem_rs2_rdata[15:0]} << (align_offset * 8);
+ write_strobe = 8'b0000_0011 << align_offset;
+ end
+ // SW: Store Word
+ 3'b010: begin
+ store_data_aligned = {32'b0, mem_rs2_rdata[31:0]} << (align_offset * 8);
+ write_strobe = 8'b0000_1111 << align_offset;
+ end
+ // SD: Store Doubleword
+ 3'b011: begin
+ store_data_aligned = mem_rs2_rdata;
+ write_strobe = 8'b1111_1111;
+ end
+ default: begin
+ store_data_aligned = '0;
+ write_strobe = 8'b0000_0000;
+ end
+ endcase
+ end
+ end
+
+ assign dmem_wdata = store_data_aligned;
+ assign dmem_wstrb = write_strobe;
+
+ // --------------------------
+ // Load Data Alignment and Sign/Zero Extension
+ // --------------------------
+ always_comb begin
+ logic [2:0] align_offset;
+ logic [XLEN-1:0] shifted_rdata;
+ load_data_aligned = '0;
+
+ align_offset = mem_alu_result[2:0];
+
+ // Shift data down so the target bytes are at the bottom
+ shifted_rdata = dmem_rdata >> (align_offset * 8);
+
+ case (mem_mem_size)
+ // LB: Load Byte (Sign extend)
+ 3'b000: load_data_aligned = { {56{shifted_rdata[7]}}, shifted_rdata[7:0] };
+ // LH: Load Halfword (Sign extend)
+ 3'b001: load_data_aligned = { {48{shifted_rdata[15]}}, shifted_rdata[15:0] };
+ // LW: Load Word (Sign extend)
+ 3'b010: load_data_aligned = { {32{shifted_rdata[31]}}, shifted_rdata[31:0] };
+ // LD: Load Doubleword
+ 3'b011: load_data_aligned = shifted_rdata;
+ // LBU: Load Byte Unsigned (Zero extend)
+ 3'b100: load_data_aligned = { 56'b0, shifted_rdata[7:0] };
+ // LHU: Load Half Unsigned (Zero extend)
+ 3'b101: load_data_aligned = { 48'b0, shifted_rdata[15:0] };
+ // LWU: Load Word Unsigned (Zero extend)
+ 3'b110: load_data_aligned = { 32'b0, shifted_rdata[31:0] };
+ default: load_data_aligned = '0;
+ endcase
+ end
+
+ // --------------------------
+ // Pass Output Signals to WB Stage
+ // --------------------------
+ assign mem_wb_alu_result = mem_alu_result;
+ assign mem_wb_mem_rdata = load_data_aligned;
+ assign mem_wb_pc = mem_pc;
+
+ assign mem_wb_reg_write_en = mem_reg_write_en;
+ assign mem_wb_rd_addr = mem_rd_addr;
+ assign mem_wb_wb_sel = mem_wb_sel;
+
+ assign mem_wb_is_csr = mem_is_csr;
+ assign mem_wb_csr_op = mem_csr_op;
+ assign mem_wb_rs1_rdata = mem_rs1_rdata;
+ assign mem_wb_rs1_addr = mem_rs1_addr;
+ assign mem_wb_imm = mem_imm;
+
+ assign mem_wb_illegal_instr = mem_illegal_instr;
+ assign mem_wb_is_ecall = mem_is_ecall;
+ assign mem_wb_is_ebreak = mem_is_ebreak;
+ assign mem_wb_is_mret = mem_is_mret;
+ assign mem_wb_is_sret = mem_is_sret;
+
+endmodule
diff --git a/rv64cpu/rtl/core/mem_wb_reg.sv b/rv64cpu/rtl/core/mem_wb_reg.sv
new file mode 100644
index 0000000..a70057c
--- /dev/null
+++ b/rv64cpu/rtl/core/mem_wb_reg.sv
@@ -0,0 +1,132 @@
+// Richard CPU
+// Copyright (C) 2026 Richard Qin
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// File path: rtl/core/mem_wb_reg.sv
+// Description: MEM/WB pipeline stage register (Memory Access to Write Back)
+// Passes the final computation results, memory read data, and write-back control signals to the write-back stage
+
+module mem_wb_reg
+ import rv64_pkg::*;
+(
+ input logic clk,
+ input logic rst,
+
+ // -----------------------
+ // Control Signals from Hazard Unit
+ input logic stall_en,
+ input logic flush_en,
+
+ // -----------------------
+ // Inputs from MEM stage
+ // -----------------------
+ // Data and addresses
+ input logic [XLEN-1:0] mem_pc,
+ input logic [XLEN-1:0] mem_alu_result,
+ input logic [XLEN-1:0] mem_mem_rdata,
+
+ // Write back control
+ input logic mem_reg_write_en,
+ input logic [4:0] mem_rd_addr,
+ input logic [1:0] mem_wb_sel,
+
+ // CSR & System
+ input logic mem_is_csr,
+ input logic [2:0] mem_csr_op,
+ input logic [XLEN-1:0] mem_rs1_rdata,
+ input logic [4:0] mem_rs1_addr,
+ input logic [XLEN-1:0] mem_imm,
+ input logic mem_illegal_instr,
+ input logic mem_is_ecall,
+ input logic mem_is_ebreak,
+ input logic mem_is_mret,
+ input logic mem_is_sret,
+
+ // -----------------------
+ // Outputs to WB Stage
+ // -----------------------
+ // Data and address
+ output logic [XLEN-1:0] wb_pc,
+ output logic [XLEN-1:0] wb_alu_result,
+ output logic [XLEN-1:0] wb_mem_rdata,
+
+ // Write back control
+ output logic wb_reg_write_en,
+ output logic [4:0] wb_rd_addr,
+ output logic [1:0] wb_wb_sel,
+
+ // CSR & System
+ output logic wb_is_csr,
+ output logic [2:0] wb_csr_op,
+ output logic [XLEN-1:0] wb_rs1_rdata,
+ output logic [4:0] wb_rs1_addr,
+ output logic [XLEN-1:0] wb_imm,
+ output logic wb_illegal_instr,
+ output logic wb_is_ecall,
+ output logic wb_is_ebreak,
+ output logic wb_is_mret,
+ output logic wb_is_sret
+
+);
+ // Sequential Logic: State Update
+ always_ff @(posedge clk) begin
+ if (rst) begin
+ wb_pc <= '0;
+ wb_alu_result <= '0;
+ wb_mem_rdata <= '0;
+ wb_reg_write_en <= '0;
+ wb_rd_addr <= '0;
+ wb_wb_sel <= '0;
+ wb_is_csr <= '0;
+ wb_csr_op <= '0;
+ wb_rs1_rdata <= '0;
+ wb_rs1_addr <= '0;
+ wb_imm <= '0;
+ wb_illegal_instr <= '0;
+ wb_is_ecall <= '0;
+ wb_is_ebreak <= '0;
+ wb_is_mret <= '0;
+ wb_is_sret <= '0;
+ end else if (flush_en) begin
+ // Target specific control signals to clear. Leave datapath elements intact
+ // to avoid extreme power consumption from massive flip-flop state changes array.
+ wb_reg_write_en <= '0;
+ wb_is_csr <= '0;
+ wb_illegal_instr <= '0;
+ wb_is_ecall <= '0;
+ wb_is_ebreak <= '0;
+ wb_is_mret <= '0;
+ wb_is_sret <= '0;
+ end else if (!stall_en) begin
+ wb_pc <= mem_pc;
+ wb_alu_result <= mem_alu_result;
+ wb_mem_rdata <= mem_mem_rdata;
+ wb_reg_write_en <= mem_reg_write_en;
+ wb_rd_addr <= mem_rd_addr;
+ wb_wb_sel <= mem_wb_sel;
+ wb_is_csr <= mem_is_csr;
+ wb_csr_op <= mem_csr_op;
+ wb_rs1_rdata <= mem_rs1_rdata;
+ wb_rs1_addr <= mem_rs1_addr;
+ wb_imm <= mem_imm;
+ wb_illegal_instr <= mem_illegal_instr;
+ wb_is_ecall <= mem_is_ecall;
+ wb_is_ebreak <= mem_is_ebreak;
+ wb_is_mret <= mem_is_mret;
+ wb_is_sret <= mem_is_sret;
+ end
+ end
+
+endmodule
diff --git a/rv64cpu/tb/CMakeLists.txt b/rv64cpu/tb/CMakeLists.txt
index cd419f9..7ca5bf0 100644
--- a/rv64cpu/tb/CMakeLists.txt
+++ b/rv64cpu/tb/CMakeLists.txt
@@ -114,6 +114,33 @@ add_verilator_target(
INCLUDE_DIRS ${RTL_COMMON_INCLUDES}
TB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/unit/tb_ex_stage.cc
)
+
+add_verilator_target(
+ NAME tb_ex_mem_reg
+ TOP_MODULE ex_mem_reg
+ SOURCES ${RTL_CORE_DIR}/include/rv64_pkg.sv
+ ${RTL_CORE_DIR}/ex_mem_reg.sv
+ INCLUDE_DIRS ${RTL_COMMON_INCLUDES}
+ TB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/unit/tb_ex_mem_reg.cc
+)
+
+add_verilator_target(
+ NAME tb_mem_stage
+ TOP_MODULE mem_stage
+ SOURCES ${RTL_CORE_DIR}/include/rv64_pkg.sv
+ ${RTL_CORE_DIR}/mem_stage.sv
+ INCLUDE_DIRS ${RTL_COMMON_INCLUDES}
+ TB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/unit/tb_mem_stage.cc
+)
+
+add_verilator_target(
+ NAME tb_mem_wb_reg
+ TOP_MODULE mem_wb_reg
+ SOURCES ${RTL_CORE_DIR}/include/rv64_pkg.sv
+ ${RTL_CORE_DIR}/mem_wb_reg.sv
+ INCLUDE_DIRS ${RTL_COMMON_INCLUDES}
+ TB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/unit/tb_mem_wb_reg.cc
+)
#
# add_verilator_target(
# NAME tb_csr
diff --git a/rv64cpu/tb/unit/tb_ex_mem_reg.cc b/rv64cpu/tb/unit/tb_ex_mem_reg.cc
new file mode 100644
index 0000000..75e5502
--- /dev/null
+++ b/rv64cpu/tb/unit/tb_ex_mem_reg.cc
@@ -0,0 +1,182 @@
+// Richard CPU
+// Copyright (C) 2026 Richard Qin
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// File path: tb/unit/tb_ex_mem_reg.cc
+// Description: Execution/Memory Stage Pipeline Register unit test
+
+#include <iostream>
+#include <cassert>
+#include <verilated.h>
+#include "Vex_mem_reg.h"
+
+int pass_count = 0;
+
+void tick(Vex_mem_reg* dut) {
+ dut->clk = 0;
+ dut->eval();
+ dut->clk = 1;
+ dut->eval();
+ dut->clk = 0;
+ dut->eval();
+}
+
+void check(bool cond, const char* name) {
+ if (cond) {
+ std::cout << "\033[32m[PASS] " << name << "\033[0m" << std::endl;
+ pass_count++;
+ } else {
+ std::cout << "\033[31m[FAIL] " << name << "\033[0m" << std::endl;
+ assert(false);
+ }
+}
+
+int main(int argc, char** argv) {
+ Verilated::commandArgs(argc, argv);
+ Vex_mem_reg* dut = new Vex_mem_reg;
+
+ std::cout << "----------------------------------" << std::endl;
+ std::cout << " EX/MEM Register Test" << std::endl;
+ std::cout << "----------------------------------" << std::endl;
+
+ // 1. Init / Reset
+ dut->stall_en = 0;
+ dut->flush_en = 0;
+
+ // Set some random dangerous EX inputs
+ dut->ex_pc = 0x80001000;
+ dut->ex_rs1_rdata = 0x11111111;
+ dut->ex_rs2_rdata = 0x22222222;
+ dut->ex_rs1_addr = 31;
+ dut->ex_alu_result = 0x88888888;
+ dut->ex_branch_taken = 1;
+ dut->ex_branch_target = 0x80001020;
+ dut->ex_is_store = 1;
+ dut->ex_is_load = 1;
+ dut->ex_reg_write_en = 1;
+ dut->ex_is_csr = 1;
+ dut->ex_illegal_instr = 1;
+
+ dut->rst = 1;
+ tick(dut);
+ dut->rst = 0;
+
+ // Verify reset clears out dangerous signals
+ check(dut->mem_pc == 0, "Reset_Clears_PC");
+ check(dut->mem_branch_taken == 0, "Reset_Clears_BranchTaken");
+ check(dut->mem_reg_write_en == 0, "Reset_Clears_RegWrite");
+ check(dut->mem_is_store == 0, "Reset_Clears_IsStore");
+ check(dut->mem_is_load == 0, "Reset_Clears_IsLoad");
+ check(dut->mem_is_csr == 0, "Reset_Clears_IsCSR");
+ check(dut->mem_illegal_instr == 0, "Reset_Clears_IllegalInstr");
+
+ // 2. Normal Propagation
+ dut->ex_pc = 0x80002000;
+ dut->ex_rs1_rdata = 0xAAAAAAAA;
+ dut->ex_rs2_rdata = 0xBBBBBBBB;
+ dut->ex_rs1_addr = 15;
+ dut->ex_alu_result = 0xCAFEBABE;
+ dut->ex_branch_taken = 1;
+ dut->ex_branch_target = 0x80002008;
+ dut->ex_is_store = 0;
+ dut->ex_is_load = 1;
+ dut->ex_reg_write_en = 1;
+ dut->ex_rd_addr = 15;
+ dut->ex_wb_sel = 1;
+
+ tick(dut);
+
+ check(dut->mem_pc == 0x80002000, "Propagate_PC");
+ check(dut->mem_rs1_rdata == 0xAAAAAAAA, "Propagate_RS1");
+ check(dut->mem_rs2_rdata == 0xBBBBBBBB, "Propagate_RS2");
+ check(dut->mem_rs1_addr == 15, "Propagate_RS1_Addr");
+ check(dut->mem_alu_result == 0xCAFEBABE, "Propagate_ALU_Result");
+ check(dut->mem_branch_taken == 1, "Propagate_BranchTaken");
+ check(dut->mem_branch_target == 0x80002008, "Propagate_BranchTarget");
+ check(dut->mem_is_load == 1, "Propagate_IsLoad");
+ check(dut->mem_reg_write_en == 1, "Propagate_RegWriteEn");
+ check(dut->mem_rd_addr == 15, "Propagate_RdAddr");
+ check(dut->mem_wb_sel == 1, "Propagate_WbSel");
+
+ // 3. Stall (Freeze)
+ // Change input, but assert stall
+ dut->stall_en = 1;
+ dut->ex_pc = 0x80003000;
+ dut->ex_rs1_rdata = 0xCCCCCCCC;
+ dut->ex_rs1_addr = 20;
+ dut->ex_branch_taken = 0;
+ dut->ex_rd_addr = 20;
+ dut->ex_reg_write_en = 0;
+ dut->ex_is_load = 0;
+
+ tick(dut);
+
+ check(dut->mem_pc == 0x80002000, "Stall_Maintains_PC");
+ check(dut->mem_rs1_rdata == 0xAAAAAAAA, "Stall_Maintains_RS1");
+ check(dut->mem_rs1_addr == 15, "Stall_Maintains_RS1_Addr");
+ check(dut->mem_branch_taken == 1, "Stall_Maintains_BranchTaken");
+ check(dut->mem_rd_addr == 15, "Stall_Maintains_RdAddr");
+ check(dut->mem_reg_write_en == 1, "Stall_Maintains_RegWriteEn");
+ check(dut->mem_is_load == 1, "Stall_Maintains_IsLoad");
+
+ // Release Stall
+ dut->stall_en = 0;
+ tick(dut);
+ check(dut->mem_pc == 0x80003000, "ReleaseStall_Captures_PC");
+ check(dut->mem_rs1_rdata == 0xCCCCCCCC, "ReleaseStall_Captures_RS1");
+ check(dut->mem_rs1_addr == 20, "ReleaseStall_Captures_RS1_Addr");
+ check(dut->mem_branch_taken == 0, "ReleaseStall_Captures_BranchTaken");
+ check(dut->mem_rd_addr == 20, "ReleaseStall_Captures_RdAddr");
+ check(dut->mem_reg_write_en == 0, "ReleaseStall_Captures_RegWriteEn");
+
+ // 4. Flush (Insert Bubble/NOP)
+ // Change input, but assert flush
+ dut->flush_en = 1;
+ dut->ex_pc = 0x80004000;
+ dut->ex_branch_taken = 1;
+ dut->ex_reg_write_en = 1;
+ dut->ex_is_store = 1;
+
+ tick(dut);
+
+ check(dut->mem_pc == 0x80003000, "Flush_Maintains_PC");
+ check(dut->mem_branch_taken == 0, "Flush_Clears_BranchTaken");
+ check(dut->mem_reg_write_en == 0, "Flush_Clears_RegWrite");
+ check(dut->mem_is_store == 0, "Flush_Clears_StoreEn");
+
+ // 5. Flush Overrides Stall
+ dut->flush_en = 0;
+ dut->ex_pc = 0x80005000;
+ dut->ex_reg_write_en = 1;
+ tick(dut);
+ check(dut->mem_pc == 0x80005000, "Pre_FlushStall_Setup");
+
+ dut->flush_en = 1;
+ dut->stall_en = 1;
+ dut->ex_pc = 0x80006000;
+ dut->ex_branch_taken = 1;
+ tick(dut);
+
+ check(dut->mem_pc == 0x80005000, "Flush_Overrides_Stall_PC");
+ check(dut->mem_branch_taken == 0, "Flush_Overrides_Stall_BranchTaken");
+ check(dut->mem_reg_write_en == 0, "Flush_Overrides_Stall_RegWrite");
+ check(dut->mem_is_store == 0, "Flush_Overrides_Stall_Store");
+
+ std::cout << "----------------------------------" << std::endl;
+ std::cout << "\033[32mAll " << pass_count << " tests passed!\033[0m" << std::endl;
+ std::cout << "----------------------------------" << std::endl;
+
+ delete dut;
+ return 0;
+}
diff --git a/rv64cpu/tb/unit/tb_id_ex_reg.cc b/rv64cpu/tb/unit/tb_id_ex_reg.cc
index ad58a87..d9cc329 100644
--- a/rv64cpu/tb/unit/tb_id_ex_reg.cc
+++ b/rv64cpu/tb/unit/tb_id_ex_reg.cc
@@ -64,6 +64,8 @@ int main(int argc, char** argv) {
dut->id_pc = 0x80001000;
dut->id_rs1_rdata = 0x11111111;
dut->id_rs2_rdata = 0x22222222;
+ dut->id_rs1_addr = 31;
+ dut->id_rs2_addr = 15;
dut->id_imm = 0x1A2B3C;
dut->id_alu_op = ALU_XOR;
dut->id_reg_write_en = 1;
@@ -92,6 +94,8 @@ int main(int argc, char** argv) {
dut->id_pc = 0x80002000;
dut->id_rs1_rdata = 0xAAAAAAAA;
dut->id_rs2_rdata = 0xBBBBBBBB;
+ dut->id_rs1_addr = 10;
+ dut->id_rs2_addr = 11;
dut->id_imm = 42;
dut->id_alu_op = ALU_ADD;
dut->id_reg_write_en = 1;
@@ -104,6 +108,8 @@ int main(int argc, char** argv) {
check(dut->ex_pc == 0x80002000, "Propagate_PC");
check(dut->ex_rs1_rdata == 0xAAAAAAAA, "Propagate_RS1");
check(dut->ex_rs2_rdata == 0xBBBBBBBB, "Propagate_RS2");
+ check(dut->ex_rs1_addr == 10, "Propagate_RS1_Addr");
+ check(dut->ex_rs2_addr == 11, "Propagate_RS2_Addr");
check(dut->ex_imm == 42, "Propagate_IMM");
check(dut->ex_alu_op == ALU_ADD, "Propagate_ALU_OP");
check(dut->ex_reg_write_en == 1, "Propagate_RegWriteEn");
@@ -142,7 +148,7 @@ int main(int argc, char** argv) {
tick(dut);
- check(dut->ex_pc == 0x0, "Flush_Clears_PC");
+ check(dut->ex_pc == 0x80003000, "Flush_Maintains_PC");
check(dut->ex_reg_write_en == 0, "Flush_Clears_RegWrite");
check(dut->ex_is_store == 0, "Flush_Clears_StoreEn");
check(dut->ex_is_branch == 0, "Flush_Clears_BranchEn");
@@ -159,7 +165,7 @@ int main(int argc, char** argv) {
dut->id_pc = 0x80006000;
tick(dut);
- check(dut->ex_pc == 0x0, "Flush_Overrides_Stall_PC");
+ check(dut->ex_pc == 0x80005000, "Flush_Overrides_Stall_PC");
check(dut->ex_reg_write_en == 0, "Flush_Overrides_Stall_RegWrite");
std::cout << "----------------------------------" << std::endl;
diff --git a/rv64cpu/tb/unit/tb_id_stage.cc b/rv64cpu/tb/unit/tb_id_stage.cc
index 0d3aa71..26dfd97 100644
--- a/rv64cpu/tb/unit/tb_id_stage.cc
+++ b/rv64cpu/tb/unit/tb_id_stage.cc
@@ -107,7 +107,9 @@ int main(int argc, char** argv) {
// rs1=x5, rs2=x0, rd=x1
dut->if_instr = 0x000280B3;
dut->eval();
- check(dut->id_rs1_rdata == 0xDEADBEEF, "RegFile_Read_x5");
+ check(dut->id_rs1_addr == 5, "R_Type_rs1_addr");
+ check(dut->id_rs2_addr == 0, "R_Type_rs2_addr");
+ check(dut->id_rs1_rdata == 0xDEADBEEF, "RegFile_Read_x5");
check(dut->id_rs2_rdata == 0, "RegFile_Read_x0");
check(dut->id_alu_src2_sel == 0, "R_Type_alu_src2_sel");
diff --git a/rv64cpu/tb/unit/tb_mem_stage.cc b/rv64cpu/tb/unit/tb_mem_stage.cc
new file mode 100644
index 0000000..2747ca9
--- /dev/null
+++ b/rv64cpu/tb/unit/tb_mem_stage.cc
@@ -0,0 +1,210 @@
+// Richard CPU
+// Copyright (C) 2026 Richard Qin
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// File path: tb/unit/tb_mem_stage.cc
+// Description: Memory Stage unit test
+
+#include <iostream>
+#include <cstdint>
+#include <cassert>
+#include <verilated.h>
+#include "Vmem_stage.h"
+
+int pass_count = 0;
+
+void tick(Vmem_stage* dut) {
+ dut->eval();
+}
+
+void check(bool cond, const char* name) {
+ if (cond) {
+ std::cout << "\033[32m[PASS] " << name << "\033[0m" << std::endl;
+ pass_count++;
+ } else {
+ std::cout << "\033[31m[FAIL] " << name << "\033[0m" << std::endl;
+ assert(false);
+ }
+}
+
+int main(int argc, char** argv) {
+ Verilated::commandArgs(argc, argv);
+ Vmem_stage* dut = new Vmem_stage;
+
+ std::cout << "----------------------------------" << std::endl;
+ std::cout << " MEM Stage Test" << std::endl;
+ std::cout << "----------------------------------" << std::endl;
+
+ // 1. Initial / Default states
+ dut->mem_alu_result = 0;
+ dut->mem_rs2_rdata = 0;
+ dut->mem_is_load = 0;
+ dut->mem_is_store = 0;
+ dut->mem_mem_size = 0;
+ dut->dmem_req_ready = 1;
+ dut->dmem_rsp_valid = 1;
+ dut->dmem_rdata = 0;
+
+ tick(dut);
+
+ check(dut->dmem_read_req == 0, "No_Read_Req");
+ check(dut->dmem_write_req == 0, "No_Write_Req");
+ check(dut->mem_stall_req == 0, "No_Stall");
+
+ // 2. Passthrough signals
+ dut->mem_reg_write_en = 1;
+ dut->mem_rd_addr = 15;
+ dut->mem_wb_sel = 2; // PC+4
+ dut->mem_pc = 0x80001000;
+ dut->mem_is_csr = 1;
+ dut->mem_csr_op = 2; // CSRRS
+ dut->mem_rs1_rdata = 0x1234;
+ dut->mem_rs1_addr = 5;
+ dut->mem_imm = 0x777;
+
+ tick(dut);
+
+ check(dut->mem_wb_reg_write_en == 1, "Passthrough_RegWriteEn");
+ check(dut->mem_wb_rd_addr == 15, "Passthrough_RdAddr");
+ check(dut->mem_wb_wb_sel == 2, "Passthrough_WbSel");
+ check(dut->mem_wb_pc == 0x80001000, "Passthrough_PC");
+ check(dut->mem_wb_is_csr == 1, "Passthrough_IsCSR");
+ check(dut->mem_wb_csr_op == 2, "Passthrough_CSROp");
+ check(dut->mem_wb_rs1_rdata == 0x1234, "Passthrough_RS1Rdata");
+ check(dut->mem_wb_rs1_addr == 5, "Passthrough_RS1Addr");
+ check(dut->mem_wb_imm == 0x777, "Passthrough_Imm");
+
+ // 3. Store Alignment (SB, SH, SW, SD)
+ dut->mem_is_store = 1;
+ dut->dmem_req_ready = 1;
+ dut->mem_rs2_rdata = 0x1122334455667788ULL;
+
+ // SB at offset 0
+ dut->mem_mem_size = 0; // SB
+ dut->mem_alu_result = 0x1000;
+ tick(dut);
+ check(dut->dmem_write_req == 1, "Store_Req");
+ check(dut->dmem_addr == 0x1000, "Store_Addr");
+ check(dut->dmem_wdata == 0x88, "SB_Data_0");
+ check(dut->dmem_wstrb == 0x01, "SB_Strobe_0");
+
+ // SB at offset 3
+ dut->mem_alu_result = 0x1003;
+ tick(dut);
+ check(dut->dmem_wdata == 0x88000000ULL, "SB_Data_3");
+ check(dut->dmem_wstrb == 0x08, "SB_Strobe_3");
+
+ // SH at offset 2
+ dut->mem_mem_size = 1; // SH
+ dut->mem_alu_result = 0x1002;
+ tick(dut);
+ check(dut->dmem_wdata == 0x77880000ULL, "SH_Data_2");
+ check(dut->dmem_wstrb == 0x0C, "SH_Strobe_2");
+
+ // SW at offset 4
+ dut->mem_mem_size = 2; // SW
+ dut->mem_alu_result = 0x1004;
+ tick(dut);
+ check(dut->dmem_wdata == 0x5566778800000000ULL, "SW_Data_4");
+ check(dut->dmem_wstrb == 0xF0, "SW_Strobe_4");
+
+ // SD
+ dut->mem_mem_size = 3; // SD
+ dut->mem_alu_result = 0x1000;
+ tick(dut);
+ check(dut->dmem_wdata == 0x1122334455667788ULL, "SD_Data");
+ check(dut->dmem_wstrb == 0xFF, "SD_Strobe");
+
+ dut->mem_is_store = 0;
+
+ // 4. Load Alignment and Extension (LB, LH, LW, LD, LBU, LHU, LWU)
+ dut->mem_is_load = 1;
+ dut->dmem_rdata = 0xAABBCCDDEEFF0011ULL;
+ dut->dmem_rsp_valid = 1;
+
+ // LB at offset 0 (0x11) -> sign extended
+ dut->mem_mem_size = 0; // LB
+ dut->mem_alu_result = 0x2000;
+ tick(dut);
+ check(dut->dmem_read_req == 1, "Load_Req");
+ check(dut->mem_wb_mem_rdata == 0x11, "LB_Data_0_Pos");
+
+ // LB at offset 1 (0x00)
+ dut->mem_alu_result = 0x2001;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0x0, "LB_Data_1_Zero");
+
+ // LB at offset 7 (0xAA) -> sign extended
+ dut->mem_alu_result = 0x2007;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0xFFFFFFFFFFFFFFAAULL, "LB_Data_7_Neg");
+
+ // LBU at offset 7 (0xAA) -> zero extended
+ dut->mem_mem_size = 4; // LBU
+ dut->mem_alu_result = 0x2007;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0xAA, "LBU_Data_7");
+
+ // LH at offset 2 (0xEEFF) -> sign extended
+ dut->mem_mem_size = 1; // LH
+ dut->mem_alu_result = 0x2002;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0xFFFFFFFFFFFFEEFFULL, "LH_Data_2_Neg");
+
+ // LHU at offset 2 (0xEEFF) -> zero extended
+ dut->mem_mem_size = 5; // LHU
+ dut->mem_alu_result = 0x2002;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0xEEFF, "LHU_Data_2");
+
+ // LW at offset 4 (0xAABBCCDD) -> sign extended
+ dut->mem_mem_size = 2; // LW
+ dut->mem_alu_result = 0x2004;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0xFFFFFFFFAABBCCDDULL, "LW_Data_4_Neg");
+
+ // LWU at offset 4 (0xAABBCCDD) -> zero extended
+ dut->mem_mem_size = 6; // LWU
+ dut->mem_alu_result = 0x2004;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0xAABBCCDD, "LWU_Data_4");
+
+ // LD
+ dut->mem_mem_size = 3; // LD
+ dut->mem_alu_result = 0x2000;
+ tick(dut);
+ check(dut->mem_wb_mem_rdata == 0xAABBCCDDEEFF0011ULL, "LD_Data");
+
+ // 5. Stall Logic
+ dut->dmem_req_ready = 0; // Cache miss / AXI busy
+ dut->dmem_rsp_valid = 0;
+ tick(dut);
+ check(dut->mem_stall_req == 1, "Stall_On_Req_NotReady");
+
+ dut->dmem_req_ready = 1;
+ dut->dmem_rsp_valid = 0; // Read request accepted, but data not valid yet
+ tick(dut);
+ check(dut->mem_stall_req == 1, "Stall_On_Rsp_NotValid");
+
+ dut->dmem_rsp_valid = 1; // Read response arrived
+ tick(dut);
+ check(dut->mem_stall_req == 0, "No_Stall_On_RspValid");
+
+ std::cout << "----------------------------------" << std::endl;
+ std::cout << "\033[32mAll " << pass_count << " tests passed!\033[0m" << std::endl;
+ std::cout << "----------------------------------" << std::endl;
+
+ delete dut;
+ return 0;
+}
diff --git a/rv64cpu/tb/unit/tb_mem_wb_reg.cc b/rv64cpu/tb/unit/tb_mem_wb_reg.cc
new file mode 100644
index 0000000..e91a354
--- /dev/null
+++ b/rv64cpu/tb/unit/tb_mem_wb_reg.cc
@@ -0,0 +1,165 @@
+// Richard CPU
+// Copyright (C) 2026 Richard Qin
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// File path: tb/unit/tb_mem_wb_reg.cc
+// Description: MEM/WB Stage Register unit test
+
+#include <iostream>
+#include <cassert>
+#include <verilated.h>
+#include "Vmem_wb_reg.h"
+
+int pass_count = 0;
+
+void tick(Vmem_wb_reg* dut) {
+ dut->clk = 0;
+ dut->eval();
+ dut->clk = 1;
+ dut->eval();
+ dut->clk = 0;
+ dut->eval();
+}
+
+void check(bool cond, const char* name) {
+ if (cond) {
+ std::cout << "\033[32m[PASS] " << name << "\033[0m" << std::endl;
+ pass_count++;
+ } else {
+ std::cout << "\033[31m[FAIL] " << name << "\033[0m" << std::endl;
+ assert(false);
+ }
+}
+
+int main(int argc, char** argv) {
+ Verilated::commandArgs(argc, argv);
+ Vmem_wb_reg* dut = new Vmem_wb_reg;
+
+ std::cout << "----------------------------------" << std::endl;
+ std::cout << " MEM/WB Register Test" << std::endl;
+ std::cout << "----------------------------------" << std::endl;
+
+ // 1. Init / Reset
+ dut->stall_en = 0;
+ dut->flush_en = 0;
+
+ // Set some dangerous inputs
+ dut->mem_pc = 0x80001000;
+ dut->mem_alu_result = 0x11111111;
+ dut->mem_mem_rdata = 0x22222222;
+ dut->mem_reg_write_en = 1;
+ dut->mem_rd_addr = 31;
+ dut->mem_wb_sel = 1;
+ dut->mem_is_csr = 1;
+ dut->mem_csr_op = 2;
+ dut->mem_rs1_rdata = 0x999;
+ dut->mem_rs1_addr = 5;
+ dut->mem_imm = 0x333;
+ dut->mem_is_ecall = 1;
+ dut->mem_illegal_instr = 1;
+ dut->mem_is_sret = 1;
+
+ dut->rst = 1;
+ tick(dut);
+ dut->rst = 0;
+
+ check(dut->wb_pc == 0, "Reset_Clears_PC");
+ check(dut->wb_alu_result == 0, "Reset_Clears_ALU");
+ check(dut->wb_reg_write_en == 0, "Reset_Clears_RegWrite");
+ check(dut->wb_is_csr == 0, "Reset_Clears_IsCSR");
+ check(dut->wb_illegal_instr == 0, "Reset_Clears_IllegalInstr");
+
+ // 2. Normal Propagation
+ dut->mem_pc = 0x80002000;
+ dut->mem_alu_result = 0xAAAAAAAA;
+ dut->mem_mem_rdata = 0xBBBBBBBB;
+ dut->mem_rd_addr = 15;
+ dut->mem_reg_write_en = 1;
+ dut->mem_wb_sel = 1;
+ dut->mem_is_csr = 1;
+ dut->mem_csr_op = 3;
+ dut->mem_rs1_rdata = 0x777;
+ dut->mem_rs1_addr = 6;
+ dut->mem_imm = 0x888;
+ dut->mem_is_ecall = 0;
+
+ tick(dut);
+
+ check(dut->wb_pc == 0x80002000, "Propagate_PC");
+ check(dut->wb_alu_result == 0xAAAAAAAA, "Propagate_ALU_Result");
+ check(dut->wb_mem_rdata == 0xBBBBBBBB, "Propagate_MemRData");
+ check(dut->wb_rd_addr == 15, "Propagate_RdAddr");
+ check(dut->wb_reg_write_en == 1, "Propagate_RegWriteEn");
+ check(dut->wb_wb_sel == 1, "Propagate_WbSel");
+ check(dut->wb_is_csr == 1, "Propagate_IsCSR");
+ check(dut->wb_csr_op == 3, "Propagate_CSROp");
+ check(dut->wb_rs1_rdata == 0x777, "Propagate_RS1_RData");
+ check(dut->wb_rs1_addr == 6, "Propagate_RS1_Addr");
+ check(dut->wb_imm == 0x888, "Propagate_Imm");
+
+ // 3. Stall (Freeze)
+ dut->stall_en = 1;
+ dut->mem_pc = 0x80003000;
+ dut->mem_rd_addr = 20;
+ dut->mem_reg_write_en = 0;
+
+ tick(dut);
+
+ check(dut->wb_pc == 0x80002000, "Stall_Maintains_PC");
+ check(dut->wb_rd_addr == 15, "Stall_Maintains_RdAddr");
+ check(dut->wb_reg_write_en == 1, "Stall_Maintains_RegWriteEn");
+
+ // Release Stall
+ dut->stall_en = 0;
+ tick(dut);
+ check(dut->wb_pc == 0x80003000, "ReleaseStall_Captures_PC");
+ check(dut->wb_rd_addr == 20, "ReleaseStall_Captures_RdAddr");
+ check(dut->wb_reg_write_en == 0, "ReleaseStall_Captures_RegWriteEn");
+
+ // 4. Flush (Insert Bubble/NOP)
+ dut->flush_en = 1;
+ dut->mem_pc = 0x80004000;
+ dut->mem_alu_result = 0xCCCCCCCC;
+ dut->mem_reg_write_en = 1;
+ dut->mem_is_csr = 1;
+
+ tick(dut);
+
+ check(dut->wb_pc == 0x80003000, "Flush_Maintains_PC");
+ check(dut->wb_alu_result == 0xAAAAAAAA, "Flush_Maintains_ALU");
+ check(dut->wb_reg_write_en == 0, "Flush_Clears_RegWrite");
+ check(dut->wb_is_csr == 0, "Flush_Clears_IsCSR");
+
+ // 5. Flush Overrides Stall
+ dut->flush_en = 0;
+ dut->mem_pc = 0x80005000;
+ dut->mem_reg_write_en = 1;
+ tick(dut);
+ check(dut->wb_pc == 0x80005000, "Pre_FlushStall_Setup");
+
+ dut->flush_en = 1;
+ dut->stall_en = 1;
+ dut->mem_pc = 0x80006000;
+ tick(dut);
+
+ check(dut->wb_pc == 0x80005000, "Flush_Overrides_Stall_PC");
+ check(dut->wb_reg_write_en == 0, "Flush_Overrides_Stall_RegWrite");
+
+ std::cout << "----------------------------------" << std::endl;
+ std::cout << "\033[32mAll " << pass_count << " tests passed!\033[0m" << std::endl;
+ std::cout << "----------------------------------" << std::endl;
+
+ delete dut;
+ return 0;
+}