use std::sync::Arc;
use ::i8051::{Instruction, Mnemonic, Operand};
pub use ::i8051::format_direct;
use crate::address::{AddressSpace, AddressValue, XrefType};
use super::{
CanonicalAddr, Certainty, ControlFlow, DataRef, DecodedInsn, EntryPoint, Platform, PlatformRef,
RegionDef, RegionKind,
};
pub const CODE: AddressSpace = AddressSpace::new("CODE");
pub const IDATA: AddressSpace = AddressSpace::new("IDATA");
pub const SFR: AddressSpace = AddressSpace::new("SFR");
pub const BIT: AddressSpace = AddressSpace::new("BIT");
pub const XDATA: AddressSpace = AddressSpace::new("XDATA");
static REGIONS: &[RegionDef] = &[
RegionDef {
space: CODE,
kind: RegionKind::Code,
area_header: ".area CODE (CODE,ABS)\n",
},
RegionDef {
space: IDATA,
kind: RegionKind::Data,
area_header: ".area IDATA (IDATA,ABS)\n",
},
RegionDef {
space: SFR,
kind: RegionKind::Register,
area_header: ".area SFR (SFR,ABS)\n",
},
RegionDef {
space: BIT,
kind: RegionKind::Register,
area_header: ".area BIT (BIT,ABS)\n",
},
RegionDef {
space: XDATA,
kind: RegionKind::Data,
area_header: ".area XDATA (XDATA,ABS)\n",
},
];
static ENTRY_POINTS: &[EntryPoint] = &[
EntryPoint {
space: CODE,
offset: 0x00,
name: "INT_reset",
reason: "power-on reset",
},
EntryPoint {
space: CODE,
offset: 0x03,
name: "INT_ext0",
reason: "external interrupt 0",
},
EntryPoint {
space: CODE,
offset: 0x0B,
name: "INT_timer0",
reason: "timer 0 overflow",
},
EntryPoint {
space: CODE,
offset: 0x13,
name: "INT_ext1",
reason: "external interrupt 1",
},
EntryPoint {
space: CODE,
offset: 0x1B,
name: "INT_timer1",
reason: "timer 1 overflow",
},
EntryPoint {
space: CODE,
offset: 0x23,
name: "INT_serial",
reason: "serial port",
},
];
#[derive(Debug, Default, Clone, Copy)]
pub struct I8051;
pub fn platform() -> PlatformRef {
Arc::new(I8051)
}
impl Platform for I8051 {
fn name(&self) -> &str {
"i8051"
}
fn regions(&self) -> &[RegionDef] {
REGIONS
}
fn entry_points(&self) -> &[EntryPoint] {
ENTRY_POINTS
}
fn canonicalize(&self, space: AddressSpace, offset: AddressValue) -> CanonicalAddr {
if space != BIT || offset > 0xFF {
return CanonicalAddr {
space,
offset,
bit: None,
};
}
let n = offset as u8;
if n < 0x80 {
CanonicalAddr {
space: IDATA,
offset: AddressValue::from(0x20 + n / 8),
bit: Some(n % 8),
}
} else {
CanonicalAddr {
space: SFR,
offset: AddressValue::from(n & 0xF8),
bit: Some(n & 0x07),
}
}
}
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: branch_operand_index(&insn, control_flow),
named_register: insn.direct_addr(),
data_refs: data_refs(&insn),
}
}
}
fn map_control_flow(cf: ::i8051::ControlFlow) -> ControlFlow {
use ::i8051::ControlFlow as I;
match cf {
I::Continue { next } => ControlFlow::Continue { next },
I::Jump { target } => ControlFlow::Jump { target },
I::Call { target, return_pc } => ControlFlow::Call { target, return_pc },
I::Choice {
fall_through,
branch_target,
} => ControlFlow::Choice {
fall_through,
branch_target,
},
I::Diverge => ControlFlow::Diverge,
}
}
fn branch_operand_index(insn: &Instruction, cf: ControlFlow) -> Option<usize> {
match cf {
ControlFlow::Jump { .. } | ControlFlow::Call { .. } | ControlFlow::Choice { .. } => {}
_ => return None,
}
match insn.mnemonic() {
Mnemonic::LJMP | Mnemonic::LCALL | Mnemonic::AJMP | Mnemonic::ACALL | Mnemonic::SJMP => {
Some(0)
}
_ => {
let decoded = insn.as_string();
let operand_count = decoded.split_once(' ').map_or(0, |(_, rest)| {
if rest.is_empty() {
0
} else {
rest.split(',').count()
}
});
operand_count.checked_sub(1)
}
}
}
#[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, index: usize) -> Option<Access> {
use Mnemonic::*;
Some(match mnemonic {
MOV => {
if index == 0 {
Access::Write
} else {
Access::Read
}
}
ANL | ORL | XRL => {
if index == 0 {
Access::ReadWrite
} else {
Access::Read
}
}
CJNE | ADD | ADDC | SUBB | PUSH | JB | JNB => Access::Read,
POP | CLR | SETB => Access::Write,
INC | DEC | CPL | DJNZ | XCH | JBC => Access::ReadWrite,
_ => return None,
})
}
fn is_dptr_load(insn: &Instruction) -> bool {
insn.mnemonic() == Mnemonic::MOV
&& matches!(
insn.operands().as_slice(),
[Operand::Dptr, Operand::Imm16(_)]
)
}
fn data_refs(insn: &Instruction) -> Vec<DataRef> {
let mut refs = Vec::new();
let dptr_load = is_dptr_load(insn);
for (index, operand) in insn.operands().as_slice().iter().enumerate() {
match operand {
Operand::Direct(addr) => {
if let Some(access) = memory_access(insn.mnemonic(), index) {
let space = if *addr >= 0x80 { SFR } else { IDATA };
refs.push(DataRef {
space,
offset: u32::from(*addr),
kind: access.kind(),
certainty: Certainty::Definite,
operand: u8::try_from(index).ok(),
});
}
}
Operand::Bit(addr) | Operand::BitNot(addr) => {
if let Some(access) = memory_access(insn.mnemonic(), index) {
refs.push(DataRef {
space: BIT,
offset: u32::from(*addr),
kind: access.kind(),
certainty: Certainty::Definite,
operand: u8::try_from(index).ok(),
});
}
}
Operand::Imm16(value) if dptr_load => {
for space in [CODE, XDATA] {
refs.push(DataRef {
space,
offset: u32::from(*value),
kind: XrefType::Pointer,
certainty: Certainty::Inferred,
operand: u8::try_from(index).ok(),
});
}
}
_ => {}
}
}
refs
}
#[cfg(test)]
mod tests {
use super::*;
use crate::platform::test_util::edges;
use pretty_assertions::assert_eq;
#[test]
fn data_xrefs_classify_space() {
use XrefType::{Jump, Pointer, Read, ReadWrite, Write};
let e = |bytes: &[u8]| edges(&I8051, bytes);
assert_eq!(e(&[0xE5, 0x90]), vec![(SFR, 0x90, Read)]);
assert_eq!(e(&[0xF5, 0x90]), vec![(SFR, 0x90, Write)]);
assert_eq!(e(&[0x05, 0x30]), vec![(IDATA, 0x30, ReadWrite)]);
assert_eq!(
e(&[0x90, 0x12, 0x34]),
vec![(CODE, 0x1234, Pointer), (XDATA, 0x1234, Pointer)]
);
assert_eq!(
e(&[0x85, 0x40, 0x30]),
vec![(IDATA, 0x30, Write), (IDATA, 0x40, Read)]
);
assert_eq!(
e(&[0x20, 0x20, 0x05]),
vec![(CODE, 8, Jump), (BIT, 0x20, Read)]
);
}
#[test]
fn bit_addresses_resolve() {
let p = I8051;
let bit = |n| p.canonicalize(BIT, n);
assert_eq!(
bit(0x65),
CanonicalAddr {
space: IDATA,
offset: 0x2C,
bit: Some(5)
}
);
assert_eq!(
bit(0x00),
CanonicalAddr {
space: IDATA,
offset: 0x20,
bit: Some(0)
}
);
assert_eq!(
bit(0x7F),
CanonicalAddr {
space: IDATA,
offset: 0x2F,
bit: Some(7)
}
);
assert_eq!(
bit(0x99),
CanonicalAddr {
space: SFR,
offset: 0x98,
bit: Some(1)
}
);
assert_eq!(
bit(0xB3),
CanonicalAddr {
space: SFR,
offset: 0xB0,
bit: Some(3)
}
);
assert_eq!(
p.canonicalize(IDATA, 0x2C),
CanonicalAddr {
space: IDATA,
offset: 0x2C,
bit: None
}
);
assert_eq!(
p.canonicalize(SFR, 0x99),
CanonicalAddr {
space: SFR,
offset: 0x99,
bit: None
}
);
assert_ne!(bit(0x99).offset, p.canonicalize(SFR, 0x99).offset);
}
}