# SPDX-License-Identifier: MIT # The PAL window is the visible area of MAME 0.285 a7800.cpp's a7800p screen (BSD-3-Clause, # Dan Boris, Fabio Priuli, Mike Saarna, Robert Tuccitto; a fact, no code). NOTICE, section 2. """Frames from MARIA's lines, and a PNG writer. The board emits colour bytes only (docs/start-here.md "Output"): per line, 454 values at MARIA-clock resolution, blank in HBLANK. This stage cuts the picture MAME 0.285 shows for comparison: lines 27-250 (a detector setting, not a hardware value) and clocks 107-426, the 320 visible clocks (DOC: [GLD] sheet 16). The board's line numbers are MAME's (the power-on placement, machine.py, OBSERVED; MSTAT falls on line 16 on both, DOC: [SG]). Both show a line's DMA on the next line: MAME's ``draw_scanline`` swaps its two line buffers before it draws, as MARIA does at LRC. (Until milestone 2's frame compare was traced this cropped lines 28-251, on the belief that MAME drew the buffer it had just built: every compared frame was one line off.) ``rgb`` is a placeholder palette computed from the colour byte's hue and luminance (hue 0 grey; hues 1-15 around the colour wheel at 24 degrees; 16 luminance steps; not a hardware value). It is **not** one of the 7800 palettes (the wiki's cool/warm/hot charts are milestone 8's frontend data); frame comparisons against detectors use colour-byte-to-RGB consistency, never this palette. """ from __future__ import annotations import math import struct import zlib FIRST_LINE, LINES = 27, 224 # detector setting (MAME 0.285's screen), not a hardware value FIRST_CLOCK, WIDTH = 107, 320 # DOC: [GLD] sheet 16, the visible clocks # The PAL crop (detector setting): lines 35-294 (260 lines) of the 292 DMA lines 16-307 # (DOC: [MT84] 2.2: 292 active PAL lines; the board's PAL row). It is the comparison # window of MAME 0.285's a7800p screen (a7800.cpp's PAL visible area, a # detector setting like the NTSC one above; the wiki's Software Guide gives # 16-308 as the PAL "visible" screen and 41-281 as the area every television # shows, which this window contains). The board's PAL line numbering is # MAME's: 30 of 37 PAL dumps are pixel-exact in it at frame 400, and the PAL # BIOS alone differs by 26 pixels in this window against 1,816 one line off # (docs/build-progress.md, milestone 3, PAL). PAL_FIRST_LINE, PAL_LINES = 35, 260 def crop_for(machine) -> tuple[int, int]: """(first line, lines) of the comparison window for a machine row.""" return (PAL_FIRST_LINE, PAL_LINES) if machine.lines == 313 else (FIRST_LINE, LINES) class Television: """Collects MARIA's lines into frames (``frames`` keeps the last ``keep``).""" def __init__(self, maria, keep: int = 2, first_line: int = FIRST_LINE, lines: int = LINES) -> None: # fmt: skip self.first_line, self.lines = first_line, lines self.current: list[bytes] = [bytes(WIDTH)] * lines self.frames: list[list[bytes]] = [] self.keep = keep self.count = 0 self._started = False maria.on_line = self._line def _line(self, line: int, pixels: bytearray, colour_kill: bool) -> None: row = line - self.first_line lines = self.lines if 0 <= row < lines: self.current[row] = bytes(pixels[FIRST_CLOCK : FIRST_CLOCK + WIDTH]) if row == 0: self._started = True if row == lines - 1: if not self._started: # A frame whose first rows passed before power (the board # powers on at line 250): not a frame. MAME's first # frame_done comes one whole frame after its power-on, so # frame N here is MAME's frame N (detector setting). self.current = [bytes(WIDTH)] * lines return self._started = False self.frames.append(self.current) self.frames = self.frames[-self.keep :] self.current = [bytes(WIDTH)] * lines self.count += 1 def rgb(colour: int) -> tuple[int, int, int]: hue, luma = colour >> 4, colour & 0x0F y = luma / 15 if hue == 0: i = q = 0.0 else: angle = math.radians((hue - 1) * 24 + 180) i, q = 0.2 * math.cos(angle), 0.2 * math.sin(angle) r = y + 0.956 * i + 0.621 * q g = y - 0.272 * i - 0.647 * q b = y - 1.106 * i + 1.703 * q return tuple(max(0, min(255, round(v * 255))) for v in (r, g, b)) def frame_png(frame: list[bytes]) -> bytes: palette = [rgb(c) for c in range(256)] rows = b"".join(b"\0" + b"".join(bytes(palette[v]) for v in line) for line in frame) def chunk(kind: bytes, data: bytes) -> bytes: crc = struct.pack(">I", zlib.crc32(kind + data)) return struct.pack(">I", len(data)) + kind + data + crc header = struct.pack(">IIBBBBB", WIDTH, len(frame), 8, 2, 0, 0, 0) return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(rows)) + chunk(b"IEND", b"")) # fmt: skip