use std::sync::Arc;
use ::mos6502::{Instruction, Mnemonic, Operand};
use crate::address::{AddressSpace, XrefType};
use super::{
Certainty, ControlFlow, DataRef, DecodedInsn, Platform, PlatformRef, RegionDef, RegionKind,
};
pub const CODE: AddressSpace = AddressSpace::new("CODE");
static REGIONS: &[RegionDef] = &[RegionDef {
space: CODE,
kind: RegionKind::Code,
area_header: ".area CODE (CODE,ABS)\n",
}];
#[derive(Debug, Default, Clone, Copy)]
pub struct Mos6502;
pub fn platform() -> PlatformRef {
Arc::new(Mos6502)
}
impl Platform for Mos6502 {
fn name(&self) -> &str {
"mos6502"
}
fn regions(&self) -> &[RegionDef] {
REGIONS
}
fn max_insn_len(&self) -> usize {
Instruction::MAX_LENGTH
}
fn decode(&self, pc: u32, bytes: &[u8]) -> DecodedInsn {
let insn = Instruction::decode_from_bytes(pc, bytes);
let control_flow = map_control_flow(insn.control_flow());
DecodedInsn {
len: insn.len() as u8,
bytes: insn.bytes().to_vec(),
text: insn.as_string(),
control_flow,
branch_operand_index: has_target(control_flow).then_some(0),
named_register: None,
data_refs: data_refs(&insn),
}
}
}
fn map_control_flow(cf: ::mos6502::ControlFlow) -> ControlFlow {
use ::mos6502::ControlFlow as M;
match cf {
M::Continue { next } => ControlFlow::Continue { next },
M::Jump { target } => ControlFlow::Jump { target },
M::Call { target, return_pc } => ControlFlow::Call { target, return_pc },
M::Choice {
fall_through,
branch_target,
} => ControlFlow::Choice {
fall_through,
branch_target,
},
M::Diverge => ControlFlow::Diverge,
}
}
fn has_target(cf: ControlFlow) -> bool {
matches!(
cf,
ControlFlow::Jump { .. } | ControlFlow::Call { .. } | ControlFlow::Choice { .. }
)
}
#[derive(Debug, Clone, Copy)]
enum Access {
Read,
Write,
ReadWrite,
}
impl Access {
fn kind(self) -> XrefType {
match self {
Access::Read => XrefType::Read,
Access::Write => XrefType::Write,
Access::ReadWrite => XrefType::ReadWrite,
}
}
}
fn memory_access(mnemonic: Mnemonic) -> Option<Access> {
use Mnemonic::*;
Some(match mnemonic {
STA | STX | STY => Access::Write,
ASL | LSR | ROL | ROR | INC | DEC => Access::ReadWrite,
LDA | LDX | LDY | CMP | CPX | CPY | ADC | SBC | AND | ORA | EOR | BIT => Access::Read,
_ => return None,
})
}
fn data_refs(insn: &Instruction) -> Vec<DataRef> {
let Some(access) = memory_access(insn.mnemonic()) else {
return Vec::new();
};
let Some(&operand) = insn.operands().as_slice().first() else {
return Vec::new();
};
let offset = match operand {
Operand::ZeroPage(a) | Operand::ZeroPageX(a) | Operand::ZeroPageY(a) => u32::from(a),
Operand::Absolute(a) | Operand::AbsoluteX(a) | Operand::AbsoluteY(a) => u32::from(a),
_ => return Vec::new(),
};
vec![DataRef {
space: CODE,
offset,
kind: access.kind(),
certainty: Certainty::Definite,
operand: Some(0),
}]
}
#[cfg(test)]
mod tests {
use super::*;
use crate::platform::branch_target;
use crate::platform::test_util::edges;
use pretty_assertions::assert_eq;
#[test]
fn control_flow_targets() {
let decode = |bytes: &[u8]| Mos6502.decode(0x1000, bytes);
let jmp = decode(&[0x4C, 0x00, 0x20]);
assert_eq!(jmp.control_flow, ControlFlow::Jump { target: 0x2000 });
assert_eq!(branch_target(&jmp), Some(0x2000));
assert_eq!(jmp.branch_operand_index, Some(0));
assert!(matches!(
decode(&[0x20, 0x00, 0x20]).control_flow,
ControlFlow::Call { target: 0x2000, .. }
));
assert_eq!(
decode(&[0x6C, 0x00, 0x20]).control_flow,
ControlFlow::Diverge
);
assert_eq!(decode(&[0x6C, 0x00, 0x20]).branch_operand_index, None);
}
#[test]
fn data_refs_classify_access() {
use XrefType::{Jump, Read, ReadWrite, Write};
let e = |bytes: &[u8]| edges(&Mos6502, bytes);
assert_eq!(e(&[0xAD, 0x34, 0x12]), vec![(CODE, 0x1234, Read)]); assert_eq!(e(&[0x8D, 0x34, 0x12]), vec![(CODE, 0x1234, Write)]); assert_eq!(e(&[0xEE, 0x34, 0x12]), vec![(CODE, 0x1234, ReadWrite)]); assert_eq!(e(&[0x4C, 0x34, 0x12]), vec![(CODE, 0x1234, Jump)]); assert!(e(&[0xA9, 0x42]).is_empty()); }
}