# SPDX-License-Identifier: MIT """Rung 2: the programs' built-in self-tests (hardware-designed self-test tier). Each command was written by the chip's programmers to pass on production silicon. Expected values: the version words from fullsnes "BIOS Functions" (0100h DSP-1/1A, 0101h DSP-1B, 0300h DSP-3, 0400h DSP-4); the data ROM transfers must return the image word for word; the memory tests' pass values were read from the programs (DSP-1 ``0Fh``: ``LDI @DR,0000h`` at 4DCh, the failure path at 4DEh returns a nonzero code built from DP; ST011 ``F1h``/``F2h``: pass parks at 086h/0ABh with DR = 00h, failures load 00FFh, 01h or 02h then the failing address), and agree with the HLE (DSP-1 command 0Fh returns 0000h). The host is the minimal harness: a byte per RQM, with ``host_delay`` instructions between accesses (an S-CPU cannot answer faster). """ from __future__ import annotations import pytest from conftest import require_image from upd7725_python import UPD7725, UPD96050_MODEL from upd7725_python.harness import Harness def harness(name, cls=UPD7725): program, data_rom = require_image(name) model = UPD96050_MODEL if name.startswith("st") else None dsp = cls(program, data_rom, model=model) if model else cls(program, data_rom) h = Harness(program, data_rom, dsp=dsp) h.run_to_idle() return h @pytest.mark.parametrize( ("name", "version"), [("dsp1", 0x0100), ("dsp1b", 0x0101), ("dsp3", 0x0300)] ) def test_dsp1_family(name, version): """DSP-1, DSP-1B, DSP-3: 2Fh version, 0Fh memory test, 1Fh data ROM transfer (each one dummy parameter word, results 16-bit), then back in the command loop with DR = 80h.""" h = harness(name) assert h.command(0x2F, [0], 1) == [version] h.run_to_idle() assert h.command(0x0F, [0], 1) == [0x0000] h.run_to_idle() assert h.command(0x1F, [0], 1024) == h.dsp.data_rom h.run_to_idle() assert h.dsp.dr == 0x0080 and h.dsp.read_sr() & 0x04 # 8-bit mode again def test_dsp4(): """DSP-4: 16-bit command words; 14h version, 13h data ROM transfer; DR = FFFFh idle.""" h = harness("dsp4") assert h.command(0x14, [], 1, command_word=True) == [0x0400] h.run_to_idle() assert h.command(0x13, [], 1024, command_word=True) == h.dsp.data_rom h.run_to_idle() assert h.dsp.dr == 0xFFFF def test_st011(): """ST011: F1h writes and checks an address pattern in all 2,048 RAM words; F2h checks that pattern survived, then fills and checks FFFFh. Both pass with DR = 00h.""" h = harness("st011") h.write_byte(0xF1) h.run_to_idle() assert (h.dsp.pc, h.read_byte()) == (0x086, 0x00) h.run_to_idle() h.write_byte(0xF2) h.run_to_idle() assert (h.dsp.pc, h.read_byte()) == (0x0AB, 0x00) assert set(h.dsp.ram) == {0xFFFF} def test_st011_f2_detects_a_lost_pattern(): """The negative control: F2 without F1 (a cleared battery RAM) fails at address 1.""" h = harness("st011") h.write_byte(0xF2) h.run_to_idle() assert h.dsp.pc == 0x0AE and h.read_byte() == 0x01 class StuckBit(UPD7725): """A faulty core: RAM word 37h's bit 0 is stuck at 1.""" def _move(self, destination, value): super()._move(destination, value) self.ram[0x37] |= 1 def test_dsp1_memory_test_detects_a_fault(): """The negative control: the memory test fails on a core with a stuck RAM bit.""" h = harness("dsp1", cls=StuckBit) assert h.command(0x0F, [0], 1) != [0x0000]