# SPDX-License-Identifier: MIT """The composite: the picture that leaves the console with a 32X attached (milestone 4, docs/handoff-brief.md; megadrive-python socket 7, ``sockets.Picture``). **What the hardware does** (service manual No. 012, ``A406a``/``A408a``): the Mega Drive's finished analog RGB reaches the 32X by the A/V cable, the Mega Drive VDP's ``-MYS`` (YS), HSYNC, VSYNC and EDCLK cross the cartridge slot, and an analog switch (IC12) puts out, instant by instant, either the Mega Drive's RGB or the 32X RAMDAC's, driven by ``YSO``, which the 32X VDP derives from YS, the 32X pixel's through bit, PRI and its mode. There is no capture of any of it (Aubrey's decision 6): every choice below is a :class:`~sega32x_python.machine.Quirk` resting on the Manual and on detectors, and docs/claims.md says so. **The rule** (``Quirks.composite_rule``): the 32X's pixel where the 32X row is not blank and either its through bit differs from PRI (Manual 3.3, "Priority", figure 3.9: with PRI = 0 the 32X is behind the Mega Drive and a through-bit pixel in front; with PRI = 1 the reverse) or the Mega Drive shows nothing there (YS; ``Quirks.composite_md_transparent``); the Mega Drive's pixel otherwise. A blank-mode row is no 32X pixel at all, so the Mega Drive's line is handed back untouched (``Quirks.blank_row``). **Where the 32X's pixels fall** (``Quirks.composite_alignment``, ``Quirks.composite_dot``): in master clocks. The Mega Drive's output pixel ``i`` starts at ``display_at + (output_delay + i) * pixel_mclk`` (socket 7); the 32X's pixel ``x`` at that line's output pixel 0 plus the mode's offset plus ``x`` 32X dots of 8 MCLK, H32 included. The line handed back is on the 32X's dot, from the Mega Drive's output pixel 0 (the left edge of every detector's frame) to the end of the 32X's last pixel: in H40, with offset 0, the two pictures one for one, 320 wide; in H32, 323 wide (3.25 of the 32X's dots on the left show only the Mega Drive, and the 32X's last pixels lie past the Mega Drive's 256, over its border). Each output pixel takes what lies under its centre from each side. Past the Mega Drive's active picture the Mega Drive shows its border, taken as YS (``Quirks.composite_border``). Only the pictures' relative place is used, never socket 7's clocks against the 32X VDP's: ``display_at + output_delay x pixel_mclk`` is where megadrive-python looks the colour up (its CRAM-write offset), not necessarily where the RGB pins show pixel 0. **When.** Socket 7 hands a line over when the Mega Drive VDP has finished it, which, the VDP being lazy, can be before or after the 32X VDP's event for the same line has run (the 32X runs in order with its SH-2s). The two meet by the line's start clock: whichever comes second composes, and a line handed over first is returned as a copy of the Mega Drive's own, filled in place with the composite when the 32X's row is drawn -- before the frame is read (``System.run_frame`` runs every clocked device to the 68000's clock after each instruction). """ from __future__ import annotations from typing import TYPE_CHECKING from .machine import Quirks from .vdp import _RGB, WIDTH if TYPE_CHECKING: from megadrive_python.sockets import PictureLine from .slot import Sega32X #: How many lines either side may run ahead of the other before an entry is forgotten. _KEEP = 8 class Composite: """Socket 7's device: the 32X's row laid over the Mega Drive's line.""" def __init__(self, device: Sega32X) -> None: self.device = device q = device.machine.quirks self.alignment = q.composite_alignment.value self.dot = q.composite_dot.value self.border_ys = q.composite_border.value == "YS" #: Rows the 32X drew whose Mega Drive line has not been handed over: line start -> #: (words or None, PRI). Lines handed over before their 32X row: start -> (line, #: the list returned for it). self.drawn: dict[int, tuple[list[int] | None, int]] = {} self.pending: dict[int, tuple[PictureLine, list[int]]] = {} self._maps: dict[tuple[int, int, int], list[int]] = {} #: Lines composed (a 32X row laid over a Mega Drive line), for tests and reports. self.composed = 0 # -- socket 7 --------------------------------------------------------------------- def picture_line(self, line: PictureLine) -> list[int] | None: if not self.device.adapter & 1: return None # ADEN = 0: the 32X shows nothing (Manual 3.5) entry = self.drawn.pop(line.start, None) if entry is not None: return self._compose(line, *entry) out = list(line.rgb) pending = self.pending pending[line.start] = (line, out) if len(pending) > _KEEP: del pending[next(iter(pending))] return out # -- from the 32X VDP --------------------------------------------------------------- def row_drawn(self, start: int, words: list[int] | None, pri: int) -> None: """The 32X VDP drew the row of the line that starts at ``start`` (its display began).""" waiting = self.pending.pop(start, None) if waiting is not None: line, out = waiting composed = self._compose(line, words, pri) if composed is not None: out[:] = composed return drawn = self.drawn drawn[start] = (words, pri) if len(drawn) > _KEEP: del drawn[next(iter(drawn))] # -- the switch ------------------------------------------------------------------------ def _map(self, line: PictureLine) -> list[tuple[int, int]]: """For each output pixel, the Mega Drive output pixel and the 32X pixel under its centre (-1 where that side has none).""" key = (line.pixel_mclk, line.width, self.alignment.get(line.hmode, 0)) cached = self._maps.get(key) if cached is None: pixel_mclk, width, offset = key dot = self.dot half = dot // 2 cached = [] for x in range((offset + WIDTH * dot - half - 1) // dot + 1): centre = x * dot + half index = centre // pixel_mclk pixel = (centre - offset) // dot cached.append((index if index < width else -1, pixel if pixel >= 0 else -1)) self._maps[key] = cached return cached def _compose(self, line: PictureLine, words: list[int] | None, pri: int) -> list[int] | None: if words is None: return None # blank mode: transparent, the Mega Drive's own line self.composed += 1 rgb, ys = line.rgb, line.ys rgb32 = _RGB pri <<= 15 out = [] append = out.append border_ys = self.border_ys for index, x in self._map(line): if x < 0: # left of the 32X's first pixel append(rgb[index]) continue word = words[x] if index < 0: if border_ys or (word ^ pri) & 0x8000: append(rgb32[word & 0x7FFF]) else: append(0) elif (word ^ pri) & 0x8000 or ys[index]: append(rgb32[word & 0x7FFF]) else: append(rgb[index]) return out def rectangle(rows: list[list[int]], quirks: Quirks | None = None) -> list[list[int]]: """A frame as a rectangle for a PNG: socket 7 hands back composite lines (320 wide, 323 in H32) where the 32X drew and the Mega Drive's own (256 in H32) where it did not, so a frame can mix them. A narrower row is laid on the 32X's dot as the composite lays the Mega Drive (``Quirks.composite_dot``), its border black. A frame of one width is returned as it is.""" widest = max((len(row) for row in rows), default=0) if all(len(row) == widest for row in rows): return rows dot = (quirks if quirks is not None else Quirks()).composite_dot.value out = [] for row in rows: pixel_mclk = 10 if len(row) == 256 else dot line = [] for x in range(widest): index = (x * dot + dot // 2) // pixel_mclk line.append(row[index] if index < len(row) else 0) out.append(line) return out __all__ = ["Composite", "rectangle"]