for (i = 0; i < 65536; i = i + 1) begin ram_data[i] = 32'h00000000; end
if (1) begin rom_file = // "user/data/hex/sw_lw.hex" "user/data/hex/addi.hex" ; $readmemh(rom_file, rom_data, 0, 16383); $display("IROM: Loaded instructions from %s", rom_file); end
end
// 写操作 (同步) always_ff @(posedge clk) begin if (we) begin ram_data[a] <= din; end end
// 复位 CPU #5; // 保持复位 25ns rst_n = 1; $display("========================================"); $display("CPU Reset Released at time %0t", $time); $display("========================================");
// 运行一段时间让 CPU 执行指令 #2000;
$display("========================================"); $display("Simulation finished at time %0t", $time); $display("========================================");
foreverbegin @(posedge clk); if (rst_n) begin // IF 级 if (u_CPU_TOP.valid_IF) $display("%0t\t| %h\t| %h\t| IF", $time, u_CPU_TOP.pc_IF, u_CPU_TOP.instr_IF);
// ID 级 if (u_CPU_TOP.valid_ID && u_CPU_TOP.instr_ID != 32'h00000013) // 跳过 NOP $display("%0t\t| %h\t| %h\t| ID", $time, u_CPU_TOP.pc_ID, u_CPU_TOP.instr_ID);
// EX 级 if (u_CPU_TOP.valid_EX) $display( "%0t\t| %h\t| --------\t| EX \t (ALU=0x%h)", $time, u_CPU_TOP.pc_EX, u_CPU_TOP.alu_result_EX );
// MEM 级 if (u_CPU_TOP.dram_we_MEM) begin $display("%0t\t| 0x%4h <| 0x%h\t|[MEM W]", $time, u_CPU_TOP.alu_result_MEM[17:2], u_CPU_TOP.rf_rd2_MEM); endelsebegin if (u_CPU_TOP.wd_sel_MEM) $display( "%0t\t| 0x%4h |> 0x%h\t|[MEM R]", $time, u_CPU_TOP.alu_result_MEM[17:2], u_CPU_TOP.DRAM_output_data ); end end end end
// 监控寄存器写回操作 initialbegin foreverbegin @(posedge clk); if (rst_n && u_CPU_TOP.rf_we_WB && u_CPU_TOP.wR_WB != 0) begin $display("%0t\t| x%-2d <= 0x%h \t|[WB]", $time, u_CPU_TOP.wR_WB, u_CPU_TOP.rf_wd_WB); end end end
// 打印寄存器堆内容的任务 taskautomatic print_register_file(); integer i; begin $display("\n========================================"); $display("Register File Contents:"); $display("========================================"); for (i = 0; i < 32; i = i + 1) begin if (u_CPU_TOP.u_registerf.rf_in[i] != 0) begin $display("x%0d\t= 0x%h\t(%0d)", i, u_CPU_TOP.u_registerf.rf_in[i], $signed(u_CPU_TOP.u_registerf.rf_in[i])); end end $display("========================================\n"); end endtask