# SPDX-License-Identifier: MIT """CRAM dots: where a CRAM write during the display shows, and in what colour, at the pins. A CRAM write while the picture is drawn shows as a dot (Kabuto names them in passing, "similar to a CRAM dot"; nothing hardware-captured here says where one lands or what colour it shows). The board draws the written colour for the one pixel of the write's access slot (``render.Renderer.cram_dot``). This sets the board's picture against the referee's, Nuked-MD @ 594ca2a run as a closed box (its RGB output pins only, ``run_plane_sizes.referee_picture``), pixel for pixel. A cartridge built here fills plane A with a solid grey (CRAM entry 1), sets the CRAM address to entry 63, which nothing on screen uses, before every write, and on every frame waits for line ``first`` and then writes entry 63 a few hundred times, white and red by turns, as fast as the FIFO takes them: every dot on the screen is a write. In H40 and H32. python scripts/run_cram_dots.py """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path[:0] = [str(ROOT / "src"), str(ROOT / "scripts")] import run_plane_sizes as pictures # noqa: E402 from run_plane_sizes import _vram_command, _w # noqa: E402 WRITES = 300 def cartridge(h40: bool, first: int = 60) -> bytes: code = bytearray() code += _w(0x46FC, 0x2700) # move.w #$2700,sr code += bytes.fromhex("23fc5345474100a14000") # move.l #'SEGA',$A14000 code += _w(0x41F9, 0x00C0, 0x0000) # lea $C00000,a0 (data) 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, 0x04), (0x02, 0x30), (0x03, 0x00), (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) # move.w #$8rvv,(a1) code += _w(0x22BC, *_vram_command(0)) # VRAM write 0: clear it code += _w(0x303C, 0x7FFF, 0x30BC, 0x0000, 0x51C8, 0xFFFA) # move.w #0,(a0) x 32768 code += _w(0x22BC, *_vram_command(0x20)) # tile 1: solid colour 1 code += _w(0x303C, 0x000F, 0x30BC, 0x1111, 0x51C8, 0xFFFA) code += _w(0x22BC, *_vram_command(0xC000)) # plane A: tile 1 everywhere code += _w(0x303C, 0x07FF, 0x30BC, 0x0001, 0x51C8, 0xFFFA) code += _w(0x22BC, 0xC000, 0x0000) # CRAM 0 black, 1 grey code += _w(0x30BC, 0x0000, 0x30BC, 0x0444) code += _w(0x263C, 0xC07E, 0x0000) # move.l #$C07E0000,d3: CRAM write 63 code += _w(0x323C, 0x0EEE, 0x343C, 0x000E) # move.w #$EEE,d1 (white); move.w #$E,d2 (red) code += _w(0x32BC, 0x8144) # display on loop = len(code) code += _w(0x1012, 0x0C00, first, 0x66F8) # L: move.b (a2),d0; cmp.b #first,d0; bne.s L for _ in range(WRITES // 2): # the address set again before each write code += _w(0x2283, 0x3081, 0x2283, 0x3082) # move.l d3,(a1); move.w d1/d2,(a0) code += _w(0x1012, 0x0C00, first, 0x67F8) # wait for the line to pass code += _w(0x6000, (loop - (len(code) + 2)) & 0xFFFF) # bra.w L 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[0x1F0:0x1F4] = bytes.fromhex("4e722700") # stop #$2700 rom[0x100:0x110] = b"SEGA GENESIS " rom[0x200 : 0x200 + len(code)] = code return bytes(rom) def main() -> int: failed = 0 for h40 in (True, False): rom = cartridge(h40) width = 320 if h40 else 256 theirs = pictures.referee_picture(rom, width=width) ours = pictures.board_picture(rom, width=width) same = sum(1 for a, b in zip(theirs, ours, strict=False) for x, y in zip(a, b, strict=False) if x == y) # fmt: skip dots_theirs = sum(1 for row in theirs for p in row if p not in ((0, 0, 0), (4, 4, 4))) dots_ours = sum(1 for row in ours for p in row if p not in ((0, 0, 0), (4, 4, 4))) total = width * pictures.HEIGHT failed += same != total print(f"{'H40' if h40 else 'H32'}: {same} of {total} pixels identical; dots: referee " f"{dots_theirs}, board {dots_ours}") # fmt: skip return 1 if failed else 0 if __name__ == "__main__": raise SystemExit(main())