# SPDX-License-Identifier: MIT """The BIOS's own test, run by the BIOS itself: does the console start this image in 7800 mode or in 2600 mode? docs/cartridge.md "2600 dumps handed to this board": a headerless image that no table knows is put to the test the console applies, so the answer is the console's and not a heuristic of ours. The test is not re-implemented here: the BIOS image (bring your own, by hash) is executed by 6502-python on a minimal bus that has only what the test touches: INPTCTRL's MEN and EXT, the two 6116s and their shadows, the 6532's RAM, MSTAT (which reads as vertical blank on every other read, so the BIOS's waits for the beam pass quickly), the TIA and the rest reading as an undriven bus, and the image mapped as the connector would see it (the decode as board.py's, DOC: [MT84] Figure 2, [SG] App. 1; MSTAT's toggling and the once-a-frame NMI are pacing shortcuts, not hardware values): - 4 KiB or less: a 2600-sized image on the middle 24 contacts, so it answers wherever A12 is high and repeats every its-own-size (DOC: the connector); - otherwise flat, ending at $FFFF (a 7800 cartridge with no switching; the loader asks only about images of 48 KiB or less, Open decision 2). The verdict is where the BIOS jumps: through the image's reset vector with EXT and MEN set ("7800"), or into the 2600 start code it copies to the 6532's RAM at $0480 ("2600"). The NTSC BIOS checks Atari's signature; the PAL BIOS checks only the region byte (C8; what the dumps' own code does). """ from __future__ import annotations from sixfiveohtwo import MOS6502 MEN, EXT = 0x02, 0x04 # INPTCTRL bits (DOC: [SG]) # CPU cycles a frame, NTSC (DOC: [HF], reported console measurement); only the BIOS's # pace depends on it FRAME_CYCLES = 29_850 class _Bus: def __init__(self, bios: bytes, image: bytes) -> None: self.bios = bios self.image = image self.small = len(image) <= 0x1000 self.base = 0x10000 - len(image) self.ram = bytearray(0x1000) self.riot_ram = bytearray(0x80) self.inptctrl = 0 self.locked = False self.data = 0 self.mstat = 0 self.dma_on = False def _cart(self, address: int) -> int | None: if self.small: if address & 0x1000: return self.image[address % len(self.image)] return None if address >= self.base: return self.image[address - self.base] return None def read(self, address: int) -> int: control = self.inptctrl value = None if not control & MEN: if address & 0x1000: value = ( self.bios[address & (len(self.bios) - 1)] if not control & EXT else self._cart(address) ) elif address & 0x80 and not address & 0x200: value = self.riot_ram[address & 0x7F] elif address < 0x0400: low = address & 0xFF if low == 0x28: self.mstat ^= 0x80 value = self.mstat elif low >= 0x40 and not address & 0x200: value = self.ram[0x800 | (address & 0x1FF)] elif address < 0x0600: if address & 0x80: value = self.riot_ram[address & 0x7F] elif 0x1800 <= address < 0x2800: value = self.ram[address - 0x1800] elif 0x2800 <= address < 0x3000: value = self.ram[0x800 | (address & 0x7FF)] elif address >= 0x3000: if address & 0x8000 and not control & EXT: value = self.bios[address & (len(self.bios) - 1)] else: value = self._cart(address) if value is None: value = self.data self.data = value return value def write(self, address: int, value: int) -> None: self.data = value control = self.inptctrl tia = ( (not address & 0x1080) if not control & MEN else (address < 0x400 and not address & 0xE0) ) if tia: if not self.locked: self.inptctrl = value & 0x0F self.locked = bool(value & 1) return if not control & MEN: if not address & 0x1000 and address & 0x80 and not address & 0x200: self.riot_ram[address & 0x7F] = value return if address < 0x0400: if address & 0xFF == 0x3C: # CTRL: DMA mode 2 means MARIA will raise DLIs self.dma_on = (value >> 5) & 3 == 2 elif (address & 0xFF) >= 0x40 and not address & 0x200: self.ram[0x800 | (address & 0x1FF)] = value elif address < 0x0600: if address & 0x80: self.riot_ram[address & 0x7F] = value elif 0x1800 <= address < 0x2800: self.ram[address - 0x1800] = value elif 0x2800 <= address < 0x3000: self.ram[0x800 | (address & 0x7FF)] = value def bios_verdict(bios: bytes, image: bytes, *, max_instructions: int = 5_000_000) -> str: """ "7800" or "2600": the mode the BIOS starts ``image`` in (see the module docstring).""" if not image or len(image) > 0xC000: raise ValueError("the BIOS test is asked only about images of 1 byte to 48 KiB") bus = _Bus(bios, image) cpu = MOS6502(bus.read, bus.write) cpu.request_reset() cpu.step() vector = bus._cart(0xFFFC) | bus._cart(0xFFFD) << 8 cycles = 0 for _ in range(max_instructions): cycles += cpu.step() if cycles >= FRAME_CYCLES: # The NTSC BIOS advances its signature check from its DLI handler # (it waits on $F0, which the NMI routine sets): one per frame, # once it has turned MARIA's DMA on. cycles -= FRAME_CYCLES if bus.dma_on: cpu.request_non_maskable_interrupt() pc = cpu.pc if 0x0480 <= pc < 0x0500: return "2600" if pc == vector and bus.inptctrl & (MEN | EXT) == MEN | EXT: return "7800" raise RuntimeError("the BIOS reached no verdict within the instruction budget")