# SPDX-License-Identifier: MIT """Mikey's eight timers and the four audio channels' timers: one shared prescaler. Modelling level: cycle (every source clock is an event on the 16 MHz tick line). Sources, all DOC unless tagged otherwise: - Spec §10 and Appx 2 (``$FD00-$FD1F``, ``$FD20-$FD3F``): eight 8-bit down counters, BKUP/CTLA/CNT/CTLB; source 0-6 = 1, 2, 4, 8, 16, 32, 64 µs; 7 = the linked predecessor; links 0 -> 2 -> 4 and 1 -> 3 -> 5 -> 7 -> audio 0 -> 1 -> 2 -> 3 -> 1. - LT timers guide (lynx-tests, MIT; measured on consoles): a count of N borrows on the N+1th clock; the interrupt is requested on the borrow, i.e. when the counter is already zero and is clocked again; DONE is sticky; DONE, the pending bit and the interrupt enable are independent; a CTLB write with bit 1 injects one clock. - Spec §16 defects: DONE must be clear for a (non-reloading) timer to count (C6: TEST ROM ``timers`` 5, 7 show a reloading timer keeps counting); RESET_DONE (CTLA bit 6) is a level (C5). """ from __future__ import annotations TICKS_PER_US = 16 # DOC: 16 MHz crystal, 1 tick = 62.5 ns # DOC (Appx 2, LT table): the fixed successor of each timer; 8-11 are audio 0-3. SUCCESSOR = {0: 2, 2: 4, 1: 3, 3: 5, 5: 7, 7: 8, 8: 9, 9: 10, 10: 11, 11: 1} class Timer: """One down counter. Plain integers only (the Rust transcription follows it).""" __slots__ = ( "backup", "count", "ctla", "done", "index", "irq_bit", "next_edge", "successor", ) # fmt: skip def __init__(self, index: int) -> None: self.index = index self.backup = 0 # DOC Appx 2: "Timers are reset to 0" self.ctla = 0 self.count = 0 self.done = 0 self.next_edge = -1 # tick of the next source clock, -1 when not prescaled self.successor: Timer | None = None # DOC Appx 2 INTSET: bits 0-3, 5-7 = timers 0-3, 5-7; bit 4 is the UART, not timer 4. self.irq_bit = (1 << index) if index < 8 and index != 4 else 0 def period(self) -> int: return TICKS_PER_US << (self.ctla & 7) def ctlb(self, now: int) -> int: """CTLB as read at tick ``now``. DOC Appx 2 (FD27 text): "B2 = last clock state (0->1 causes count), B1 = borrow in (1 causes count), B0 = borrow out (count=0 and borrow in)": levels of the timer's input and output, not latches. TEST ROM audio2 1-3 (linked Timer 1 and audio 3 read $08 and $A8 after borrowing; prescaled Timer 7 and audio 3 read $04 / $34 just after a count; audio 0 $30 or $34) and LT timers ("CTLB RD/WR": a write of $0A reads $08): the borrow pulses are over by the time the CPU reads, so B0 and B1 read 0; a linked timer's input (its predecessor's borrow pulse) reads 0. TEST ROM timers 1: a stopped timer (CTLA $00) reads $00, so the input is gated by the count enable. HYPOTHESIS: a prescaled source is a square wave, high for the first half of its period after the counting edge. """ level = 0 if self.ctla & 0x08 and self.ctla & 7 != 7: period = self.period() level = 1 if now % period < period >> 1 else 0 return (self.done << 3) | (level << 2) class TimerBank: """The twelve counters on one prescaler. ``on_borrow(index)`` lets Mikey attach the display (0, 2), the UART (4) and the audio shift registers (8-11).""" __slots__ = ("next_event", "on_borrow", "pending", "timers") def __init__(self, on_borrow) -> None: self.timers = [Timer(i) for i in range(12)] for i, j in SUCCESSOR.items(): self.timers[i].successor = self.timers[j] self.pending = 0 # INTSET/INTRST bits set by timer borrows and software self.next_event = 1 << 62 self.on_borrow = on_borrow # -- time ------------------------------------------------------------------------- def schedule(self, timer: Timer, now: int) -> None: """Place the timer's next source clock after ``now``. HYPOTHESIS (U15): the prescaler runs from reset with every source's clock edge at a whole multiple of its period on the tick line; the phase at reset is not documented (Spec §14 says it may not be deterministic). The first interval is therefore N to N+1 periods (DOC Spec §10, LT). """ if timer.ctla & 0x08 and timer.ctla & 7 != 7: period = timer.period() timer.next_edge = (now // period + 1) * period else: timer.next_edge = -1 self._next() def _next(self) -> None: edges = [t.next_edge for t in self.timers if t.next_edge >= 0] self.next_event = min(edges) if edges else 1 << 62 def run_until(self, tick: int) -> None: while self.next_event <= tick: when = self.next_event for timer in self.timers: # index order: timers 0-7, then audio 0-3 if timer.next_edge == when: timer.next_edge = when + timer.period() self.clock(timer, when) self._next() # -- counting --------------------------------------------------------------------- def clock(self, timer: Timer, when: int, forced: bool = False) -> None: """One input clock. ``forced`` is a software borrow-in (a CTLB write with bit 1): TEST ROM timers 1 (timer stopped, CTLB $00, CNT $80, write $0A -> CNT $7F) shows it counts with the count enable off; HYPOTHESIS: it also ignores DONE.""" ctla = timer.ctla if not forced: if not ctla & 0x08: return if ctla & 0x40: timer.done = 0 # DOC §16: RESET_DONE is a level, not a pulse if timer.done and not ctla & 0x10: return # DOC §16 + TEST ROM timers 5/7: a done non-reloading timer stops if timer.count: timer.count -= 1 return # Terminal borrow (LT: on the clock that finds the count already zero). timer.done = 1 if ctla & 0x10: timer.count = timer.backup if ctla & 0x80 and timer.irq_bit: self.pending |= timer.irq_bit self.on_borrow(timer.index, when) successor = timer.successor if successor is not None and successor.ctla & 7 == 7: self.clock(successor, when) # -- registers --------------------------------------------------------------------- def read(self, index: int, reg: int, now: int) -> int: timer = self.timers[index] if reg == 0: return timer.backup if reg == 1: return timer.ctla if reg == 2: return timer.count return timer.ctlb(now) def write(self, index: int, reg: int, value: int, now: int) -> None: timer = self.timers[index] if reg == 0: timer.backup = value elif reg == 1: timer.ctla = value if value & 0x40: timer.done = 0 self.schedule(timer, now) elif reg == 2: timer.count = value else: # TEST ROM (LT timers, "CTLB RD/WR"): CNT=$80, write $0A -> CTLB=$08, CNT=$7F: # bit 3 writes DONE, bit 1 injects one clock and does not stay set; bits 0 # and 2 are hardware levels (see Timer.ctlb). if value & 0x02: self.clock(timer, now, forced=True) timer.done = (value >> 3) & 1