# 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. """ from __future__ import annotations from .connector import Cartridge AMPLITUDE = (0x0, 0x4, 0x5, 0x9, 0x6, 0xA, 0xB, 0xF) # DOC Horton; index order HYPOTHESIS NTSC_CPU_HZ = 315e6 / 88 / 3 # DOC: the NTSC colour clock / 3 MUSIC_HZ = 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. class DPC(Cartridge): name = "DPC" def __init__( self, image: bytes, *, start_bank: int = 1, cpu_hz: float = NTSC_CPU_HZ, music_hz: float = MUSIC_HZ, ) -> None: if len(image) < 0x2800: raise ValueError(f"DPC needs the 8K program and 2K graphics, not {len(image)} bytes") self.rom = bytes(image[:0x2000]) self.graphics = bytes(image[0x2000:0x2800]) self.bank = start_bank << 12 self.top = [0] * 8 # start counts self.bottom = [0] * 8 # end counts self.pointer = [0] * 8 # 11 bits self.flag = [False] * 8 self.music = [False] * 8 self.osc = [False] * 8 self.random = 0 self.movamt = 0 self.latch62 = 0 self.latch64 = 0 self.ticks_per_cycle = music_hz / cpu_hz self.music_time = 0.0 # oscillator ticks owed, in CPU cycles' worth self.last_t = 0 # -- the fetchers ---------------------------------------------------- def _compare(self, n: int) -> None: low = self.pointer[n] & 0xFF if low == self.top[n]: self.flag[n] = True if low == self.bottom[n]: self.flag[n] = False def _decrement(self, n: int) -> None: pointer = (self.pointer[n] - 1) & 0x7FF if pointer & 0xFF == 0xFF and self.music[n]: pointer = (pointer & 0x700) | self.top[n] self.pointer[n] = pointer self._compare(n) def _catch_up(self, t: int) -> None: self.music_time += (t - self.last_t) * self.ticks_per_cycle self.last_t = t ticks = int(self.music_time) if ticks: self.music_time -= ticks clocked = [n for n in (5, 6, 7) if self.osc[n]] for n in clocked: for _ in range(min(ticks, 512)): # 512 steps cover any state self._decrement(n) # -- the bus --------------------------------------------------------- def access(self, address: int, value: int, t: int, write: bool = False) -> int | None: if not address & 0x1000: return None if address in (0x1FF8, 0x1FF9): self.bank = (address - 0x1FF8) << 12 offset = address & 0xFFF if offset >= 0x80: return self.rom[self.bank | offset] self._catch_up(t) n = offset & 7 if offset < 0x40: return self._read(offset, n) self._write(offset & 0x38, n, value) return None def _read(self, offset: int, n: int) -> int: if offset < 0x08: if offset < 0x04: r = self.random feedback = ~((r >> 7) ^ (r >> 5) ^ (r >> 4) ^ (r >> 3)) & 1 self.random = ((r << 1) | feedback) & 0xFF return self.random if offset < 0x06: self.latch62 = self.latch64 total = self.latch62 + self.top[4] self.latch64 = total & 0xFF carry = total > 0xFF index = sum(1 << (k - 5) for k in (5, 6, 7) if self.music[k] and self.flag[k]) return (self.movamt & 0xF0 if carry else 0) | AMPLITUDE[index] group = offset & 0x38 byte = self.graphics[~self.pointer[n] & 0x7FF] shown = self.flag[n] if group == 0x08: data = byte elif not shown: data = 0 elif group == 0x10: data = byte elif group == 0x18: data = ((byte << 4) | (byte >> 4)) & 0xFF elif group == 0x20: data = int(f"{byte:08b}"[::-1], 2) elif group == 0x28: data = byte >> 1 elif group == 0x30: data = (byte << 1) & 0xFF else: data = 0xFF if n < 5 or not self.osc[n]: self._decrement(n) return data def _write(self, group: int, n: int, value: int) -> None: if group == 0x00: self.top[n] = value self.flag[n] = False # DOC MAME dpc.cpp:203-207, then the compare below self._compare(n) elif group == 0x08: self.bottom[n] = value self._compare(n) elif group == 0x10: self.pointer[n] = (self.pointer[n] & 0x700) | value if n == 4: self.latch64 = value if self.music[n]: self.pointer[n] = (self.pointer[n] & 0x700) | self.top[n] self._compare(n) elif group == 0x18: self.pointer[n] = ((value & 7) << 8) | (self.pointer[n] & 0xFF) if n >= 5: self.music[n] = bool(value & 0x10) self.osc[n] = bool(value & 0x20) if self.music[n] and self.pointer[n] & 0xFF == 0xFF: self.pointer[n] = (self.pointer[n] & 0x700) | self.top[n] self._compare(n) elif group == 0x20: self.movamt = value elif group == 0x30: self.random = 0