"""Milestone 4: the sound chips as the board uses them. Since 2026-11-12/13 the PSG and the YM2612 are the restarted sn76489-python and ym2612-python, written from documents. Each core judges its own chip (its tests, faults script and closed-box rung: their docs/claims.md). What is here is what the board relies on: the rates, BUSY and the status ports, the timers, the variants' output stages, the reset line, and the board's audio path. The earlier version of this file judged the withdrawn cores against Nuked-PSG, Nuked-OPN2 and YM2608-LLE, two of its tests by reading tables out of the models' source. Those tests are gone with the cores they judged. """ from __future__ import annotations from sn76489_python import CLOCKS_PER_SAMPLE as PSG_CLOCKS_PER_SAMPLE from sn76489_python import SN76489, YM7101_PSG from ym2612_python import BUSY_CLOCKS, CLOCKS_PER_SAMPLE, YM2612, YM3438_ASIC from ym2612_python import YM2612_DISCRETE as FM_DISCRETE from megadrive_python.audio import ( FM_UNITS_PER_STEP, MCLK_PER_FM_SAMPLE, MCLK_PER_PSG_SAMPLE, AudioPath, ) from megadrive_python.machine import M68K_DIVIDER, MACHINES, Z80_DIVIDER def test_the_rates_the_audio_path_assumes() -> None: """The FM finishes a sample every 144 of its clocks (MCLK/7), the PSG every 16 of the Z80's (MCLK/15): 1,008 and 240 master clocks.""" assert CLOCKS_PER_SAMPLE * M68K_DIVIDER == MCLK_PER_FM_SAMPLE assert PSG_CLOCKS_PER_SAMPLE * Z80_DIVIDER == MCLK_PER_PSG_SAMPLE psg = SN76489(YM7101_PSG) psg.run(PSG_CLOCKS_PER_SAMPLE * 100) assert len(psg.take_samples()) == 100 def test_busy_follows_a_data_write_for_192_clocks() -> None: """Eke's measurement: BUSY is set by a data write and lasts 32 internal clocks, 192 of the chip's (docs/timing.md; ym2612-python spec 12.2).""" chip = YM2612(FM_DISCRETE) chip.write(0, 0x22) # an address write: no BUSY assert chip.read(0) & 0x80 == 0 chip.write(1, 0x00) assert chip.read(0) & 0x80 chip.run(BUSY_CLOCKS - 1) assert chip.read(0) & 0x80 chip.run(1) assert chip.read(0) & 0x80 == 0 def test_status_ports_by_variant() -> None: """The discrete chip drives status on port 0 only (``read`` gives None on the others, and the board keeps the last status there for a while); the ASIC drives all four (Eke).""" for variant, driven in ((FM_DISCRETE, {0}), (YM3438_ASIC, {0, 1, 2, 3})): chip = YM2612(variant) chip.write(1, 0x00) for port in range(4): value = chip.read(port) assert (value is not None) == (port in driven) if value is not None: assert value & 0x80 def test_timer_a_overflows_after_its_reload() -> None: """Timer A counts one per sample and overflows at 1,024 - reload (the manuals).""" chip = YM2612(FM_DISCRETE) reload_value = 1000 for register, value in ((0x24, reload_value >> 2), (0x25, reload_value & 3), (0x27, 0x05)): chip.write(0, register) chip.write(1, value) samples = 0 while not chip.read(0) & 0x01 and samples < 200: chip.run(CLOCKS_PER_SAMPLE) samples += 1 assert chip.read(0) & 0x01 assert 1024 - reload_value - 2 <= samples <= 1024 - reload_value + 2 assert chip.irq def test_the_ladder_is_the_discrete_chips_and_not_the_asics() -> None: """A silent discrete chip rests above zero (its provisional ladder constants); the ASIC's rests at zero.""" frames = {} for variant in (FM_DISCRETE, YM3438_ASIC): chip = YM2612(variant) chip.run(CLOCKS_PER_SAMPLE * 4) frames[variant.name] = set(chip.take_samples()) assert frames[YM3438_ASIC.name] == {(0, 0)} ((left, right),) = frames[FM_DISCRETE.name] assert left == right > 0 assert FM_DISCRETE.ladder and not YM3438_ASIC.ladder def test_fm_is_held_by_its_reset_line() -> None: """ "For as long as the Z80 is reset, the YM2612 cannot be used" (CMD 2.2): the board drives /IC low with ZRES, and writes are ignored while it is.""" chip = YM2612(FM_DISCRETE) chip.reset_line(0) chip.write(0, 0x22) chip.write(1, 0x08) assert chip.lfo_enabled == 0 chip.reset_line(1) chip.write(0, 0x22) chip.write(1, 0x08) assert chip.lfo_enabled == 1 def test_audio_path_mixes_and_resamples() -> None: path = AudioPath(MACHINES["genesis"], sample_rate=44_100) fm = [(100, -100)] * 5000 psg = [0.0] * 21000 out = path.mix(fm, psg) # 5,000 samples at 53,267 Hz is 0.0939 s, which is 4,141 at 44,100. assert 4100 <= len(out) <= 4180 assert all(-32768 <= left <= 32767 and -32768 <= right <= 32767 for left, right in out) def test_fm_frames_are_scaled_to_the_units_the_psg_gain_was_fitted_in() -> None: """The withdrawn core's idle level was 72 a sample; the new core's is 24: the path multiplies by three before mixing, so PSG_GAIN keeps its meaning.""" chip = YM2612(FM_DISCRETE) chip.run(CLOCKS_PER_SAMPLE) ((left, _right),) = chip.take_samples() assert left * FM_UNITS_PER_STEP == 72