Skip to content

Commit 7a2b5e3

Browse files
committed
Branch halting works: hrishikesh
1 parent 824b87a commit 7a2b5e3

File tree

7 files changed

+48
-8
lines changed

7 files changed

+48
-8
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pipelined/test_results/
2-
sequential/test_results/
2+
sequential/test_results/
3+
test.c
4+
./a.out

a.out

15.6 KB
Binary file not shown.

pipelined/testcases/1.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
begin:
22
addi x1, x0, 1
33
addi x2, x0, 2
4+
45
exit:
56
nop

pipelined/testcases/2.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
begin:
2+
addi x1, x0, 3 # x1 = 3
3+
addi x2, x0, 7 # x2 = 7
4+
5+
loop:
6+
beq x1, x0, exit # if x1==0, end program
7+
add x2, x2, x1 # x2 = x1 + x2
8+
addi x1, x1, -1 # x1--
9+
beq x0, x0, loop # loop
10+
11+
exit:
12+
nop

pipelined/testcases/3.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
begin:
2+
addi x4, x5, 1
3+
addi x8, x0, 0
4+
addi x8, x0, 0
5+
beq x1, x4, L1
6+
addi x6, x0, 3
7+
8+
L1:
9+
addi x5, x0, 4
10+
11+
end:
12+
nop

pipelined/verilog/cpu_pipelined.v

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,21 @@ module cpu_pipelined(
4545
wire [31:0] if_id_instruction;
4646
wire if_id_end_instruction, if_id_nop_instruction;
4747

48+
// check if_id_instruction to see if it is beq(opcode 1100011), send addi x0 x0 0, also sent pc_next = pc_current
49+
wire branch_halt;
50+
assign branch_halt = if_id_instruction[6:0] == 7'b1100011;
51+
wire [31:0] halt_instruction;
52+
assign halt_instruction = branch_halt ? 32'b00000000000000000000000000010011 : instruction;
53+
4854
if_id_register if_id(
4955
.clk(clk),
5056
.reset(reset),
5157
.en(1'b1),
52-
.d({pc_current, instruction , nop_instruction}),
58+
.d({pc_current, halt_instruction , nop_instruction}),
5359
.q({if_id_pc , if_id_instruction, if_id_nop_instruction})
5460
);
5561

62+
5663
// control signals - CPU ko batate hai kya karna hai
5764
wire branch; // branch instruction hai ya nahi
5865
wire mem_read; // memory se padhna hai
@@ -82,7 +89,6 @@ module cpu_pipelined(
8289
// Register File ke inputs set karo
8390
assign rs1 = if_id_instruction[19:15]; // source register 1 ka number
8491
assign rs2 = if_id_instruction[24:20]; // source register 2 ka number
85-
// assign rd = if_id_instruction[11:7]; // destination register ka number
8692

8793
// Register File - CPU ke registers ko handle karta hai
8894
register_file reg_file(
@@ -149,7 +155,7 @@ module cpu_pipelined(
149155

150156
wire branch_taken; // jump karna hai ya nahi
151157
assign branch_taken = ex_mem_branch & ex_mem_zero; // branch lena hai ya nahi
152-
assign pc_next = branch_taken ? ex_mem_branch_target : pc_current + 4; // next PC set karo
158+
assign pc_next = branch_taken ? ex_mem_branch_target : (branch_halt? pc_current : pc_current + 4); // next PC set karo
153159

154160
// memory ke signals
155161
wire signed [63:0] mem_read_data; // memory se padhi hui value

pipelined/verilog/testbench_pipelined.v

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ module testbench_pipelined();
1616
reset = 1;
1717

1818
// Initialize instruction memory first
19-
cpu.imem.memory[0] = 32'b00000000000100000000000010010011;
20-
cpu.imem.memory[1] = 32'b00000000001000000000000100010011;
21-
cpu.imem.memory[2] = 32'b00000000000000000000000000000000;
22-
19+
cpu.imem.memory[0] = 32'b00000000000100101000001000010011;
20+
cpu.imem.memory[1] = 32'b00000000000000000000010000010011;
21+
cpu.imem.memory[2] = 32'b00000000000000000000010000010011;
22+
cpu.imem.memory[3] = 32'b00000000010000001000010001100011;
23+
cpu.imem.memory[4] = 32'b00000000001100000000001100010011;
24+
cpu.imem.memory[5] = 32'b00000000010000000000001010010011;
25+
cpu.imem.memory[6] = 32'b00000000000000000000000000000000;
26+
27+
// promper initialization
28+
#10 reset = 0;
29+
2330
forever #5 clk = ~clk;
2431
end
2532

0 commit comments

Comments
 (0)