|
| 1 | +`timescale 1ns / 1ps |
| 2 | + |
| 3 | +module register_file_tb; |
| 4 | + |
| 5 | + // Inputs |
| 6 | + reg clk; |
| 7 | + reg [1:0] MUX_tgt; // 00:mem_out 01:alu_out 10:pc+1 |
| 8 | + reg MUX_rf; // 0:rC(ADD/NAND) 1:rA(SW/BEQ) |
| 9 | + reg WE_rf; // 0:SW/BEQ 1:ADD/ADDI/NAND/LUI/LW/JALR |
| 10 | + reg [15:0] mem_out, alu_out, pc, instruction; |
| 11 | + |
| 12 | + // Outputs |
| 13 | + wire [15:0] reg_out1, reg_out2; |
| 14 | + |
| 15 | + // Instantiate the Unit Under Test (UUT) |
| 16 | + register_file uut ( |
| 17 | + .clk(clk), |
| 18 | + .MUX_tgt(MUX_tgt), |
| 19 | + .MUX_rf(MUX_rf), |
| 20 | + .WE_rf(WE_rf), |
| 21 | + .mem_out(mem_out), |
| 22 | + .alu_out(alu_out), |
| 23 | + .pc(pc), |
| 24 | + .instruction(instruction), |
| 25 | + .reg_out1(reg_out1), |
| 26 | + .reg_out2(reg_out2) |
| 27 | + ); |
| 28 | + |
| 29 | + // --- Clock Generator --- |
| 30 | + initial begin |
| 31 | + clk = 0; |
| 32 | + end |
| 33 | + always begin |
| 34 | + #5 clk = ~clk; |
| 35 | + end |
| 36 | + |
| 37 | + // --- Verification Task --- |
| 38 | + task check_and_print_regs; |
| 39 | + input [15:0] exp_r0, exp_r1, exp_r2, exp_r3, exp_r4, exp_r5, exp_r6, exp_r7; |
| 40 | + reg [15:0] expected_regs [0:7]; |
| 41 | + integer i; |
| 42 | + integer errors; |
| 43 | + |
| 44 | + begin |
| 45 | + expected_regs[0] = exp_r0; expected_regs[1] = exp_r1; expected_regs[2] = exp_r2; |
| 46 | + expected_regs[3] = exp_r3; expected_regs[4] = exp_r4; expected_regs[5] = exp_r5; |
| 47 | + expected_regs[6] = exp_r6; expected_regs[7] = exp_r7; |
| 48 | + |
| 49 | + errors = 0; |
| 50 | + $display(" -> Verifying all register values..."); |
| 51 | + for (i = 0; i < 8; i = i + 1) begin |
| 52 | + if (uut.register_file[i] !== expected_regs[i]) begin |
| 53 | + $display(" R%0d: Expected=%h, Actual=%h <-- MISMATCH", i, expected_regs[i], uut.register_file[i]); |
| 54 | + errors = errors + 1; |
| 55 | + end else begin |
| 56 | + $display(" R%0d: Expected=%h, Actual=%h", i, expected_regs[i], uut.register_file[i]); |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + if (errors == 0) $display(">> PASSED: All register values are correct."); |
| 61 | + else $display(">> FAILED: %0d register value(s) are incorrect.", errors); |
| 62 | + end |
| 63 | + endtask |
| 64 | + |
| 65 | + // --- Test Sequence --- |
| 66 | + initial begin |
| 67 | + $display("Starting Official RiSC-16 Register File Testbench..."); |
| 68 | + |
| 69 | + // 1. Initialize all inputs. |
| 70 | + WE_rf <= 0; MUX_tgt <= 2'bxx; MUX_rf <= 1'bx; instruction <= 16'h0000; |
| 71 | + alu_out <= 16'h0000; mem_out <= 16'h0000; pc <= 16'h0000; |
| 72 | + @(posedge clk); @(posedge clk); |
| 73 | + |
| 74 | + // TEST 2: Check initial state (all zeros). |
| 75 | + $display("\n[TEST] Verifying initial register state..."); |
| 76 | + @(posedge clk); @(posedge clk); |
| 77 | + check_and_print_regs(16'h0, 16'h0, 16'h0, 16'h0, 16'h0, 16'h0, 16'h0, 16'h0); |
| 78 | + |
| 79 | + // TEST 3: LUI (op:011) write 0xE380 to R1. |
| 80 | + $display("\n[TEST] LUI writing 0xE380 to R1..."); |
| 81 | + WE_rf <= 1; MUX_tgt <= 2'b01; MUX_rf <= 0; alu_out <= 16'hE380; // Assume ALU left shifts imm-10 by 6. MUX_rf val doesn't matter |
| 82 | + // instr: op=011, rA=R1, imm=10b1110001110 = 10'h38E: This value shifts to 1110001110_000000 = 16'hE380 = 58240 |
| 83 | + instruction <= 16'b011_001_1110001110; |
| 84 | + @(posedge clk); @(posedge clk); |
| 85 | + check_and_print_regs(16'h0, 16'hE380, 16'h0, 16'h0, 16'h0, 16'h0, 16'h0, 16'h0); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + // TEST 4: ADDI (op:001) add 7'b1110001 to R1. |
| 90 | + $display("\n[TEST] ADDI adding 7'b1110001 to R1 and store in R3"); |
| 91 | + WE_rf <= 1; MUX_tgt <= 2'b01; MUX_rf <= 1; // Assume ALU computes rB(R1) + imm. MUX_rf val doesn't matter |
| 92 | + // instr: op=001, rA=R3, rB=R1, imm = 7'd113/7'h71 |
| 93 | + instruction <= 16'b001_011_001_1110001; |
| 94 | + alu_out <= 16'hE3F1; // 16'hE380 + 7'h71 = 58240 + 113 = 58353 = 16'hE3F1 |
| 95 | + @(posedge clk); @(posedge clk); |
| 96 | + |
| 97 | + // Check SRC1 is correct val |
| 98 | + $display("\n[TEST OUTPUT] reg_out1 should be contents of rB/R1: 16'hE380"); |
| 99 | + if(reg_out1 == 16'hE380) begin |
| 100 | + $display("\n[TEST PASSED]"); |
| 101 | + end else begin |
| 102 | + $display("\n[TEST FAILED] reg_out1 contains %h", reg_out1); |
| 103 | + end |
| 104 | + // Check regs |
| 105 | + check_and_print_regs(16'h0, 16'hE380, 16'h0, 16'hE3F1, 16'h0, 16'h0, 16'h0, 16'h0); |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + // TEST 5: ADD (op:000) attempt to ADD to R0. SHOULD HAVE NO EFFECT |
| 110 | + $display("\n[TEST] ADD adding R1 and R3 to R0 (should have no effect)"); |
| 111 | + WE_rf <= 1; MUX_tgt <= 2'b01; alu_out <= 16'hC771; |
| 112 | + MUX_rf <= 0; //MUX_rf chooses the output of rC for the second input |
| 113 | + // instr: op=000, rA=R0, rB=R1, rC=R3 |
| 114 | + instruction <= 16'b000_000_001_0000_011; |
| 115 | + @(posedge clk); |
| 116 | + @(posedge clk); |
| 117 | + // Check SRC1 is correct val |
| 118 | + $display("\n[TEST OUTPUT 1] reg_out1 should be contents of rB/R1: 16'hE380"); |
| 119 | + if(reg_out1 == 16'hE380) begin |
| 120 | + $display("[TEST PASSED]"); |
| 121 | + end else begin |
| 122 | + $display("[TEST FAILED] reg_out1 contains %h", reg_out1); |
| 123 | + end |
| 124 | + // Check SRC2 is correct val |
| 125 | + $display("\n[TEST OUTPUT 2] reg_out2 should be contents of rC/R3: 16'hE3F1"); |
| 126 | + if(reg_out2 == 16'hE3F1) begin |
| 127 | + $display("[TEST PASSED]"); |
| 128 | + end else begin |
| 129 | + $display("[TEST FAILED] reg_out2 contains %h", reg_out2); |
| 130 | + end |
| 131 | + check_and_print_regs(16'h0, 16'hE380, 16'h0000, 16'hE3F1, 16'h0, 16'h0, 16'h0, 16'h0); |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + // TEST 6: NAND (op:010) R1 and R3, store in R5 |
| 136 | + $display("\n[TEST] NAND R1 and R3, store in R5"); |
| 137 | + WE_rf <= 1; MUX_tgt <= 2'b01; |
| 138 | + MUX_rf <= 0; //MUX_rf chooses the output of rC for the second input |
| 139 | + // instr: op=010, rA=R5, rB=R1, rC=R3 |
| 140 | + instruction <= 16'b010_101_001_0000_011; |
| 141 | + alu_out <= ~(16'hE380 & 16'hE3F1); // Result is ~(16'hE380) = 16'h1C7F |
| 142 | + @(posedge clk); @(posedge clk); |
| 143 | + // Check SRC1 is correct val |
| 144 | + $display("\n[TEST OUTPUT 1] reg_out1 should be contents of rB/R1: 16'hE380"); |
| 145 | + if(reg_out1 == 16'hE380) begin |
| 146 | + $display("[TEST PASSED]"); |
| 147 | + end else begin |
| 148 | + $display("[TEST FAILED] reg_out1 contains %h", reg_out1); |
| 149 | + end |
| 150 | + // Check SRC2 is correct val |
| 151 | + $display("\n[TEST OUTPUT 2] reg_out2 should be contents of rC/R3: 16'hE3F1"); |
| 152 | + if(reg_out2 == 16'hE3F1) begin |
| 153 | + $display("[TEST PASSED]"); |
| 154 | + end else begin |
| 155 | + $display("[TEST FAILED] reg_out2 contains %h", reg_out2); |
| 156 | + end |
| 157 | + check_and_print_regs(16'h0, 16'hE380, 16'h0, 16'hE3F1, 16'h0, 16'h1C7F, 16'h0, 16'h0); |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + // TEST 7: LW (op:100) write 0xBEEF from memory to R6 (dest:rA). |
| 162 | + $display("\n[TEST] LW writing 0xBEEF to R6..."); |
| 163 | + WE_rf <= 1; MUX_tgt <= 2'b00; mem_out <= 16'hBEEF; //16'hBEEF is hypothetical val at mem loc [rB] + imm |
| 164 | + // instr: op=100, rA=R6, rB=R5 |
| 165 | + instruction <= 16'b100_110_101_0001110; // Loads instruction from memory location [rB] + imm |
| 166 | + @(posedge clk); @(posedge clk); |
| 167 | + // Check SRC1 is correct val |
| 168 | + $display("\n[TEST OUTPUT 1] reg_out1 should be contents of rB/R5: 16'h1C7F"); |
| 169 | + if(reg_out1 == 16'h1C7F) begin |
| 170 | + $display("[TEST PASSED]"); |
| 171 | + end else begin |
| 172 | + $display("[TEST FAILED] reg_out1 contains %h", reg_out1); |
| 173 | + end |
| 174 | + check_and_print_regs(16'h0, 16'hE380, 16'h0, 16'hE3F1, 16'h0, 16'h1C7F, 16'hBEEF, 16'h0); |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + // TEST 8: SW (op:101) Write Inhibit test. Should NOT change any registers. |
| 180 | + // reg_out1 should be the value in rB, reg_out2 should be the value in rA to be sent to memory |
| 181 | + $display("\n[TEST] SW Write Inhibit. Should not change registers..."); |
| 182 | + MUX_rf <= 1; WE_rf <= 0; // For SW |
| 183 | + MUX_tgt <= 2'b00; //doesn't matter |
| 184 | + // instr: op=101, rA=R5 (source), rB=R3, Random imm val |
| 185 | + instruction <= 16'b101_101_011_0000111; // Storing [R5] in mem loc [R3] + imm |
| 186 | + @(posedge clk); @(posedge clk); |
| 187 | + // Check SRC1 is correct val |
| 188 | + $display("\n[TEST OUTPUT 1] reg_out1 should be contents of rB/R3: 16'hE3F1"); |
| 189 | + if(reg_out1 == 16'hE3F1) begin |
| 190 | + $display("[TEST PASSED]"); |
| 191 | + end else begin |
| 192 | + $display("[TEST FAILED] reg_out1 contains %h", reg_out1); |
| 193 | + end |
| 194 | + // Check SRC2 is correct val |
| 195 | + $display("\n[TEST OUTPUT 2] reg_out2 should be contents of rA/R5: 16'h1C7F"); |
| 196 | + if(reg_out2 == 16'h1C7F) begin |
| 197 | + $display("[TEST PASSED]"); |
| 198 | + end else begin |
| 199 | + $display("[TEST FAILED] reg_out2 contains %h", reg_out2); |
| 200 | + end |
| 201 | + check_and_print_regs(16'h0, 16'hE380, 16'h0, 16'hE3F1, 16'h0, 16'h1C7F, 16'hBEEF, 16'h0); |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + // TEST 9: JALR (op:111) write PC+1 to R7 (dest:rA). |
| 206 | + // reg_out1 should be [rB] |
| 207 | + // pc+1 stored in rA |
| 208 | + $display("\n[TEST] JALR writing PC+1 (0xF00E) to R7..."); |
| 209 | + WE_rf <= 1; MUX_tgt <= 2'b10; pc <= 16'hF00D; |
| 210 | + // instr: op=111, rA=R7, rB= R1 |
| 211 | + instruction <= 16'b111_111_001_0000000; |
| 212 | + @(posedge clk); @(posedge clk); |
| 213 | + // Check SRC1 is correct val |
| 214 | + $display("\n[TEST OUTPUT 1] reg_out1 should be contents of rB/R1: 16'hE380"); |
| 215 | + if(reg_out1 == 16'hE380) begin |
| 216 | + $display("[TEST PASSED]"); |
| 217 | + end else begin |
| 218 | + $display("[TEST FAILED] reg_out1 contains %h", reg_out1); |
| 219 | + end |
| 220 | + check_and_print_regs(16'h0, 16'hE380, 16'h0, 16'hE3F1, 16'h0, 16'h1C7F, 16'hBEEF, 16'hF00E); |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + // TEST 10: BEQ (op:110) Write Inhibit test. Should NOT change any registers. |
| 225 | + // reg_out1 should be [rA], reg_out2 should be [rB] |
| 226 | + $display("\n[TEST] BEQ Write Inhibit. Should not change registers..."); |
| 227 | + MUX_tgt <= 2'b01; // Doesn't matter |
| 228 | + MUX_rf <= 1; //For BEQ |
| 229 | + WE_rf <= 0; // For BEQ |
| 230 | + // instr: op=110, rA=R1 (source), rB=R3 (source) |
| 231 | + instruction <= 16'b110_001_011_0000000; |
| 232 | + @(posedge clk); @(posedge clk); |
| 233 | + // Check SRC1 is correct val |
| 234 | + $display("\n[TEST OUTPUT 1] reg_out1 should be contents of rB/R3: 16'hE3F1"); |
| 235 | + if(reg_out1 == 16'hE3F1) begin |
| 236 | + $display("[TEST PASSED]"); |
| 237 | + end else begin |
| 238 | + $display("[TEST FAILED] reg_out1 contains %h", reg_out1); |
| 239 | + end |
| 240 | + // Check SRC2 is correct val |
| 241 | + $display("\n[TEST OUTPUT 2] reg_out2 should be contents of rA/R1: 16'hE380"); |
| 242 | + if(reg_out2 == 16'hE380) begin |
| 243 | + $display("[TEST PASSED]"); |
| 244 | + end else begin |
| 245 | + $display("[TEST FAILED] reg_out2 contains %h", reg_out2); |
| 246 | + end |
| 247 | + check_and_print_regs(16'h0, 16'hE380, 16'h0, 16'hE3F1, 16'h0, 16'h1C7F, 16'hBEEF, 16'hF00E); |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | + #20; |
| 252 | + $display("\nAll tests completed."); |
| 253 | + $finish; |
| 254 | + end |
| 255 | +endmodule |
| 256 | + |
0 commit comments