Skip to content

Commit f3f9fc3

Browse files
authored
Update: RiSC-16 PC files (#393)
* Update nm4207.md * Create pc.v * Add files via upload * Update pc.v * Update pc_tb.v
1 parent 0e93086 commit f3f9fc3

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ module pc (
22
input clk,
33
input rst_n,
44
input [1:0] MUX_output, // Controlled by Control module
5-
input [15:0] pc_plus1,
6-
input [15:0] pc_plus1_imm,
5+
input [15:0] imm, // Immediate value
76
input [15:0] alu_out, // The output from regB that has been passed through the ALU
87
output [15:0] nxt_instr
98
);
@@ -24,13 +23,13 @@ begin
2423
// On a clock edge, the next value of the PC is determined by the MUX selector.
2524
case (MUX_output)
2625
2'b00:
27-
pc_reg <= pc_plus1;
26+
pc_reg <= pc_reg + 1;
2827
2'b01:
29-
pc_reg <= pc_plus1_imm;
28+
pc_reg <= pc_reg + imm + 1;
3029
2'b10:
3130
pc_reg <= alu_out;
3231
default:
33-
pc_reg <= pc_plus1; // safety
32+
pc_reg <= pc_reg + 1; // safety
3433
endcase
3534
end
3635
end

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

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ module pc_tb;
77
reg clk;
88
reg rst_n;
99
reg [1:0] MUX_output;
10-
reg [15:0] pc_plus1;
11-
reg [15:0] pc_plus1_imm;
10+
reg [15:0] imm;
1211
reg [15:0] alu_out;
1312

1413
// Output from the DUT
@@ -19,8 +18,7 @@ module pc_tb;
1918
.clk(clk),
2019
.rst_n(rst_n),
2120
.MUX_output(MUX_output),
22-
.pc_plus1(pc_plus1),
23-
.pc_plus1_imm(pc_plus1_imm),
21+
.imm(imm),
2422
.alu_out(alu_out),
2523
.nxt_instr(nxt_instr)
2624
);
@@ -35,8 +33,7 @@ module pc_tb;
3533
clk = 0;
3634
rst_n = 1;
3735
MUX_output = 2'b00;
38-
pc_plus1 = 16'h0000;
39-
pc_plus1_imm = 16'h0000;
36+
imm = 16'h0000;
4037
alu_out = 16'h0000;
4138
#10; // Wait for a moment
4239

@@ -57,7 +54,6 @@ module pc_tb;
5754
// 3. Test Case: Sequential Increment (MUX_output = 00)
5855
$display("\n T=%0t: [TEST] Testing sequential increment (MUX_output = 2'b00).", $time);
5956
MUX_output = 2'b00;
60-
pc_plus1 = 16'h0001;
6157
@(posedge clk);
6258
#1;
6359
if (nxt_instr === 16'h0001) begin
@@ -66,7 +62,6 @@ module pc_tb;
6662
$display("T=%0t: [FAIL] PC is 0x%h, expected 0x0001.", $time, nxt_instr);
6763
end
6864

69-
pc_plus1 = 16'h0002;
7065
@(posedge clk);
7166
#1;
7267
if (nxt_instr === 16'h0002) begin
@@ -79,26 +74,24 @@ module pc_tb;
7974
// 4. Test Case: Branch with Immediate (MUX_output = 01)
8075
$display("\n T=%0t: [TEST] Testing branch with immediate (MUX_output = 2'b01).", $time);
8176
MUX_output = 2'b01;
82-
pc_plus1_imm = 16'h1234;
77+
imm = 16'h000E; // Adds 14
8378
// Set other inputs to different values to ensure they aren't selected
84-
pc_plus1 = 16'hFFFF;
8579
alu_out = 16'hEEEE;
8680
@(posedge clk);
8781
#1;
88-
if (nxt_instr === 16'h1234) begin
82+
if (nxt_instr === 16'h0011) begin
8983
$display("T=%0t: [PASS] PC branched to immediate address 0x%h.", $time, nxt_instr);
9084
end else begin
91-
$display("T=%0t: [FAIL] PC is 0x%h, expected 0x1234.", $time, nxt_instr);
85+
$display("T=%0t: [FAIL] PC is 0x%h, expected 0x0011.", $time, nxt_instr);
9286
end
9387

9488

9589
// 5. Test Case: Jump via ALU (MUX_output = 10)
9690
$display("\n T=%0t: [TEST] Testing jump via ALU output (MUX_output = 2'b10).", $time);
9791
MUX_output = 2'b10;
9892
alu_out = 16'hABCD;
99-
// Set other inputs to different values
100-
pc_plus1 = 16'hFFFF;
101-
pc_plus1_imm = 16'hEEEE;
93+
// Set other input to different values
94+
imm = 16'hEEEE;
10295
@(posedge clk);
10396
#1;
10497
if (nxt_instr === 16'hABCD) begin
@@ -112,7 +105,6 @@ module pc_tb;
112105

113106
// a. Increment
114107
MUX_output = 2'b00;
115-
pc_plus1 = 16'hABCE;
116108
@(posedge clk);
117109
#1;
118110
$display("T=%0t: [INFO] Current PC: 0x%h (After increment)", $time, nxt_instr);
@@ -126,12 +118,12 @@ module pc_tb;
126118

127119
// c. Branch
128120
MUX_output = 2'b01;
129-
pc_plus1_imm = 16'hCAFE;
121+
imm = 16'h010F;
130122
@(posedge clk);
131123
#1;
132124
$display("T=%0t: [INFO] Current PC: 0x%h (After branch)", $time, nxt_instr);
133-
if (nxt_instr !== 16'hCAFE) begin
134-
$display("T=%0t: [FAIL] Back-to-back sequence failed. PC is 0x%h, expected 0xCAFE.", $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);
135127
end else begin
136128
$display("T=%0t: [PASS] Back-to-back sequence successful.", $time);
137129
end
@@ -141,15 +133,14 @@ module pc_tb;
141133
// of incrementing the PC
142134
$display("\n T=%0t: [TEST] Testing undefined MUX select (2'b11).", $time);
143135
MUX_output = 2'b11;
144-
pc_plus1 = 16'h0009; // Change inputs to see if they are ignored
145-
pc_plus1_imm = 16'h000A;
146-
alu_out = 16'h000B;
136+
imm = 16'h148A; // Random Inputs to ensure they're ignored
137+
alu_out = 16'h000B;// Random Inputs to ensure they're ignored
147138
@(posedge clk);
148139
#1;
149-
if (nxt_instr === 16'h0009) begin
150-
$display("T=%0t: [PASS] PC correctly incremented to PC+1 at 16'h0009.", $time, nxt_instr);
140+
if (nxt_instr === 16'hC000) begin
141+
$display("T=%0t: [PASS] PC correctly incremented to PC+1 at 0x%h.", $time, nxt_instr);
151142
end else begin
152-
$display("T=%0t: [FAIL] PC changed to 16'h000A or 16'h000B, expected it to go to 16'h0009.", $time, nxt_instr);
143+
$display("T=%0t: [FAIL] PC changed to 0x%h, expected it to go to 16'hC000.", $time, nxt_instr);
153144
end
154145

155146
// 8. Test Case: Asynchronous Reset again

0 commit comments

Comments
 (0)