use std::sync::Arc;
use ::m6805::{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 M6805;
pub fn platform() -> PlatformRef {
Arc::new(M6805)
}
impl Platform for M6805 {
fn name(&self) -> &str {
"m6805"
}
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());
let branch_operand_index = has_target(control_flow)
.then(|| insn.operands().as_slice().len().checked_sub(1))
.flatten();
DecodedInsn {
len: insn.len() as u8,
bytes: insn.bytes().to_vec(),
text: insn.as_string(),
control_flow,
branch_operand_index,
named_register: None,
data_refs: data_refs(&insn),
}
}
}
fn map_control_flow(cf: ::m6805::ControlFlow) -> ControlFlow {
use ::m6805::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 | CLR => Access::Write,
NEG | COM | LSR | ROR | ASR | LSL | ROL | DEC | INC | BSET | BCLR => Access::ReadWrite,
LDA | LDX | CMP | CPX | BIT | SUB | SBC | AND | EOR | ADC | ORA | ADD | TST | BRSET
| BRCLR => Access::Read,
_ => return None,
})
}
fn data_refs(insn: &Instruction) -> Vec<DataRef> {
let Some(access) = memory_access(insn.mnemonic()) else {
return Vec::new();
};
let offset = insn.operands().as_slice().iter().find_map(|op| match op {
Operand::Direct(a) => Some(u32::from(*a)),
Operand::Extended(a) => Some(u32::from(*a)),
_ => None,
});
match offset {
Some(offset) => vec![DataRef {
space: CODE,
offset,
kind: access.kind(),
certainty: Certainty::Definite,
operand: Some(0),
}],
None => Vec::new(),
}
}
#[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_and_targets() {
let decode = |bytes: &[u8]| M6805.decode(0x1000, bytes);
let jsr = decode(&[0xCD, 0x0C, 0xB5]);
assert!(matches!(
jsr.control_flow,
ControlFlow::Call { target: 0x0CB5, .. }
));
assert_eq!(branch_target(&jsr), Some(0x0CB5));
assert_eq!(jsr.branch_operand_index, Some(0));
let brset = decode(&[0x0E, 0x50, 0x10]);
assert!(matches!(brset.control_flow, ControlFlow::Choice { .. }));
assert_eq!(brset.branch_operand_index, Some(2));
assert_eq!(decode(&[0xFC]).control_flow, ControlFlow::Diverge);
}
#[test]
fn data_refs_classify_access() {
use XrefType::{Jump, Read, ReadWrite, Write};
let e = |bytes: &[u8]| edges(&M6805, bytes);
assert_eq!(e(&[0xB6, 0x50]), vec![(CODE, 0x50, Read)]); assert_eq!(e(&[0xC7, 0x12, 0x34]), vec![(CODE, 0x1234, Write)]); assert_eq!(e(&[0x3A, 0x50]), vec![(CODE, 0x50, ReadWrite)]); assert_eq!(e(&[0x16, 0x50]), vec![(CODE, 0x50, ReadWrite)]); assert_eq!(e(&[0xCC, 0x12, 0x34]), vec![(CODE, 0x1234, Jump)]); assert!(e(&[0xA6, 0x42]).is_empty()); }
}