nesrust/core/src/cpu/disassembler.rs
2025-01-17 22:18:08 -05:00

49 lines
1.7 KiB
Rust

use crate::cpu::op::AddressingMode::*;
use crate::cpu::op::{AddressingMode, Instruction};
use crate::cpu::Cpu;
impl Cpu<'_> {
pub fn disassemble_next_instr(&self) {
let registers = self.registers;
let op_code = self.memory_bus.get_byte(registers.pc);
let instr: Instruction = Self::INSTRUCTIONS[op_code];
let operation = instr.op().to_string();
let addr_mode = self.disassemble_addr_mode(instr.addr_mode());
dbg!(
"DIS {:#04x} - {:#02x} {:?} {:?} - A {:#02x}, X {:#02x}, Y {:#02x}, SP: {:#02x}, F: {:#02x}",
op_code,
registers.pc,
operation,
addr_mode,
registers.a,
registers.x,
registers.y,
registers.sp,
registers.status
);
}
fn disassemble_addr_mode(&self, addr_mode: AddressingMode, pc: u16) -> String {
let word_peek = self.memory_bus.get_word(pc);
let byte_peek = (word_peek & 0xFF) as u8;
match addr_mode {
ZP0 => format!("${:#02x}", byte_peek),
ZPX => format!("${:#02x},x", byte_peek),
ZPY => format!("${:#02x},x", byte_peek),
ABS => format!("${:#04x}", word_peek),
ABX => format!("${:#04x},x", word_peek),
ABY => format!("${:#04x},y", word_peek),
IND => format!("(${:#04x})", word_peek),
IDX => format!("(${:#02x},x)", word_peek),
IDY => format!("(${:#02x}),y", word_peek),
REL => format!("#${:#04x}", word_peek),
ACC => String::from('A'),
IMP => String::from(""),
IMM => format!("{:#02x}", byte_peek),
}
}
}