use std::sync::Arc;
use crate::address::{AddressSpace, AddressValue, PhysicalAddr, Xref, XrefType};
pub mod i8051;
pub mod m6805;
pub mod mos6502;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ControlFlow {
Continue { next: AddressValue },
Jump { target: AddressValue },
Call {
target: AddressValue,
return_pc: AddressValue,
},
Choice {
fall_through: AddressValue,
branch_target: AddressValue,
},
Diverge,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DataRef {
pub space: AddressSpace,
pub offset: AddressValue,
pub kind: XrefType,
pub certainty: Certainty,
pub operand: Option<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Certainty {
Definite,
Inferred,
}
#[derive(Debug, Clone)]
pub struct DecodedInsn {
pub len: u8,
pub bytes: Vec<u8>,
pub text: String,
pub control_flow: ControlFlow,
pub branch_operand_index: Option<usize>,
pub named_register: Option<u8>,
pub data_refs: Vec<DataRef>,
}
impl DecodedInsn {
#[inline]
pub fn len(&self) -> usize {
self.len as usize
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub fn bytes(&self) -> &[u8] {
&self.bytes
}
#[inline]
pub fn control_flow(&self) -> ControlFlow {
self.control_flow
}
#[inline]
pub fn as_string(&self) -> &str {
&self.text
}
#[inline]
pub fn direct_addr(&self) -> Option<u8> {
self.named_register
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegionKind {
Code,
Register,
Data,
}
#[derive(Debug, Clone)]
pub struct RegionDef {
pub space: AddressSpace,
pub kind: RegionKind,
pub area_header: &'static str,
}
#[derive(Debug, Clone, Copy)]
pub struct EntryPoint {
pub space: AddressSpace,
pub offset: AddressValue,
pub name: &'static str,
pub reason: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CanonicalAddr {
pub space: AddressSpace,
pub offset: AddressValue,
pub bit: Option<u8>,
}
pub trait Platform: Send + Sync {
fn name(&self) -> &str;
fn regions(&self) -> &[RegionDef];
fn entry_points(&self) -> &[EntryPoint] {
&[]
}
fn canonicalize(&self, space: AddressSpace, offset: AddressValue) -> CanonicalAddr {
CanonicalAddr {
space,
offset,
bit: None,
}
}
fn max_insn_len(&self) -> usize;
fn decode(&self, pc: AddressValue, bytes: &[u8]) -> DecodedInsn;
fn area_header(&self, space: AddressSpace) -> Option<&'static str> {
self.regions()
.iter()
.find(|r| r.space == space)
.map(|r| r.area_header)
}
}
pub fn xrefs_from_instruction(insn: &DecodedInsn, source: PhysicalAddr) -> Vec<Xref> {
let mut xrefs = Vec::new();
let mut push = |space, offset, xref_type, certainty| {
xrefs.push(Xref {
from: source,
to: PhysicalAddr { space, offset },
xref_type,
certainty,
});
};
match insn.control_flow {
ControlFlow::Jump { target } => {
push(source.space, target, XrefType::Jump, Certainty::Definite)
}
ControlFlow::Call { target, .. } => {
push(source.space, target, XrefType::Call, Certainty::Definite)
}
ControlFlow::Choice { branch_target, .. } => push(
source.space,
branch_target,
XrefType::Jump,
Certainty::Definite,
),
_ => {}
}
for data_ref in &insn.data_refs {
push(
data_ref.space,
data_ref.offset,
data_ref.kind,
data_ref.certainty,
);
}
xrefs
}
pub fn branch_target(insn: &DecodedInsn) -> Option<AddressValue> {
match insn.control_flow {
ControlFlow::Jump { target } | ControlFlow::Call { target, .. } => Some(target),
ControlFlow::Choice { branch_target, .. } => Some(branch_target),
_ => None,
}
}
pub fn branch_target_operand_index(insn: &DecodedInsn) -> Option<usize> {
insn.branch_operand_index
}
pub type PlatformRef = Arc<dyn Platform>;
pub const BUILTIN_PLATFORMS: &[&str] = &["i8051", "mos6502", "m6805"];
pub fn by_name(name: &str) -> Option<PlatformRef> {
match name {
"i8051" => Some(i8051::platform()),
"mos6502" => Some(mos6502::platform()),
"m6805" => Some(m6805::platform()),
_ => None,
}
}
pub fn suggest_names(query: &str) -> Vec<String> {
let q = query.to_ascii_lowercase();
let mut ranked: Vec<(bool, usize, &str)> = BUILTIN_PLATFORMS
.iter()
.map(|&name| {
let n = name.to_ascii_lowercase();
let substring = n.contains(&q) || q.contains(&n);
(substring, crate::strings::levenshtein(&q, &n), name)
})
.collect();
ranked.sort_by(|a, b| {
b.0.cmp(&a.0)
.then_with(|| a.1.cmp(&b.1))
.then_with(|| a.2.cmp(b.2))
});
ranked
.into_iter()
.map(|(_, _, name)| name.to_string())
.collect()
}
#[cfg(test)]
mod suggest_tests {
use super::*;
#[test]
fn suggest_prefers_substring() {
assert_eq!(suggest_names("8051")[0], "i8051");
assert_eq!(suggest_names("6502")[0], "mos6502");
}
#[test]
fn suggest_orders_by_distance() {
let ranked = suggest_names("z80");
assert_eq!(ranked.len(), 3);
assert!(ranked.contains(&"i8051".to_string()));
}
}
#[cfg(test)]
pub(crate) mod test_util {
use super::*;
pub(crate) fn edges(
driver: &dyn Platform,
bytes: &[u8],
) -> Vec<(AddressSpace, AddressValue, XrefType)> {
let source = PhysicalAddr {
space: driver.regions()[0].space,
offset: 0,
};
xrefs_from_instruction(&driver.decode(0, bytes), source)
.into_iter()
.map(|x| (x.to.space, x.to.offset, x.xref_type))
.collect()
}
}