// SPDX-License-Identifier: MIT //! Pitfall II's DPC (Display Processor Chip): data fetchers, music, a random number generator. //! //! Sources, in order of weight: //! //! - David Crane's patent US 4,644,495, "Video memory system" (filed January //! 1984), mostly as Horton summarises it (DOC; its passages on the music //! oscillator and the cartridge's signals were read directly on Google //! Patents' text of US4644495A, 2026-11-14; pinning the whole text is still //! owed, docs/claims.md). //! - Kevin Horton's mapper document, section "DPC (Pitfall 2)" (DOC, cited //! "Horton"; `reference/docs/kevtris-atari-2600-mappers.txt` lines 527-754), //! written from the patent and his own FPGA console and cartridge. //! - MAME 0.285's BSD-3-Clause `src/devices/bus/vcs/dpc.cpp` (copyright Fabio //! Priuli; notice in NOTICE), cited "MAME dpc.cpp": an emulator's source, used //! where Horton is silent or ambiguous. //! //! The image is 10,495 bytes: the program (8K, two 4K banks switched as F8 at //! $1FF8/$1FF9, MAME dpc.cpp:249-253 and `vcs_slot.cpp`'s size rule), the DPC's //! 2K graphics ROM, and 255 bytes nothing reads (HYPOTHESIS: a table a dumping //! tool appended; the program never addresses them). //! //! **Registers** (DOC Horton's address map): reads $1000-$103F, writes //! $1040-$107F; each group of eight addresses is one register per fetcher //! (address bits 0-2). The cartridge has no R/W line: an access to the write //! range stores the byte on the bus whichever way the CPU drives it, as the other //! schemes' RAM write windows do. HYPOTHESIS: the DPC drives nothing on a read of //! its write range (it holds the program ROM's chip enable, Horton). Horton also //! says the DPC "shows up twice", at $1000-$107F and $1800-$18FF; MAME maps only //! $1000; the model follows MAME (HYPOTHESIS: if the mirror were there, Pitfall //! II would have to avoid running code at $1800-$18FF; a disassembly check or a //! console probe would settle it). //! //! **Fetchers** (DOC Horton): eight, each a pointer (8 low bits written at //! $1050-$1057, 3 high bits at $1058-$105F), a start count ($1040-$1047), an end //! count ($1048-$104F) and a flag. A data read returns the graphics byte at the //! pointer and then decrements the pointer. The flag sets when the pointer's low //! 8 bits equal the start count and clears when they equal the end count; reads //! at $1010-$1037 return the byte masked by the flag (0 when clear), $1008-$100F //! never masked, $1038-$103F the flag as $FF/$00. Writing the start count //! clears the flag and then compares (DOC MAME dpc.cpp:203-207). Horton writes //! "Writing to the start count register also sets the register"; read as //! setting the flag, it makes Pitfall II draw a column of stray sprite bytes //! above Harry on its first screen, which the shipped game does not show (the //! dump's own behaviour), and Stella's frames (OBSERVED, 15 of 15 agreeing with //! MAME's rule, none with that reading) found the difference. Horton's sentence //! is therefore read as loading the start count (docs/claims.md). The graphics ROM is indexed by the //! pointer's complement (MAME dpc.cpp:127: the dump stores the 2K in the order //! the pointer counts down; a dump-format fact, not hardware). //! //! **Music** (DOC Horton): fetchers 5-7 have two more bits in their high-pointer //! register, `xxSM PPPP` read with Horton's names: S (bit 5) clocks the pointer //! from the music oscillator instead of reads, M (bit 4) is music mode, where the //! low 8 bits count down and reload from the start count after $FF. The three //! flags address an 8-entry table, read at $1004-$1007 bits 0-3: 0, 4, 5, 9, 6, //! A, B, F. HYPOTHESIS on the table's index order: Horton's header "567" is read //! as MAME dpc.cpp:113-118 does, fetcher 5 the low bit; Stella's audio, compared //! as a closed box, would tell the orders apart. The oscillator is an RC on the //! cartridge (560 kΩ, 5 %) and varies by cartridge: the patent prefers 42 kHz; //! Horton chose 21 kHz, "fairly close to my actual cartridge" (DOC, by ear); //! MAME uses 18.4 kHz. The model takes Horton's 21 kHz as its default, a //! parameter (FITTED by Horton's ear, no measurement). What clocks it is //! settled (DOC): the patent calls it "an internally generated asynchronous //! clock signal (42 kHz in the preferred embodiment)", "free running", and //! lists the cartridge's only signals as twelve address lines, chip select, //! eight data lines and power, no clock; nocash's slot pin-out has no clock //! pin; Horton bent the pitch by touching the resistor of his own cartridge. //! So the oscillator runs in its own hertz on every console, and its ticks //! per CPU cycle are `music_hz / cpu_hz` with the console's own CPU clock //! (on a PAL console 0.92 % more ticks per cycle than on NTSC, the same //! pitch). //! //! **The random number generator** (DOC Horton): an 8-bit shift register, //! shifted left with the inverse of bits 7, 5, 4 and 3 XORed together fed into //! bit 0, all ones illegal, 255 states; it steps on every read of $1000-$1003 //! and an access to $1070-$1077 clears it. HYPOTHESIS: a read returns the value //! after the step (MAME dpc.cpp:105-108 does). //! //! **The draw-line adder** ($1060-$1067 MOVAMT, and $1004/$1006's upper bits): //! Horton did not study it and Pitfall II does not use it; modelled as MAME //! dpc.cpp:109-120 does (DOC MAME), untested. //! //! Rust-forced differences (a2600-python `cartridge/dpc.py` at 28fb095): the //! constructor returns `Result`; the oscillator's owed time is the same //! IEEE double arithmetic (`f64`). use serde::{Deserialize, Serialize}; use super::connector::{Cartridge, Restore}; use crate::cartridge_state; pub const AMPLITUDE: [u8; 8] = [0x0, 0x4, 0x5, 0x9, 0x6, 0xA, 0xB, 0xF]; // DOC Horton; index order HYPOTHESIS pub const NTSC_CPU_HZ: f64 = 315e6 / 88.0 / 3.0; // DOC: the NTSC colour clock / 3 pub const MUSIC_HZ: f64 = 21_000.0; // DOC Horton, by ear ("fairly close to my actual cartridge") // The oscillator is free of the console's clock (DOC patent: "internally // generated asynchronous clock", "free running", no clock among the slot's // signals; DOC Horton: an RC on the cartridge), so MUSIC_HZ is in hertz and // `cpu_hz` must be the clock of the console the cartridge sits in: // System::from_rom passes its row's, a host passes its own. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct DPC { pub rom: Vec, pub graphics: Vec, pub bank: usize, pub top: [u8; 8], // start counts pub bottom: [u8; 8], // end counts pub pointer: [u16; 8], // 11 bits pub flag: [bool; 8], pub music: [bool; 8], pub osc: [bool; 8], pub random: u8, pub movamt: u8, pub latch62: u8, pub latch64: u8, pub ticks_per_cycle: f64, pub music_time: f64, // oscillator ticks owed, in CPU cycles' worth pub last_t: u64, } impl DPC { pub fn new(image: &[u8], start_bank: usize, cpu_hz: f64, music_hz: f64) -> Result { if image.len() < 0x2800 { return Err(format!( "DPC needs the 8K program and 2K graphics, not {} bytes", image.len() )); } Ok(DPC { rom: image[..0x2000].to_vec(), graphics: image[0x2000..0x2800].to_vec(), bank: start_bank << 12, top: [0; 8], bottom: [0; 8], pointer: [0; 8], flag: [false; 8], music: [false; 8], osc: [false; 8], random: 0, movamt: 0, latch62: 0, latch64: 0, ticks_per_cycle: music_hz / cpu_hz, music_time: 0.0, last_t: 0, }) } // -- the fetchers ---------------------------------------------------- fn compare(&mut self, n: usize) { let low = (self.pointer[n] & 0xFF) as u8; if low == self.top[n] { self.flag[n] = true; } if low == self.bottom[n] { self.flag[n] = false; } } fn decrement(&mut self, n: usize) { let mut pointer = self.pointer[n].wrapping_sub(1) & 0x7FF; if pointer & 0xFF == 0xFF && self.music[n] { pointer = (pointer & 0x700) | self.top[n] as u16; } self.pointer[n] = pointer; self.compare(n); } fn catch_up(&mut self, t: u64) { self.music_time += (t as i64 - self.last_t as i64) as f64 * self.ticks_per_cycle; self.last_t = t; let ticks = self.music_time.trunc(); if ticks != 0.0 { self.music_time -= ticks; let clocked: Vec = [5, 6, 7].into_iter().filter(|&n| self.osc[n]).collect(); for n in clocked { let repeat = if ticks < 512.0 { ticks as u64 } else { 512 }; // 512 steps cover any state for _ in 0..repeat { self.decrement(n); } } } } fn read(&mut self, offset: u16, n: usize) -> u8 { if offset < 0x08 { if offset < 0x04 { let r = self.random; let feedback = !((r >> 7) ^ (r >> 5) ^ (r >> 4) ^ (r >> 3)) & 1; self.random = (r << 1) | feedback; return self.random; } if offset < 0x06 { self.latch62 = self.latch64; } let total = self.latch62 as u16 + self.top[4] as u16; self.latch64 = (total & 0xFF) as u8; let carry = total > 0xFF; let index: usize = [5, 6, 7] .into_iter() .filter(|&k| self.music[k] && self.flag[k]) .map(|k| 1 << (k - 5)) .sum(); return (if carry { self.movamt & 0xF0 } else { 0 }) | AMPLITUDE[index]; } let group = offset & 0x38; let byte = self.graphics[(!self.pointer[n] & 0x7FF) as usize]; let shown = self.flag[n]; let data = if group == 0x08 { byte } else if !shown { 0 } else if group == 0x10 { byte } else if group == 0x18 { byte.rotate_left(4) } else if group == 0x20 { byte.reverse_bits() } else if group == 0x28 { byte >> 1 } else if group == 0x30 { byte << 1 } else { 0xFF }; if n < 5 || !self.osc[n] { self.decrement(n); } data } fn write(&mut self, group: u16, n: usize, value: u8) { if group == 0x00 { self.top[n] = value; self.flag[n] = false; // DOC MAME dpc.cpp:203-207, then the compare below self.compare(n); } else if group == 0x08 { self.bottom[n] = value; self.compare(n); } else if group == 0x10 { self.pointer[n] = (self.pointer[n] & 0x700) | value as u16; if n == 4 { self.latch64 = value; } if self.music[n] { self.pointer[n] = (self.pointer[n] & 0x700) | self.top[n] as u16; } self.compare(n); } else if group == 0x18 { self.pointer[n] = (((value & 7) as u16) << 8) | (self.pointer[n] & 0xFF); if n >= 5 { self.music[n] = value & 0x10 != 0; self.osc[n] = value & 0x20 != 0; if self.music[n] && self.pointer[n] & 0xFF == 0xFF { self.pointer[n] = (self.pointer[n] & 0x700) | self.top[n] as u16; self.compare(n); } } } else if group == 0x20 { self.movamt = value; } else if group == 0x30 { self.random = 0; } } } impl Restore for DPC {} impl Cartridge for DPC { fn name(&self) -> &str { "DPC" } // -- the bus --------------------------------------------------------- fn access(&mut self, address: u16, value: u8, t: u64, _write: bool) -> Option { if address & 0x1000 == 0 { return None; } if address == 0x1FF8 || address == 0x1FF9 { self.bank = ((address - 0x1FF8) as usize) << 12; } let offset = address & 0xFFF; if offset >= 0x80 { return Some(self.rom[self.bank | offset as usize]); } self.catch_up(t); let n = (offset & 7) as usize; if offset < 0x40 { return Some(self.read(offset, n)); } self.write(offset & 0x38, n, value); None } cartridge_state!(); }