# SPDX-License-Identifier: MIT """The S-SMP's I/O page, IPL overlay, timers and ports (snes_python.smp), from fullsnes "SNES APU SPC700 I/O Ports" and anomie's timer-tick steps. The IPL ROM here is a made-up 64-byte stand-in (the real one is bring-your-own and never committed).""" from __future__ import annotations import pytest from snes_python.smp import SMP, load_ipl from snes_python.spc_file import parse_spc def fake_ipl(entry: int = 0x0200) -> bytes: rom = bytearray([0x2F, 0xFE] * 32) # BRA $ everywhere rom[62], rom[63] = 0xC0, 0xFF # reset vector $FFC0 return bytes(rom) def program(code: bytes, origin: int = 0x0200) -> SMP: smp = SMP() smp.aram[origin : origin + len(code)] = code smp.skip_ipl(origin) return smp def test_reset_reads_the_vector_through_the_ipl_overlay(): smp = SMP(ipl=fake_ipl()) assert smp.cpu.pc == 0xFFC0 assert smp.ipl_enable # DOC fullsnes: CONTROL $B0 at power-on, ROM mapped def test_writes_under_the_ipl_go_to_ram(): smp = SMP(ipl=fake_ipl()) smp._write(0xFFC0, 0x12) assert smp.aram[0xFFC0] == 0x12 and smp._read(0xFFC0) == 0x2F smp._write(0x00F1, 0x00) # ROM off assert smp._read(0xFFC0) == 0x12 def test_io_writes_pass_to_ram_and_dsp_port(): smp = program(b"") smp._write(0x00F2, 0x2C) smp._write(0x00F3, 0x55) assert smp.dsp.read(0x2C) == 0x55 and smp._read(0x00F3) == 0x55 assert smp.aram[0x00F3] == 0x55 # DOC fullsnes: "writes are also passed to RAM" smp._write(0x00F2, 0xAC) # $80-$FF: read-only mirror smp._write(0x00F3, 0x11) assert smp.dsp.read(0x2C) == 0x55 and smp._read(0x00F3) == 0x55 def test_ports_are_two_sets_of_latches(): smp = program(b"") smp.cpu_write_port(1, 0xAB) smp._write(0x00F5, 0xCD) assert smp._read(0x00F5) == 0xAB and smp.cpu_read_port(1) == 0xCD smp._write(0x00F1, 0x10) # DOC fullsnes CONTROL bit 4: reset input latches F4/F5 assert smp._read(0x00F5) == 0x00 and smp.cpu_read_port(1) == 0xCD def test_a_posted_cpu_store_is_seen_from_its_own_smp_cycle(): """An S-CPU store posted for S-SMP cycle c is seen by the S-SMP's access in cycle c (made at clock c + 1) and not by one in cycle c - 1 (DESIGN: docs/timing.md "Two clock domains").""" smp = program(b"") ports = smp.ports c = smp.clock + 3 ports.cpu_post(1, 0x42, c) assert ports.smp_read(1, c) == 0x00 # cycle c - 1 assert ports.smp_read(1, c + 1) == 0x42 # cycle c ports.settle() assert ports.pending == [] and ports.smp_read(1) == 0x42 def test_a_clear_after_a_cpu_store_wins_even_when_the_smp_ran_past_it(): """CONTROL bit 4 written in S-SMP cycle k clears an S-CPU store of cycle k or earlier, whether the store was posted before the catch-up or arrives after the S-SMP has passed it.""" smp = program(b"") ports = smp.ports k = smp.clock + 5 ports.cpu_post(0, 0x11, k) # in the clear's cycle: cleared ports.cpu_post(1, 0x22, k + 1) # the cycle after: survives ports.smp_clear(0, k + 1) ports.settle() assert ports.from_cpu[:2] == [0x00, 0x22] ports.cpu_write(0, 0x33, k - 2) # arrives late, but is earlier than the clear assert ports.from_cpu[0] == 0x00 ports.cpu_write(0, 0x44, k + 1) assert ports.from_cpu[0] == 0x44 def test_timer_rates(): """DOC fullsnes: T0 counts at 8 kHz (every 128 S-SMP clocks), T2 at 64 kHz (every 16).""" smp = program(bytes([0x2F, 0xFE])) # BRA $: 4 cycles per loop smp._write(0x00FA, 1) smp._write(0x00FC, 1) smp._write(0x00F1, 0x05) smp._read(0x00FD) smp._read(0x00FF) start = smp.clock smp.run(128 * 10) elapsed = smp.clock - start t0, t2 = smp._read(0x00FD), smp._read(0x00FF) assert abs(t0 - elapsed // 128) <= 1 assert abs(t2 - (elapsed // 16) % 16) <= 1 def test_timer_out_resets_on_read_and_divider_256(): smp = program(bytes([0x2F, 0xFE])) smp._write(0x00FA, 0) # DOC fullsnes: 00h = divide by 256 smp._write(0x00F1, 0x01) smp.run(128 * 255) assert smp._read(0x00FD) == 0 smp.run(128 * 2) assert smp._read(0x00FD) == 1 assert smp._read(0x00FD) == 0 # DOC fullsnes: reset to 0 after reading def test_write_only_registers_read_zero(): smp = program(b"") for address in (0x00F0, 0x00F1, 0x00FA, 0x00FB, 0x00FC): assert smp._read(address) == 0 def test_one_dsp_step_per_cycle(): smp = program(bytes([0x00] * 64)) # NOPs, 2 cycles each start = smp.clock for _ in range(16): smp.step() assert smp.clock - start == 32 and len(smp.dsp.samples) == 1 def test_load_ipl_requires_64_bytes(tmp_path): path = tmp_path / "ipl.bin" path.write_bytes(bytes(63)) with pytest.raises(ValueError): load_ipl(path) path.write_bytes(fake_ipl()) assert load_ipl(path) == fake_ipl() def test_parse_spc_layout(): """snesmusic.org "SPC File Format v0.30": registers at $25, RAM at $100, DSP at $10100.""" data = bytearray(0x10200) data[:33] = b"SNES-SPC700 Sound File Data v0.30" data[0x23] = 27 data[0x25:0x2C] = bytes((0x34, 0x12, 1, 2, 3, 0x02, 0xEF)) data[0x100 + 0x1234] = 0x99 data[0x10100 + 0x6C] = 0x20 spc = parse_spc(bytes(data)) assert (spc.pc, spc.a, spc.x, spc.y, spc.psw, spc.sp) == (0x1234, 1, 2, 3, 2, 0xEF) assert spc.ram[0x1234] == 0x99 and spc.dsp_registers[0x6C] == 0x20 smp = SMP() smp.load_spc(spc) assert smp.cpu.pc == 0x1234 and smp.dsp.flg == 0x20 def test_real_ipl_signals_ready(): """With a real IPL ROM ($SNES_IPL, never committed): after boot it writes $AA/$BB to ports 0/1 (DOC fullsnes "Boot ROM Disassembly": "notify Main CPU that APU is ready").""" ipl = load_ipl() if ipl is None: pytest.skip("SNES_IPL not set") smp = SMP(ipl=ipl) smp.run(20000) assert (smp.cpu_read_port(0), smp.cpu_read_port(1)) == (0xAA, 0xBB)