"""The whole system: z80-python on the board, interrupts, pause, and ZEXDOC's start.""" from __future__ import annotations import zipfile from pathlib import Path import pytest from sms_python.board import SG1000, SMS2 from sms_python.cartridge import Cartridge from sms_python.system import System ZEX = Path(__file__).resolve().parents[1] / "tests" / "test_roms" / "github" / "ZEXALL-SMS-0.21.zip" def program(code: dict[int, bytes]) -> Cartridge: image = bytearray(0x8000) for address, data in code.items(): image[address : address + len(data)] = data return Cartridge(bytes(image)) # At $0000: IM 1; enable the frame interrupt (VDP register 1 = $20); EI; HALT loop. # At $0038: count in $C000, read status (acknowledges), EI, RETI. # At $0066: count in $C001, RETN. BOOT = bytes.fromhex( "31 F0 DF" # ld sp,$DFF0 "ED 56" # im 1 "3E 20 D3 BF 3E 81 D3 BF" # vdp register 1 = $20 "FB" # ei "76 18 FD" # halt; jr halt ) IRQ = bytes.fromhex("F5 3A 00 C0 3C 32 00 C0 DB BF F1 FB ED 4D") NMI = bytes.fromhex("F5 3A 01 C0 3C 32 01 C0 F1 ED 45") def test_frame_interrupt_every_frame(): system = System(SMS2, program({0x0000: BOOT, 0x0038: IRQ, 0x0066: NMI}), audio=False) for _ in range(10): system.run_frame() assert 9 <= system.board.ram[0] <= 10 def test_pause_is_an_nmi(): system = System(SMS2, program({0x0000: BOOT, 0x0038: IRQ, 0x0066: NMI}), audio=False) system.run_frame() system.board.press_pause() system.run_frame() assert system.board.ram[1] == 1 def test_sg1000_frame_interrupt_too(): system = System(SG1000, program({0x0000: BOOT, 0x0038: IRQ, 0x0066: NMI}), audio=False) for _ in range(5): system.run_frame() assert 4 <= system.board.ram[0] <= 5 @pytest.mark.skipif(not ZEX.exists(), reason="ZEXALL-SMS not fetched") def test_zexdoc_starts_and_finds_its_outputs(): # The full run is scripts/run_zex.py (about six minutes on PyPy). with zipfile.ZipFile(ZEX) as archive: image = archive.read("zexdoc.sms") system = System(SMS2, Cartridge(image), sdsc_console=True, audio=False) system.board.vdp.render = False system.run_cycles(3_579_545 * 2) console = system.console assert console.startswith("Z80 Instruction Exerciser 0.21\nDocumented flags version") assert "* SMS Mode 4" in console # it found our VDP in Mode 4 assert "SCF/CCF tests:" in console