Skip to content

Commit bf351ba

Browse files
authored
update: RiSC-16 PC NoahMS 9-24-25 (#395)
* Update pc.v imm value changed to 7 bit input * Update pc_tb.v Fit testbench with 7-bit input imm value
1 parent c6b6369 commit bf351ba

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/getting_started/onboarding/RiSC-16/pc.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ module pc (
22
input clk,
33
input rst_n,
44
input [1:0] MUX_output, // Controlled by Control module
5-
input [15:0] imm, // Immediate value
5+
input [6:0] imm, // Immediate value
66
input [15:0] alu_out, // The output from regB that has been passed through the ALU
77
output [15:0] nxt_instr
88
);
99

1010
reg [15:0] pc_reg; // Internal Register to hold state of pc
11+
wire [15:0] se_imm; // Sign extended immediate value
1112

1213
assign nxt_instr = pc_reg; // Assigns output to be linked to pc_reg
14+
assign se_imm = {{9{imm[6]}}, imm[6:0]}; // Sign extends the least significant 7 bits from immediate value to 16 digits
1315

1416
always @(posedge clk or negedge rst_n)
1517
begin
@@ -25,7 +27,7 @@ begin
2527
2'b00:
2628
pc_reg <= pc_reg + 1;
2729
2'b01:
28-
pc_reg <= pc_reg + imm + 1;
30+
pc_reg <= pc_reg + 1 + se_imm;
2931
2'b10:
3032
pc_reg <= alu_out;
3133
default:

src/getting_started/onboarding/RiSC-16/pc_tb.v

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module pc_tb;
77
reg clk;
88
reg rst_n;
99
reg [1:0] MUX_output;
10-
reg [15:0] imm;
10+
reg [6:0] imm; // Input is in 2's compliment, so MSB is the sign bit
1111
reg [15:0] alu_out;
1212

1313
// Output from the DUT
@@ -33,7 +33,7 @@ module pc_tb;
3333
clk = 0;
3434
rst_n = 1;
3535
MUX_output = 2'b00;
36-
imm = 16'h0000;
36+
imm = 7'b0;
3737
alu_out = 16'h0000;
3838
#10; // Wait for a moment
3939

@@ -74,7 +74,7 @@ module pc_tb;
7474
// 4. Test Case: Branch with Immediate (MUX_output = 01)
7575
$display("\n T=%0t: [TEST] Testing branch with immediate (MUX_output = 2'b01).", $time);
7676
MUX_output = 2'b01;
77-
imm = 16'h000E; // Adds 14
77+
imm = 7'b0001110; // Adds 14
7878
// Set other inputs to different values to ensure they aren't selected
7979
alu_out = 16'hEEEE;
8080
@(posedge clk);
@@ -91,7 +91,7 @@ module pc_tb;
9191
MUX_output = 2'b10;
9292
alu_out = 16'hABCD;
9393
// Set other input to different values
94-
imm = 16'hEEEE;
94+
imm = 7'b1111110;
9595
@(posedge clk);
9696
#1;
9797
if (nxt_instr === 16'hABCD) begin
@@ -118,12 +118,12 @@ module pc_tb;
118118

119119
// c. Branch
120120
MUX_output = 2'b01;
121-
imm = 16'h010F;
121+
imm = 7'b0100110; // imm = 38. Jumps to BF15 + 1 = BF16
122122
@(posedge clk);
123123
#1;
124124
$display("T=%0t: [INFO] Current PC: 0x%h (After branch)", $time, nxt_instr);
125-
if (nxt_instr !== 16'hBFFF) begin
126-
$display("T=%0t: [FAIL] Back-to-back sequence failed. PC is 0x%h, expected 0xBFFF.", $time, nxt_instr);
125+
if (nxt_instr !== 16'hBF16) begin
126+
$display("T=%0t: [FAIL] Back-to-back sequence failed. PC is 0x%h, expected 0xBF16.", $time, nxt_instr);
127127
end else begin
128128
$display("T=%0t: [PASS] Back-to-back sequence successful.", $time);
129129
end
@@ -133,14 +133,14 @@ module pc_tb;
133133
// of incrementing the PC
134134
$display("\n T=%0t: [TEST] Testing undefined MUX select (2'b11).", $time);
135135
MUX_output = 2'b11;
136-
imm = 16'h148A; // Random Inputs to ensure they're ignored
136+
imm = 7'b1010101; // Random Inputs to ensure they're ignored
137137
alu_out = 16'h000B;// Random Inputs to ensure they're ignored
138138
@(posedge clk);
139139
#1;
140-
if (nxt_instr === 16'hC000) begin
140+
if (nxt_instr === 16'hBF17) begin
141141
$display("T=%0t: [PASS] PC correctly incremented to PC+1 at 0x%h.", $time, nxt_instr);
142142
end else begin
143-
$display("T=%0t: [FAIL] PC changed to 0x%h, expected it to go to 16'hC000.", $time, nxt_instr);
143+
$display("T=%0t: [FAIL] PC changed to 0x%h, expected it to go to 16'hBF17.", $time, nxt_instr);
144144
end
145145

146146
// 8. Test Case: Asynchronous Reset again

0 commit comments

Comments
 (0)