@@ -5,7 +5,6 @@ module testbench_pipelined();
55 reg reset;
66 wire end_program;
77
8-
98 integer cycle_count = 0 ;
109 real execution_time;
1110 real execution_time_ms;
@@ -15,16 +14,18 @@ module testbench_pipelined();
1514 initial begin
1615 clk = 0 ;
1716 reset = 1 ;
18- #6 reset = 0 ;
1917
20-
18+ // Initialize instruction memory first
2119 cpu.imem.memory[0 ] = 32'b00000000000100000000000010010011 ;
2220 cpu.imem.memory[1 ] = 32'b00000000001000000000000100010011 ;
2321 cpu.imem.memory[2 ] = 32'b00000000001100000000000110010011 ;
2422 cpu.imem.memory[3 ] = 32'b00000000010000000000001000010011 ;
2523 cpu.imem.memory[4 ] = 32'b00000000010100000000001010010011 ;
2624 cpu.imem.memory[5 ] = 32'b00000000000000000000000000000000 ;
27-
25+
26+ // promper initialization
27+ #10 reset = 0 ;
28+
2829 forever #5 clk = ~ clk;
2930 end
3031
@@ -45,6 +46,9 @@ module testbench_pipelined();
4546 while (! end_program) begin
4647 @(posedge clk);
4748 end
49+
50+ // Allow pipeline to flush completely
51+ repeat (5 ) @(posedge clk);
4852
4953 // Print register contents
5054 $display ("Register file contents:" );
@@ -75,4 +79,72 @@ module testbench_pipelined();
7579 $finish ;
7680 end
7781
82+ always @(posedge clk) begin
83+ if (! reset) begin
84+ cycle_count = cycle_count + 1 ;
85+ $display ("--------------------------------" );
86+ $display ("Time=%0t, Cycle=%0d" , $time , cycle_count);
87+
88+ $display ("PIPELINE STATE:" );
89+ $display ("IF Stage: PC=%h, Instruction=%h" , cpu.pc_current, cpu.instruction);
90+
91+ // Decode current instruction in IF stage
92+ if (cpu.instruction != 0 ) begin
93+ case (cpu.instruction[6 :0 ])
94+ 7'b0110011 : begin // R-type
95+ case (cpu.instruction[14 :12 ])
96+ 3'b000 : begin
97+ if (cpu.instruction[31 :25 ] == 7'b0000000 )
98+ $display ("IF: add x%0d, x%0d, x%0d" ,
99+ cpu.instruction[11 :7 ], cpu.instruction[19 :15 ], cpu.instruction[24 :20 ]);
100+ else
101+ $display ("IF: sub x%0d, x%0d, x%0d" ,
102+ cpu.instruction[11 :7 ], cpu.instruction[19 :15 ], cpu.instruction[24 :20 ]);
103+ end
104+ 3'b111 : $display ("IF: and x%0d, x%0d, x%0d" ,
105+ cpu.instruction[11 :7 ], cpu.instruction[19 :15 ], cpu.instruction[24 :20 ]);
106+ 3'b110 : $display ("IF: or x%0d, x%0d, x%0d" ,
107+ cpu.instruction[11 :7 ], cpu.instruction[19 :15 ], cpu.instruction[24 :20 ]);
108+ endcase
109+ end
110+ 7'b0000011 : $display ("IF: ld x%0d, %0d(x%0d)" ,
111+ cpu.instruction[11 :7 ], $signed ({{52 {cpu.instruction[31 ]}}, cpu.instruction[31 :20 ]}), cpu.instruction[19 :15 ]);
112+ 7'b0100011 : $display ("IF: sd x%0d, %0d(x%0d)" ,
113+ cpu.instruction[24 :20 ], $signed ({{52 {cpu.instruction[31 ]}}, cpu.instruction[31 :25 ], cpu.instruction[11 :7 ]}), cpu.instruction[19 :15 ]);
114+ 7'b1100011 : $display ("IF: beq x%0d, x%0d, %0d" ,
115+ cpu.instruction[19 :15 ], cpu.instruction[24 :20 ],
116+ $signed ({{51 {cpu.instruction[31 ]}}, cpu.instruction[7 ], cpu.instruction[30 :25 ], cpu.instruction[11 :8 ], 1'b0 }));
117+ 7'b0010011 : $display ("IF: addi x%0d, x%0d, %0d" ,
118+ cpu.instruction[11 :7 ], cpu.instruction[19 :15 ],
119+ $signed ({{52 {cpu.instruction[31 ]}}, cpu.instruction[31 :20 ]}));
120+ endcase
121+ end
122+
123+ // Show ID stage activity
124+ $display ("ID Stage: rs1=x%0d (%0d), rs2=x%0d (%0d), rd=x%0d" ,
125+ cpu.rs1, cpu.reg_read_data1, cpu.rs2, cpu.reg_read_data2, cpu.rd);
126+
127+ // Show EX stage activity (removed alu_control as it doesn't exist)
128+ $display ("EX Stage: ALU Result=%0h" , cpu.alu_result);
129+
130+ // Show MEM stage activity
131+ if (cpu.mem_write)
132+ $display ("MEM Stage: Writing %0d to address %0d" ,
133+ cpu.reg_read_data2, cpu.alu_result);
134+ if (cpu.mem_read)
135+ $display ("MEM Stage: Reading from address %0d, value=%0d" ,
136+ cpu.alu_result, cpu.mem_read_data);
137+
138+ // Show WB stage activity
139+ if (cpu.reg_write && cpu.rd != 0 )
140+ $display ("WB Stage: Writing %0d to register x%0d" ,
141+ cpu.reg_write_data, cpu.rd);
142+
143+ // Control signals
144+ $display ("Control signals: branch=%b, mem_read=%b, mem_to_reg=%b, mem_write=%b, alu_src=%b, reg_write=%b" ,
145+ cpu.branch, cpu.mem_read, cpu.mem_to_reg, cpu.mem_write, cpu.alu_src, cpu.reg_write);
146+
147+ end
148+ end
149+
78150endmodule
0 commit comments