Skip to content

Commit a8d070a

Browse files
committed
fixed data_memory in sequential
1 parent 0c283ba commit a8d070a

File tree

11 files changed

+661
-44
lines changed

11 files changed

+661
-44
lines changed

pipelined/modules/program_counter.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ module program_counter(
22
input clk,
33
input reset,
44
input [63:0] next_pc,
5+
input stall,
56
output reg [63:0] pc
67
);
78
always @(posedge clk or posedge reset) begin
89
if (reset)
910
pc <= 64'b0;
10-
else
11+
else if (!stall)
1112
pc <= next_pc;
1213
end
1314
endmodule

pipelined/test_pipelined.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if [ $? -eq 0 ]; then
2929
# if you wanna generate waveforms bruhhhh
3030
if [ -f test_results/cpu_pipelined_test.vcd ]; then
3131
echo "Generating waveform..."
32-
gtkwave test_results/cpu_pipelined_test.vcd &
32+
gtkwave -A --rcvar 'fontname_signals Monospace 12' --rcvar 'fontname_waves Monospace 12' test_results/cpu_pipelined_test.vcd &
3333
else
3434
echo "Error: Waveform file not generated"
3535
fi

pipelined/testcases/3.s

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
begin:
22
beq x0, x0, L1
3-
addi x6, x0, 3
3+
addi x6, x0, 4
4+
5+
L2:
6+
addi x7, x0, 8
47

58
L1:
69
addi x5, x0, 4
710

811
end:
9-
nop
12+
nop
13+
14+
15+
# only x5

pipelined/testcases/4.s

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
begin:
2-
beq x0, x0, end
2+
addi x4, x0, 1
3+
addi x8, x0, 1
4+
addi x8, x0, 1
5+
beq x0, x4, L1
6+
addi x6, x0, 4
37

48
L1:
59
addi x5, x0, 4
610

7-
L2:
8-
addi x6, x0, 4
11+
end:
12+
nop
913

10-
L3:
11-
addi x7, x0, 4
1214

13-
end:
14-
nop
15+
# both x5 and x6

pipelined/testcases/assembler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ def main():
6767

