"""The sound unit's rules, each against the source that states it (docs/start-here.md "The sound unit"). A synthetic BIOS and cartridge, as in ``test_board.py``; the judges are the test ROMs and the comparison with NanoBoyAdvance (docs/validation.md, rung 5).""" from __future__ import annotations import ast from pathlib import Path import gb_apu_python import pytest from test_board import make from gba_python import GBA, state IO = 0x04000000 SOUND1CNT_H, SOUND1CNT_X, SOUND2CNT_L, SOUND2CNT_H = 0x62, 0x64, 0x68, 0x6C SOUND3CNT_L, SOUND3CNT_H, SOUND3CNT_X = 0x70, 0x72, 0x74 SOUNDCNT_L, SOUNDCNT_H, SOUNDCNT_X, SOUNDBIAS, WAVE_RAM, FIFO_A = 0x80, 0x82, 0x84, 0x88, 0x90, 0xA0 def write(gba: GBA, offset: int, value: int) -> None: gba.bus_write(IO + offset, 16, value, 0) def read(gba: GBA, offset: int) -> int: return gba.bus_read(IO + offset, 16, 0) def run_to(gba: GBA, cycle: int) -> None: gba.run(cycle - gba.scheduler.now) def sounding(soundcnt_h: int = 2) -> GBA: """Sound on, every channel to both sides at full volume, channel 2 a square at volume 15.""" gba = make() write(gba, SOUNDCNT_X, 0x80) write(gba, SOUNDCNT_L, 0xFF77) write(gba, SOUNDCNT_H, soundcnt_h) write(gba, SOUND2CNT_L, 0xF080) write(gba, SOUND2CNT_H, 0x8000) return gba # ----- the Game Boy channels behind the GBA's registers (GBATEK "GBA Sound Controller") -------- def test_channel_registers_reach_the_core_at_their_gba_offsets(): gba = make() write(gba, SOUNDCNT_X, 0x80) write(gba, SOUND1CNT_H, 0xF7C0) # NR11 duty 3, NR12 volume 15 assert read(gba, SOUND1CNT_H) == 0xF7C0 assert read(gba, SOUNDCNT_X) == 0x80 write(gba, SOUND1CNT_X, 0xC123) # NR13, NR14: trigger, length on assert read(gba, SOUND1CNT_X) == 0x4000 # only the length flag reads assert read(gba, SOUNDCNT_X) == 0x81 # channel 1 plays assert gba.apu.psg.ch1.frequency == 0x123 assert gba.apu.registers[SOUND1CNT_H >> 1] == 0xF7C0 # as written, for state reports # NR13 lands before NR14's trigger: the period the trigger loads is the new one write(gba, SOUND1CNT_X, 0x87FF) assert gba.apu.psg.ch1.countdown < 16 def test_a_byte_write_reaches_only_its_register(): gba = make() write(gba, SOUNDCNT_X, 0x80) gba.bus_write(IO + SOUND1CNT_H + 1, 8, 0xF0, 0) # NR12 alone assert read(gba, SOUND1CNT_H) == 0xF000 def test_master_enable_off_clears_and_locks_the_channel_registers_and_empties_the_fifos(): gba = sounding() gba.bus_write(IO + FIFO_A, 32, 0x11223344, 0) assert gba.apu.fifo[0].count == 1 write(gba, SOUNDCNT_X, 0) assert read(gba, SOUND2CNT_L) == 0 and read(gba, SOUNDCNT_L) == 0 assert read(gba, SOUNDCNT_H) == 2 # SOUNDCNT_H is not the Game Boy's: it stays assert gba.apu.fifo[0].count == 0 # NanoBoyAdvance registers.cpp write(gba, SOUND2CNT_L, 0xF080) assert read(gba, SOUND2CNT_L) == 0 gba.bus_write(IO + FIFO_A, 32, 0x11223344, 0) assert gba.apu.fifo[0].count == 0 assert gba.apu.mix() == (0, 0) def test_wave_ram_reaches_the_bank_that_is_not_selected(): gba = make() write(gba, SOUNDCNT_X, 0x80) write(gba, WAVE_RAM, 0x2211) # bank 0 selected: this is bank 1 write(gba, SOUND3CNT_L, 0x40) assert read(gba, SOUND3CNT_L) == 0x40 write(gba, WAVE_RAM, 0x4433) assert read(gba, WAVE_RAM) == 0x4433 write(gba, SOUND3CNT_L, 0x00) assert read(gba, WAVE_RAM) == 0x2211 write(gba, SOUND3CNT_H, 0x8000) assert read(gba, SOUND3CNT_H) == 0x8000 # the force-75 % bit def test_frame_sequencer_steps_every_32768_cycles_from_power_on(): """512 Hz from the system clock (NanoBoyAdvance s_cycles_per_step). The length counter is clocked on every other step: a length of 2 ends at the third step, cycle 98,304.""" gba = make() write(gba, SOUNDCNT_X, 0x80) write(gba, SOUND2CNT_L, 0xF03E) # length 64 - 62 = 2 write(gba, SOUND2CNT_H, 0xC000) # trigger, length on assert gba.scheduler.now < 16384 run_to(gba, 98_000) assert read(gba, SOUNDCNT_X) == 0x82 run_to(gba, 98_400) assert read(gba, SOUNDCNT_X) == 0x80 def test_channels_run_at_a_quarter_of_the_system_clock(): gba = sounding() write(gba, SOUND2CNT_H, 0x8000 | 2047) # the shortest period: a duty step every 4 T gba.run(400) # past the start-up delay gba.apu.sync() start = gba.apu.psg.ch2.index gba.run(5 * 4 * 4) # 20 T of the core's clock: five duty steps gba.apu.sync() assert (gba.apu.psg.ch2.index - start) & 7 == 5 # ----- the mixer (NanoBoyAdvance APU::StepMixer) ------------------------------------------------ def test_psg_volume_is_25_50_100_percent_and_scaled_by_nr50(): levels = [] for soundcnt_h in (2, 1, 0, 3): left, right = sounding(soundcnt_h).apu.mix() assert left == right levels.append(abs(left)) assert levels[0] == 120 # 8 x 15, x4, x8 / 32 assert levels[1] == 60 assert levels[2] in (30, 31) # the shift rounds down: -30.x is -31 assert levels[3] == 0 # the prohibited setting is silent gba = sounding() write(gba, SOUNDCNT_L, 0xFF03) # left volume 0 (1/8), right 3 (4/8) left, right = gba.apu.mix() assert abs(left) == 15 and abs(right) == 60 def test_nr51_routes_each_channel_to_a_side(): gba = sounding() write(gba, SOUNDCNT_L, 0x2077) # channel 2 left only left, right = gba.apu.mix() assert abs(left) == 120 and right == 0 write(gba, SOUNDCNT_L, 0x0277) left, right = gba.apu.mix() assert left == 0 and abs(right) == 120 def test_fifo_volume_sides_and_the_10_bit_clip(): gba = make() write(gba, SOUNDCNT_X, 0x80) fifo_a, fifo_b = gba.apu.fifo fifo_a.sample = 100 write(gba, SOUNDCNT_H, 0x0300) # A to both sides at 50 % assert gba.apu.mix() == (200, 200) write(gba, SOUNDCNT_H, 0x0304) # 100 % assert gba.apu.mix() == (400, 400) write(gba, SOUNDCNT_H, 0x0104) # right only assert gba.apu.mix() == (0, 400) fifo_b.sample = 100 write(gba, SOUNDCNT_H, 0x220C) # A and B to the left at 100 %: 800 clips at 3FFh - 200h assert gba.apu.mix() == (511, 0) fifo_a.sample = fifo_b.sample = -128 assert gba.apu.mix() == (-512, 0) def test_soundbias_level_moves_the_clip(): gba = make() write(gba, SOUNDCNT_X, 0x80) write(gba, SOUNDCNT_H, 0x0304) gba.apu.fifo[0].sample = 100 write(gba, SOUNDBIAS, 0x0100) # 400 + 100h, less 200h assert gba.apu.mix() == (144, 144) assert read(gba, SOUNDBIAS) == 0x0100 def test_capture_gives_32768_stereo_samples_a_second_at_every_resolution(): for bias in (0x0200, 0xC200): # 32,768 Hz and 262,144 Hz mixing gba = sounding() write(gba, SOUNDBIAS, bias) assert gba.apu.take_samples().tolist() == [] # nothing is mixed until asked gba.apu.start_capture() start = gba.scheduler.now gba.run(512 * 100) samples = gba.apu.take_samples() expected = (gba.scheduler.now >> 9) - (start >> 9) assert len(samples) == 2 * expected assert {abs(s) for s in samples} == {120 * 64} def test_sound_state_round_trips_while_capturing(): a = sounding() a.apu.start_capture() a.run(10_000) b = state.load(state.save(a)) a.run(50_000) b.run(50_000) assert a.apu.take_samples() == b.apu.take_samples() assert a.apu.registers == b.apu.registers # ----- the core stays a core ---------------------------------------------------------------- def _imports(path: Path) -> set[str]: found: set[str] = set() for node in ast.walk(ast.parse(path.read_text())): if isinstance(node, ast.ImportFrom) and node.module: found.add(node.module) elif isinstance(node, ast.Import): found.update(alias.name for alias in node.names) return found def test_the_game_boy_channels_come_from_the_core_and_it_imports_no_board(): apu = Path(__file__).resolve().parents[1] / "src" / "gba_python" / "apu.py" assert "gb_apu_python" in _imports(apu) for path in Path(gb_apu_python.__file__).parent.rglob("*.py"): for module in _imports(path): assert not module.startswith(("gba_python", "arm7tdmi_python")), f"{path}: {module}" FIFO_ROM = Path(__file__).parents[1] / "reference/src/gba-tests-alyosha/fifo_dma/fifo.gba" @pytest.mark.skipif(not FIFO_ROM.exists(), reason="reference test ROMs not fetched") def test_a_request_during_a_refill_reads_a_fifth_word_and_leaves_three() -> None: """alyosha-tas ``fifo.gba`` on hardware: timer 1 reads 68h and 8Bh (test 2), 68h (test 3), 68h and A5h (test 4). The built ROM does not compare test 2's second read (its source was corrected to 8Bh after the build), so all five are read here (C28).""" gba = make(FIFO_ROM.read_bytes()) reads: list[int] = [] counter = gba.timers.read_counter def read_counter(index: int) -> int: value = counter(index) if index == 1: reads.append(value) return value gba.timers.read_counter = read_counter gba.io._build() gba.run(4000) assert reads[:5] == [0x68, 0x8B, 0x68, 0x68, 0xA5] def test_a_request_for_a_refill_still_waiting_adds_nothing() -> None: """A second overflow while the refill waits (behind the CPU or a channel above it) is not the request-during-a-refill of C28: the refill moves its four words and no fifth is read. Pokemon Mystery Dungeon holds both FIFO channels behind a 32,768-cycle DMA0.""" gba = sounding() write(gba, 0xBC, 0x0000) # DMA1 source 2000000 write(gba, 0xBE, 0x0200) write(gba, 0xC0, FIFO_A) # destination 40000A0 write(gba, 0xC2, 0x0400) write(gba, 0xC6, 0xB640) # enabled, on the FIFO's request, repeating, words, fixed channel = gba.dma.channels[1] for _ in range(3): # the first makes it runnable; no CPU access lets it run gba.dma.request_fifo(0) gba.scheduler.run_until(gba.scheduler.now + 8) assert gba.dma.runnable == 2 assert not channel.fifo_again gba.dma.run() assert channel.latch_src == 0x02000010 assert channel.latch_length == 4