i8051_disassembler/platform/
mos6502.rs1use std::sync::Arc;
8
9use ::mos6502::{Instruction, Mnemonic, Operand};
10
11use crate::address::{AddressSpace, XrefType};
12
13use super::{
14 Certainty, ControlFlow, DataRef, DecodedInsn, Platform, PlatformRef, RegionDef, RegionKind,
15};
16
17pub const CODE: AddressSpace = AddressSpace::new("CODE");
19
20static REGIONS: &[RegionDef] = &[RegionDef {
22 space: CODE,
23 kind: RegionKind::Code,
24 area_header: ".area CODE (CODE,ABS)\n",
25}];
26
27#[derive(Debug, Default, Clone, Copy)]
29pub struct Mos6502;
30
31pub fn platform() -> PlatformRef {
33 Arc::new(Mos6502)
34}
35
36impl Platform for Mos6502 {
37 fn name(&self) -> &str {
38 "mos6502"
39 }
40
41 fn regions(&self) -> &[RegionDef] {
42 REGIONS
43 }
44
45 fn max_insn_len(&self) -> usize {
46 Instruction::MAX_LENGTH
47 }
48
49 fn decode(&self, pc: u32, bytes: &[u8]) -> DecodedInsn {
50 let insn = Instruction::decode_from_bytes(pc, bytes);
51 let control_flow = map_control_flow(insn.control_flow());
52 DecodedInsn {
53 len: insn.len() as u8,
54 bytes: insn.bytes().to_vec(),
55 text: insn.as_string(),
56 control_flow,
57 branch_operand_index: has_target(control_flow).then_some(0),
59 named_register: None,
60 data_refs: data_refs(&insn),
61 }
62 }
63}
64
65fn map_control_flow(cf: ::mos6502::ControlFlow) -> ControlFlow {
66 use ::mos6502::ControlFlow as M;
67 match cf {
68 M::Continue { next } => ControlFlow::Continue { next },
69 M::Jump { target } => ControlFlow::Jump { target },
70 M::Call { target, return_pc } => ControlFlow::Call { target, return_pc },
71 M::Choice {
72 fall_through,
73 branch_target,
74 } => ControlFlow::Choice {
75 fall_through,
76 branch_target,
77 },
78 M::Diverge => ControlFlow::Diverge,
79 }
80}
81
82fn has_target(cf: ControlFlow) -> bool {
83 matches!(
84 cf,
85 ControlFlow::Jump { .. } | ControlFlow::Call { .. } | ControlFlow::Choice { .. }
86 )
87}
88
89#[derive(Debug, Clone, Copy)]
91enum Access {
92 Read,
93 Write,
94 ReadWrite,
95}
96
97impl Access {
98 fn kind(self) -> XrefType {
99 match self {
100 Access::Read => XrefType::Read,
101 Access::Write => XrefType::Write,
102 Access::ReadWrite => XrefType::ReadWrite,
103 }
104 }
105}
106
107fn memory_access(mnemonic: Mnemonic) -> Option<Access> {
111 use Mnemonic::*;
112 Some(match mnemonic {
113 STA | STX | STY => Access::Write,
114 ASL | LSR | ROL | ROR | INC | DEC => Access::ReadWrite,
115 LDA | LDX | LDY | CMP | CPX | CPY | ADC | SBC | AND | ORA | EOR | BIT => Access::Read,
116 _ => return None,
117 })
118}
119
120fn data_refs(insn: &Instruction) -> Vec<DataRef> {
124 let Some(access) = memory_access(insn.mnemonic()) else {
125 return Vec::new();
126 };
127 let Some(&operand) = insn.operands().as_slice().first() else {
128 return Vec::new();
129 };
130 let offset = match operand {
131 Operand::ZeroPage(a) | Operand::ZeroPageX(a) | Operand::ZeroPageY(a) => u32::from(a),
132 Operand::Absolute(a) | Operand::AbsoluteX(a) | Operand::AbsoluteY(a) => u32::from(a),
133 _ => return Vec::new(),
134 };
135 vec![DataRef {
136 space: CODE,
137 offset,
138 kind: access.kind(),
139 certainty: Certainty::Definite,
140 operand: Some(0),
141 }]
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147 use crate::platform::branch_target;
148 use crate::platform::test_util::edges;
149 use pretty_assertions::assert_eq;
150
151 #[test]
152 fn control_flow_targets() {
153 let decode = |bytes: &[u8]| Mos6502.decode(0x1000, bytes);
154
155 let jmp = decode(&[0x4C, 0x00, 0x20]);
157 assert_eq!(jmp.control_flow, ControlFlow::Jump { target: 0x2000 });
158 assert_eq!(branch_target(&jmp), Some(0x2000));
159 assert_eq!(jmp.branch_operand_index, Some(0));
160
161 assert!(matches!(
163 decode(&[0x20, 0x00, 0x20]).control_flow,
164 ControlFlow::Call { target: 0x2000, .. }
165 ));
166 assert_eq!(
167 decode(&[0x6C, 0x00, 0x20]).control_flow,
168 ControlFlow::Diverge
169 );
170 assert_eq!(decode(&[0x6C, 0x00, 0x20]).branch_operand_index, None);
171 }
172
173 #[test]
174 fn data_refs_classify_access() {
175 use XrefType::{Jump, Read, ReadWrite, Write};
176 let e = |bytes: &[u8]| edges(&Mos6502, bytes);
177 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()); }
183}