"""M4: the SN76489A at port $FF through the board, the mixer, the WAV writer. The tone frequency of a period N is the CPU clock / (32 N) (TI's N/16 count, two half-cycles; sn76489-python's spec 5.1-5.2): period 169 is 661.9 Hz. """ from __future__ import annotations import itertools import struct import wave from conftest import program_board from sn76489_python import SN76489, SN76489AN from colecovision_python.audio import Mixer, write_wav from colecovision_python.board import Board # Channel 0 period 169 ($A9: latch low nibble 9, data $0A), attenuation 0, others off. TONE = bytes( [0x3E, 0x9F, 0xD3, 0xFF, 0x3E, 0xBF, 0xD3, 0xFF, 0x3E, 0xDF, 0xD3, 0xFF, 0x3E, 0xFF, 0xD3, 0xFF, 0x3E, 0x89, 0xD3, 0xFF, 0x3E, 0x0A, 0xD3, 0xFF, 0x3E, 0x90, 0xD3, 0xFF, 0x18, 0xFE] ) # fmt: skip def render(board, seconds=0.25, rate=44_100): mixer = Mixer(rate) out = [] end = int(3_579_545 * seconds) while board.clock < end: board.step() if board.clock - board.psg_clock > 10_000: board.run_psg(board.clock) out.extend(mixer.mix(board.psg.take_samples())) return out, board.psg_clock / 3_579_545 def frequency(samples, seconds=0.25): """Rising zero crossings per second of emulated time (not per sample, so a chip clocked wrongly cannot hide behind the mixer's rate).""" skip = len(samples) // 4 # past the start-up writes and the DC blocker's settling tail = samples[skip:] mean = sum(tail) / len(tail) crossings = sum(1 for a, b in itertools.pairwise(tail) if (a - mean) < 0 <= (b - mean)) return crossings / (seconds * len(tail) / len(samples)) def test_a_written_tone_plays_at_its_frequency(): board = program_board(TONE, psg=SN76489(SN76489AN)) f = frequency(*render(board)) assert abs(f - 3_579_545 / (32 * 169)) < 5, f class HalfClockedPSG(Board): """Seeded fault: the chip run at half the CPU clock.""" def run_psg(self, clock): delta = clock - self.psg_clock if delta > 1: self.psg.run(delta // 2) self.psg_clock = clock def test_seeded_fault_half_clock_is_caught(): bios = bytearray(b"\xff" * 8192) bios[0:3] = b"\xc3\x00\x80" image = bytearray(b"\xff" * 0x8000) image[: len(TONE)] = TONE from colecovision_python import PlainCartridge, mame_options board = HalfClockedPSG( bytes(bios), PlainCartridge(bytes(image)), options=mame_options(), psg=SN76489(SN76489AN) ) f = frequency(*render(board)) assert abs(f - 3_579_545 / (32 * 169)) > 100, f def test_writes_reach_the_chip_with_the_ready_wait(): psg = SN76489(SN76489AN) board = program_board(TONE, psg=psg) board.step() assert board.step() == 8 # LD A,n: 7 + 1 assert board.step() == 40 # OUT ($FF),A: 11 + 1 + 28 assert psg.volume[0] == 15 # $9F reached the chip def test_mixer_rate_and_wav(tmp_path): mixer = Mixer(44_100, dc_block_hz=0) ticks = [(4.0, 4.0)] * 223_722 # one second of the chip at full level out = mixer.mix(ticks) assert abs(len(out) - 44_100) <= 1 assert abs(out[100] - 1.0) < 1e-9 path = tmp_path / "t.wav" write_wav(path, [0.5, -0.5]) with wave.open(str(path)) as w: assert (w.getnchannels(), w.getframerate(), w.getsampwidth()) == (1, 44_100, 2) assert struct.unpack("<2h", w.readframes(2)) == (16383, -16383)