# SPDX-License-Identifier: MIT """The Starpath (Arcadia) Supercharger, AR: 6K of RAM, a 2K BIOS ROM, a cassette input. Main source: Kevin Horton's mapper document, sections "AR (Starpath Supercharger)" and "Supercharger Tape Format" (DOC, cited "Horton"; lines 757-1240 of ``reference/docs/kevtris-atari-2600-mappers.txt``), written from his FPGA console running the real BIOS. Where Horton is silent, MAME 0.285's BSD-3-Clause ``src/devices/bus/vcs/scharger.cpp`` (copyright Fabio Priuli; notice in NOTICE) is cited as "MAME scharger.cpp". The Supercharger's BIOS is Starpath's firmware: never shipped here; a player supplies it (``bios=``). **Registers** (DOC Horton): $1FF8 is the control register ``DDDB BBWE`` (write delay, bank mode, RAM write enable, ROM power off) and $1FF9 the tape input (bit 0; the other bits read 0). Neither can be written directly: the cartridge latches the low byte of the address of any access to $1000-$10FF, and an access to $1FF8 then copies the latch into the control register ("THERE IS NO UPPER LIMIT on the number of cycles" between the two). **RAM writes** (DOC Horton): after an access to $1000-$10FF, the cartridge counts changes of the 13 address lines ("BUS TRANSITIONS", not cycles: a repeated address does not count); on the fifth it drives the latched byte onto the data bus and into RAM at that address, if writes are enabled and RAM is mapped there. The CPU reads the byte the cartridge drives. With writes enabled, every access to $1000-$10FF re-latches, so reading there corrupts RAM (Horton). The count is cancelled by a control-register access (MAME scharger.cpp:137-138) and by a tape read (MAME scharger.cpp:214-215). **Bank modes** (DOC Horton's table): the two 2K halves of the window show RAM bank 3/ROM, 1/ROM, 3/1, 1/3, 3/ROM, 2/ROM, 3/2, 2/3 for modes 0-7. A read of the ROM half with the ROM powered off floats (HYPOTHESIS: the PNP switch cuts the ROM's supply, so nothing drives the bus; MAME returns $FF). Power-on: mode 0, writes disabled, the ROM on (HYPOTHESIS; MAME scharger.cpp:76-86). **Loading.** Two ways: - **From tape** with the player's BIOS (``bios=`` 2K) and a cassette input (``tape_level(t) -> bool``, t in CPU cycles). This path is built from Horton's description and is UNTESTED here: no BIOS is available to this project (docs/claims.md). - **"Fast load"**, the shortcut every emulator offers (labelled as such, not hardware): the load's pages are copied into RAM as its page table says, and the BIOS ROM is replaced by a short stub this project wrote from Horton's description of how the BIOS ends a load (copy ``CMP $FFF8; JMP start`` into RIOT RAM at $FA, latch the control byte through $F0xx, jump there). Multi-load games run their first load only: a later load is requested from the BIOS, whose entry points are not documented in any permitted source (HYPOTHESIS-free gap, recorded in docs/claims.md). **The image format** (``.bin``, 8,448 bytes per load; DOC: the dumps themselves, every one self-checking): 32 pages of 256 bytes of tape data in load order (unused pages padded), then at $2000 the tape header (Horton's eight bytes: start address, control word, page count, header checksum, multi-load number, progress-bar speed), the page table (Horton's block locations, ``000B BBRR``) at $2010, and the pages' checksums at $2040. Every header and page in the local dumps sums to $55 as Horton says, which is how the layout was confirmed. """ from __future__ import annotations from collections.abc import Callable from .connector import Cartridge LOAD_SIZE = 8448 # DOC Horton's Supercharger format: 8K of pages + a 256-byte header block HEADER = 0x2000 # DOC Horton: the tape header follows the 8K of pages WRITE_TRANSITIONS = 5 # DOC Horton: "it waits 5 address bus transitions" # Bank mode -> (bank at $1000, bank at $1800); 0-2 are RAM banks 1-3, 3 the ROM # (DOC Horton's bank-mode table). BANK_MODES = ((2, 3), (0, 3), (2, 0), (0, 2), (2, 3), (1, 3), (2, 1), (1, 2)) def parse_load(image: bytes, load: int = 0) -> tuple[dict, list[tuple[int, bytes]]]: """One load's header and its pages as (block location, 256 bytes).""" base = load * LOAD_SIZE header = image[base + HEADER : base + HEADER + 8] if len(header) < 8 or sum(header) & 0xFF != 0x55: raise ValueError(f"Supercharger load {load}: header checksum is not $55") info = { "start": header[0] | header[1] << 8, "control": header[2], "pages": header[3], "multiload": header[5], } pages = [] for i in range(info["pages"]): location = image[base + HEADER + 0x10 + i] data = image[base + i * 256 : base + (i + 1) * 256] check = image[base + HEADER + 0x40 + i] if (location + check + sum(data)) & 0xFF != 0x55: raise ValueError(f"Supercharger load {load}: page {i} checksum is not $55") pages.append((location, data)) return info, pages def fast_load_stub(start: int, control: int) -> bytes: """2K standing in for the BIOS at $1800-$1FFF (this project's code, not Starpath's). It does what Horton describes the BIOS doing at the end of a load: put ``CMP $FFF8; JMP start`` in RIOT RAM at $FA-$FF (the new bank mode may take the ROM away from under the CPU), latch the control byte by reading $F0cc, and jump to $FA. """ tail = (0xCD, 0xF8, 0xFF, 0x4C, start & 0xFF, start >> 8) code = bytearray() for i, byte in enumerate(tail): code += bytes((0xA9, byte, 0x85, 0xFA + i)) # LDA #byte; STA $FA+i code += bytes((0xAD, control, 0xF0)) # LDA $F0cc: latch the control byte code += bytes((0x4C, 0xFA, 0x00)) # JMP $00FA rom = bytearray(code) + bytes(0x800 - len(code)) rom[0x7FC:0x7FE] = (0x00, 0xF8) # reset vector: $F800 return bytes(rom) class Supercharger(Cartridge): name = "AR" def __init__( self, image: bytes, *, bios: bytes | None = None, tape_level: Callable[[int], bool] | None = None, ram_fill: int = 0x00, ) -> None: if len(image) % LOAD_SIZE: raise ValueError(f"AR images are 8448 bytes per load, not {len(image)}") self.image = bytes(image) self.loads = len(image) // LOAD_SIZE self.ram = bytearray([ram_fill]) * 0x1800 self.tape_level = tape_level self.fast_load = bios is None if bios is None: info, pages = parse_load(self.image, 0) for location, data in pages: bank, block = location & 3, (location >> 2) & 7 start = bank * 0x800 + block * 0x100 self.ram[start : start + 0x100] = data self.rom = fast_load_stub(info["start"], info["control"]) else: if len(bios) != 0x800: raise ValueError("the Supercharger BIOS is 2048 bytes") self.rom = bytes(bios) self.latch = 0 self.transitions = WRITE_TRANSITIONS + 1 # nothing latched self.last_address = -1 self.set_control(0) def set_control(self, value: int) -> None: self.control = value self.write_enable = bool(value & 0x02) self.rom_on = not value & 0x01 self.lower, self.upper = BANK_MODES[(value >> 2) & 7] def access(self, address: int, value: int, t: int, write: bool = False) -> int | None: if address != self.last_address: self.last_address = address if self.transitions <= WRITE_TRANSITIONS: self.transitions += 1 if not address & 0x1000: return None if address == 0x1FF8: self.set_control(self.latch) self.transitions = WRITE_TRANSITIONS + 1 elif address == 0x1FF9: self.transitions = WRITE_TRANSITIONS + 1 level = self.tape_level(t) if self.tape_level is not None else False return 1 if level else 0 offset = address & 0x7FF bank = self.upper if address & 0x800 else self.lower if self.transitions == WRITE_TRANSITIONS and self.write_enable and bank != 3: self.ram[bank * 0x800 + offset] = self.latch return self.latch if not address & 0xF00: self.latch = address & 0xFF self.transitions = 0 if bank == 3: return self.rom[offset] if self.rom_on else None return self.ram[bank * 0x800 + offset]