"""The FM unit: a YM2413 at ports $F0-$F2 (the wiki's AudioControlPort and FMChipDetection pages). The chip itself is ym2413-python's; these check the board's ports, clocking and mix.""" from __future__ import annotations import pytest from sms_python.board import MARK_III_FM, SMS2, SMS_JAPAN, Board from sms_python.cartridge import BANK, Cartridge from sms_python.vdp import MCLOCKS_PER_LINE NTSC_CLOCK = 3_579_545 def rom(banks: int) -> Cartridge: return Cartridge(b"".join(bytes([bank]) * BANK for bank in range(banks))) def detect(board: Board) -> bool: """Phantasy Star's detection (FMChipDetection.pmwiki): seven writes of bit 0, each read back in the low three bits, with the I/O chip disabled.""" board.write_port(0x3E, board.memory_control | 0x04) passed = 0 for b in range(7, 0, -1): board.write_port(0xF2, b & 1) passed += (board.read_port(0xF2) & 0x07) == b & 1 return passed == 7 def fm_write(board: Board, register: int, value: int) -> None: board.write_port(0xF0, register) board.advance(12) board.write_port(0xF1, value) board.advance(84) def play(board: Board) -> None: fm_write(board, 0x30, 0x10) # instrument 1, volume 0 fm_write(board, 0x10, 0x81) fm_write(board, 0x20, 0x19) # key on, block 4: about 440 Hz def test_the_japanese_console_and_the_fm_unit_are_detected(): assert detect(Board(SMS_JAPAN, rom(1))) assert detect(Board(MARK_III_FM, rom(1))) assert not detect(Board(SMS2, rom(1))) def test_the_mark_iii_fm_unit_forces_bit_1_to_0(): board = Board(MARK_III_FM, rom(1)) board.write_port(0xF2, 0x03) assert board.read_port(0xF2) == 0x01 # ApolloBoy's "03 01 01" def test_the_counter_counts_lines_in_bits_7_to_5(): board = Board(SMS_JAPAN, rom(1)) board.write_port(0xF2, 0x02) assert board.read_port(0xF2) == 0x02 cpu_cycles_per_line = MCLOCKS_PER_LINE // 3 board.advance(cpu_cycles_per_line * 8 + 1) # eight lines begun: bit 3 assert board.read_port(0xF2) & 0xE0 == 0x20 board.advance(cpu_cycles_per_line * (2048 - 8)) # 2048 lines: bit 11, bits 7, 3 clear assert board.read_port(0xF2) & 0xE0 == 0x80 def test_fm_sounds_only_when_enabled(): board = Board(SMS_JAPAN, rom(1)) play(board) board.advance(NTSC_CLOCK // 10) muted = board.take_samples() assert all(left == pytest.approx(1 / 3) for left, _ in muted) # $00: the PSG alone, silent board.write_port(0xF2, 0x01) board.advance(NTSC_CLOCK // 10) heard = [left for left, _ in board.take_samples()] assert max(heard) - min(heard) > 0.02 # one channel at full scale swings 1/6 assert all(0.0 <= level <= 1.0 for level in heard) def test_the_psg_is_muted_by_fm_only(): board = Board(SMS_JAPAN, rom(1)) for value in (0x81, 0x00, 0x90): # PSG tone 1 at its top note, full volume board.write_port(0x7F, value) board.advance(32) board.advance(16 * 400) heard = [left for left, _ in board.take_samples()[-40:]] # A third each for the PSG and the FM unit; the tone averages 1/8 of the PSG's range. assert sum(heard) / len(heard) == pytest.approx((0.125 + 1) / 3, abs=0.005) board.write_port(0xF2, 0x01) board.advance(16 * 400) # The PSG gone; a silent YM2413 still drives its slots' sign words (0 or 1). assert board.take_samples()[-1][0] == pytest.approx(1 / 3, abs=0.01) def test_one_second_gives_the_sample_rate_with_fm(): board = Board(SMS_JAPAN, rom(1), sample_rate=44_100) board.advance(NTSC_CLOCK) assert abs(len(board.take_samples()) - 44_100) <= 2