# SPDX-License-Identifier: MIT """A 68000 waiting in ``STOP`` wakes for every VINT (``system.IPL_SYNC_WHILE_STOPPED``). The regression: the IPL synchronizer's deadline was the 68000's latest bus cycle less :data:`~megadrive_python.system.IPL_SYNC_MCLK`, and a stopped 68000 makes no bus cycles, so every VINT raised after the ``STOP`` counted as too late and the program never woke (our own demo cartridges RICK ROLL MD and BAD APPLE MD showed one black frame forever). The program here is hand-assembled; each encoding is in its comment. """ from __future__ import annotations from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import IPL_SYNC_MCLK, System HANDLER = 0x400 COUNT = 0xFF0000 #: An idle step of a stopped 68000: 4 of its clocks. IDLE_MCLK = 4 * 7 def _w(*words: int) -> bytes: return b"".join(x.to_bytes(2, "big") for x in words) def _cartridge() -> Cartridge: """Display and VINT on, then ``stop #$2300; bra.s`` forever; the handler counts.""" rom = bytearray(0x10000) rom[0:4] = (0x00FFFE00).to_bytes(4, "big") # initial SSP rom[4:8] = (0x00000200).to_bytes(4, "big") # reset vector for vector in range(2, 64): # every other exception stops for good, so a fault shows rom[vector * 4 : vector * 4 + 4] = (0x000001F0).to_bytes(4, "big") rom[0x1F0:0x1F4] = _w(0x4E72, 0x2700) # stop #$2700 rom[0x78:0x7C] = HANDLER.to_bytes(4, "big") # level 6 autovector: VINT rom[0x100:0x110] = b"SEGA GENESIS " code = bytearray() code += _w(0x46FC, 0x2700) # move.w #$2700,sr code += _w(0x33FC, 0x8004, 0x00C0, 0x0004) # move.w #$8004,$C00004: mode 5 code += _w(0x33FC, 0x8164, 0x00C0, 0x0004) # move.w #$8164,$C00004: display, VINT on code += _w(0x4E72, 0x2300) # loop: stop #$2300 (VINT, level 6, above the mask) code += _w(0x60FA) # bra.s loop rom[0x200 : 0x200 + len(code)] = code handler = _w(0x5279, 0x00FF, 0x0000) # addq.w #1,$FF0000 handler += _w(0x4A79, 0x00C0, 0x0004) # tst.w $C00004: the status read clears VINT handler += _w(0x4E73) # rte rom[HANDLER : HANDLER + len(handler)] = handler return Cartridge(bytes(rom)) def test_a_stopped_68000_wakes_for_every_vint() -> None: system = System(MACHINES["genesis"], _cartridge()) system.board.fm.enabled = False cpu, vdp = system.m68k, system.vdp system.run_frame() # the program reaches its STOP in the first frame first = system.board.read_word(COUNT) wakes: list[int] = [] for _ in range(10): frame = vdp.frame while vdp.frame == frame: stopped, start = cpu.stopped, system.mclk system.step() if stopped and not cpu.stopped: wakes.append(start - vdp.vint_raised) assert system.board.read_word(COUNT) - first == 10 # one VINT a frame, every frame assert len(wakes) == 10 # Taken at the first idle step that begins IPL_SYNC_MCLK or more after VINT rose. assert all(IPL_SYNC_MCLK <= w < IPL_SYNC_MCLK + IDLE_MCLK for w in wakes), wakes