# SPDX-License-Identifier: MIT # Follows, where noted as [MiS], Atari7800_MiSTer (k7800) by Jamie Blanks, MIT License, # Copyright (c) 2019-2026 Jamie Blanks: the HALT rounding sally.sv captures. No HDL # copied. Full notice: NOTICE, section 3. """The 7800 board: one MARIA-clock timeline, both decoders, INPTCTRL, HALT and RDY, the floating data bus. **One timeline** (docs/timing.md "What a cycle looks like to the board"). 6502-python calls :meth:`Board.read_byte` or :meth:`Board.write_byte` once per CPU cycle, dummy cycles included. Inside each call the board advances the MARIA clock (crystal / 2) and everything that hangs off it, one clock at a time: MARIA, the TIA (one half colour clock per MARIA clock: MARIA supplies the TIA's 3.58 MHz clock, DOC: [MT84] 2.1), and, once per Φ2 period, the RIOT's pulse count and the cartridge's :meth:`tick`. ``step()``'s returned cycle count is never used for time. A cycle, in order: 1. **HALT** (top of both callbacks). While MARIA asks for the bus and the INPTCTRL gate lets HALT through, whole Φ2 periods of 4 clocks pass with the CPU's clock held (DOC: [MT84] 2.1, C1): MARIA's DMA reads happen inside them, the RIOT and the cartridge still count them (DOC: two reports in timing.md, no trace; U10, a PCLK0 capture across DMA would settle it). The CPU's divider keeps its phase across the DMA, so the CPU resumes at the first period boundary after MARIA lets go (HYPOTHESIS for U2, consistent with "HALT is low for a whole number of CPU cycles", [MiS] sally.sv's reported capture (DOC), docs/timing.md "Rounding to the CPU's cycle"; a PCLK0 capture would settle it). 2. **RDY** (reads only): while MARIA's WSYNC latch or the TIA's RDY holds the line low (the two are wire-ANDed, DOC: [1702B] pins), the read repeats in 4-clock periods; ``note_stall()`` is called as each begins, as famicom-python does for its DMC DMA, because a DLI can arrive while the CPU waits. 3. **The cycle**: 6 MARIA clocks if the address is in a slow row of [MT84] Figure 2 (TIA, 6532), or for every cycle while MEN = 0 (the 2600 decoder); 4 otherwise (DOC: [MT84] Figure 2, [1702B] p. 29). A read takes its value after all but the last clock (Φ2 high); a write lands on the last clock, where Φ2 falls (HYPOTHESIS, U3: [1702B] p. 29's diagram or [GLD] would settle the shape; the TIA takes it on that step, as a2600-python's board does, DIE). 4. The cartridge sees the cycle (address, data, R/W, HALT false). **Two decoders, chosen by INPTCTRL D1 (MEN)**, both from [MT84] Figure 2 (DOC: the decoder as a truth table, read off the page image at 200 dpi, p. 9): - MEN = 1 ("7800 mode"): TIA at $00-$1F (A8/A9 don't care: $100, $200, $300), MARIA at $20-$3F (same mirrors), 6532 ports at $280-$2FF/$380-$3FF, 6532 RAM at $480-$4FF/$580-$5FF, RAM1 $1800-$1FFF, RAM0 $2000-$27FF and its zero/one page shadows $040-$0FF/$140-$1FF. Two places where Figure 2 and [SG] App. 1 disagree are numbered C14 ($240-$2FF/$340-$3FF) and C15 ($2800-$2FFF); the board follows [SG] in both (DOC), see docs/undocumented-behavior.md. - MEN = 0 ("2600 mode" in Figure 2): only A12 and A7 decode: A12 = 1 the cartridge (or the BIOS while EXT = 0), else A7 = 0 the TIA, A7 = 1 the 6532; **every cycle slow** (DOC). This is also the decoder at power-on, before the BIOS writes INPTCTRL for the first time (DOC: [SG], INPTCTRL powers up 0). **INPTCTRL** (DOC: [SG] "INPTCTRL register"): any write to the TIA's addresses while unlocked also writes the latch: D0 lock, D1 MEN (MARIA and system RAM), D2 EXT (cartridge instead of BIOS in the upper half), D3 TIA-EN. HALT reaches the CPU only after two writes (DOC: [SG]). EXT = 0 maps the BIOS over $8000-$FFFF; the ROM repeats every its-own-size there (HYPOTHESIS, U19: the console schematic's decoder would settle what a console returns). **The floating bus**: :attr:`data_bus` is the last byte on the bus; TIA reads drive D7/D6 only (DIE: a2600-python's Sim2600 judge, C17), undriven addresses return the latch (DOC: the wiki's M.I.A. bus-capacitance note, C18; start-here.md). """ from __future__ import annotations from collections.abc import Callable from a2600_python.riot import RIOT from a2600_python.tia import TIA from .cartridge.connector import Cartridge from .machine import Machine from .maria import Maria FAST, SLOW, PERIOD = 4, 6, 4 # MARIA clocks; DOC: [MT84] 2.1, [1702B] p. 29 # INPTCTRL bits (DOC: [SG]). LOCK, MEN, EXT, TIA_EN = 0x01, 0x02, 0x04, 0x08 class Board: def __init__(self, machine: Machine, cartridge: Cartridge, bios: bytes | None) -> None: self.machine = machine self.cartridge = cartridge power = machine.power_on self.maria = maria = Maria( machine.lines, machine.vblank_end, machine.dma_lines, power.line, power.clock ) maria.read = self.dma_read # The TIA revision is the 2600-mode row's (a2600-python docs/api.md: the PAL # 7800's from the #699 tables, TEST ROM; the NTSC 7800's the reference 10444D, # unmeasured there); the power-on phase is this board's own (PowerOn below). self.tia = tia = TIA(power.tia_hclock, machine.mode_2600.tia_revision) # I0-I3 with nothing charging them read 0 (DOC: the 2600 schematic, a2600-python # C19); I4/I5 pulled up (DOC: schematic). tia.inputs = 0x30 self.riot = riot = RIOT( power.riot_ram_fill, power.riot_timer_value, power.riot_timer_divider, power.riot_timer_first_decrement, ) # fmt: skip riot.input_b = machine.switches self.ram = bytearray([power.ram_fill]) * 0x1000 # $1800-$27FF: RAM1, RAM0; DOC [MT84] self.bios = bios self.inptctrl = 0 # DOC: [SG] (timing.md "Power-on and reset") self.inptctrl_writes = 0 # HALT reaches the CPU after two (DOC: [SG]) self.locked = False self.clocks = 0 # MARIA clocks since power self.periods = 0 # Φ2 periods since power: the RIOT's and the cartridge's count self.slow_cycles = 0 self.halted_periods = 0 # periods the CPU's clock was held by HALT self.stalled_periods = 0 # periods a read was repeated for RDY self.data_bus = 0 self.cpu = None # set by the system: note_stall(), interrupts # Observers of every bus cycle: (kind, address, value, clock); kind is # "R"/"W" for the CPU and "D" for a MARIA DMA read. self.bus_observers: list[Callable[[str, int, int, int], None]] = [] # Called after every INPTCTRL write with the new value (mode changes). self.on_inptctrl: Callable[[int], None] | None = None # Seeded faults, by name, for proving the gates catch something: # "fast-tia" (TIA cycles left at 4 clocks), "ignore-lock" (the latch # keeps taking writes after D0). Empty in every real run; not hardware values. self.faults: set[str] = set() # MARIA's seeded faults ("end-test-zero", "last-line-cost-dropped") share the set. maria.faults = self.faults self.nmi_after: int | None = None # a DLI waits for the period count to pass this # -- time --------------------------------------------------------------- def _advance(self, clocks: int) -> None: maria_clock, tia_step = self.maria.clock, self.tia.step for _ in range(clocks): maria_clock() tia_step() self.clocks += clocks def _period_end(self) -> None: self.periods += 1 self.cartridge.tick() if self.cartridge.irq and self.cpu is not None: self.cpu.request_maskable_interrupt() def _halted(self) -> None: """MARIA owns the bus: whole 4-clock periods with the CPU's clock held.""" maria = self.maria maria.granted = True while maria.halt: self._advance(PERIOD) self.halted_periods += 1 self._period_end() maria.granted = False self._take_dli() def _take_dli(self) -> None: """A DLI raised by the DMA that just ended reaches the CPU's NMI "one cycle after HALT- is released" (DOC: [1702B] p. 14): it is requested at the top of the second CPU cycle after the release (6502-python counts a request made in a cycle's callback as arriving during that cycle).""" maria = self.maria if maria.nmi: maria.nmi = False self.nmi_after = self.periods - (1 if "dli-early" in self.faults else 0) def _top(self) -> None: """What happens before every CPU cycle: a due DLI, then HALT.""" if self.nmi_after is not None and self.periods > self.nmi_after: self.nmi_after = None if self.cpu is not None: self.cpu.request_non_maskable_interrupt() maria = self.maria if maria.halt and self.inptctrl_writes >= 2: # DOC: [SG] INPTCTRL self._halted() elif maria.nmi: self._take_dli() # a DMA that ran beside the CPU (HALT still gated off) if self.nmi_after is not None and self.periods > self.nmi_after: self.nmi_after = None # only the seeded "dli-early" fault is due already if self.cpu is not None: self.cpu.request_non_maskable_interrupt() @property def halt_enabled(self) -> bool: return self.inptctrl_writes >= 2 # -- the decoders -------------------------------------------------------- def is_slow(self, address: int) -> bool: """Figure 2's slow rows (TIA, 6532), or any cycle under the 2600 decoder (DOC: [MT84]).""" if not self.inptctrl & MEN: return True if address >= 0x0600: return False if address < 0x0400: if not address & 0xE0: return "fast-tia" not in self.faults # TIA: $00-$1F, A8/A9 free return address & 0x280 == 0x280 # 6532 ports: $280-$2FF, $380-$3FF return bool(address & 0x80) # 6532 RAM: $480-$4FF, $580-$5FF def _read_device(self, address: int) -> int | None: """The console's own devices; None where none responds (the cartridge's turn). Decode: DOC, [MT84] Figure 2 and [SG] App. 1; TIA D5-D0 from the bus: DIE (C17); the BIOS mirrored by its size: HYPOTHESIS (U19).""" control = self.inptctrl if not control & MEN: if address & 0x1000: if not control & EXT and self.bios is not None: return self.bios[address & (len(self.bios) - 1)] return None if address & 0x80: return self.riot.read(address, self.periods) return self.tia.read(address) | (self.data_bus & 0x3F) if address < 0x0400: low = address & 0xFF if low < 0x20: return self.tia.read(address) | (self.data_bus & 0x3F) if low < 0x40: return self.maria.read_register(low) if not address & 0x200: # $040-$0FF, $140-$1FF: RAM0 shadows (DOC: [SG] row 6) return self.ram[0x800 | (address & 0x1FF)] if address & 0x80: return self.riot.read(address, self.periods) return None # $240-$27F, $340-$37F (DOC: [SG], C14) if address < 0x0600: if address & 0x80: return self.riot.read(address & 0x7F, self.periods) # 6532 RAM return None if address < 0x1800: return None if address < 0x2800: return self.ram[address - 0x1800] if address < 0x3000: return self.ram[0x800 | (address & 0x7FF)] # DOC: [SG] row 7 (C15) if address & 0x8000 and not control & EXT and self.bios is not None: return self.bios[address & (len(self.bios) - 1)] return None def _write_device(self, address: int, value: int) -> bool: """Perform a console-device write except the TIA's; True if a device took it. Decode as :meth:`_read_device` (DOC).""" control = self.inptctrl if not control & MEN: if address & 0x1000: return False if address & 0x80: self.riot.write(address, value, self.periods) return True if address < 0x0400: low = address & 0xFF if low < 0x20: return True if low < 0x40: self.maria.write_register(low, value) return True if not address & 0x200: self.ram[0x800 | (address & 0x1FF)] = value return True if address & 0x80: self.riot.write(address, value, self.periods) return True return False if address < 0x0600: if address & 0x80: self.riot.write(address & 0x7F, value, self.periods) return True return False if address < 0x1800: return False if address < 0x2800: self.ram[address - 0x1800] = value return True if address < 0x3000: self.ram[0x800 | (address & 0x7FF)] = value return True return False def _is_tia(self, address: int) -> bool: if not self.inptctrl & MEN: return not address & 0x1080 return address < 0x0400 and not address & 0xE0 def _write_inptctrl(self, address: int, value: int) -> None: """The latch takes TIA-selected writes with A5 = 0 (DOC: [SG]: "mapped to $0000-$001F"; under MEN = 0 the TIA select also covers $20-$7F, and whether the latch sees those is unknown: HYPOTHESIS, U23, the console schematic's latch wiring would settle it). Lock until power-off: DOC, [SG].""" if address & 0x20: return if self.locked and "ignore-lock" not in self.faults: return self.inptctrl = value & 0x0F self.inptctrl_writes += 1 self.locked = bool(value & LOCK) self.maria.enabled = bool(value & MEN) if self.inptctrl_writes == 2 and not self.maria.halt: self.maria.granted = False # HALT now reaches the CPU: the bus is given per DMA if self.on_inptctrl is not None: self.on_inptctrl(value) # -- one CPU cycle --------------------------------------------------------- def read_byte(self, address: int) -> int: maria = self.maria self._top() if maria.rdy_low or self.tia.rdy_low: cpu = self.cpu while maria.rdy_low or self.tia.rdy_low: if cpu is not None: cpu.note_stall() self._advance(PERIOD) self.stalled_periods += 1 self._period_end() self._top() if cpu is not None: cpu.dma_stalled = True elif self.cpu is not None: self.cpu.dma_stalled = False slow = self.is_slow(address) if slow: self.slow_cycles += 1 self._advance((SLOW if slow else FAST) - 1) t = self.clocks driven = self.cartridge.access(address, self.data_bus, True, False, t) value = self._read_device(address) if value is None: value = self.data_bus if driven is None else driven self.data_bus = value if self.bus_observers: for observer in self.bus_observers: observer("R", address, value, t) self._advance(1) self._period_end() return value def write_byte(self, address: int, value: int) -> None: maria = self.maria self._top() slow = self.is_slow(address) if slow: self.slow_cycles += 1 self._advance((SLOW if slow else FAST) - 1) t = self.clocks self.data_bus = value self.cartridge.access(address, value, False, False, t) if self._is_tia(address): self._write_inptctrl(address, value) maria.clock() self.tia.step((address & 0x3F, value)) self.clocks += 1 else: self._write_device(address, value) self._advance(1) if self.bus_observers: for observer in self.bus_observers: observer("W", address, value, t) self._period_end() # -- MARIA's side of the bus ------------------------------------------------- def dma_read(self, address: int) -> int: """One DMA read by MARIA (HALT asserted): the decoder still selects (DOC: [MT84] Figure 2's footnote on par23); the cartridge sees it with halt true. What a DMA read of the TIA or MARIA's registers ($00-$3F) returns: the bus latch, HYPOTHESIS (U12; [GLD] or a capture would settle it).""" driven = self.cartridge.access(address, self.data_bus, True, True, self.clocks) value = self._read_device(address) if address >= 0x40 else None if value is None: value = self.data_bus if driven is None else driven self.data_bus = value if self.bus_observers: for observer in self.bus_observers: observer("D", address, value, self.clocks) return value