# SPDX-License-Identifier: MIT """Rung 1: JP. Every BRCH code of DS p.23 in both senses, CALL/RT and the stack depth (C5, U10), and the uPD96050's bank bits, HJMP, HCALL and JMPSO (U11, research tier; C8).""" from __future__ import annotations import pytest from words import core, jp, op from upd7725_python import ( FLAG_C, FLAG_OV0, FLAG_OV1, FLAG_S0, FLAG_S1, FLAG_Z, UPD7725_MODEL, UPD96050_MODEL, ) FLAG_CODES = [ (0x080, "flaga", FLAG_C), (0x084, "flagb", FLAG_C), (0x088, "flaga", FLAG_Z), (0x08C, "flagb", FLAG_Z), (0x090, "flaga", FLAG_OV0), (0x094, "flagb", FLAG_OV0), (0x098, "flaga", FLAG_OV1), (0x09C, "flagb", FLAG_OV1), (0x0A0, "flaga", FLAG_S0), (0x0A4, "flagb", FLAG_S0), (0x0A8, "flaga", FLAG_S1), (0x0AC, "flagb", FLAG_S1), ] def target_after(word, **registers): dsp = core(word, **registers) dsp.step() return dsp.pc @pytest.mark.parametrize(("code", "register", "bit"), FLAG_CODES) def test_flag_conditions(code, register, bit): """DS p.23: the even code jumps when the flag is 0 (JNxx), code + 2 when it is 1.""" assert target_after(jp(code, na=0x123), **{register: 0}) == 0x123 assert target_after(jp(code, na=0x123), **{register: bit}) == 1 assert target_after(jp(code + 2, na=0x123), **{register: bit}) == 0x123 assert target_after(jp(code + 2, na=0x123), **{register: 0x3F & ~bit}) == 1 @pytest.mark.parametrize( ("code", "taken"), [(0x0B0, {0x40}), (0x0B1, {0x41, 0x4F}), (0x0B2, {0x4F}), (0x0B3, {0x40, 0x41})], ) def test_dpl_conditions(code, taken): """DS p.23: JDPL0, JDPLN0, JDPLF, JDPLNF test DP bits 3-0.""" for dp in (0x40, 0x41, 0x4F): assert target_after(jp(code, na=0x10), dp=dp) == (0x10 if dp in taken else 1) def test_rqm_conditions(): assert target_after(jp(0x0BE, na=0x10), sr=0x8000) == 0x10 # JRQM assert target_after(jp(0x0BE, na=0x10), sr=0) == 1 assert target_after(jp(0x0BC, na=0x10), sr=0) == 0x10 # JNRQM assert target_after(jp(0x0BC, na=0x10), sr=0x8000) == 1 def test_serial_acknowledge_conditions(): """C12: the latches, always 0 on the SNES (no serial clock).""" assert target_after(jp(0x0B4, na=0x10)) == 0x10 # JNSIAK assert target_after(jp(0x0B6, na=0x10)) == 1 assert target_after(jp(0x0B8, na=0x10)) == 0x10 # JNSOAK assert target_after(jp(0x0BA, na=0x10)) == 1 def test_unknown_brch_falls_through(): for code in (0x001, 0x081, 0x0BF, 0x102, 0x1FF): assert target_after(jp(code, na=0x10)) == 1 def test_call_and_return(): dsp = core(jp(0x140, na=0x10)) dsp.program[0x10] = op(rt=True) dsp.step() assert (dsp.pc, dsp.sp, dsp.stack[0]) == (0x10, 1, 1) dsp.step() assert (dsp.pc, dsp.sp) == (1, 0) @pytest.mark.parametrize("model", [UPD7725_MODEL, UPD96050_MODEL]) def test_stack_depth_wraps(model): """C5: 4 levels (DS (3)), 8 on the uPD96050 (fullsnes); U10: beyond that the pointer wraps (undocumented; the core's choice).""" depth = model.stack_depth dsp = core(*[jp(0x140, na=i + 1) for i in range(depth + 1)], model=model) dsp.run(depth) assert dsp.sp == 0 and dsp.stack == list(range(1, depth + 1)) dsp.step() # one more CALL overwrites the oldest entry assert dsp.sp == 1 and dsp.stack[0] == depth + 1 def test_upd7725_drops_bank_bits(): """The uPD7725's PC is 11 bits; JP bits 1-0 are "Reserved" (C8: the extra codes still execute, masked).""" assert target_after(jp(0x100, na=0x7FF, bank=3)) == 0x7FF assert target_after(jp(0x101, na=0x123)) == 0x123 # HJMP: bit 13 masked away def test_upd96050_jumps(): """U11: bank bits are NA bits 12-11; JMP/CALL clear PC bit 13, HJMP/HCALL set it, conditional jumps keep it; JMPSO jumps to SO.""" def after(word, **registers): dsp = core(model=UPD96050_MODEL, **registers) dsp.program[dsp.pc] = word dsp.step() return dsp assert after(jp(0x100, na=0x123, bank=2)).pc == 0x1123 assert after(jp(0x101, na=0x123, bank=2)).pc == 0x3123 assert after(jp(0x100, na=0x5, bank=0), pc=0x2005).pc == 0x0005 dsp = after(jp(0x141, na=0x7, bank=1), pc=0x10) assert (dsp.pc, dsp.stack[0]) == (0x2807, 0x11) assert after(jp(0x0BC, na=0x7, bank=1), pc=0x2010).pc == 0x2807 # JNRQM keeps bit 13 assert after(jp(0x0BC, na=0x7, bank=1), pc=0x0010).pc == 0x0807 assert after(jp(0x000), so=0x2ABC).pc == 0x2ABC # JMPSO