use m68k::CpuType;
pub const APPEARANCE_MANAGER_VERSION_BCD: u16 = 0x0101;
pub(crate) const QUICKTIME_NUM_VERSION: u32 = 0x0403_8000;
pub(crate) const KEYBOARD_ENVIRON_TYPE: u16 = 4;
pub(crate) const KEYBOARD_PHYSICAL_TYPE: u8 = 2;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MachineProfile {
pub model_id: i16,
pub gestalt_machine_type: u16,
pub system_version_bcd: u16,
pub gestalt_native_cpu_type: u32,
pub gestalt_processor_type: u32,
pub gestalt_fpu_type: u32,
pub gestalt_mmu_type: u32,
pub ram_size_bytes: u32,
pub screen_width: u16,
pub screen_height: u16,
pub screen_depth: u16,
pub vbl_hz: f64,
pub realtime_cpu_mhz: f64,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct GuestExecutionCapabilities {
pub(crate) system_architecture: Option<u32>,
pub(crate) native_cpu_type: u32,
pub(crate) processor_type: u32,
pub(crate) fpu_type: u32,
pub(crate) mmu_type: u32,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct HostExecutionPolicy {
pub(crate) scripted_instructions_per_tick: u32,
pub(crate) realtime_m68k_cpu_mhz: f64,
pub(crate) realtime_powerpc_cpu_mhz: f64,
}
pub(crate) const DEFAULT_HOST_EXECUTION_POLICY: HostExecutionPolicy = HostExecutionPolicy {
scripted_instructions_per_tick: 12_000,
realtime_m68k_cpu_mhz: 25.0,
realtime_powerpc_cpu_mhz: 120.0,
};
pub(crate) const REFERENCE_POWERPC_CPU_CLOCK_HZ: u32 = 120_000_000;
impl MachineProfile {
pub(crate) const fn m68k_execution_capabilities(self) -> GuestExecutionCapabilities {
GuestExecutionCapabilities {
system_architecture: Some(1),
native_cpu_type: self.gestalt_native_cpu_type,
processor_type: self.gestalt_processor_type,
fpu_type: self.gestalt_fpu_type,
mmu_type: self.gestalt_mmu_type,
}
}
pub(crate) const fn powerpc_execution_capabilities(self) -> GuestExecutionCapabilities {
GuestExecutionCapabilities {
system_architecture: Some(2),
native_cpu_type: 0x0104,
processor_type: 3,
fpu_type: self.gestalt_fpu_type,
mmu_type: self.gestalt_mmu_type,
}
}
pub fn cpu_type(self) -> CpuType {
CpuType::M68040
}
pub const fn has_fpu(self) -> bool {
self.gestalt_fpu_type != 0
}
pub const fn screen_row_bytes(self) -> u32 {
self.screen_row_bytes_at_depth(self.screen_depth)
}
pub const fn screen_row_bytes_at_depth(self, depth: u16) -> u32 {
let bytes = (self.screen_width as u32 * depth as u32).div_ceil(8);
(bytes / 16 + 1) * 16
}
}
pub const BASILISK_II_PLAY_PROFILE: MachineProfile = MachineProfile {
model_id: 14,
gestalt_machine_type: 20,
system_version_bcd: 0x0810,
gestalt_native_cpu_type: 4,
gestalt_processor_type: 5,
gestalt_fpu_type: 3,
gestalt_mmu_type: 4,
ram_size_bytes: 128 * 1024 * 1024,
screen_width: 800,
screen_height: 600,
screen_depth: 8,
vbl_hz: 60.15,
realtime_cpu_mhz: 25.0,
};
pub const REFERENCE_MACHINE_PROFILE: MachineProfile = BASILISK_II_PLAY_PROFILE;
pub(crate) const POWERPC_SYSTEM_VERSION_BCD: u16 = 0x0900;
pub(crate) const POWERPC_CARBON_VERSION_BCD: u16 = 0x0130;
pub(crate) const POWERPC_GESTALT_MACHINE_TYPE: u16 = 67;
pub(crate) const REFERENCE_M68K_EXECUTION_CAPABILITIES: GuestExecutionCapabilities =
REFERENCE_MACHINE_PROFILE.m68k_execution_capabilities();
pub(crate) const REFERENCE_POWERPC_EXECUTION_CAPABILITIES: GuestExecutionCapabilities =
REFERENCE_MACHINE_PROFILE.powerpc_execution_capabilities();
pub fn reference_machine_profile() -> MachineProfile {
static RESOLVED: std::sync::OnceLock<MachineProfile> = std::sync::OnceLock::new();
*RESOLVED.get_or_init(resolve_reference_machine_profile)
}
fn resolve_reference_machine_profile() -> MachineProfile {
let mut p = REFERENCE_MACHINE_PROFILE;
if let Ok(w) = std::env::var("SYSTEMLESS_SCREEN_WIDTH") {
if let Ok(w) = w.parse::<u16>() {
if w != 0 {
p.screen_width = w;
}
}
}
if let Ok(h) = std::env::var("SYSTEMLESS_SCREEN_HEIGHT") {
if let Ok(h) = h.parse::<u16>() {
if h != 0 {
p.screen_height = h;
}
}
}
if let Ok(ram) = std::env::var("SYSTEMLESS_RAM_SIZE") {
if let Ok(ram) = ram.parse::<u32>() {
p.ram_size_bytes = ram;
}
}
p
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn m68k_capabilities_derive_from_machine_profile_fields() {
let profile = MachineProfile {
gestalt_native_cpu_type: 0x11,
gestalt_processor_type: 0x22,
gestalt_fpu_type: 0x33,
gestalt_mmu_type: 0x44,
..REFERENCE_MACHINE_PROFILE
};
assert_eq!(
profile.m68k_execution_capabilities(),
GuestExecutionCapabilities {
system_architecture: Some(1),
native_cpu_type: 0x11,
processor_type: 0x22,
fpu_type: 0x33,
mmu_type: 0x44,
}
);
}
#[test]
fn host_execution_policy_is_independent_of_guest_cpu_speed() {
let guest_profile = MachineProfile {
realtime_cpu_mhz: 1.0,
..REFERENCE_MACHINE_PROFILE
};
assert_eq!(DEFAULT_HOST_EXECUTION_POLICY.realtime_m68k_cpu_mhz, 25.0);
assert_ne!(
DEFAULT_HOST_EXECUTION_POLICY.realtime_m68k_cpu_mhz,
guest_profile.realtime_cpu_mhz
);
}
#[test]
fn m68k_capability_record_preserves_shipped_values() {
assert_eq!(
REFERENCE_M68K_EXECUTION_CAPABILITIES,
GuestExecutionCapabilities {
system_architecture: Some(1),
native_cpu_type: 4,
processor_type: 5,
fpu_type: 3,
mmu_type: 4,
}
);
}
#[test]
fn powerpc_capability_record_reports_native_architecture_and_legacy_processor() {
assert_eq!(
REFERENCE_POWERPC_EXECUTION_CAPABILITIES,
GuestExecutionCapabilities {
system_architecture: Some(2),
native_cpu_type: 0x0104,
processor_type: 3,
fpu_type: 3,
mmu_type: 4,
}
);
}
}