use super::manager::{raw_trap_route, RawTrapRoute, OS_TRAP_TABLE_SLOTS, TOOLBOX_TRAP_TABLE_SLOTS};
use std::collections::{HashMap, HashSet};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum TrapTableProfile {
M68k68040,
PowerPc604,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ProfileTrapRoute {
pub(crate) raw: RawTrapRoute,
pub(crate) default_is_unimplemented: bool,
pub(crate) has_permanent_come_from: bool,
pub(crate) default_gateway_word: u16,
}
pub(crate) const M68K_68040_COME_FROM_TRAPS: &[u16] = &[
0xA002, 0xA003, 0xA008, 0xA00A, 0xA012, 0xA023, 0xA024, 0xA030, 0xA031, 0xA054, 0xA078, 0xA823,
0xA869, 0xA873, 0xA879, 0xA88B, 0xA893, 0xA89C, 0xA8A1, 0xA8B5, 0xA8CF, 0xA8E4, 0xA8E5, 0xA8EA,
0xA8EC, 0xA905, 0xA908, 0xA909, 0xA90A, 0xA90C, 0xA90D, 0xA91E, 0xA924, 0xA956, 0xA972, 0xA999,
0xA9A0, 0xA9A2, 0xA9C9, 0xA9DC, 0xA9E1, 0xA9ED, 0xA9EF, 0xAA00, 0xAA1F, 0xAA27, 0xAA43, 0xAA4E,
0xAAFB,
];
pub(crate) const POWERPC_604_COME_FROM_TRAPS: &[u16] = &[0xA823, 0xA851, 0xA996, 0xA999, 0xAAFB];
const M68K_68040_DEFAULT_GATEWAY_ALIASES: &[(u16, u16)] = &[(0xAA02, 0xA87D), (0xAA26, 0xAA08)];
const MAC_OS_81_UNIMPLEMENTED_TRAPS: &[u16] = &[0xAA6E];
impl TrapTableProfile {
fn come_from_traps(self) -> &'static [u16] {
match self {
Self::M68k68040 => M68K_68040_COME_FROM_TRAPS,
Self::PowerPc604 => POWERPC_604_COME_FROM_TRAPS,
}
}
fn unimplemented_traps(self) -> &'static [u16] {
match self {
Self::M68k68040 | Self::PowerPc604 => MAC_OS_81_UNIMPLEMENTED_TRAPS,
}
}
fn default_gateway_word(self, canonical_word: u16) -> u16 {
match self {
Self::M68k68040 => M68K_68040_DEFAULT_GATEWAY_ALIASES
.iter()
.find_map(|&(alias, target)| (alias == canonical_word).then_some(target))
.unwrap_or(canonical_word),
Self::PowerPc604 => canonical_word,
}
}
fn default_gateway_identity(self, trap_word: u16) -> u16 {
let canonical_word = raw_trap_route(trap_word).canonical_word;
if self.unimplemented_traps().contains(&canonical_word) {
0xAA6E
} else {
self.default_gateway_word(canonical_word)
}
}
pub(crate) fn route(self, trap_word: u16) -> ProfileTrapRoute {
let raw = *raw_trap_route(trap_word);
ProfileTrapRoute {
raw,
default_is_unimplemented: self.unimplemented_traps().contains(&raw.canonical_word),
has_permanent_come_from: self.come_from_traps().contains(&raw.canonical_word),
default_gateway_word: self.default_gateway_word(raw.canonical_word),
}
}
}
pub(crate) trait TrapCodeMemory {
fn trap_code_allocation_bucket(&self, size: u32) -> u32;
fn can_allocate_trap_code(&self, size: u32) -> bool;
fn publish_trap_code(&mut self, existing: Option<u32>, words: &[u16]) -> u32;
}
pub(crate) struct TrapTableImage {
pub(crate) raw_entries: Vec<u32>,
pub(crate) exception_vectors: [u32; 2],
}
#[derive(Default)]
pub(crate) struct TrapSystemGateways {
defaults: HashMap<u16, u32>,
}
impl TrapSystemGateways {
pub(crate) fn get(&self, word: u16) -> Option<u32> {
self.defaults
.get(&raw_trap_route(word).canonical_word)
.copied()
}
#[cfg(test)]
pub(crate) fn is_empty(&self) -> bool {
self.defaults.is_empty()
}
pub(crate) fn get_or_create(&mut self, memory: &mut impl TrapCodeMemory, word: u16) -> u32 {
let route = raw_trap_route(word);
let words = if route.is_toolbox {
[route.canonical_word | 0x0400, 0]
} else {
[route.canonical_word, 0x4E75]
};
let words = &words[..if route.is_toolbox { 1 } else { 2 }];
let address = memory.publish_trap_code(self.get(word), words);
self.defaults.insert(route.canonical_word, address);
address
}
pub(crate) fn default_gateway(&self, profile: TrapTableProfile, word: u16) -> Option<u32> {
self.get(profile.default_gateway_identity(word))
}
pub(crate) fn required_bytes(
&self,
profile: TrapTableProfile,
bucket: impl Fn(u32) -> u32,
) -> u32 {
let mut words = HashSet::from([0xAA6E]);
for word in Self::table_words() {
let route = profile.route(word);
if !route.default_is_unimplemented {
words.insert(route.default_gateway_word);
}
}
words
.into_iter()
.filter(|word| self.get(*word).is_none())
.map(|word| {
bucket(if raw_trap_route(word).is_toolbox {
2
} else {
4
})
})
.sum::<u32>()
+ profile.come_from_traps().len() as u32 * bucket(10)
+ 2 * bucket(2)
}
fn table_words() -> impl Iterator<Item = u16> {
(0..OS_TRAP_TABLE_SLOTS)
.map(|slot| 0xA000 | slot)
.chain((0..TOOLBOX_TRAP_TABLE_SLOTS).map(|slot| 0xA800 | slot))
}
pub(crate) fn create_table(
&mut self,
memory: &mut impl TrapCodeMemory,
profile: TrapTableProfile,
) -> Option<TrapTableImage> {
let required =
self.required_bytes(profile, |size| memory.trap_code_allocation_bucket(size));
if !memory.can_allocate_trap_code(required) {
return None;
}
let unimplemented = self.get_or_create(memory, 0xAA6E);
let mut raw_entries =
Vec::with_capacity(usize::from(OS_TRAP_TABLE_SLOTS + TOOLBOX_TRAP_TABLE_SLOTS));
for word in Self::table_words() {
let route = profile.route(word);
let default = if route.default_is_unimplemented {
self.defaults.insert(word, unimplemented);
unimplemented
} else {
self.get_or_create(memory, route.default_gateway_word)
};
raw_entries.push(if route.has_permanent_come_from {
Self::create_come_from_head(memory, default)
} else {
default
});
}
let exception_vectors = [
memory.publish_trap_code(None, &[0x4E73]),
memory.publish_trap_code(None, &[0x4E73]),
];
Some(TrapTableImage {
raw_entries,
exception_vectors,
})
}
pub(crate) fn create_come_from_head(memory: &mut impl TrapCodeMemory, successor: u32) -> u32 {
memory.publish_trap_code(
None,
&[
0x6006,
0x4EF9,
(successor >> 16) as u16,
successor as u16,
0x60F8,
],
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_gateway_identity_matches_full_route_for_every_trap_word() {
for profile in [TrapTableProfile::M68k68040, TrapTableProfile::PowerPc604] {
for word in 0..=u16::MAX {
let route = profile.route(word);
let expected = if route.default_is_unimplemented {
0xAA6E
} else {
route.default_gateway_word
};
assert_eq!(
profile.default_gateway_identity(word),
expected,
"{word:#06X}"
);
}
}
}
struct CodeMemory {
next: u32,
limit: u32,
bucket: fn(u32) -> u32,
code: HashMap<u32, Vec<u16>>,
}
impl TrapCodeMemory for CodeMemory {
fn trap_code_allocation_bucket(&self, size: u32) -> u32 {
(self.bucket)(size)
}
fn can_allocate_trap_code(&self, size: u32) -> bool {
self.next
.checked_add(size)
.is_some_and(|end| end <= self.limit)
}
fn publish_trap_code(&mut self, existing: Option<u32>, words: &[u16]) -> u32 {
let address = existing.unwrap_or_else(|| {
let address = self.next;
self.next += (self.bucket)(words.len() as u32 * 2);
address
});
self.code.insert(address, words.to_vec());
address
}
}
fn four(size: u32) -> u32 {
(size + 3) & !3
}
fn eight(size: u32) -> u32 {
(size + 7) & !7
}
#[test]
fn profile_construction_accounts_for_memory_and_preserves_system_identity() {
for bucket in [four as fn(u32) -> u32, eight] {
for profile in [TrapTableProfile::M68k68040, TrapTableProfile::PowerPc604] {
let mut memory = CodeMemory {
next: 0x10000,
limit: u32::MAX,
bucket,
code: HashMap::new(),
};
let mut system = TrapSystemGateways::default();
let tick = system.get_or_create(&mut memory, 0xAD75);
let os = system.get_or_create(&mut memory, 0xA31E);
let before = memory.next;
let needed = system.required_bytes(profile, bucket);
let first = system.create_table(&mut memory, profile).unwrap();
assert_eq!(memory.next - before, needed);
assert_eq!(first.raw_entries.len(), 1280);
assert_eq!(system.get(0xA975), Some(tick));
assert_eq!(system.get(0xA01E), Some(os));
for (word, &raw) in TrapSystemGateways::table_words().zip(&first.raw_entries) {
let route = profile.route(word);
let default = system.default_gateway(profile, word).unwrap();
if route.has_permanent_come_from {
assert_ne!(raw, default);
assert_eq!(
memory.code[&raw],
[
0x6006,
0x4EF9,
(default >> 16) as u16,
default as u16,
0x60F8
]
);
} else {
assert_eq!(raw, default);
}
let gateway_word = if route.default_is_unimplemented {
0xAA6E
} else {
route.default_gateway_word
};
assert_eq!(
memory.code[&default],
if route.raw.is_toolbox {
vec![gateway_word | 0x0400]
} else {
vec![gateway_word, 0x4E75]
}
);
}
assert_eq!(
system.default_gateway(profile, 0xAA02) == system.get(0xA87D),
profile == TrapTableProfile::M68k68040
);
let before = memory.next;
let needed = system.required_bytes(profile, bucket);
let second = system.create_table(&mut memory, profile).unwrap();
assert_eq!(memory.next - before, needed);
assert_eq!(
needed,
profile.come_from_traps().len() as u32 * bucket(10) + 2 * bucket(2)
);
for (word, (&old, &new)) in TrapSystemGateways::table_words()
.zip(first.raw_entries.iter().zip(&second.raw_entries))
{
assert_eq!(old != new, profile.route(word).has_permanent_come_from);
}
for (old, new) in first
.exception_vectors
.into_iter()
.zip(second.exception_vectors)
{
assert_ne!(old, new);
assert_eq!(memory.code[&old], [0x4E73]);
assert_eq!(memory.code[&new], [0x4E73]);
}
}
}
}
#[test]
fn profile_construction_refuses_capacity_before_publication_and_retries() {
for profile in [TrapTableProfile::M68k68040, TrapTableProfile::PowerPc604] {
for reused in [false, true] {
let mut memory = CodeMemory {
next: 0x10000,
limit: u32::MAX,
bucket: eight,
code: HashMap::new(),
};
let mut system = TrapSystemGateways::default();
if reused {
system.create_table(&mut memory, profile).unwrap();
}
let before = memory.next;
let code = memory.code.clone();
let defaults = system.defaults.clone();
let required = system.required_bytes(profile, eight);
memory.limit = before + required - 1;
assert!(system.create_table(&mut memory, profile).is_none());
assert_eq!(memory.next, before);
assert_eq!(memory.code, code);
assert_eq!(system.defaults, defaults);
memory.limit += 1;
assert!(system.create_table(&mut memory, profile).is_some());
assert_eq!(memory.next, before + required);
}
}
}
#[test]
fn saved_gateway_republication_uses_canonical_identity_without_allocating() {
let mut memory = CodeMemory {
next: 0x10000,
limit: u32::MAX,
bucket: four,
code: HashMap::new(),
};
let mut system = TrapSystemGateways::default();
for (first_word, second_word, expected) in [
(0xAD75, 0xA975, vec![0xAD75]),
(0xA31E, 0xA01E, vec![0xA01E, 0x4E75]),
] {
let gateway = system.get_or_create(&mut memory, first_word);
memory.code.insert(gateway, vec![0]);
let before = memory.next;
assert_eq!(system.get_or_create(&mut memory, second_word), gateway);
assert_eq!(memory.next, before);
assert_eq!(memory.code[&gateway], expected);
}
}
}