@@ -329,7 +329,7 @@ def generate_testbench(instructions):
329329 $display("ID Stage: rs1=x%0d (%0d), rs2=x%0d (%0d), rd=x%0d",
330330 cpu.rs1, cpu.reg_read_data1, cpu.rs2, cpu.reg_read_data2, cpu.reg_rd);
331331
332- // Show EX stage activity (removed alu_control as it doesn't exist)
332+ // Show EX stage activity
333333 $display("EX Stage: ALU Result=%0h", cpu.alu_result);
334334
335335 // Show MEM stage activity
@@ -349,6 +349,49 @@ def generate_testbench(instructions):
349349 $display("Control signals: branch=%b, mem_read=%b, mem_to_reg=%b, mem_write=%b, alu_src=%b, reg_write=%b",
350350 cpu.branch, cpu.mem_read, cpu.mem_to_reg, cpu.mem_write, cpu.alu_src, cpu.reg_write);
351351
352+ // Add pipeline register contents - MORE DETAILED INFO
353+ $display("Pipeline Registers:");
354+ $display("IF/ID: PC=%h, Instruction=%h", cpu.if_id_pc, cpu.if_id_instruction);
355+ $display("ID/EX: PC=%h, Instruction=%h, rs1=x%0d, rs2=x%0d, rd=x%0d, RegWrite=%b, MemWrite=%b",
356+ cpu.id_ex_pc, cpu.id_ex_instruction, cpu.id_ex_rs1, cpu.id_ex_rs2,
357+ cpu.id_ex_rd, cpu.id_ex_reg_write, cpu.id_ex_mem_write);
358+ $display("EX/MEM: Instruction=%h, rd=x%0d, RegWrite=%b, MemWrite=%b, ALUResult=%h",
359+ cpu.ex_mem_instruction, cpu.ex_mem_rd, cpu.ex_mem_reg_write,
360+ cpu.ex_mem_mem_write, cpu.ex_mem_alu_result);
361+ $display("MEM/WB: Instruction=%h, rd=x%0d, RegWrite=%b, MemToReg=%b",
362+ cpu.mem_wb_instruction, cpu.mem_wb_rd, cpu.mem_wb_reg_write,
363+ cpu.mem_wb_mem_to_reg);
364+
365+ // Hazard detection information
366+ if (cpu.stall) begin // Use cpu.stall directly instead of cpu.hazard_detection_unit.stall_pipeline
367+ $display("HAZARD DETECTED: Stalling pipeline");
368+ $display("Load-use hazard between instructions at PC=%h and PC=%h",
369+ cpu.if_id_pc - 4, cpu.if_id_pc);
370+ end
371+
372+ // Forwarding information
373+ if (cpu.forwardA != 0 || cpu.forwardB != 0) begin // Use cpu.forwardA/B directly
374+ $display("Forwarding active:");
375+ if (cpu.forwardA == 2'b10)
376+ $display("EX → EX Forwarding to rs1 (x%0d)", cpu.id_ex_rs1);
377+ if (cpu.forwardA == 2'b01)
378+ $display("MEM → EX Forwarding to rs1 (x%0d)", cpu.id_ex_rs1);
379+ if (cpu.forwardB == 2'b10)
380+ $display("EX → EX Forwarding to rs2 (x%0d)", cpu.id_ex_rs2);
381+ if (cpu.forwardB == 2'b01)
382+ $display("MEM → EX Forwarding to rs2 (x%0d)", cpu.id_ex_rs2);
383+ end
384+
385+ // Branch information
386+ if (cpu.branch && cpu.zero) begin // Use cpu.zero instead of cpu.alu_zero
387+ $display("Branch at PC=%h: Taking branch", cpu.pc_current);
388+ if (cpu.branch_mispredicted) // Use cpu.branch_mispredicted directly
389+ $display("Branch mispredicted: Pipeline flush required");
390+ end
391+
392+ // Pipeline stall/flush status
393+ if (cpu.flush) // Use cpu.flush instead of cpu.if_id_flush
394+ $display("Pipeline flushed");
352395 end
353396 end
354397
@@ -365,6 +408,7 @@ def generate_testbench(instructions):
365408
366409 print (f"Generated testbench file: { output_file } " )
367410
411+
368412def get_instruction_type (instruction ):
369413 if instruction in ["add" , "sub" , "or" , "and" ]:
370414 return 'R'
0 commit comments