# SPDX-License-Identifier: MIT """DPC's music oscillator keeps its own hertz on every row (docs/cartridge.md, "DPC"). DOC Crane's patent US 4,644,495: the oscillator is "an internally generated asynchronous clock signal", "free running", and the cartridge receives no clock; DOC Horton: an RC on the cartridge sets it. So a second of any console's CPU cycles is MUSIC_HZ oscillator ticks, whichever crystal the console has. One test per row, through ``System.from_rom`` and through ``load(..., cpu_hz=)``. """ from __future__ import annotations import pytest from a2600_python.cartridge import load from a2600_python.cartridge.dpc import MUSIC_HZ, NTSC_CPU_HZ from a2600_python.machine import HOSTED_MACHINES, MACHINES, VCS_NTSC, VCS_PAL from a2600_python.system import System ROWS = {**MACHINES, **HOSTED_MACHINES} IMAGE = bytes(0x2800 + 255) # 8K program, 2K graphics, the 255 trailing bytes STEP = 1000 # CPU cycles between accesses: under 512 oscillator ticks each def ticks_in_one_second(cartridge, cpu_hz: float) -> int: """Oscillator ticks counted by fetcher 5 over one second of ``cpu_hz`` CPU cycles.""" cartridge.access(0x105D, 0x20, 0, True) # fetcher 5: S (oscillator clock), not music mode total, last = 0, cartridge.pointer[5] second = round(cpu_hz) for t in [*range(STEP, second, STEP), second]: cartridge.access(0x1038, 0, t) # fetcher 0's flag: any register access catches up now = cartridge.pointer[5] total += (last - now) & 0x7FF last = now return total @pytest.mark.parametrize("name", sorted(ROWS)) def test_from_rom_gives_the_oscillator_the_rows_clock(name): machine = ROWS[name] cartridge = System.from_rom(IMAGE, machine, scheme="DPC").cartridge assert cartridge.ticks_per_cycle == MUSIC_HZ / machine.timing.cpu_hz assert abs(ticks_in_one_second(cartridge, machine.timing.cpu_hz) - MUSIC_HZ) <= 1 @pytest.mark.parametrize("name", sorted(ROWS)) def test_load_with_the_rows_clock_keeps_the_oscillators_hertz(name): machine = ROWS[name] cartridge = load(IMAGE, scheme="DPC", cpu_hz=machine.timing.cpu_hz).cartridge assert abs(ticks_in_one_second(cartridge, machine.timing.cpu_hz) - MUSIC_HZ) <= 1 def test_the_ntsc_row_is_the_old_default_to_the_bit(): """The reference row did not change: its clock is the scheme's default.""" assert VCS_NTSC.timing.cpu_hz == NTSC_CPU_HZ from_rom = System.from_rom(IMAGE, VCS_NTSC, scheme="DPC").cartridge assert from_rom.ticks_per_cycle == load(IMAGE, scheme="DPC").cartridge.ticks_per_cycle def test_the_ntsc_clock_on_a_pal_console_would_play_flat(): """What the rows did before: NTSC's clock on PAL's cycles, 0.92 % fewer ticks.""" wrong = load(IMAGE, scheme="DPC").cartridge ticks = ticks_in_one_second(wrong, VCS_PAL.timing.cpu_hz) assert ticks == pytest.approx(MUSIC_HZ * VCS_PAL.timing.cpu_hz / NTSC_CPU_HZ, abs=1) assert MUSIC_HZ - ticks > 150 def test_an_explicit_clock_overrides_the_row(): cartridge = System.from_rom(IMAGE, VCS_PAL, scheme="DPC", cpu_hz=NTSC_CPU_HZ).cartridge assert cartridge.ticks_per_cycle == MUSIC_HZ / NTSC_CPU_HZ