# 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 :data:`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"). """ from __future__ import annotations from dataclasses import dataclass, field @dataclass(frozen=True) class Timing: """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. """ name: str colour_clock_hz: float nominal_lines: int @property def cpu_hz(self) -> float: return self.colour_clock_hz / 3 # DOC: the NTSC colour subcarrier, 315/88 MHz (nocash 2k6specs "Clocks": # 3.579545 MHz); 262 lines by convention (nocash's vertical timing table). NTSC = Timing("NTSC", 315e6 / 88, 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. PAL = Timing("PAL", 3_546_894.0, 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. SECAM = Timing("SECAM", 3_546_894.0, 312) @dataclass(frozen=True) class TIARevision: """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.) """ name: str source: str merge_steps: int = 0x1F strobe_drops: int = 0x00 REFERENCE_TIA = TIARevision( "10444D (reference)", "DIE: the visual6502 photograph Sim2600 simulates; 39 die traces match", ) P0, P1, M0, M1, BL = 0x10, 0x08, 0x04, 0x02, 0x01 # TEST ROM: ale-79's #699 tables, console by console (validation.md "Milestone 7"). TIA_REVISIONS = { r.name: r for r in ( REFERENCE_TIA, TIARevision("PAL 7800 (#699)", "TEST ROM: #699 early_HMOVE", merge_steps=0x1F & ~P0), TIARevision("PAL Jr. '84 (#699)", "TEST ROM: #699 HMOVE_RESPx_M0/M1", strobe_drops=M0 | M1), TIARevision( "PAL Jr. '91 (#699)", "TEST ROM: #699 early_HMOVE, HMOVE_RESPx_M0/M1", merge_steps=0x1F & ~P0, strobe_drops=M0 | M1, ), TIARevision( "PAL light sixer (#699)", "TEST ROM: #699 HMOVE_RESPx, _P0/P1/M0/M1", strobe_drops=P0 | P1 | M0 | M1, ), TIARevision("PAL Vader (#699)", "TEST ROM: #699 HMOVE_RESPx_M0/M1", strobe_drops=M0 | M1), TIARevision("SECAM woody (#699)", "TEST ROM: #699: every table as the reference"), ) } @dataclass(frozen=True) class PowerOn: """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. """ a: int = 0 x: int = 0x80 y: int = 0 s: int = 0 p: int = 0x26 ram_fill: int = 0x00 timer_value: int = 0xFF timer_divider: int = 1024 timer_first_decrement: int = 1023 hclock: int = 224 half_step: int = 0 @dataclass(frozen=True) class Machine: """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. """ name: str timing: Timing certified: bool power_on: PowerOn = field(default_factory=PowerOn) switches: int = 0x3F palette: str = "ntsc" # a key of palettes.PALETTES: the frontend's default tia_revision: TIARevision = REFERENCE_TIA tia_clocks_cpu: bool = True VCS_NTSC = Machine("Atari VCS (NTSC)", NTSC, certified=False) VCS_PAL = Machine("Atari VCS (PAL)", PAL, certified=False, palette="pal") VCS_SECAM = Machine("Atari VCS (SECAM)", SECAM, certified=False, palette="secam", switches=0x37) def _console(name: str, revision: str, timing: Timing = PAL, **fields) -> Machine: palette = "secam" if timing is SECAM else "pal" return Machine( name, timing, certified=True, palette=palette, tia_revision=TIA_REVISIONS[revision], **fields, ) # 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. PAL_JR_84 = _console("Atari 2600 Jr. '84 (PAL, #699)", "PAL Jr. '84 (#699)") PAL_JR_91 = _console("Atari 2600 Jr. '91 (PAL, #699)", "PAL Jr. '91 (#699)") PAL_LIGHT_SIXER = _console("Atari VCS light sixer (PAL, #699)", "PAL light sixer (#699)") PAL_VADER = _console("Atari 2600 Vader (PAL, #699)", "PAL Vader (#699)") SECAM_WOODY = _console("Atari 2600 woody (SECAM, #699)", "SECAM woody (#699)", SECAM, switches=0x37) MACHINES = { m.name: m for m in ( VCS_NTSC, VCS_PAL, VCS_SECAM, PAL_JR_84, PAL_JR_91, PAL_LIGHT_SIXER, PAL_VADER, SECAM_WOODY, ) } # 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. PAL_7800 = _console("Atari 7800 in 2600 mode (PAL, #699)", "PAL 7800 (#699)", tia_clocks_cpu=False) NTSC_7800 = Machine( "Atari 7800 in 2600 mode (NTSC, TIA unmeasured)", NTSC, certified=False, tia_clocks_cpu=False, ) HOSTED_MACHINES = {m.name: m for m in (PAL_7800, NTSC_7800)} # The #699 console names (gates.CONSOLES) -> the row their tables judge. CONSOLE_ROWS = { "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, }