@@ -106,15 +106,6 @@ void CommitStage::execute(CPUState& state) {
106106 // 这确保Store指令提交到内存后,相应的Store Buffer条目被清除
107107 state.store_buffer ->retire_stores_before (committed_inst->get_instruction_id ());
108108
109- // DiffTest: 当乱序CPU提交一条指令时,同步执行参考CPU并比较状态
110- if (state.cpu_interface && state.cpu_interface ->isDiffTestEnabled ()) {
111- LOGT (DIFFTEST , " inst=%" PRId64 " [COMMIT_TRACK] commit count=%" PRId64,
112- committed_inst->get_instruction_id (), state.instruction_count );
113- // 使用提交指令的PC进行DiffTest
114- state.cpu_interface ->performDiffTestWithCommittedPC (committed_inst->get_pc ());
115- LOGT (COMMIT , " run difftest comparison" );
116- }
117-
118109 // 处理跳转指令:只有is_jump=true的指令才会改变PC
119110 if (committed_inst->is_jump ()) {
120111 state.pc = committed_inst->get_jump_target ();
@@ -127,14 +118,32 @@ void CommitStage::execute(CPUState& state) {
127118
128119 // 处理系统调用
129120 if (committed_inst->get_decoded_info ().opcode == Opcode::SYSTEM ) {
130- if (InstructionExecutor::isSystemCall (committed_inst->get_decoded_info ())) {
121+ const auto & sys_inst = committed_inst->get_decoded_info ();
122+
123+ if (InstructionExecutor::isCsrInstruction (sys_inst)) {
124+ const uint32_t csr_addr = static_cast <uint32_t >(sys_inst.imm ) & 0xFFFU ;
125+ const auto csr_result = InstructionExecutor::executeCsrInstruction (
126+ sys_inst, committed_inst->get_src1_value (), state.csr_registers [csr_addr]);
127+ state.csr_registers [csr_addr] = csr_result.write_value ;
128+ LOGT (COMMIT , " inst=%" PRId64 " commit csr[0x%03x]: old=0x%" PRIx64 " , new=0x%" PRIx64,
129+ committed_inst->get_instruction_id (), csr_addr,
130+ csr_result.read_value , csr_result.write_value );
131+ } else if (InstructionExecutor::isSystemCall (sys_inst)) {
131132 // ECALL
132133 handle_ecall (state, committed_inst->get_pc ());
133- } else if (InstructionExecutor::isBreakpoint (committed_inst-> get_decoded_info () )) {
134+ } else if (InstructionExecutor::isBreakpoint (sys_inst )) {
134135 // EBREAK
135136 handle_ebreak (state);
136137 }
137138 }
139+
140+ // DiffTest: 在提交阶段所有体系结构状态更新完成后再做比较
141+ if (state.cpu_interface && state.cpu_interface ->isDiffTestEnabled ()) {
142+ LOGT (DIFFTEST , " inst=%" PRId64 " [COMMIT_TRACK] commit count=%" PRId64,
143+ committed_inst->get_instruction_id (), state.instruction_count );
144+ state.cpu_interface ->performDiffTestWithCommittedPC (committed_inst->get_pc ());
145+ LOGT (COMMIT , " run difftest comparison" );
146+ }
138147
139148 // 如果没有更多指令可提交,跳出循环
140149 if (!commit_result.has_more ) {
0 commit comments