// SPDX-License-Identifier: MIT //! The consoles, as data: one board, several machines. //! //! Every console this project models is the same board with a different TIA //! (colour encoding, crystal) and different power-on state, so each is a row of //! [`MACHINES`]; adding PAL or SECAM is adding a row, not a code path. Each //! field names its source and that source's tier (docs/validation.md). The //! reference is the NTSC console with the visual6502-photographed 10444D TIA, the //! chip Sim2600 models (docs/handoff-brief.md, "Decisions"). //! //! Rust-forced differences (a2600-python `machine.py` at 28fb095): the frozen //! dataclasses are `Copy` structs with `&'static str` names, built as `const`s; //! the dictionaries `TIA_REVISIONS`, `MACHINES`, `HOSTED_MACHINES` and //! `CONSOLE_ROWS` are arrays of rows with lookup functions; Python's `is` //! (identity) becomes `==` on the rows. /// A video standard's clocks (docs/start-here.md "Clocks"). /// /// The TIA's oscillator is the colour clock; the 6507 runs at a third of it and /// a line is always 228 colour clocks, 76 CPU cycles. The number of lines is /// the program's (there is no vertical counter); `nominal_lines` is only what /// well-behaved games produce, for the television stage. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Timing { pub name: &'static str, pub colour_clock_hz: f64, pub nominal_lines: u32, } impl Timing { pub fn cpu_hz(&self) -> f64 { self.colour_clock_hz / 3.0 } } // DOC: the NTSC colour subcarrier, 315/88 MHz (nocash 2k6specs "Clocks": // 3.579545 MHz); 262 lines by convention (nocash's vertical timing table). pub const NTSC: Timing = Timing { name: "NTSC", colour_clock_hz: 315e6 / 88.0, nominal_lines: 262, }; // DOC: nocash 2k6specs "Clocks": 3.546894 MHz (4/5 of the PAL subcarrier); // the PAL console schematic (reference/schematics, C012283 rev. B) shows its // own 3.54 MHz crystal for the TIA beside the 4.43 MHz one for the colour // modulator; 312 lines by convention. pub const PAL: Timing = Timing { name: "PAL", colour_clock_hz: 3_546_894.0, nominal_lines: 312, }; // HYPOTHESIS: the SECAM console's oscillator is the PAL one's. No document // found gives it (nocash names only NTSC and PAL), and no SECAM schematic is // pinned. It runs PAL software at 312 lines (DOC: nocash "SECAM ... takes the // PAL software"), which needs a line rate near 15.6 kHz, so the clock is // within a percent of 3.56 MHz; the ale-79 serial tables decode the same at // either clock. Settled by the crystal's marking on a SECAM board. pub const SECAM: Timing = Timing { name: "SECAM", colour_clock_hz: 3_546_894.0, nominal_lines: 312, }; /// Where a console's TIA differs from the reference 10444D, as data. /// /// Each field is a mask over the five objects, P0 P1 M0 M1 BL as bits 4..0 /// (the TIA's own order), and defaults to the reference chip, the one the /// die traces judge. A field exists only because consoles' results split /// on it: ale-79's serial tables from six PAL and SECAM consoles (Stella /// #699, docs/validation.md "Milestone 7"), derived from the tables by this /// project (where the consoles split, the split is the field). Objects a /// table does not exercise keep the reference behaviour, HYPOTHESIS until a /// table or a photo shows otherwise. /// /// - `merge_steps`: objects that an HMOVE pulse arriving outside HBLANK /// (merged into the visible-line object clock) steps half a colour clock /// early before it swallows the next MOTCK. DIE: the 10444D does /// (tia-fpga "HMOVE"; die traces `hmove`, `hmove_cycles`), so an /// HMOVE in the visible line moves nothing. TEST ROM: on two consoles /// (PAL 7800, PAL Jr. '91) `early_HMOVE` shows player 0 moved right by /// one clock per such pulse on every HMOVE from cycle 4 on: the pulse /// swallows a MOTCK without stepping. /// - `strobe_drops`: objects whose RESxx loses a stuffed (HMOVE) pulse /// that arrives in the two half clocks after the strobe's own (while the /// strobe is still low, before its hold reaches the ring). DIE: the 10444D /// keeps every pulse from the strobe's next half clock until its hold lets /// go and steps the ring with it afterwards (C28, `hmove_resp0`). TEST /// ROM: on four consoles the `HMOVE_RESPx` tables put the object one /// clock further right in exactly the cells where a stuffed pulse falls /// two half clocks after the strobe (one alignment in four cycles): the /// players on the PAL light sixer, the missiles on the light sixer, both /// Jr.s and the Vader. (A pulse in the hold's later half clocks is kept /// on every console: dropping those instead misses 515 cells of the light /// sixer's `_P0` table; dropping these misses none.) #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct TIARevision { pub name: &'static str, pub source: &'static str, pub merge_steps: u8, pub strobe_drops: u8, } impl TIARevision { /// A revision with the reference's fields (the dataclass defaults). pub const fn new(name: &'static str, source: &'static str) -> TIARevision { TIARevision { name, source, merge_steps: 0x1F, strobe_drops: 0x00, } } } pub const REFERENCE_TIA: TIARevision = TIARevision::new( "10444D (reference)", "DIE: the visual6502 photograph Sim2600 simulates; 39 die traces match", ); pub const P0: u8 = 0x10; pub const P1: u8 = 0x08; pub const M0: u8 = 0x04; pub const M1: u8 = 0x02; pub const BL: u8 = 0x01; // TEST ROM: ale-79's #699 tables, console by console (validation.md "Milestone 7"). pub const TIA_REVISIONS: [TIARevision; 7] = [ REFERENCE_TIA, TIARevision { merge_steps: 0x1F & !P0, ..TIARevision::new("PAL 7800 (#699)", "TEST ROM: #699 early_HMOVE") }, TIARevision { strobe_drops: M0 | M1, ..TIARevision::new("PAL Jr. '84 (#699)", "TEST ROM: #699 HMOVE_RESPx_M0/M1") }, TIARevision { merge_steps: 0x1F & !P0, strobe_drops: M0 | M1, ..TIARevision::new("PAL Jr. '91 (#699)", "TEST ROM: #699 early_HMOVE, HMOVE_RESPx_M0/M1") }, TIARevision { strobe_drops: P0 | P1 | M0 | M1, ..TIARevision::new("PAL light sixer (#699)", "TEST ROM: #699 HMOVE_RESPx, _P0/P1/M0/M1") }, TIARevision { strobe_drops: M0 | M1, ..TIARevision::new("PAL Vader (#699)", "TEST ROM: #699 HMOVE_RESPx_M0/M1") }, TIARevision::new("SECAM woody (#699)", "TEST ROM: #699: every table as the reference"), ]; /// `TIA_REVISIONS[name]`. pub fn tia_revision(name: &str) -> Option { TIA_REVISIONS.iter().copied().find(|r| r.name == name) } const fn revision_at(index: usize) -> TIARevision { TIA_REVISIONS[index] } /// What a console holds when it is switched on; real consoles vary. /// /// On hardware nearly all of this is undefined (docs/timing.md "Power-up"): /// the 6507's A/X/Y/S, RIOT RAM, the interval timer, the TIA's latches and the /// phase of its horizontal counter against the CPU's first cycle. The defaults /// are chosen to match MAME 0.285's power-on state, so that the instruction /// lockstep of milestone 1 starts aligned (docs/mame-oracle.md); they are /// detector-matched choices, not facts about consoles, and a test that depends /// on one must say so. /// /// - `a`/`x`/`y`/`s`: the registers before the reset sequence (S = 0 /// and the sequence's three stack reads leave S = $FD). X = $80 is MAME's /// (its first traced instruction shows it); Stella randomises A/X/Y. /// - `p`: the status flags before the reset sequence, which sets only I. /// MAME's first instruction shows Z set ($36 with its always-1 bits 4/5). /// - `ram_fill`: RIOT RAM (MAME zeroes it, a2600.cpp:476; Stella randomises). /// - `timer_value`/`timer_divider`/`timer_first_decrement`: the interval /// timer at power and the cycle of its first decrement (MAME: 255 at a /// 1024-cycle interval, mos6530.cpp:142-144; the first decrement's cycle is /// read off MAME's trace; Gopher2600: 0 at 1024; Stella: a random value). /// - `hclock`: the TIA's colour clock within the line at power, counted from /// HBLANK's start. On a 2600 the 6507's cycles are the TIA's (it reloads the /// CPU clock divider at every line start), so power-on must fall where a /// cycle ends: 2 mod 3 (DIE: tia-fpga, PH0 "rises on exactly the half clock /// where HBLANK starts"). The default is chosen to match MAME's line phase /// (docs/mame-oracle.md; OBSERVED, MAME 0.285). /// - `half_step`: one more half colour clock (0 or 1), for a machine whose /// CPU clock does not come from the TIA (`Machine.tia_clocks_cpu` /// False: the 7800, where MARIA's divider clocks the CPU) and so may start /// at any half-clock phase. 0 on the 2600 rows. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct PowerOn { pub a: u8, pub x: u8, pub y: u8, pub s: u8, pub p: u8, pub ram_fill: u8, pub timer_value: u8, pub timer_divider: i64, pub timer_first_decrement: i64, pub hclock: u32, pub half_step: u32, } pub const POWER_ON: PowerOn = PowerOn { a: 0, x: 0x80, y: 0, s: 0, p: 0x26, ram_fill: 0x00, timer_value: 0xFF, timer_divider: 1024, timer_first_decrement: 1023, hclock: 224, half_step: 0, }; impl Default for PowerOn { fn default() -> Self { POWER_ON } } /// One console model. /// /// `switches` is SWCHB's input bits as the console presents them with no /// switch pressed: Reset (D0) and Select (D1) released, Colour (D3), both /// difficulties on B (D6, D7 low), and the unconnected D2/D4/D5 reading high. /// $3F is what MAME presents (BSD-3 a2600.cpp INPUT_PORTS "SWB"; DOC for the /// bit meanings: Stella Programmer's Guide, SWCHB). A SECAM console has no /// colour switch: D3 is grounded (DOC: nocash 2k6specs, SWCHB). /// /// `tia_clocks_cpu`: the TIA's Φ0 clocks the 6507, so power-on lies on the /// TIA's cycle grid (every 2600). False for a host that clocks the CPU /// itself (the 7800's 2600 mode), which then may enter at any phase. #[derive(Clone, Copy, Debug, PartialEq)] pub struct Machine { pub name: &'static str, pub timing: Timing, pub certified: bool, pub power_on: PowerOn, pub switches: u8, pub palette: &'static str, // a key of palettes::PALETTES: the frontend's default pub tia_revision: TIARevision, pub tia_clocks_cpu: bool, } impl Machine { /// A row with the dataclass defaults. pub const fn new(name: &'static str, timing: Timing, certified: bool) -> Machine { Machine { name, timing, certified, power_on: POWER_ON, switches: 0x3F, palette: "ntsc", tia_revision: REFERENCE_TIA, tia_clocks_cpu: true, } } } pub const VCS_NTSC: Machine = Machine::new("Atari VCS (NTSC)", NTSC, false); pub const VCS_PAL: Machine = Machine { palette: "pal", ..Machine::new("Atari VCS (PAL)", PAL, false) }; pub const VCS_SECAM: Machine = Machine { palette: "secam", switches: 0x37, ..Machine::new("Atari VCS (SECAM)", SECAM, false) }; /// `_console(name, revision, timing)`: a certified #699 row. const fn console(name: &'static str, revision: usize, timing: Timing, secam: bool) -> Machine { let palette = if secam { "secam" } else { "pal" }; Machine { palette, tia_revision: revision_at(revision), ..Machine::new(name, timing, true) } } // The six consoles of ale-79's #699 tables, each certified against its own // tables cell by cell (docs/validation.md "Milestone 7"). "Certified" means // exactly that: these seven test ROMs, every cell. pub const PAL_JR_84: Machine = console("Atari 2600 Jr. '84 (PAL, #699)", 2, PAL, false); pub const PAL_JR_91: Machine = console("Atari 2600 Jr. '91 (PAL, #699)", 3, PAL, false); pub const PAL_LIGHT_SIXER: Machine = console("Atari VCS light sixer (PAL, #699)", 4, PAL, false); pub const PAL_VADER: Machine = console("Atari 2600 Vader (PAL, #699)", 5, PAL, false); pub const SECAM_WOODY: Machine = Machine { switches: 0x37, ..console("Atari 2600 woody (SECAM, #699)", 6, SECAM, true) }; pub const MACHINES: [Machine; 8] = [ VCS_NTSC, VCS_PAL, VCS_SECAM, PAL_JR_84, PAL_JR_91, PAL_LIGHT_SIXER, PAL_VADER, SECAM_WOODY, ]; /// `MACHINES[name]`. pub fn machine(name: &str) -> Option { MACHINES.iter().copied().find(|m| m.name == name) } // Other consoles running the 2600, kept out of MACHINES (the 2600's own // listing): the 7800 in its 2600 mode, where MARIA's divider, not the TIA, // clocks the CPU, and the B/W line (SWCHB D3) is the 7800's pause button // (a7800-python's handoff brief). The PAL 7800's TIA is the only 7800 TIA // with evidence (#699); the NTSC 7800 row takes the reference, unmeasured. pub const PAL_7800: Machine = Machine { tia_clocks_cpu: false, ..console("Atari 7800 in 2600 mode (PAL, #699)", 1, PAL, false) }; pub const NTSC_7800: Machine = Machine { tia_clocks_cpu: false, ..Machine::new("Atari 7800 in 2600 mode (NTSC, TIA unmeasured)", NTSC, false) }; pub const HOSTED_MACHINES: [Machine; 2] = [PAL_7800, NTSC_7800]; // The #699 console names (gates::CONSOLES) -> the row their tables judge. pub const CONSOLE_ROWS: [(&str, Machine); 6] = [ ("PAL_7800", PAL_7800), ("PAL_JR_84", PAL_JR_84), ("PAL_JR_91", PAL_JR_91), ("PAL_L6", PAL_LIGHT_SIXER), ("PAL_VADER", PAL_VADER), ("SECAM_WOODY", SECAM_WOODY), ]; /// `CONSOLE_ROWS[console]`. pub fn console_row(console: &str) -> Option { CONSOLE_ROWS.iter().find(|(name, _)| *name == console).map(|(_, m)| *m) }