# SPDX-License-Identifier: MIT """Rung 5: the FIFOs' enable rules, the mixer's levels and SOUNDBIAS, and the capture's promise that recording changes nothing the CPU can see (docs/sound.md).""" from __future__ import annotations from pathlib import Path import pytest from programs import board_for from gba_python.board import GBA from gba_python.cartridge.rom import GamePakROM from gba_python.ppu.timing import FRAME_CYCLES from gba_python.sound import FIFO_A, FIFO_B, fifo_level, pcm16_at_32768, psg_level ROOT = Path(__file__).resolve().parents[1] def board(): return board_for(b"\x00" * 0x100) def io_write(b, offset, value, mask=0xFFFF, time=0): b.bus.io.write16(offset, value, mask, time) def test_psg_level_spans_a_quarter(): """GBATEK "Max Output Levels": a PSG spans +/-80h at the largest settings (U-SND-9).""" assert psg_level(15, 7, 0) == 0x80 assert psg_level(0, 7, 0) == -0x80 assert psg_level(15, 7, 1) == 0x40 # SOUNDCNT_H 50 % assert psg_level(15, 7, 2) == 0x20 # 25 % assert psg_level(15, 0, 0) == 16 # NR50 0 is 1/8 def test_fifo_level_spans_the_full_range(): """GBATEK: each FIFO spans +/-200h at 100 %, half of it at 50 %.""" assert fifo_level(0x7F, True) == 0x1FC assert fifo_level(0x80, True) == -0x200 assert fifo_level(0x80, False) == -0x100 assert fifo_level(0x01, False) == 2 def test_fifo_writes_while_sound_off_load_nothing(): """alyosha-tas fifo_dma/fifo.asm test 2 (TEST ROM prose: "on console a DMA happens indicating no samples are loaded").""" b = board() io_write(b, 0x84, 0x00) b.sound.write16(0xA0, 0x1234, 0xFFFF, 0) assert b.sound.fifos[0] == [] io_write(b, 0x84, 0x80) b.sound.write16(0xA0, 0x1234, 0xFFFF, 0) assert b.sound.fifos[0] == [0x34, 0x12] def test_sound_off_empties_the_fifos(): """alyosha-tas fifo_dma/fifo.asm test 3 ("Test if disabling clears the buffer").""" b = board() io_write(b, 0x84, 0x80) b.sound.write16(0xA4, 0x5678, 0xFFFF, 0) io_write(b, 0x84, 0x00) assert b.sound.fifos[1] == [] def test_mix_adds_bias_and_clips(): b = board() s = b.sound io_write(b, 0x84, 0x80) s.soundbias = 0x200 s.soundcnt_h = 0x0300 | 0x04 # FIFO A to both sides, 100 % s.samples[0] = 0x7F assert s.mix() == (0x200 + 0x1FC, 0x200 + 0x1FC) s.samples[0] = 0x80 assert s.mix() == (0, 0) s.soundbias = 0x3FE s.samples[0] = 0x7F assert s.mix() == (0x3FF, 0x3FF) # clipped (DOC) io_write(b, 0x84, 0x00) assert s.mix() == (0x3FE, 0x3FE) # sound off: the bias alone def test_timer_drives_the_fifo_into_the_output(): """A FIFO's sample reaches the output at its timer's overflow; the output is the 10-bit sum halved (9 bits at the default resolution).""" b = board() s = b.sound io_write(b, 0x84, 0x80) io_write(b, 0x88, 0x0200) io_write(b, 0x82, 0x0B04) # FIFO A: right+left, timer 0, reset, 100 % s.write16(0xA0, 0x40C0, 0xFFFF, 0) # samples C0 (-64) then 40 (+64) s.start_capture() timers = b.timers timers.write16(0x100, 0x10000 - 1024, 0xFFFF, 0) timers.write16(0x102, 0x0080, 0xFFFF, 0) b.bus.now = 4096 b.run_events() values = [left for left, _right, _period in s.take_output()] assert values[0] == 0x100 # nothing popped yet: the bias, halved assert (0x200 - 256) >> 1 in values # -64 * 4 under the bias assert (0x200 + 256) >> 1 in values assert len(pcm16_at_32768([(0x100, 0x100, 512)] * 3)) == 12 def test_capture_changes_nothing_the_cpu_sees(): """Recording is a host feature: the same ROM with and without it ends in the same CPU state, time and picture.""" rom = ROOT / "reference/src/gba-tests-alyosha/fifo_dma/fifo_3.gba" if not rom.exists(): pytest.skip("test ROMs not fetched") results = [] for capture in (False, True): b = GBA(GamePakROM.from_path(rom), bios="free") if capture: b.sound.start_capture() b.run_until(12 * FRAME_CYCLES) results.append((list(b.cpu.r), b.cpu.cpsr, b.now, b.picture())) if capture: assert b.sound.take_output() assert results[0] == results[1] def test_fifo_pops_and_asks_below_16_bytes(): """Each overflow of the FIFO's timer moves one byte out; a FIFO that held fewer than 16 bytes before the move asks its DMA for more, and a full one (32 bytes) asks as an empty one would (alyosha-tas fifo_dma/fifo_4 and fifo_5; TEST ROM; sound.REQUEST_BELOW).""" b = board() s = b.sound asked = [] b.dma.fifo_request = lambda address, time: asked.append((address, len(s.fifos[0]))) io_write(b, 0x84, 0x80) io_write(b, 0x82, 0x4000) # FIFO A on timer 0, FIFO B on timer 1 for word in range(5): s.write16(0xA0, 0x0100 * word + word, 0xFFFF, 0) # 10 bytes s.write16(0xA2, 0x0100 * word + word, 0xFFFF, 0) assert len(s.fifos[0]) == 20 for _ in range(4): s.timer_overflow(0, 0) assert len(s.fifos[0]) == 16 and asked == [] # held 17 before the last move s.timer_overflow(0, 0) assert len(s.fifos[0]) == 15 and asked == [] # held 16: no request yet s.timer_overflow(0, 0) assert len(s.fifos[0]) == 14 and asked == [(FIFO_A, 14)] # held 15 s.timer_overflow(1, 0) # the other timer: FIFO A does not move (FIFO B, empty, asks) assert len(s.fifos[0]) == 14 and asked == [(FIFO_A, 14), (FIFO_B, 14)] for _ in range(18): s.write16(0xA0, 0, 0xFFFF, 0) assert len(s.fifos[0]) == 32 asked.clear() s.timer_overflow(0, 0) assert len(s.fifos[0]) == 31 and asked == [(FIFO_A, 31)] # full counts as empty