# SPDX-License-Identifier: MIT """The APU as the S-CPU sees it: four ports at $2140-$2143. Milestone 1 has no S-SMP yet. ``PortStub`` answers like the IPL ROM's upload loop as fullsnes describes it ("SNES APU Main CPU Communication Port"): after reset the S-SMP shows $AA, $BB on ports 0 and 1, and while it receives it echoes the CPU's port-0 value. That is enough for programs that upload sound code and do not wait for it to answer. Milestone 3 replaces it with the S-SMP and S-DSP behind the same four calls. """ from __future__ import annotations class PortStub: """A stand-in S-SMP (DESIGN): IPL-ready values, then port-0 echo.""" def __init__(self) -> None: self.reset() def reset(self) -> None: # DOC: fullsnes "SNES APU Main CPU Communication Port": the IPL # writes $AA to $2140 and $BB to $2141 when ready. self.to_cpu = [0xAA, 0xBB, 0x00, 0x00] self.from_cpu = [0, 0, 0, 0] def catch_up(self, master_clock: int) -> None: """Run the APU to ``master_clock`` (nothing to run in the stub).""" def cpu_read(self, port: int, master_clock: int) -> int: return self.to_cpu[port & 3] def cpu_write(self, port: int, value: int, master_clock: int) -> None: port &= 3 self.from_cpu[port] = value if port == 0: # The IPL's transfer loop acknowledges each byte by echoing the # index the CPU put in port 0 (DOC: fullsnes, the IPL protocol). self.to_cpu[0] = value class APU: """The S-SMP and S-DSP (``snes_python.smp``) on the S-CPU's timeline. The APU is a second clock domain (docs/timing.md "Two clock domains"): one S-SMP clock is 24 resonator clocks (DOC: wiki/Timing "Audio"). Its time is kept as an exact fraction of master time and it is caught up before every port access and at each frame's end, so the S-CPU reads what the S-SMP has written so far. The S-SMP runs whole instructions and may end a few clocks past the target; it then waits for the S-CPU to pass it. Port accesses made in that overshoot keep their own times (``smp.CPUPorts``: the S-SMP's stores are logged, the S-CPU's are posted before the catch-up). """ def __init__( self, ipl: bytes | None = None, master_hz: float = 21_477_272.0, apu_hz: float = 24_576_000.0, keep_samples: int = 32000 * 4, ) -> None: from fractions import Fraction from snes_python.smp import SMP self.smp = SMP(ipl=ipl) if ipl is None: # No IPL ROM: the S-SMP idles at the IPL's ready loop's values, # and the port stub's answers stand in (DESIGN). self.smp.ports.to_cpu[:] = [0xAA, 0xBB, 0x00, 0x00] # DOC: wiki/Timing: NTSC master clock = 6 x 315/88 MHz exactly. if round(master_hz) in (21_477_272, 21_477_273): master = Fraction(236_250_000, 11) else: master = Fraction(master_hz) # S-SMP clocks per master clock. self.ratio = Fraction(apu_hz) / 24 / master self.has_ipl = ipl is not None self.samples: list[tuple[int, int]] = [] self.keep_samples = keep_samples def catch_up(self, master_clock: int) -> None: if not self.has_ipl: return due = int(master_clock * self.ratio) smp = self.smp while smp.clock < due: smp.cpu.step() out = smp.dsp.take_samples() if out: self.samples.extend(out) if len(self.samples) > self.keep_samples: del self.samples[: len(self.samples) - self.keep_samples] def cpu_read(self, port: int, master_clock: int) -> int: self.catch_up(master_clock) if not self.has_ipl: return self.smp.cpu_read_port(port) return self.smp.cpu_read_port(port, int(master_clock * self.ratio)) def cpu_write(self, port: int, value: int, master_clock: int) -> None: if not self.has_ipl: # Without the IPL ROM nothing runs; echo port 0 as the stub does. if port & 3 == 0: self.smp.ports.stub_echo(value) self.smp.cpu_write_port(port, value) return # The store is posted at its own S-SMP cycle before the catch-up, so the S-SMP's port # reads and clears in that cycle and after see it even inside the catch-up's last # instruction (CPUPorts; docs/timing.md "Two clock domains"). ports = self.smp.ports ports.cpu_post(port, value, int(master_clock * self.ratio)) self.catch_up(master_clock) ports.settle() def take_samples(self) -> list[tuple[int, int]]: out, self.samples = self.samples, [] return out __all__ = ["APU", "PortStub"]