6868
# Handle NOP instruction
6969
if tokens[0] == "nop":
70-
# NOP is encoded as all zeros
71-
binary_instruction = "00000000000000000000000000000000"
70+
# NOP is encoded as all ones
71+
binary_instruction = "11111111111111111111111111111111"
7272
instructions.append(f" cpu.imem.memory[{instruction_index}] = 32'b{binary_instruction};")
7373
current_address += 4
7474
instruction_index += 1
@@ -231,7 +231,7 @@ def generate_testbench(instructions):
231231
// promper initialization
232232
#10 reset = 0;
233233
234-
forever #5 clk = ~clk;
234+
forever #15 clk = ~clk;
235235
end
236236
237237
cpu_pipelined cpu(

pipelined/verilog/cpu_pipelined.v

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,36 @@ module cpu_pipelined(
2828
.clk(clk),
2929
.reset(reset),
3030
.next_pc(pc_next), // agli instruction ka address
31+
.stall(branch_stall),
3132
.pc(pc_current) // current instruction ka address
3233
);
3334

35+
3436
// program instructions ko store karta hai
3537
instruction_memory imem( // REMEMBER INITIALIZED AS imem, so you can do cpu.imem.memory[0] in testbench
3638
.pc(pc_current), // current PC se
3739
.instruction(instruction) // instruction nikalo
3840
);
3941

4042
wire nop_instruction;
41-
assign nop_instruction = instruction == 0; // no operation instruction
43+
assign nop_instruction = instruction == -1; // no operation instruction
4244

4345
// IF/ID Pipeline Register
4446
wire [63:0] if_id_pc;
4547
wire [31:0] if_id_instruction;
4648
wire if_id_end_instruction, if_id_nop_instruction;
4749

4850
// 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;
51+
wire branch_stall;
52+
assign branch_stall = if_id_instruction[6:0] == 7'b1100011;
53+
wire [31:0] stall_instruction;
54+
assign stall_instruction = branch_stall ? 32'b00000000000000000000000000000000 : instruction;
5355

5456
if_id_register if_id(
5557
.clk(clk),
5658
.reset(reset),
5759
.en(1'b1),
58-
.d({pc_current, halt_instruction , nop_instruction}),
60+
.d({pc_current, stall_instruction, nop_instruction}),
5961
.q({if_id_pc , if_id_instruction, if_id_nop_instruction})
6062
);
6163

@@ -154,8 +156,10 @@ module cpu_pipelined(
154156
);
155157

156158
wire branch_taken; // jump karna hai ya nahi
157-
assign branch_taken = ex_mem_branch & ex_mem_zero; // branch lena hai ya nahi
158-
assign pc_next = branch_taken ? ex_mem_branch_target : (branch_halt? pc_current : pc_current + 4); // next PC set karo
159+
assign branch_taken = id_ex_branch & zero; // branch lena hai ya nahi
160+
assign pc_next = branch_taken ? branch_target : id_ex_pc + 4; // next PC set karo
161+
// assign branch_taken = ex_mem_branch & ex_mem_zero; // branch lena hai ya nahi
162+
// assign pc_next = branch_taken ? ex_mem_branch_target : ex_mem_pc + 4; // next PC set karo
159163

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

pipelined/verilog/testbench_pipelined.v

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

1818
// Initialize instruction memory first
19-
cpu.imem.memory[0] = 32'b00000000000000000000100001100011;
20-
cpu.imem.memory[1] = 32'b00000000010000000000001010010011;
21-
cpu.imem.memory[2] = 32'b00000000010000000000001100010011;
22-
cpu.imem.memory[3] = 32'b00000000010000000000001110010011;
23-
cpu.imem.memory[4] = 32'b00000000000000000000000000000000;
19+
cpu.imem.memory[0] = 32'b00000000000000000000011001100011;
20+
cpu.imem.memory[1] = 32'b00000000010000000000001100010011;
21+
cpu.imem.memory[2] = 32'b00000000100000000000001110010011;
22+
cpu.imem.memory[3] = 32'b00000000010000000000001010010011;
23+
cpu.imem.memory[4] = 32'b11111111111111111111111111111111;
2424

2525
// promper initialization
2626
#10 reset = 0;
2727

28-
forever #5 clk = ~clk;
28+
forever #15 clk = ~clk;
2929
end
3030

3131
cpu_pipelined cpu(

sequential/modules/data_memory.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module data_memory(
1515
end
1616

1717
always @(posedge clk) begin
18-
if (address > 1016) begin
18+
if (address > 1016 && mem_write) begin
1919
$fatal(1, "\n\nError: Invalid memory address %d\n", address);
2020
end
2121
if (mem_write && address <= 1016) begin
@@ -31,7 +31,7 @@ module data_memory(
3131
end
3232

3333
always @(*) begin
34-
if (address > 1016) begin
34+
if (address > 1016 && mem_read) begin
3535
$fatal(1, "\n\nError: Invalid memory address %d\n", address);
3636
end
3737
if (mem_read && address <= 1016) begin

sequential/testcases/8.s

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
begin:
2-
beq x0, x0, end
2+
beq x0, x0, L1
3+
addi x6, x0, 4
4+
5+
L2:
6+
addi x7, x0, 8
37

48
L1:
59
addi x5, x0, 4
610

7-
L2:
8-
addi x6, x0, 4
11+
end:
12+
nop
913

10-
L3:
11-
addi x7, x0, 4
1214

13-
end:
14-
nop
15+
# only x5

sequential/verilog/testbench_sequential.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
nb`timescale 1ns/1ps
1+
`timescale 1ns/1ps
22

33
module testbench_sequential();
44
reg clk;
@@ -26,10 +26,10 @@ module testbench_sequential();
2626
);
2727

2828
initial begin
29-
cpu.imem.memory[0] = 32'b00000000000000000000100001100011;
30-
cpu.imem.memory[1] = 32'b00000000010000000000001010010011;
31-
cpu.imem.memory[2] = 32'b00000000010000000000001100010011;
32-
cpu.imem.memory[3] = 32'b00000000010000000000001110010011;
29+
cpu.imem.memory[0] = 32'b00000000000000000000011001100011;
30+
cpu.imem.memory[1] = 32'b00000000010000000000001100010011;
31+
cpu.imem.memory[2] = 32'b00000000100000000000001110010011;
32+
cpu.imem.memory[3] = 32'b00000000010000000000001010010011;
3333
cpu.imem.memory[4] = 32'b00000000000000000000000000000000;
3434
end
3535

0 commit comments

Comments
 (0)