1
0
mirror of https://github.com/mre/mos6502.git synced 2024-06-04 03:29:27 +00:00

Add Addressing mode to Display for DecodedInstr

This commit is contained in:
Matthias 2023-06-29 10:45:11 +02:00
parent 902b69a053
commit efb1c3a3b8

View File

@ -115,6 +115,17 @@ pub enum OpInput {
UseAddress(u16),
}
impl Display for OpInput {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
OpInput::UseImplied => write!(f, ""),
OpInput::UseImmediate(v) => write!(f, "#${:02X}", v),
OpInput::UseRelative(v) => write!(f, "${:04X}", v),
OpInput::UseAddress(v) => write!(f, "${:04X}", v),
}
}
}
#[derive(Copy, Clone)]
pub enum AddressingMode {
Accumulator, // 1 LSR A work directly on accumulator
@ -158,11 +169,33 @@ pub struct DecodedInstr(pub Instruction, pub OpInput);
impl Display for DecodedInstr {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
// get addressing mode from instruction (if it exists)
let am: Option<AddressingMode> = OPCODES.get(self.0 as usize).and_then(|x| x.map(|x| x.1));
match self.1 {
OpInput::UseImplied => write!(f, "{:?}", self.0),
OpInput::UseImmediate(v) => write!(f, "{:?} #${:02X}", self.0, v),
OpInput::UseRelative(v) => write!(f, "{:?} ${:04X}", self.0, v),
OpInput::UseAddress(v) => write!(f, "{:?} ${:04X}", self.0, v),
OpInput::UseImmediate(imm) => write!(f, "{:?} #${:02X}", self.0, imm),
OpInput::UseRelative(rel) => write!(f, "{:?} ${:04X}", self.0, rel),
OpInput::UseAddress(addr) => match am {
Some(AddressingMode::Accumulator) => write!(f, "{:?} A", self.0),
Some(AddressingMode::Implied) => write!(f, "{:?}", self.0),
Some(AddressingMode::Immediate) => write!(f, "{:?} #${:02X}", self.0, addr),
Some(AddressingMode::ZeroPage) => write!(f, "{:?} :?${:02X}", self.0, addr),
Some(AddressingMode::ZeroPageX) => write!(f, "{:?} :?${:02X},X", self.0, addr),
Some(AddressingMode::ZeroPageY) => write!(f, "{:?} :?${:02X},Y", self.0, addr),
Some(AddressingMode::Relative) => write!(f, "{:?} ${:04X}", self.0, addr),
Some(AddressingMode::Absolute) => write!(f, "{:?} ${:04X}", self.0, addr),
Some(AddressingMode::AbsoluteX) => write!(f, "{:?} ${:04X},X", self.0, addr),
Some(AddressingMode::AbsoluteY) => write!(f, "{:?} ${:04X},Y", self.0, addr),
Some(AddressingMode::Indirect) => write!(f, "{:?} (${:04X})", self.0, addr),
Some(AddressingMode::IndexedIndirectX) => {
write!(f, "{:?} (${:04X},X)", self.0, addr)
}
Some(AddressingMode::IndirectIndexedY) => {
write!(f, "{:?} (${:04X}),Y", self.0, addr)
}
None => write!(f, "{:?} {}", self.0, self.1),
},
}
}
}