# SPDX-License-Identifier: MIT """Does VINT fire on a frame whose vertical border never started? At the pins. Kabuto's V28/V30 trick: switch to V30 inside the active picture and back to V28 after line 224 but before line 240, and the VDP "forgets" to start the vertical border; "the resulting picture is still stable since vblank still works normally". Whether the vertical interrupt still comes on such a frame is not said. ``vdp.VINT_WITHOUT_BORDER`` is the board's switch. A cartridge built here enables VINT, counts its interrupts in work RAM ($FF0000), and on every frame waits for V = 100, sets V30, waits for V = $E6 (230), sets V28 and counts the frame ($FF0004). A second cartridge counts the same way without switching. Both run on the referee, Nuked-MD @ 594ca2a, as a closed box (only work RAM is read at the end, what the 68000 wrote), and on the board with the switch each way. python scripts/run_vint_border.py [--frames 12] """ from __future__ import annotations import argparse import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path[:0] = [str(ROOT / "src"), str(ROOT / "scripts")] import nuked_md # noqa: E402 from run_vscroll_latch import _w # noqa: E402 FRAME_MCLK = 262 * 3420 # NTSC def cartridge(trick: bool) -> bytes: code = bytearray() code += _w(0x46FC, 0x2700) # move.w #$2700,sr code += bytes.fromhex("23fc5345474100a14000") # move.l #'SEGA',$A14000 code += _w(0x43F9, 0x00C0, 0x0004) # lea $C00004,a1 (control) code += _w(0x45F9, 0x00C0, 0x0008) # lea $C00008,a2 (HV counter) for reg, value in ((0x00, 0x04), (0x01, 0x64), (0x02, 0x30), (0x04, 0x07), (0x05, 0x78), (0x0C, 0x81), (0x0D, 0x3F), (0x0F, 0x02), (0x10, 0x01)): # fmt: skip code += _w(0x32BC, 0x8000 | reg << 8 | value) # move.w #$8rvv,(a1) code += _w(0x42B9, 0x00FF, 0x0000, 0x42B9, 0x00FF, 0x0004) # clr.l $FF0000; clr.l $FF0004 code += _w(0x46FC, 0x2000) # move.w #$2000,sr: interrupts on loop = len(code) code += _w(0x1012, 0x0C00, 0x0064, 0x66F8) # L1: move.b (a2),d0; cmp.b #100,d0; bne.s L1 code += _w(0x32BC, 0x816C if trick else 0x8164) # V30 (or V28 again) code += _w(0x1012, 0x0C00, 0x00E6, 0x66F8) # L2: the same for V = $E6 code += _w(0x32BC, 0x8164) # V28 code += _w(0x52B9, 0x00FF, 0x0004) # addq.l #1,$FF0004: a frame code += _w(0x6000, (loop - (len(code) + 2)) & 0xFFFF) # bra.w L1 rom = bytearray(0x10000) rom[0:4] = (0x00FFFE00).to_bytes(4, "big") rom[4:8] = (0x00000200).to_bytes(4, "big") for vector in range(2, 64): rom[vector * 4 : vector * 4 + 4] = (0x000001F0).to_bytes(4, "big") rom[30 * 4 : 31 * 4] = (0x00000300).to_bytes(4, "big") # level 6: VINT rom[0x1F0:0x1F4] = bytes.fromhex("4e722700") # stop #$2700 rom[0x100:0x110] = b"SEGA GENESIS " rom[0x200 : 0x200 + len(code)] = code rom[0x300:0x308] = _w(0x52B9, 0x00FF, 0x0000, 0x4E73) # addq.l #1,$FF0000; rte return bytes(rom) def counts(ram: bytes) -> tuple[int, int]: return int.from_bytes(ram[0:4], "big"), int.from_bytes(ram[4:8], "big") def referee(rom: bytes, frames: int) -> tuple[int, int]: return counts(nuked_md.run(rom, frames * FRAME_MCLK, dump_ram=True).ram) def board(rom: bytes, frames: int, rule: bool) -> tuple[int, int]: from megadrive_python import vdp from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System saved = vdp.VINT_WITHOUT_BORDER vdp.VINT_WITHOUT_BORDER = rule try: system = System(MACHINES["nuked-md"], Cartridge(rom)) system.board.fm.enabled = False while system.mclk < frames * FRAME_MCLK: system.step() return counts(bytes(system.board.ram)) finally: vdp.VINT_WITHOUT_BORDER = saved def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--frames", type=int, default=12) args = parser.parse_args() print("(VINTs taken, frames counted)") for trick in (False, True): rom = cartridge(trick) name = "V28/V30 trick" if trick else "control " print(f"{name}: referee {referee(rom, args.frames)}, board " f"{board(rom, args.frames, False)} with VINT_WITHOUT_BORDER False, " f"{board(rom, args.frames, True)} with True") # fmt: skip return 0 if __name__ == "__main__": raise SystemExit(main())