Skip to content

Commit c09d8bd

Browse files
committed
added signal for end of program, BUG TO FIX: 1.s output sets all the registers except x1
1 parent ce9e284 commit c09d8bd

File tree

8 files changed

+57
-39
lines changed

8 files changed

+57
-39
lines changed

pipelined/modules/ex_mem_register.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module ex_mem_register (
44
input wire clk,
55
input wire reset,
66
input wire en,
7-
input wire [95:0] d,
8-
output reg [95:0] q
7+
input wire [294:0] d,
8+
output reg [294:0] q
99
);
1010

1111
always @(posedge clk or posedge reset) begin
1212
if (reset)
13-
q <= {96{1'b0}};
13+
q <= {295{1'b0}};
1414
else if (en)
1515
q <= d;
1616
end

pipelined/modules/id_ex_register.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module id_ex_register (
44
input wire clk,
55
input wire reset,
66
input wire en,
7-
input wire [229:0] d,
8-
output reg [229:0] q
7+
input wire [230:0] d,
8+
output reg [230:0] q
99
);
1010

1111
always @(posedge clk or posedge reset) begin
1212
if (reset)
13-
q <= {230{1'b0}};
13+
q <= {231{1'b0}};
1414
else if (en)
1515
q <= d;
1616
end

pipelined/modules/if_id_register.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module if_id_register (
44
input wire clk,
55
input wire reset,
66
input wire en,
7-
input wire [95:0] d,
8-
output reg [95:0] q
7+
input wire [96:0] d,
8+
output reg [96:0] q
99
);
1010

1111
always @(posedge clk or posedge reset) begin
1212
if (reset)
13-
q <= {96{1'b0}};
13+
q <= {97{1'b0}};
1414
else if (en)
1515
q <= d;
1616
end

pipelined/modules/mem_wb_register.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module mem_wb_register (
44
input wire clk,
55
input wire reset,
66
input wire en,
7-
input wire [161:0] d,
8-
output reg [161:0] q
7+
input wire [162:0] d,
8+
output reg [162:0] q
99
);
1010

1111
always @(posedge clk or posedge reset) begin
1212
if (reset)
13-
q <= {162{1'b0}};
13+
q <= {163{1'b0}};
1414
else if (en)
1515
q <= d;
1616
end

pipelined/testcases/1.s

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
begin:
2-
addi x1, x0, 3
2+
addi x1, x0, 1
3+
addi x2, x0, 2
4+
addi x3, x0, 3
5+
addi x4, x0, 4
6+
addi x5, x0, 5
37

48
exit:
59
nop

pipelined/testcases/assembler.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ def generate_testbench(instructions):
213213
module testbench_pipelined();
214214
reg clk;
215215
reg reset;
216+
wire end_program;
217+
216218
217219
integer cycle_count = 0;
218220
real execution_time;
@@ -227,12 +229,13 @@ def generate_testbench(instructions):
227229
228230
initial begin
229231
reset = 1;
230-
#15 reset = 0;
232+
#6 reset = 0;
231233
end
232234
233235
cpu_pipelined cpu(
234236
.clk(clk),
235-
.reset(reset)
237+
.reset(reset),
238+
.end_program(end_program)
236239
);
237240
238241
initial begin
@@ -247,10 +250,8 @@ def generate_testbench(instructions):
247250
248251
@(negedge reset);
249252
250-
#15;
251-
252-
// Run simulation until a NOP (halt)
253-
while (cpu.instruction !== 32'b0) begin
253+
// Run simulation until end_program is high
254+
while (!end_program) begin
254255
@(posedge clk);
255256
end
256257

pipelined/verilog/cpu_pipelined.v

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
module cpu_pipelined(
1515
input clk,
16-
input reset
16+
input reset,
17+
output end_program
1718
);
1819
// program counter wale baba
1920
wire [63:0] pc_next;
@@ -35,17 +36,21 @@ module cpu_pipelined(
3536
.pc(pc_current), // current PC se
3637
.instruction(instruction) // instruction nikalo
3738
);
39+
40+
wire nop_instruction;
41+
assign nop_instruction = instruction == 0; // no operation instruction
3842

3943
// IF/ID Pipeline Register
4044
wire [63:0] if_id_pc;
4145
wire [31:0] if_id_instruction;
46+
wire if_id_end_instruction, if_id_nop_instruction;
4247

4348
if_id_register if_id(
4449
.clk(clk),
4550
.reset(reset),
4651
.en(1'b1),
47-
.d({pc_current, instruction}),
48-
.q({ if_id_pc, if_id_instruction})
52+
.d({pc_current, instruction , nop_instruction}),
53+
.q({if_id_pc , if_id_instruction, if_id_nop_instruction})
4954
);
5055

5156
// control signals - CPU ko batate hai kya karna hai
@@ -94,14 +99,14 @@ module cpu_pipelined(
9499
// ID/EX Pipeline Register
95100
wire [63:0] id_ex_pc, id_ex_reg_read_data1, id_ex_reg_read_data2;
96101
wire [31:0] id_ex_instruction;
97-
wire id_ex_branch, id_ex_mem_read, id_ex_mem_write, id_ex_mem_to_reg, id_ex_reg_write, id_ex_alu_src;
102+
wire id_ex_branch, id_ex_mem_read, id_ex_mem_write, id_ex_mem_to_reg, id_ex_reg_write, id_ex_alu_src, id_ex_nop_instruction;
98103

99104
id_ex_register id_ex(
100105
.clk(clk),
101106
.reset(reset),
102107
.en(1'b1),
103-
.d({if_id_pc, reg_read_data1, reg_read_data2, if_id_instruction, branch, mem_read, mem_write, mem_to_reg, reg_write, alu_src}),
104-
.q({id_ex_pc, id_ex_reg_read_data1, id_ex_reg_read_data2, id_ex_instruction, id_ex_branch, id_ex_mem_read, id_ex_mem_write, id_ex_mem_to_reg, id_ex_reg_write, id_ex_alu_src})
108+
.d({if_id_pc, reg_read_data1, reg_read_data2, if_id_instruction, branch, mem_read, mem_write, mem_to_reg, reg_write, alu_src, if_id_nop_instruction}),
109+
.q({id_ex_pc, id_ex_reg_read_data1, id_ex_reg_read_data2, id_ex_instruction, id_ex_branch, id_ex_mem_read, id_ex_mem_write, id_ex_mem_to_reg, id_ex_reg_write, id_ex_alu_src, id_ex_nop_instruction})
105110
);
106111

107112
// ALU ke signals
@@ -132,14 +137,14 @@ module cpu_pipelined(
132137
// EX/MEM Pipeline Register
133138
wire [63:0] ex_mem_pc, ex_mem_alu_result, ex_mem_reg_read_data2, ex_mem_branch_target;
134139
wire [31:0] ex_mem_instruction;
135-
wire ex_mem_zero, ex_mem_branch, ex_mem_mem_read, ex_mem_mem_write, ex_mem_mem_to_reg, ex_mem_reg_write;
140+
wire ex_mem_zero, ex_mem_branch, ex_mem_mem_read, ex_mem_mem_write, ex_mem_mem_to_reg, ex_mem_reg_write, ex_mem_nop_instruction;
136141

137142
ex_mem_register ex_mem(
138143
.clk(clk),
139144
.reset(reset),
140145
.en(1'b1),
141-
.d({id_ex_pc , alu_result , id_ex_reg_read_data2 , branch_target , id_ex_instruction , zero , id_ex_branch , id_ex_mem_read , id_ex_mem_write , id_ex_mem_to_reg , id_ex_reg_write}),
142-
.q({ex_mem_pc, ex_mem_alu_result, ex_mem_reg_read_data2, ex_mem_branch_target, ex_mem_instruction, ex_mem_zero, ex_mem_branch, ex_mem_mem_read, ex_mem_mem_write, ex_mem_mem_to_reg, ex_mem_reg_write})
146+
.d({id_ex_pc , alu_result , id_ex_reg_read_data2 , branch_target , id_ex_instruction , zero , id_ex_branch , id_ex_mem_read , id_ex_mem_write , id_ex_mem_to_reg , id_ex_reg_write , id_ex_nop_instruction}),
147+
.q({ex_mem_pc, ex_mem_alu_result, ex_mem_reg_read_data2, ex_mem_branch_target, ex_mem_instruction, ex_mem_zero, ex_mem_branch, ex_mem_mem_read, ex_mem_mem_write, ex_mem_mem_to_reg, ex_mem_reg_write, ex_mem_nop_instruction})
143148
);
144149

145150
wire branch_taken; // jump karna hai ya nahi
@@ -163,19 +168,22 @@ module cpu_pipelined(
163168
// MEM/WB Pipeline Register
164169
wire [63:0] mem_wb_mem_read_data, mem_wb_alu_result;
165170
wire [31:0] mem_wb_instruction;
166-
wire mem_wb_mem_to_reg, mem_wb_reg_write;
171+
wire mem_wb_mem_to_reg, mem_wb_reg_write, mem_wb_nop_instruction;
167172

168173
mem_wb_register mem_wb(
169174
.clk(clk),
170175
.reset(reset),
171176
.en(1'b1),
172-
.d({mem_read_data , ex_mem_alu_result, ex_mem_instruction, ex_mem_mem_to_reg, ex_mem_reg_write}),
173-
.q({mem_wb_mem_read_data, mem_wb_alu_result, mem_wb_instruction, mem_wb_mem_to_reg, mem_wb_reg_write})
177+
.d({mem_read_data , ex_mem_alu_result, ex_mem_instruction, ex_mem_mem_to_reg, ex_mem_reg_write, ex_mem_nop_instruction}),
178+
.q({mem_wb_mem_read_data, mem_wb_alu_result, mem_wb_instruction, mem_wb_mem_to_reg, mem_wb_reg_write, mem_wb_nop_instruction})
174179
);
175180

176181

177182
// register me value write back karo
178183
assign reg_write_data = mem_wb_mem_to_reg ? mem_wb_mem_read_data : mem_wb_alu_result; // memory se ya ALU se value select karo
179184
assign rd = mem_wb_instruction[11:7]; // destination register ka number
180185

186+
// end_program signal
187+
assign end_program = mem_wb_nop_instruction;
188+
181189
endmodule

pipelined/verilog/testbench_pipelined.v

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module testbench_pipelined();
44
reg clk;
55
reg reset;
6+
wire end_program;
7+
68

79
integer cycle_count = 0;
810
real execution_time;
@@ -17,17 +19,22 @@ module testbench_pipelined();
1719

1820
initial begin
1921
reset = 1;
20-
#15 reset = 0;
22+
#6 reset = 0;
2123
end
2224

2325
cpu_pipelined cpu(
2426
.clk(clk),
25-
.reset(reset)
27+
.reset(reset),
28+
.end_program(end_program)
2629
);
2730

2831
initial begin
29-
cpu.imem.memory[0] = 32'b00000000001100000000000010010011;
30-
cpu.imem.memory[1] = 32'b00000000000000000000000000000000;
32+
cpu.imem.memory[0] = 32'b00000000000100000000000010010011;
33+
cpu.imem.memory[1] = 32'b00000000001000000000000100010011;
34+
cpu.imem.memory[2] = 32'b00000000001100000000000110010011;
35+
cpu.imem.memory[3] = 32'b00000000010000000000001000010011;
36+
cpu.imem.memory[4] = 32'b00000000010100000000001010010011;
37+
cpu.imem.memory[5] = 32'b00000000000000000000000000000000;
3138
end
3239

3340

@@ -38,10 +45,8 @@ module testbench_pipelined();
3845

3946
@(negedge reset);
4047

41-
#15;
42-
43-
// Run simulation until a NOP (halt)
44-
while (cpu.instruction !== 32'b0) begin
48+
// Run simulation until end_program is high
49+
while (!end_program) begin
4550
@(posedge clk);
4651
end
4752

0 commit comments

Comments
 (0)