← Back to Pull requests

Add instruction cache and store-load forwarding #18

Closed
spr4bhu closed this pull request without merging on Feb 24, 2026 17:44 UTC
View on GitHub
spr4bhu spr4bhu commented on Dec 24, 2025 23:48 UTC
Author

Add Instruction Cache and Store-Load Forwarding

Changes

- Instruction Cache: 4-way set-associative cache (4KB) with round-robin replacement and FENCE.I support
- Store-Load Forwarding: Detects memory hazards and forwards store data directly to loads, bypassing memory
- Tests: Added tests for both features (test_icache.py, test_store_load.py)

The instruction cache reduces fetch latency for frequently executed code. Store-load forwarding ensures memory consistency
without pipeline stalls.

Diff: 1852 lines (+1427 / -425) across 18 files Reviewer: 5iri Review duration: 88916m
All Vouch review confidence checks passed
Residual risk 0.14 = Change Risk (0.72) × (1 − Review Confidence (0.80)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.72 · 1852 lines diff across 18 files
0.72
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.72 · 5 substantive comment(s)
0.72
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
80%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.400
Mass File Touch mass_file_touch
0.080
Real PR Conversation Timeline
33 items
5iri 5iri commented on code on Dec 31, 2025 15:00 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -382,21 +398,22 @@ module riscv_cpu (
     wire mem_wb_inst0_rd_valid_out;
     wire [31:0] mem_wb_inst0_mem_data_out;
 
-    // Add wires for store-load forwarding
     wire store_load_hazard;
     wire [31:0] forwarded_store_data;
 
-    // Instantiate store-load hazard detector
     store_load_detector store_load_detector_inst0 (
         .load_instr_id(ex_mem_inst0_instr_id_out),
         .load_addr(ex_mem_inst0_mem_addr_out),
         .prev_store_instr_id(mem_wb_inst0_instr_id_out),
         .prev_store_addr(mem_wb_inst0_mem_addr_out),
+        .rs2_value(mem_wb_inst0_rs2_value_out),
         .store_load_hazard(store_load_hazard),
-        .forwarded_data(forwarded_store_data),
-        .rs2_value(ex_mem_inst0_rs2_value_out)  // Forwarded store data
+        .forwarded_data(forwarded_store_data)
     );
 
+    wire [31:0] mem_data_to_wb;
+    assign mem_data_to_wb = store_load_hazard ? forwarded_store_data : module_read_data_in;

this would feed the data to all addresses, not only RAM. but since will be using MMIO as well, this will be problematic. Add a mux select to keep it only in the RAM region.

5iri 5iri commented on code on Dec 31, 2025 15:07 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -382,21 +398,22 @@ module riscv_cpu (
     wire mem_wb_inst0_rd_valid_out;
     wire [31:0] mem_wb_inst0_mem_data_out;
 
-    // Add wires for store-load forwarding
     wire store_load_hazard;
     wire [31:0] forwarded_store_data;
 
-    // Instantiate store-load hazard detector
     store_load_detector store_load_detector_inst0 (
         .load_instr_id(ex_mem_inst0_instr_id_out),
         .load_addr(ex_mem_inst0_mem_addr_out),
         .prev_store_instr_id(mem_wb_inst0_instr_id_out),
         .prev_store_addr(mem_wb_inst0_mem_addr_out),
+        .rs2_value(mem_wb_inst0_rs2_value_out),
         .store_load_hazard(store_load_hazard),
-        .forwarded_data(forwarded_store_data),
-        .rs2_value(ex_mem_inst0_rs2_value_out)  // Forwarded store data
+        .forwarded_data(forwarded_store_data)
     );
 
+    wire [31:0] mem_data_to_wb;
+    assign mem_data_to_wb = store_load_hazard ? forwarded_store_data : module_read_data_in;

Ideally add this as a test in the test_icache.py as well

5iri 5iri commented on code on Jan 01, 2026 18:21 UTC
Code Review
rtl/core_modules/alu.v:1
@@ -19,17 +19,17 @@ module alu (
             INSTR_SLL:   ALUoutput = rs1 << rs2[4:0];  // Logical left shift
             INSTR_SRL:   ALUoutput = rs1 >> rs2[4:0];  // Logical right shift
             INSTR_SRA:   ALUoutput = $signed(rs1) >>> rs2[4:0];  // Arithmetic right shift
-            INSTR_SLT:   ALUoutput = {32{$signed(rs1) < $signed(rs2)}};  // Set less than (signed comparison)
-            INSTR_SLTU:  ALUoutput = {32{rs1 < rs2}};  // Set less than (unsigned comparison)
+            INSTR_SLT:   ALUoutput = {31'b0, $signed(rs1) < $signed(rs2)};  // Set less than (signed comparison)
+            INSTR_SLTU:  ALUoutput = {31'b0, rs1 < rs2};  // Set less than (unsigned comparison)
             INSTR_ADDI:  ALUoutput = $signed(rs1) + $signed(imm);  // Add immediate
             INSTR_XORI:  ALUoutput = rs1 ^ imm;  // Bitwise XOR with immediate
             INSTR_ORI:   ALUoutput = rs1 | imm;  // Bitwise OR with immediate
             INSTR_ANDI:  ALUoutput = rs1 & imm;  // Bitwise AND with immediate
             INSTR_SLLI:  ALUoutput = rs1 << imm[4:0];  // Logical left shift with immediate
             INSTR_SRLI:  ALUoutput = rs1 >> imm[4:0];  // Logical right shift with immediate
             INSTR_SRAI:  ALUoutput = $signed(rs1) >>> imm[4:0];  // Arithmetic right shift with immediate
-            INSTR_SLTI:  ALUoutput = {32{$signed(rs1) < $signed(imm)}};  // Set less than immediate (signed comparison)
-            INSTR_SLTIU: ALUoutput = {32{rs1 < imm}};  // Set less than immediate (unsigned comparison)
+            INSTR_SLTI:  ALUoutput = {31'b0, $signed(rs1) < $signed(imm)};  // Set less than immediate (signed comparison)
+            INSTR_SLTIU: ALUoutput = {31'b0, rs1 < imm};  // Set less than immediate (unsigned comparison)

why pad with only 0s? shouldn't it be padded with sign?

spr4bhu spr4bhu commented on code on Jan 03, 2026 10:12 UTC
Code Review
rtl/core_modules/alu.v:1
@@ -19,17 +19,17 @@ module alu (
             INSTR_SLL:   ALUoutput = rs1 << rs2[4:0];  // Logical left shift
             INSTR_SRL:   ALUoutput = rs1 >> rs2[4:0];  // Logical right shift
             INSTR_SRA:   ALUoutput = $signed(rs1) >>> rs2[4:0];  // Arithmetic right shift
-            INSTR_SLT:   ALUoutput = {32{$signed(rs1) < $signed(rs2)}};  // Set less than (signed comparison)
-            INSTR_SLTU:  ALUoutput = {32{rs1 < rs2}};  // Set less than (unsigned comparison)
+            INSTR_SLT:   ALUoutput = {31'b0, $signed(rs1) < $signed(rs2)};  // Set less than (signed comparison)
+            INSTR_SLTU:  ALUoutput = {31'b0, rs1 < rs2};  // Set less than (unsigned comparison)
             INSTR_ADDI:  ALUoutput = $signed(rs1) + $signed(imm);  // Add immediate
             INSTR_XORI:  ALUoutput = rs1 ^ imm;  // Bitwise XOR with immediate
             INSTR_ORI:   ALUoutput = rs1 | imm;  // Bitwise OR with immediate
             INSTR_ANDI:  ALUoutput = rs1 & imm;  // Bitwise AND with immediate
             INSTR_SLLI:  ALUoutput = rs1 << imm[4:0];  // Logical left shift with immediate
             INSTR_SRLI:  ALUoutput = rs1 >> imm[4:0];  // Logical right shift with immediate
             INSTR_SRAI:  ALUoutput = $signed(rs1) >>> imm[4:0];  // Arithmetic right shift with immediate
-            INSTR_SLTI:  ALUoutput = {32{$signed(rs1) < $signed(imm)}};  // Set less than immediate (signed comparison)
-            INSTR_SLTIU: ALUoutput = {32{rs1 < imm}};  // Set less than immediate (unsigned comparison)
+            INSTR_SLTI:  ALUoutput = {31'b0, $signed(rs1) < $signed(imm)};  // Set less than immediate (signed comparison)
+            INSTR_SLTIU: ALUoutput = {31'b0, rs1 < imm};  // Set less than immediate (unsigned comparison)

The old code replicated the 1-bit result 32 times, so when true it produced 0xFFFFFFFF (-1) instead of 0x00000001 (1). SLTI and SLTIU are boolean comparison instructions that must return 0 or 1 per the RISC-V spec. Zero-padding the 1-bit result gives 0x00000000 for false and 0x00000001 for true, which matches the spec.

spr4bhu spr4bhu commented on code on Jan 06, 2026 13:05 UTC
Code Review
rtl/core_modules/alu.v:1
@@ -19,17 +19,17 @@ module alu (
             INSTR_SLL:   ALUoutput = rs1 << rs2[4:0];  // Logical left shift
             INSTR_SRL:   ALUoutput = rs1 >> rs2[4:0];  // Logical right shift
             INSTR_SRA:   ALUoutput = $signed(rs1) >>> rs2[4:0];  // Arithmetic right shift
-            INSTR_SLT:   ALUoutput = {32{$signed(rs1) < $signed(rs2)}};  // Set less than (signed comparison)
-            INSTR_SLTU:  ALUoutput = {32{rs1 < rs2}};  // Set less than (unsigned comparison)
+            INSTR_SLT:   ALUoutput = {31'b0, $signed(rs1) < $signed(rs2)};  // Set less than (signed comparison)
+            INSTR_SLTU:  ALUoutput = {31'b0, rs1 < rs2};  // Set less than (unsigned comparison)
             INSTR_ADDI:  ALUoutput = $signed(rs1) + $signed(imm);  // Add immediate
             INSTR_XORI:  ALUoutput = rs1 ^ imm;  // Bitwise XOR with immediate
             INSTR_ORI:   ALUoutput = rs1 | imm;  // Bitwise OR with immediate
             INSTR_ANDI:  ALUoutput = rs1 & imm;  // Bitwise AND with immediate
             INSTR_SLLI:  ALUoutput = rs1 << imm[4:0];  // Logical left shift with immediate
             INSTR_SRLI:  ALUoutput = rs1 >> imm[4:0];  // Logical right shift with immediate
             INSTR_SRAI:  ALUoutput = $signed(rs1) >>> imm[4:0];  // Arithmetic right shift with immediate
-            INSTR_SLTI:  ALUoutput = {32{$signed(rs1) < $signed(imm)}};  // Set less than immediate (signed comparison)
-            INSTR_SLTIU: ALUoutput = {32{rs1 < imm}};  // Set less than immediate (unsigned comparison)
+            INSTR_SLTI:  ALUoutput = {31'b0, $signed(rs1) < $signed(imm)};  // Set less than immediate (signed comparison)
+            INSTR_SLTIU: ALUoutput = {31'b0, rs1 < imm};  // Set less than immediate (unsigned comparison)

made the changes

5iri 5iri commented on code on Jan 29, 2026 10:49 UTC
Code Review
rtl/core_modules/registerfile.v:1
@@ -1,80 +1,61 @@
+`default_nettype none
 
 module registerfile (
-    input clk,
-    input [4:0] rs1,
-    input [4:0] rs2,
-    input rs1_valid,
-    input rs2_valid,
-    input [4:0] rd,
-    input wr_en,
-    input [31:0] rd_value,
+    input wire clk,
+    input wire rst,
+    input wire [4:0] rs1,
+    input wire [4:0] rs2,
+    input wire rs1_valid,
+    input wire rs2_valid,
+    input wire [4:0] rd,
+    input wire wr_en,
+    input wire [31:0] rd_value,
 
     output reg [31:0] rs1_value,
     output reg [31:0] rs2_value
 );
 
     reg [31:0] register_file[31:0];
+    integer i;
 
-    initial begin
-        register_file[0]  = 0;
-        register_file[1]  = 0;
-        register_file[2]  = 0;
-        register_file[3]  = 0;
-        register_file[4]  = 0;
-        register_file[5]  = 0;
-        register_file[6]  = 0;
-        register_file[7]  = 0;
-        register_file[8]  = 0;
-        register_file[9]  = 0;
-        register_file[10] = 0;
-        register_file[11] = 0;
-        register_file[12] = 0;
-        register_file[13] = 0;
-        register_file[14] = 0;
-        register_file[15] = 0;
-        register_file[16] = 0;
-        register_file[17] = 0;
-        register_file[18] = 0;
-        register_file[19] = 0;
-        register_file[20] = 0;
-        register_file[21] = 0;
-        register_file[22] = 0;
-        register_file[23] = 0;
-        register_file[24] = 0;
-        register_file[25] = 0;
-        register_file[26] = 0;
-        register_file[27] = 0;
-        register_file[28] = 0;
-        register_file[29] = 0;
-        register_file[30] = 0;
-        register_file[31] = 0;
-    end
-
+    // Combinational read with write-through forwarding

why remove initial begin?

5iri 5iri commented on code on Jan 29, 2026 10:51 UTC
Code Review
rtl/include/memory_map.vh:1
@@ -49,7 +49,7 @@
 `define UART_BAUD           32'h2000000C  // Baud rate divisor
 
 // Memory access helper macros
-`define IS_INSTR_MEM(addr)  ((addr) <= `INSTR_MEM_SIZE)
+`define IS_INSTR_MEM(addr)  ((addr) <= `INSTR_MEM_END)

keep this INSTR_MEM_SIZE

5iri 5iri commented on code on Jan 29, 2026 10:52 UTC
Code Review
rtl/pipeline_stages/store_load_detector.v:1
@@ -1,36 +1,63 @@
 `default_nettype none
 `include "instr_defines.vh"
+
+// Store-load hazard detector - forwards store data to immediately following loads at same address
 module store_load_detector (
-    // Current load instruction
-    input wire [5:0] load_instr_id,
-    input wire [31:0] load_addr,
-    
-    // Previous store instructions
-    input wire [5:0] prev_store_instr_id,
-    input wire [31:0] prev_store_addr,
-    input wire [31:0] rs2_value, // Value to be stored in case of hazard
-    
+    // Current load instruction (in MEM stage)
+    input  wire [5:0] load_instr_id,
+    input  wire [31:0] load_addr,
+
+    // Previous store instruction (in WB stage)
+    input  wire [5:0] prev_store_instr_id,
+    input  wire [31:0] prev_store_addr,
+    input  wire [31:0] rs2_value, // Source data of the store (rs2)
+
     // Output signals
-    output wire store_load_hazard,
+    output wire        store_load_hazard,

what is this tab doing her?

5iri 5iri commented on code on Jan 29, 2026 10:52 UTC
Code Review
rtl/pipeline_stages/store_load_detector.v:1
@@ -1,36 +1,63 @@
 `default_nettype none
 `include "instr_defines.vh"
+
+// Store-load hazard detector - forwards store data to immediately following loads at same address
 module store_load_detector (
-    // Current load instruction
-    input wire [5:0] load_instr_id,
-    input wire [31:0] load_addr,
-    
-    // Previous store instructions
-    input wire [5:0] prev_store_instr_id,
-    input wire [31:0] prev_store_addr,
-    input wire [31:0] rs2_value, // Value to be stored in case of hazard
-    
+    // Current load instruction (in MEM stage)
+    input  wire [5:0] load_instr_id,
+    input  wire [31:0] load_addr,
+
+    // Previous store instruction (in WB stage)
+    input  wire [5:0] prev_store_instr_id,
+    input  wire [31:0] prev_store_addr,
+    input  wire [31:0] rs2_value, // Source data of the store (rs2)
+
     // Output signals
-    output wire store_load_hazard,
+    output wire        store_load_hazard,
     output wire [31:0] forwarded_data
 );
-    
+
     // Detect if current instruction is a load
-    wire is_load = (load_instr_id == INSTR_LB) || (load_instr_id == INSTR_LH) || 
-                   (load_instr_id == INSTR_LW) || (load_instr_id == INSTR_LBU) || 
+    wire is_load = (load_instr_id == INSTR_LB)  ||
+                   (load_instr_id == INSTR_LH)  ||
+                   (load_instr_id == INSTR_LW)  ||
+                   (load_instr_id == INSTR_LBU) ||
                    (load_instr_id == INSTR_LHU);
-                   
+
     // Detect if previous instruction was a store
-    wire is_store = (prev_store_instr_id == INSTR_SB) || (prev_store_instr_id == INSTR_SH) || 
+    wire is_store = (prev_store_instr_id == INSTR_SB) ||
+                    (prev_store_instr_id == INSTR_SH) ||
                     (prev_store_instr_id == INSTR_SW);
-                    
-    // Check if addresses match
+
     wire addr_match = (load_addr == prev_store_addr);
-    
-    // Signal hazard when we have a load following a store to the same address
-    assign store_load_hazard = is_load && is_store && addr_match;
-    
-    // Forward data from store if hazard exists
-    assign forwarded_data = store_load_hazard ? rs2_value : 32'b0;
-    
-endmodule
\ No newline at end of file
+

keep same style format everwhere

5iri 5iri commented on code on Jan 29, 2026 10:57 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -16,14 +19,41 @@ module riscv_cpu (
     // Interrupt inputs
     input wire timer_interrupt,
     input wire software_interrupt,
-    input wire external_interrupt
+    input wire external_interrupt,
+    
+    // Instruction cache interface
+    input wire icache_stall,                     // Instruction cache miss stall
+    output wire fence_i_signal                   // FENCE.I invalidation signal
 );
 
+    // Startup NOP injection: inject NOPs after reset to sync pipeline registers before first instruction reaches writeback
+    reg [2:0] startup_nop_counter;
+    wire startup_sequence;
+    assign startup_sequence = (startup_nop_counter < 3'd4);
+

this is not needed imo

5iri 5iri commented on code on Jan 29, 2026 10:59 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -16,14 +19,41 @@ module riscv_cpu (
     // Interrupt inputs
     input wire timer_interrupt,
     input wire software_interrupt,
-    input wire external_interrupt
+    input wire external_interrupt,
+    
+    // Instruction cache interface
+    input wire icache_stall,                     // Instruction cache miss stall
+    output wire fence_i_signal                   // FENCE.I invalidation signal
 );
 
+    // Startup NOP injection: inject NOPs after reset to sync pipeline registers before first instruction reaches writeback
+    reg [2:0] startup_nop_counter;
+    wire startup_sequence;
+    assign startup_sequence = (startup_nop_counter < 3'd4);
+
+    always @(posedge clk or posedge rst) begin
+        if (rst) begin
+            startup_nop_counter <= 3'b000;
+        end else if (startup_sequence) begin
+            startup_nop_counter <= startup_nop_counter + 3'd1;
+        end
+    end
+
     // Instantiate PC
     wire [31:0] pc_inst0_out;
     wire pc_inst0_j_signal;
     wire [31:0] pc_inst0_jump;
     wire stall_pipeline; // For load-use hazards
+    
+    // Stalls: pipeline (hazards, cache), PC (pipeline + startup)
+    wire pipeline_stall;

there are two stall pipelines wires?

5iri 5iri commented on code on Jan 29, 2026 11:03 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -44,14 +74,21 @@ module riscv_cpu (
     wire [31:0] if_id_pc_out;
     wire [31:0] if_id_instr_out;
     wire branch_flush;
-    assign branch_flush = ex_inst0_jump_signal_out; // Flush IF/ID if branch taken
-    // If branch taken, flush IF/ID by setting instruction to 0 (NOP)
+    assign branch_flush = ex_inst0_jump_signal_out;
+    wire if_id_stall;
+    // Do NOT stall IF/ID during startup sequence; we want NOPs to flow through the pipeline.
+    assign if_id_stall = pipeline_stall && !branch_flush;
+    
+    // Inject NOPs (ADDI x0, x0, 0 = 0x00000013) during startup or branch flush
+    wire [31:0] instruction_to_if_id;
+    assign instruction_to_if_id = (branch_flush || startup_sequence) ? 32'h00000013 : module_instr_in;
+    

this is not necessary

5iri 5iri commented on code on Jan 29, 2026 11:08 UTC
Code Review
rtl/top.v:1
@@ -84,37 +92,70 @@ module top (
         .module_read_addr(cpu_mem_read_addr),
         .module_write_addr(cpu_mem_write_addr),
         .module_write_byte_enable(cpu_write_byte_enable),
-        .module_load_type(cpu_load_type)
+        .module_load_type(cpu_load_type),
+        .icache_stall(icache_stall),
+        .fence_i_signal(fence_i_signal)
     );
 
-    // Instantiate instruction memory
-    instr_mem #(
-        .DATA_WIDTH(32),
+    icache #(
         .ADDR_WIDTH(32),
-        .MEM_SIZE(131072)  // 512KB / 4 bytes = 128K words
-    ) instr_mem_inst (
-        .instr_addr(cpu_pc_out),
-        .instr_addr_p2(data_mem_addr),
-        .load_type(cpu_load_type),
-        .instr(instr_to_cpu),
-        .instr_p2(instr_read_data)
+        .DATA_WIDTH(32),
+        .NUM_WAYS(4),
+        .NUM_SETS(64),
+        .CACHE_LINE_WORDS(4)
+    ) icache_inst (
+        .clk(clk),
+        .rst(rst),
+        .cpu_addr(cpu_pc_out),
+        .cpu_req(!rst),
+        .cpu_data(instr_to_cpu),
+        .cpu_valid(),
+        .cpu_stall(icache_stall),
+        .mem_addr(cache_mem_addr),
+        .mem_req(cache_mem_req),
+        .mem_data(cache_mem_data),
+        .mem_valid(cache_mem_valid),
+        .invalidate(fence_i_signal)
     );
 
-    // Instantiate data memory  
-    data_mem #(
-        .DATA_WIDTH(32),
+    assign cache_mem_valid = cache_mem_req;
+
+    wire [31:0] unified_memory_data_out;
+
+    // Unified memory address and control signals
+    wire [31:0] instr_addr;
+    wire [31:0] data_addr;
+    wire        data_we;
+    wire        data_re;
+
+    // Instruction and Data port
+    assign instr_addr = cache_mem_addr;
+    assign data_addr = instr_mem_access ? (data_mem_addr - `INSTR_MEM_BASE) :
+                                          (data_mem_addr - `DATA_MEM_BASE + `INSTR_MEM_SIZE);
+
+    // Data port enables
+    assign data_we = cpu_mem_write_en && data_mem_access;
+    assign data_re = cpu_mem_read_en && (data_mem_access || instr_mem_access);
+
+    unified_memory #(
         .ADDR_WIDTH(32),
-        .MEM_SIZE(1048576)  // 1MB in bytes
-    ) data_mem_inst (
+        .DATA_WIDTH(32),
+        .MEM_SIZE(2097152)

write the total in MB here in comments for understanding

5iri 5iri commented on code on Jan 29, 2026 11:14 UTC
Code Review
tests/unit_tests/test_alu.py:1
@@ -93,15 +93,15 @@ async def test_shifts(dut):
 async def test_comparisons(dut):
     """Test comparisons operations"""
     # Set less than (signed)
-    await verify_alu_operation(dut, 10, 20, 0, 0x9, 0, 0xFFFFFFFF, "SLT true")
+    await verify_alu_operation(dut, 10, 20, 0, 0x9, 0, 0x00000001, "SLT true")

why this change?

5iri 5iri requested changes on Jan 29, 2026 11:14 UTC
Changes Requested Reviewer

Requested changes.

spr4bhu spr4bhu commented on code on Feb 10, 2026 10:00 UTC
Code Review
tests/unit_tests/test_alu.py:1
@@ -93,15 +93,15 @@ async def test_shifts(dut):
 async def test_comparisons(dut):
     """Test comparisons operations"""
     # Set less than (signed)
-    await verify_alu_operation(dut, 10, 20, 0, 0x9, 0, 0xFFFFFFFF, "SLT true")
+    await verify_alu_operation(dut, 10, 20, 0, 0x9, 0, 0x00000001, "SLT true")

fixing the boolean logic of set instruction

spr4bhu spr4bhu commented on code on Feb 10, 2026 10:06 UTC
Code Review
rtl/include/memory_map.vh:1
@@ -49,7 +49,7 @@
 `define UART_BAUD           32'h2000000C  // Baud rate divisor
 
 // Memory access helper macros
-`define IS_INSTR_MEM(addr)  ((addr) <= `INSTR_MEM_SIZE)
+`define IS_INSTR_MEM(addr)  ((addr) <= `INSTR_MEM_END)

In INSTR_MEM_SIZE it also allows 0x00080000 but the memory addresses are only till 0x0007FFFF

spr4bhu spr4bhu commented on code on Feb 10, 2026 16:40 UTC
Code Review
rtl/top.v:1
@@ -84,37 +92,70 @@ module top (
         .module_read_addr(cpu_mem_read_addr),
         .module_write_addr(cpu_mem_write_addr),
         .module_write_byte_enable(cpu_write_byte_enable),
-        .module_load_type(cpu_load_type)
+        .module_load_type(cpu_load_type),
+        .icache_stall(icache_stall),
+        .fence_i_signal(fence_i_signal)
     );
 
-    // Instantiate instruction memory
-    instr_mem #(
-        .DATA_WIDTH(32),
+    icache #(
         .ADDR_WIDTH(32),
-        .MEM_SIZE(131072)  // 512KB / 4 bytes = 128K words
-    ) instr_mem_inst (
-        .instr_addr(cpu_pc_out),
-        .instr_addr_p2(data_mem_addr),
-        .load_type(cpu_load_type),
-        .instr(instr_to_cpu),
-        .instr_p2(instr_read_data)
+        .DATA_WIDTH(32),
+        .NUM_WAYS(4),
+        .NUM_SETS(64),
+        .CACHE_LINE_WORDS(4)
+    ) icache_inst (
+        .clk(clk),
+        .rst(rst),
+        .cpu_addr(cpu_pc_out),
+        .cpu_req(!rst),
+        .cpu_data(instr_to_cpu),
+        .cpu_valid(),
+        .cpu_stall(icache_stall),
+        .mem_addr(cache_mem_addr),
+        .mem_req(cache_mem_req),
+        .mem_data(cache_mem_data),
+        .mem_valid(cache_mem_valid),
+        .invalidate(fence_i_signal)
     );
 
-    // Instantiate data memory  
-    data_mem #(
-        .DATA_WIDTH(32),
+    assign cache_mem_valid = cache_mem_req;
+
+    wire [31:0] unified_memory_data_out;
+
+    // Unified memory address and control signals
+    wire [31:0] instr_addr;
+    wire [31:0] data_addr;
+    wire        data_we;
+    wire        data_re;
+
+    // Instruction and Data port
+    assign instr_addr = cache_mem_addr;
+    assign data_addr = instr_mem_access ? (data_mem_addr - `INSTR_MEM_BASE) :
+                                          (data_mem_addr - `DATA_MEM_BASE + `INSTR_MEM_SIZE);
+
+    // Data port enables
+    assign data_we = cpu_mem_write_en && data_mem_access;
+    assign data_re = cpu_mem_read_en && (data_mem_access || instr_mem_access);
+
+    unified_memory #(
         .ADDR_WIDTH(32),
-        .MEM_SIZE(1048576)  // 1MB in bytes
-    ) data_mem_inst (
+        .DATA_WIDTH(32),
+        .MEM_SIZE(2097152)

donezo

spr4bhu spr4bhu commented on code on Feb 10, 2026 16:40 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -44,14 +74,21 @@ module riscv_cpu (
     wire [31:0] if_id_pc_out;
     wire [31:0] if_id_instr_out;
     wire branch_flush;
-    assign branch_flush = ex_inst0_jump_signal_out; // Flush IF/ID if branch taken
-    // If branch taken, flush IF/ID by setting instruction to 0 (NOP)
+    assign branch_flush = ex_inst0_jump_signal_out;
+    wire if_id_stall;
+    // Do NOT stall IF/ID during startup sequence; we want NOPs to flow through the pipeline.
+    assign if_id_stall = pipeline_stall && !branch_flush;
+    
+    // Inject NOPs (ADDI x0, x0, 0 = 0x00000013) during startup or branch flush
+    wire [31:0] instruction_to_if_id;
+    assign instruction_to_if_id = (branch_flush || startup_sequence) ? 32'h00000013 : module_instr_in;
+    

removed

spr4bhu spr4bhu commented on code on Feb 10, 2026 16:40 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -16,14 +19,41 @@ module riscv_cpu (
     // Interrupt inputs
     input wire timer_interrupt,
     input wire software_interrupt,
-    input wire external_interrupt
+    input wire external_interrupt,
+    
+    // Instruction cache interface
+    input wire icache_stall,                     // Instruction cache miss stall
+    output wire fence_i_signal                   // FENCE.I invalidation signal
 );
 
+    // Startup NOP injection: inject NOPs after reset to sync pipeline registers before first instruction reaches writeback
+    reg [2:0] startup_nop_counter;
+    wire startup_sequence;
+    assign startup_sequence = (startup_nop_counter < 3'd4);
+
+    always @(posedge clk or posedge rst) begin
+        if (rst) begin
+            startup_nop_counter <= 3'b000;
+        end else if (startup_sequence) begin
+            startup_nop_counter <= startup_nop_counter + 3'd1;
+        end
+    end
+
     // Instantiate PC
     wire [31:0] pc_inst0_out;
     wire pc_inst0_j_signal;
     wire [31:0] pc_inst0_jump;
     wire stall_pipeline; // For load-use hazards
+    
+    // Stalls: pipeline (hazards, cache), PC (pipeline + startup)
+    wire pipeline_stall;

removed

spr4bhu spr4bhu commented on code on Feb 10, 2026 16:40 UTC
Code Review
rtl/riscv_cpu.v:1
@@ -16,14 +19,41 @@ module riscv_cpu (
     // Interrupt inputs
     input wire timer_interrupt,
     input wire software_interrupt,
-    input wire external_interrupt
+    input wire external_interrupt,
+    
+    // Instruction cache interface
+    input wire icache_stall,                     // Instruction cache miss stall
+    output wire fence_i_signal                   // FENCE.I invalidation signal
 );
 
+    // Startup NOP injection: inject NOPs after reset to sync pipeline registers before first instruction reaches writeback
+    reg [2:0] startup_nop_counter;
+    wire startup_sequence;
+    assign startup_sequence = (startup_nop_counter < 3'd4);
+

removed

spr4bhu spr4bhu commented on code on Feb 10, 2026 16:40 UTC
Code Review
rtl/pipeline_stages/store_load_detector.v:1
@@ -1,36 +1,63 @@
 `default_nettype none
 `include "instr_defines.vh"
+
+// Store-load hazard detector - forwards store data to immediately following loads at same address
 module store_load_detector (
-    // Current load instruction
-    input wire [5:0] load_instr_id,
-    input wire [31:0] load_addr,
-    
-    // Previous store instructions
-    input wire [5:0] prev_store_instr_id,
-    input wire [31:0] prev_store_addr,
-    input wire [31:0] rs2_value, // Value to be stored in case of hazard
-    
+    // Current load instruction (in MEM stage)
+    input  wire [5:0] load_instr_id,
+    input  wire [31:0] load_addr,
+
+    // Previous store instruction (in WB stage)
+    input  wire [5:0] prev_store_instr_id,
+    input  wire [31:0] prev_store_addr,
+    input  wire [31:0] rs2_value, // Source data of the store (rs2)
+
     // Output signals
-    output wire store_load_hazard,
+    output wire        store_load_hazard,
     output wire [31:0] forwarded_data
 );
-    
+
     // Detect if current instruction is a load
-    wire is_load = (load_instr_id == INSTR_LB) || (load_instr_id == INSTR_LH) || 
-                   (load_instr_id == INSTR_LW) || (load_instr_id == INSTR_LBU) || 
+    wire is_load = (load_instr_id == INSTR_LB)  ||
+                   (load_instr_id == INSTR_LH)  ||
+                   (load_instr_id == INSTR_LW)  ||
+                   (load_instr_id == INSTR_LBU) ||
                    (load_instr_id == INSTR_LHU);
-                   
+
     // Detect if previous instruction was a store
-    wire is_store = (prev_store_instr_id == INSTR_SB) || (prev_store_instr_id == INSTR_SH) || 
+    wire is_store = (prev_store_instr_id == INSTR_SB) ||
+                    (prev_store_instr_id == INSTR_SH) ||
                     (prev_store_instr_id == INSTR_SW);
-                    
-    // Check if addresses match
+
     wire addr_match = (load_addr == prev_store_addr);
-    
-    // Signal hazard when we have a load following a store to the same address
-    assign store_load_hazard = is_load && is_store && addr_match;
-    
-    // Forward data from store if hazard exists
-    assign forwarded_data = store_load_hazard ? rs2_value : 32'b0;
-    
-endmodule
\ No newline at end of file
+

okays

spr4bhu spr4bhu commented on code on Feb 10, 2026 16:41 UTC
Code Review
rtl/pipeline_stages/store_load_detector.v:1
@@ -1,36 +1,63 @@
 `default_nettype none
 `include "instr_defines.vh"
+
+// Store-load hazard detector - forwards store data to immediately following loads at same address
 module store_load_detector (
-    // Current load instruction
-    input wire [5:0] load_instr_id,
-    input wire [31:0] load_addr,
-    
-    // Previous store instructions
-    input wire [5:0] prev_store_instr_id,
-    input wire [31:0] prev_store_addr,
-    input wire [31:0] rs2_value, // Value to be stored in case of hazard
-    
+    // Current load instruction (in MEM stage)
+    input  wire [5:0] load_instr_id,
+    input  wire [31:0] load_addr,
+
+    // Previous store instruction (in WB stage)
+    input  wire [5:0] prev_store_instr_id,
+    input  wire [31:0] prev_store_addr,
+    input  wire [31:0] rs2_value, // Source data of the store (rs2)
+
     // Output signals
-    output wire store_load_hazard,
+    output wire        store_load_hazard,

removed

spr4bhu spr4bhu commented on code on Feb 10, 2026 16:41 UTC
Code Review
rtl/core_modules/registerfile.v:1
@@ -1,80 +1,61 @@
+`default_nettype none
 
 module registerfile (
-    input clk,
-    input [4:0] rs1,
-    input [4:0] rs2,
-    input rs1_valid,
-    input rs2_valid,
-    input [4:0] rd,
-    input wr_en,
-    input [31:0] rd_value,
+    input wire clk,
+    input wire rst,
+    input wire [4:0] rs1,
+    input wire [4:0] rs2,
+    input wire rs1_valid,
+    input wire rs2_valid,
+    input wire [4:0] rd,
+    input wire wr_en,
+    input wire [31:0] rd_value,
 
     output reg [31:0] rs1_value,
     output reg [31:0] rs2_value
 );
 
     reg [31:0] register_file[31:0];
+    integer i;
 
-    initial begin
-        register_file[0]  = 0;
-        register_file[1]  = 0;
-        register_file[2]  = 0;
-        register_file[3]  = 0;
-        register_file[4]  = 0;
-        register_file[5]  = 0;
-        register_file[6]  = 0;
-        register_file[7]  = 0;
-        register_file[8]  = 0;
-        register_file[9]  = 0;
-        register_file[10] = 0;
-        register_file[11] = 0;
-        register_file[12] = 0;
-        register_file[13] = 0;
-        register_file[14] = 0;
-        register_file[15] = 0;
-        register_file[16] = 0;
-        register_file[17] = 0;
-        register_file[18] = 0;
-        register_file[19] = 0;
-        register_file[20] = 0;
-        register_file[21] = 0;
-        register_file[22] = 0;
-        register_file[23] = 0;
-        register_file[24] = 0;
-        register_file[25] = 0;
-        register_file[26] = 0;
-        register_file[27] = 0;
-        register_file[28] = 0;
-        register_file[29] = 0;
-        register_file[30] = 0;
-        register_file[31] = 0;
-    end
-
+    // Combinational read with write-through forwarding

added

5iri 5iri commented on code on Feb 10, 2026 18:24 UTC
Code Review
rtl/core_modules/alu.v:1
@@ -19,17 +19,17 @@ module alu (
             INSTR_SLL:   ALUoutput = rs1 << rs2[4:0];  // Logical left shift
             INSTR_SRL:   ALUoutput = rs1 >> rs2[4:0];  // Logical right shift
             INSTR_SRA:   ALUoutput = $signed(rs1) >>> rs2[4:0];  // Arithmetic right shift
-            INSTR_SLT:   ALUoutput = {32{$signed(rs1) < $signed(rs2)}};  // Set less than (signed comparison)
-            INSTR_SLTU:  ALUoutput = {32{rs1 < rs2}};  // Set less than (unsigned comparison)
+            INSTR_SLT:   ALUoutput = {31'b0, $signed(rs1) < $signed(rs2)};  // Set less than (signed comparison)
+            INSTR_SLTU:  ALUoutput = {31'b0, rs1 < rs2};  // Set less than (unsigned comparison)
             INSTR_ADDI:  ALUoutput = $signed(rs1) + $signed(imm);  // Add immediate
             INSTR_XORI:  ALUoutput = rs1 ^ imm;  // Bitwise XOR with immediate
             INSTR_ORI:   ALUoutput = rs1 | imm;  // Bitwise OR with immediate
             INSTR_ANDI:  ALUoutput = rs1 & imm;  // Bitwise AND with immediate
             INSTR_SLLI:  ALUoutput = rs1 << imm[4:0];  // Logical left shift with immediate
             INSTR_SRLI:  ALUoutput = rs1 >> imm[4:0];  // Logical right shift with immediate
             INSTR_SRAI:  ALUoutput = $signed(rs1) >>> imm[4:0];  // Arithmetic right shift with immediate
-            INSTR_SLTI:  ALUoutput = {32{$signed(rs1) < $signed(imm)}};  // Set less than immediate (signed comparison)
-            INSTR_SLTIU: ALUoutput = {32{rs1 < imm}};  // Set less than immediate (unsigned comparison)
+            INSTR_SLTI:  ALUoutput = {31'b0, $signed(rs1) < $signed(imm)};  // Set less than immediate (signed comparison)
+            INSTR_SLTIU: ALUoutput = {31'b0, rs1 < imm};  // Set less than immediate (unsigned comparison)

nice catch!

5iri 5iri commented on Feb 10, 2026 18:26 UTC
Reviewer

you haven't updated the commit even though I see you have made some comments?

5iri 5iri commented on code on Feb 11, 2026 05:27 UTC
Code Review
rtl/core_modules/alu.v:1
@@ -19,17 +19,17 @@ module alu (
             INSTR_SLL:   ALUoutput = rs1 << rs2[4:0];  // Logical left shift
             INSTR_SRL:   ALUoutput = rs1 >> rs2[4:0];  // Logical right shift
             INSTR_SRA:   ALUoutput = $signed(rs1) >>> rs2[4:0];  // Arithmetic right shift
-            INSTR_SLT:   ALUoutput = {32{$signed(rs1) < $signed(rs2)}};  // Set less than (signed comparison)
-            INSTR_SLTU:  ALUoutput = {32{rs1 < rs2}};  // Set less than (unsigned comparison)
+            INSTR_SLT:   ALUoutput = {31'b0, $signed(rs1) < $signed(rs2)};  // Set less than (signed comparison)

there's an extra SLT now?

5iri 5iri commented on code on Feb 11, 2026 05:29 UTC
Code Review
rtl/include/memory_map.vh:1
@@ -49,7 +49,7 @@
 `define UART_BAUD           32'h2000000C  // Baud rate divisor
 
 // Memory access helper macros
-`define IS_INSTR_MEM(addr)  ((addr) <= `INSTR_MEM_SIZE)
+`define IS_INSTR_MEM(addr)  ((addr) <= `INSTR_MEM_END)

yeah got it! nice!

5iri 5iri commented on code on Feb 11, 2026 05:31 UTC
Code Review
tests/system_tests/test_fibonacci.py:1
@@ -51,7 +51,7 @@ def compile_fibonacci():
     try:
         # Create .o file from C source
         subprocess.run([
-            "riscv64-unknown-elf-gcc",
+            "riscv64-elf-gcc",

we should use riscv-unknown-elf-gcc only here

why the change?

5iri 5iri commented on Feb 11, 2026 05:34 UTC
Reviewer

imo this is a very big diff that is not possible to review in one PR. my bad. I think you should separate this work into 3 PRs.

one, where you add icache, one where you fix the SLTI/SLTIU bug and the third one where we fix the memory map.

this allows me to review your work thoroughly and with no mistakes.

@spr4bhu

spr4bhu spr4bhu commented on code on Feb 11, 2026 07:37 UTC
Code Review
rtl/core_modules/alu.v:1
@@ -19,17 +19,17 @@ module alu (
             INSTR_SLL:   ALUoutput = rs1 << rs2[4:0];  // Logical left shift
             INSTR_SRL:   ALUoutput = rs1 >> rs2[4:0];  // Logical right shift
             INSTR_SRA:   ALUoutput = $signed(rs1) >>> rs2[4:0];  // Arithmetic right shift
-            INSTR_SLT:   ALUoutput = {32{$signed(rs1) < $signed(rs2)}};  // Set less than (signed comparison)
-            INSTR_SLTU:  ALUoutput = {32{rs1 < rs2}};  // Set less than (unsigned comparison)
+            INSTR_SLT:   ALUoutput = {31'b0, $signed(rs1) < $signed(rs2)};  // Set less than (signed comparison)

There's no extra SLT
just fixed the output to be boolean

5iri 5iri commented on Feb 24, 2026 17:44 UTC
Contributor

closing this since we don't need this anymore.

Reviewers
Assignees
spr4bhu spr4bhu
Labels
risk: low confidence: 80.2% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 71.5% (HIGH) · Higher = larger blast radius
71.5%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 80.2% · Lower = rushed or shallow review
80.2%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 14.2% · Safe to merge
14.2%