#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Where a mid-line change to the colour output shows, measured on the die model. Kabuto places H40's second mid-tiles access slot "at the pixel with X=-3 (display area coordinates)" for palette writes ("Access timing"): the colour output runs behind the H counter. That is hardware-measured research for one slot; Nuked-MD, the die-derived referee, gives every slot, both modes, and register writes too. Two cartridges built here, both with the planes transparent so the display is the backdrop. On each new line the 68000 waits (V & 63) ``dbra`` loops, sweeping across the line over 64 lines, then changes the backdrop's colour one of two ways, fixed by V: - ``cram``: writes CRAM entry 0 (the backdrop) -- $000E on even V, $008E on odd; - ``reg7``: writes register 7, the backdrop's CRAM index -- 1 (red) or 2 (green); and a third, ``vram``, where plane A shows one tile everywhere and each line rewrites the row of it the line shows: that change acts where tiles are fetched, not at the output. For each line it finds the output pixel where the colour changes, on the referee (Nuked-MD @ 594ca2a, a closed box: its RGB pins, ``NMD_PLOT``; display pixel 0 is sample 154 in H40 and 130 in H32, two samples a pixel) and on the board, and prints referee minus board per line. The referee's rows are numbered by its /HSYNC pin from its /VSYNC fall; which row is the board's line 0 is FITTED (the offset at which the most lines agree on one difference). It once used the model's internal V counter for that, and mapped the internal H counter at each CRAM commit to its pixel; those taps were removed from the harness in provenance wave 2 (2026-11-19). Whole-slot differences (multiples of 16) are the 68000's polling phase, which differs by a slot between the two runs on some lines. python scripts/run_output_delay.py """ from __future__ import annotations import collections import os import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) sys.path.insert(0, str(ROOT / "scripts")) import nuked_md # noqa: E402 from run_vscroll_latch import _w, nuked_md_cartridge # noqa: E402 FRAME = 262 * 3420 FIRST_SAMPLE = {True: 154, False: 130} def cartridge(h40: bool = True, kind: str = "cram") -> bytes: code = bytearray() code += _w(0x46FC, 0x2700) code += bytes.fromhex("23fc5345474100a14000") code += _w(0x41F9, 0x00C0, 0x0000, 0x43F9, 0x00C0, 0x0004, 0x45F9, 0x00C0, 0x0008) for reg, value in ((0x00, 0x04), (0x01, 0x04), (0x02, 0x30), (0x04, 0x07), (0x05, 0x78), (0x07, 0x00), (0x0A, 0xFF), (0x0B, 0x00), (0x0C, 0x81 if h40 else 0x00), (0x0D, 0x3F), (0x0F, 0x02), (0x10, 0x01), (0x11, 0x00), (0x12, 0x00)): # fmt: skip code += _w(0x32BC, 0x8000 | reg << 8 | value) code += _w(0x22BC, 0x4000, 0x0000, 0x303C, 0x7FFF) # clear VRAM code += _w(0x30BC, 0x0000, 0x51C8, 0xFFFA) code += _w(0x22BC, 0xC000, 0x0000, 0x30BC, 0x000E) # CRAM 0 red code += _w(0x30BC, 0x000E, 0x30BC, 0x00E0) # CRAM 1 red, 2 green (the reg 7 test) code += _w(0x32BC, 0x8144) # display on code += _w(0x7800, 0x7200) loop = bytearray() loop += _w(0x1012, 0xB001, 0x67FA) # L0: move.b (a2),d0; cmp.b d1,d0; beq.s L0 loop += _w(0x1200, 0x3400, 0x0242, 0x003F) # move.b d0,d1; move.w d0,d2; andi #63 loop += _w(0x51CA, 0xFFFE) # D: dbra d2,D if kind == "cram": loop += _w(0x22BC, 0xC000, 0x0000) # CRAM write 0 loop += _w(0x1801, 0x0244, 0x0001, 0xEF4C) # d4 = (V & 1) << 7 loop += _w(0x0044, 0x000E) # ori.w #$E,d4 loop += _w(0x3084) # move.w d4,(a0) assert len(loop) == 38 loop += _w(0x60D8) # bra.s L0 else: # register 7, the backdrop's CRAM index: 1 (red) on even V, 2 (green) on odd loop += _w(0x1801, 0x0244, 0x0001) # move.b d1,d4; andi.w #1,d4 loop += _w(0x5244) # addq.w #1,d4 loop += _w(0x0044, 0x8700) # ori.w #$8700,d4 loop += _w(0x3284) # move.w d4,(a1) assert len(loop) == 32 loop += _w(0x60DE) # bra.s L0 return nuked_md_cartridge(bytes(code + loop)) def referee(rom: bytes, first: int) -> dict[int, float]: """The referee's picture: plot row (in /HSYNC order from /VSYNC) -> the output pixel where the row's colour changes.""" nuked_md.ensure_built() with tempfile.TemporaryDirectory() as tmp: work = Path(tmp) (work / "rom.bin").write_bytes(rom) env = dict(os.environ, NMD_STOP=str(13 * FRAME * 2), NMD_PLOT=str(work / "p.bin"), NMD_PLOT_FRAME="11") # fmt: skip subprocess.run(["nice", str(nuked_md.BINARY), str(work / "rom.bin")], env=env, cwd=work, check=True, capture_output=True) # fmt: skip plot = (work / "p.bin").read_bytes() change = {} for i, raw in enumerate(nuked_md.plot_rows(plot)): if len(raw) < 3 * 600: continue row = [raw[3 * x : 3 * x + 3] for x in range(len(raw) // 3)] for x in range(first + 1, first + 640): if row[x] != row[x - 1]: change[i] = (x - first) / 2 break return change def by_line(theirs: dict[int, float], ours: dict[int, float]) -> dict[int, float]: """The referee's changes by the board's line number (:func:`nuked_md.fit_row_offset`).""" k = nuked_md.fit_row_offset(theirs, ours, range(0, 160)) return {y - k: x for y, x in theirs.items() if 0 <= y - k < 224} def board(rom: bytes): from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System system = System(MACHINES["genesis"], Cartridge(rom)) system.board.fm.enabled = False vdp = system.vdp commits = collections.defaultdict(list) for _ in range(12): system.run_frame() cram = vdp.cram class Rec(list): def __setitem__(self, i, v): if i == 0 and vdp.frame == 12: h = vdp.hmode.hvals[vdp.hmode.position(vdp.mclk - vdp.line_start)] commits[vdp.v].append(h) super().__setitem__(i, v) vdp.cram = Rec(cram) system.run_frame() rows = system.vdp.renderer.frame_rgb() change = {} for y, row in enumerate(rows[:224]): for x in range(1, len(row)): if row[x] != row[x - 1]: change[y] = x break return change, commits def vram_cartridge(h40: bool = True) -> bytes: """Plane A shows tile 1 everywhere; each line rewrites the row of tile 1 the line shows, (V & 7), at the sweeping delay, with colour 1 or 2 by (V >> 3) & 1 -- so the new row always differs from the one written eight lines before.""" code = bytearray() code += _w(0x46FC, 0x2700) code += bytes.fromhex("23fc5345474100a14000") code += _w(0x41F9, 0x00C0, 0x0000, 0x43F9, 0x00C0, 0x0004, 0x45F9, 0x00C0, 0x0008) for reg, value in ((0x00, 0x04), (0x01, 0x04), (0x02, 0x30), (0x04, 0x07), (0x05, 0x78), (0x07, 0x00), (0x0A, 0xFF), (0x0B, 0x00), (0x0C, 0x81 if h40 else 0x00), (0x0D, 0x3F), (0x0F, 0x02), (0x10, 0x01), (0x11, 0x00), (0x12, 0x00)): # fmt: skip code += _w(0x32BC, 0x8000 | reg << 8 | value) code += _w(0x22BC, 0xC000, 0x0000) code += _w(0x30BC, 0x0000, 0x30BC, 0x000E, 0x30BC, 0x00E0) # black, red, green code += _w(0x22BC, 0x4000, 0x0000, 0x303C, 0x7FFF) # clear VRAM code += _w(0x30BC, 0x0000, 0x51C8, 0xFFFA) code += _w(0x22BC, 0x4000, 0x0003) # plane A: every cell names tile 1 code += _w(0x303C, 0x07FF, 0x30BC, 0x0001, 0x51C8, 0xFFFA) code += _w(0x32BC, 0x8144) code += _w(0x7800, 0x7200) loop = bytearray() loop += _w(0x1012, 0xB001, 0x67FA, 0x1200, 0x3400, 0x0242, 0x003F) loop += _w(0x1A01, 0x0285, 0x0000, 0x0007, 0xE58D) # d5 = (V & 7) << 2 loop += _w(0x0685, 0x0000, 0x0020, 0x4845) # + $20 (tile 1), swap loop += _w(0x0085, 0x4000, 0x0000) # VRAM write command loop += _w(0x1801, 0xE60C, 0x0244, 0x0001, 0x5244) # d4 = ((V >> 3) & 1) + 1 loop += _w(0xC8FC, 0x1111) # mulu #$1111,d4: $1111 or $2222 loop += _w(0x51CA, 0xFFFE) # D: dbra d2,D -- the sweep loop += _w(0x2285) # move.l d5,(a1) loop += _w(0x3084, 0x3084) # the row's two words n = len(loop) loop += _w(0x6000, (-(n + 2)) & 0xFFFF) # bra.w L0 return nuked_md_cartridge(bytes(code + loop)) def main() -> int: for h40 in (True, False): width = 320 if h40 else 256 for kind in ("cram", "reg7"): rom = cartridge(h40, kind) board_change, _ = board(rom) ref_change = by_line(referee(rom, FIRST_SAMPLE[h40]), board_change) lines = [y for y in range(224) if y in ref_change and y in board_change and ref_change[y] < width and board_change[y] < width] # fmt: skip diffs = collections.Counter(round(ref_change[y] - board_change[y]) for y in lines) name = f"{'H40' if h40 else 'H32'} {kind}" print(f"{name}: referee minus board, per line: {sorted(diffs.items())}") rom = vram_cartridge(h40) board_change, _ = board(rom) ref_change = by_line(referee(rom, FIRST_SAMPLE[h40]), board_change) lines = [y for y in range(224) if y in ref_change and y in board_change and ref_change[y] < width and board_change[y] < width] # fmt: skip diffs = collections.Counter(round(ref_change[y] - board_change[y]) for y in lines) print(f"{'H40' if h40 else 'H32'} vram (a tile row): referee minus board, per line: " f"{sorted(diffs.items())}") # fmt: skip return 0 if __name__ == "__main__": sys.exit(main())