# SPDX-License-Identifier: MIT """The MOS 6532 RIOT: 128 bytes of RAM, two 8-bit ports, the interval timer. Kept behind its own small interface (``read``/``write`` with a CPU-cycle count, the port inputs as attributes) so it can be extracted into a core when a second board needs it (the 7800; docs/handoff-brief.md). **Decode** (6532 data sheet; docs/start-here.md "The RIOT"): the board selects the RIOT when A12 = 0 and A7 = 1. Then A9 = 0 is RAM (A0-A6). A9 = 1 is I/O: with A2 = 0, A1-A0 pick SWCHA, SWACNT, SWCHB, SWBCNT; with A2 = 1, a read with A0 = 0 is INTIM and with A0 = 1 the interrupt flags, a write with A4 = 1 loads the timer (A1-A0 the interval, A3 its interrupt enable) and with A4 = 0 sets the PA7 edge (A0 = 1 positive). The IRQ pin is not connected on the 2600. **The timer** is evaluated lazily, in CPU cycles (one φ2 per cycle): - A write loads the counter and selects the interval; the first decrement comes one cycle after the write, then one every interval. DOC: MAME's BSD-3 mos6530.cpp:315-321; OBSERVED: Stella (closed box) agrees, milestone 1's lockstep failing at once with the decrement a cycle late; TEST ROM: the #606 console results match with it. - When it decrements past 0 it reads $FF, sets the timer flag (TIMINT D7), and from then on decrements every cycle. - Reading INTIM clears the flag and the counter goes back to the interval, continuing from the prescaler's current phase: the console results of Stella issue #97 (docs/undocumented-behavior.md C7, hardware-captured photos and tables). MAME instead restarts a whole interval on that read (BSD-3 mos6530.cpp:510-526); this is one place the MAME lockstep may diverge. - A read of INTIM, or a write to the timer, in the very cycle the counter wraps does not clear the flag: "the reading or writing of the timer at the same time interrupt occurs will not reset the interrupt flag" (MCS6500 Family Hardware Manual p.78), confirmed on consoles for both, on the first wrap and on later ones, by the TimerTest ROMs of Stella issue #606 (docs/undocumented-behavior.md C6). A write there loads the counter, which then counts every cycle because the flag is still set. """ from __future__ import annotations TIMER_FLAG = 0x80 # DOC: 6532 data sheet, interrupt flag register D7 PA7_FLAG = 0x40 # DOC: 6532 data sheet, interrupt flag register D6 INTERVALS = (1, 8, 64, 1024) # DOC: 6532 data sheet, A1-A0 of a timer write; TEST ROM: #606 class RIOT: def __init__( self, ram_fill: int, timer_value: int, timer_divider: int, first_decrement: int ) -> None: self.ram = bytearray([ram_fill]) * 128 self.out_a = self.ddr_a = 0 self.out_b = self.ddr_b = 0 # The pins' input levels, set by the controllers and console switches: # port A = left joystick in D7-D4, right in D3-D0 (active low). self.input_a = 0xFF self.input_b = 0x3F self.flags = 0 self.edge_positive = False self._pa7 = True # The timer: the counter, the interval, the cycles left until the # prescaler's next decrement (1..divider), the cycle the state is as of, # and the cycle the counter last wrapped past 0 (-1: never). self.value = timer_value & 0xFF self.divider = timer_divider self._countdown = first_decrement self._as_of = 0 self._wrapped_at = -1 # A device that watches the port A pins the 6532 drives (a SaveKey's # I2C lines): called as ``listener(levels, cycle)`` after every write # to SWCHA or SWACNT, with each pin's level as the RIOT leaves it (1 # where it is an input or outputs 1). The device answers through # :meth:`set_input_a`. self.port_a_listener = None # -- the timer --------------------------------------------------------- def _catch_up(self, cycle: int) -> None: n = cycle - self._as_of if n <= 0: return self._as_of = cycle divider = self.divider if not self.flags & TIMER_FLAG: countdown = self._countdown if n < countdown: self._countdown = countdown - n return # The decrement due after ``countdown`` cycles, then one per interval. n -= countdown steps = 1 + n // divider self._countdown = divider - n % divider if steps <= self.value: self.value -= steps return # Past 0: the wrap happens on the (value + 1)th decrement, at the # cycle below; every cycle after it decrements once more. wrap = cycle - n + (self.value * divider) self.flags |= TIMER_FLAG self._wrapped_at = wrap + (cycle - wrap) // 256 * 256 # and every 256 after self.value = (0xFF - (cycle - wrap)) & 0xFF return # Wrapped: one decrement per cycle, the prescaler still turning; the # counter wraps again whenever it passes 0. # The k-th of these n decrements wraps when k = value + 1 (mod 256). if n > self.value: last = n - (n - self.value - 1) % 256 self._wrapped_at = cycle - n + last self.value = (self.value - n) & 0xFF self._countdown = divider - (divider - self._countdown + n) % divider def timer_at(self, cycle: int) -> int: """INTIM as of ``cycle``, without a read's side effect (for debuggers and detectors).""" self._catch_up(cycle) return self.value def _load_timer(self, value: int, divider: int, cycle: int) -> None: self.value = value self.divider = divider self._countdown = 1 self._as_of = cycle if self._wrapped_at != cycle: self.flags &= ~TIMER_FLAG # -- the bus ----------------------------------------------------------- def read(self, address: int, cycle: int) -> int: if not address & 0x200: return self.ram[address & 0x7F] self._catch_up(cycle) if not address & 0x04: register = address & 0x03 if register == 0: # Wired-AND: an output bit drives low only where SWACNT makes it an output. return (self.out_a | (~self.ddr_a & 0xFF)) & self.input_a if register == 1: return self.ddr_a if register == 2: return (self.out_b & self.ddr_b) | (self.input_b & ~self.ddr_b & 0xFF) return self.ddr_b if not address & 0x01: if self._wrapped_at != cycle: self.flags &= ~TIMER_FLAG return self.value flags = self.flags self.flags &= ~PA7_FLAG return flags def write(self, address: int, value: int, cycle: int) -> None: if not address & 0x200: self.ram[address & 0x7F] = value return self._catch_up(cycle) if not address & 0x04: register = address & 0x03 if register == 0: self.out_a = value elif register == 1: self.ddr_a = value elif register == 2: self.out_b = value else: self.ddr_b = value self._edge_detect() if register < 2 and self.port_a_listener is not None: self.port_a_listener((self.out_a | (~self.ddr_a & 0xFF)) & 0xFF, cycle) elif address & 0x10: self._load_timer(value, INTERVALS[address & 0x03], cycle) else: self.edge_positive = bool(address & 0x01) def set_input_a(self, value: int) -> None: """The port A pins as the controllers drive them (active low).""" self.input_a = value & 0xFF self._edge_detect() def _edge_detect(self) -> None: pa7 = bool((self.out_a | (~self.ddr_a & 0xFF)) & self.input_a & 0x80) if pa7 != self._pa7 and pa7 == self.edge_positive: self.flags |= PA7_FLAG self._pa7 = pa7