"""Rung 2: each mode's picture. Directed tests build pictures from TI's address tables (tier 2) and Young's descriptions of the undocumented modes (tier 3). Then register streams recorded from MAME 0.285 (detector, tier 5; docs/mame-oracle.md, validation/synthetic.py) are replayed with windows off and compared with MAME's final VRAM and last frame. They are skipped when the git-ignored traces are absent. """ from __future__ import annotations import sys from pathlib import Path import pytest from helpers import LINE, set_reg, write_block from tms9918_python import TMS9918, TMS9918A, TMS9928A from tms9918_python import tables as T ROOT = Path(__file__).resolve().parents[1] TRACES = ROOT / "tests" / "mame_traces" sys.path.insert(0, str(ROOT / "validation")) def chip(**regs) -> TMS9918: vdp = TMS9918(TMS9928A, windows=False) set_reg(vdp, 1, 0x80) # 16K before VRAM is written return vdp def picture(vdp: TMS9918, regs: dict[int, int]) -> list[bytes]: for reg, value in regs.items(): set_reg(vdp, reg, value) vdp.run(LINE * 262 * 2) return vdp.take_frame().pixels # --- directed: TI 2.4 and table 3-2 ------------------------------------------------- def test_graphics1_pixels(): # [TI 2.4.1]: pattern byte bits are pixels, MSB on the left; the colour byte's high # nibble colours 1s, low nibble 0s; one colour byte per 8 names. vdp = chip() write_block(vdp, 0x1800, bytes([9])) # name (row 0, column 0) = 9 write_block(vdp, 0x0000 + 9 * 8, bytes([0b10100000])) # pattern 9, row 0 write_block(vdp, 0x2000 + 1, bytes([0x6C])) # colours for names 8-15 rows = picture(vdp, {0: 0, 1: 0xC0, 2: 0x06, 3: 0x80, 4: 0x00, 7: 0x04}) assert list(rows[0][:4]) == [6, 12, 6, 12] def test_transparent_colours_show_the_backdrop(): # [TI 2.4]: colour 0 is transparent; the backdrop (R7 low nibble) shows through. vdp = chip() write_block(vdp, 0x0000, bytes([0xF0])) write_block(vdp, 0x2000, bytes([0x00])) rows = picture(vdp, {0: 0, 1: 0xC0, 2: 0x06, 3: 0x80, 4: 0x00, 7: 0x0B}) assert set(rows[0][:8]) == {0x0B} def test_backdrop_zero_gives_index_zero(): # [TI 2.4 note]: all planes transparent and no external video: black (index 0 here). vdp = chip() rows = picture(vdp, {1: 0xC0, 2: 0x06, 3: 0x80, 7: 0x00}) assert rows[100][100] == 0 def test_blank_shows_only_the_backdrop(): # [TI 2.1.5]: "With this bit low, the entire screen will show only border color". vdp = chip() write_block(vdp, 0x0000, bytes([0xFF]) * 0x800) write_block(vdp, 0x2000, bytes([0xF1]) * 32) rows = picture(vdp, {1: 0x80, 2: 0x06, 3: 0x80, 4: 0, 7: 0x05}) assert all(set(r) == {5} for r in rows) def test_border_is_the_backdrop_per_line(): vdp = chip() for reg, value in {1: 0xC0, 7: 0x07}.items(): set_reg(vdp, reg, value) vdp.run(LINE * 262 * 2) frame = vdp.take_frame() assert set(frame.border) == {7} and len(frame.border) == 262 def test_graphics2_thirds(): # [TI table 3-2]: Graphics II's pattern and colour addresses take the vertical # counter's two MSBs; R3 bit 7 and R4 bit 2 are address bit 13. vdp = chip() for third in range(3): write_block(vdp, 0x1800 + third * 256, bytes([7])) # first name of each third write_block(vdp, (third * 256 + 7) * 8, bytes([0x80])) # pattern, row 0 write_block(vdp, 0x2000 + (third * 256 + 7) * 8, bytes([(third + 2) << 4 | 1])) rows = picture(vdp, {0: 0x02, 1: 0xC0, 2: 0x06, 3: 0xFF, 4: 0x03, 7: 0}) assert [rows[y][0] for y in (0, 64, 128)] == [2, 3, 4] assert [rows[y][1] for y in (0, 64, 128)] == [1, 1, 1] def test_multicolor_high_nibble_left(): # [TI 2.4.3]: "The 4 MSBs of the first byte define the color of the upper left # quarter" (C4); byte (row & 3) x 2 + (line >> 2 & 1). vdp = chip() write_block(vdp, 0x0800, bytes([3])) # name write_block(vdp, 3 * 8, bytes([0x5A, 0xC4])) # rows 0-3, then 4-7 rows = picture(vdp, {1: 0xC8, 2: 0x02, 4: 0x00, 7: 0x01}) assert list(rows[0][:8]) == [5] * 4 + [0xA] * 4 assert list(rows[4][:8]) == [0xC] * 4 + [4] * 4 def test_text_mode_columns(): # [TI 2.4.4]: 40 columns of 6 pixels, R7's colours, the two low pattern bits unused; # [TI table 3-3]: 6 pixels further in than the 256-pixel modes. vdp = chip() write_block(vdp, 0x0800, bytes([1, 2])) write_block(vdp, 1 * 8, bytes([0xFF])) # all eight bits set write_block(vdp, 2 * 8, bytes([0x84])) # bit 7 and bit 2 (shown) set rows = picture(vdp, {1: 0xD0, 2: 0x02, 4: 0x00, 7: 0xF4}) assert list(rows[0][:6]) == [4] * 6 # left border assert list(rows[0][6:12]) == [15] * 6 # bits 1-0 never shown assert list(rows[0][12:18]) == [15, 4, 4, 4, 4, 15] assert list(rows[0][246:]) == [4] * 10 # right border def test_table_less_modes_show_bars(): # [Young 3.7]: "40 vertical lines of text colour ... 4 pixels TC, 2 pixels BD". vdp = chip() rows = picture(vdp, {0: 0x00, 1: 0xD8, 7: 0xA7}) assert list(rows[10][6:18]) == [10, 10, 10, 10, 7, 7] * 2 def test_text_graphics2_uses_thirds(): # [Young 3.5]: Text with R4's Graphics II meaning: thirds, bits 1-0 a mask. vdp = chip() write_block(vdp, 0x0800 + 8 * 40, bytes([5])) # row 8: the second third write_block(vdp, (256 + 5) * 8, bytes([0x80])) rows = picture(vdp, {0: 0x02, 1: 0xD0, 2: 0x02, 3: 0xFF, 4: 0x03, 7: 0xF1}) assert rows[64][6] == 15 # --- MAME register streams ------------------------------------------------------ GAME_LOGS = ["coleco-Zaxxon", "coleco-Zaxxon-15s", "coleco-Zaxxon-play", "sg1000-Flicky"] SYNTHETIC_EXACT = [ "g1", "g1-mag16", "g2", "g2-r4mask", "g2-c3", "mc", "text", "text-g2", "bogus", "bogus-all", "blank", ] # fmt: skip def _need(run: str) -> Path: path = TRACES / run if not (path / "error.log").exists(): pytest.skip(f"{run}: MAME trace not recorded (docs/mame-oracle.md)") return path @pytest.mark.parametrize("run", GAME_LOGS) def test_game_stream_matches_mame(run): import mame_replay r = mame_replay.replay(_need(run)) assert r.vram_differ == 0 assert r.frame_pixels_differ == 0 assert r.data_differ == [] # Flicky's one status difference: MAME's number bits (0 at its reset, 31 copied in by # the first status read) against this core's power-on 0 (U4). docs/results.md. allowed = {9458} if run == "sg1000-Flicky" else set() assert {d[0] for d in r.status_differ} == allowed assert r.end_line == 204 # the snapshot moment: MAME's vertical blank @pytest.mark.parametrize("run", SYNTHETIC_EXACT) def test_synthetic_stream_matches_mame(run): import mame_replay r = mame_replay.replay(_need("synth-" + run)) assert (r.vram_differ, r.frame_pixels_differ) == (0, 0) def test_synthetic_4k_differs_only_by_c11(): import mame_replay path = _need("synth-g1-4k") assert mame_replay.replay(path).frame_pixels_differ > 0 # C11 on: scrambled r = mame_replay.replay(path, roll_over_4k=False, mux_4k=False) assert (r.vram_differ, r.frame_pixels_differ) == (0, 0) def test_synthetic_multicolor_graphics2_differs_only_by_mames_formula(monkeypatch): """MAME adds the row to the character number before shifting; this core reads byte (line >> 2) & 7 of the character (Young 3.6). With MAME's formula patched in for this mode only, the pictures agree: every differing pixel is that formula.""" import mame_replay from tms9918_python import chip as chip_module path = _need("synth-mc-g2") assert mame_replay.replay(path).frame_pixels_differ > 0 real = chip_module.TMS9918._draw_group plain = T.graphics2_pattern_address def mame_formula(r3, r4, char, row_byte, gate): _, mask = T.graphics2_masks(r3, r4, gate) return (r4 & 4) << 11 | ((char + row_byte) & mask) << 3 def draw(self, line, column): if self._line_mode == T.MULTICOLOR_GRAPHICS_II: monkeypatch.setattr(T, "graphics2_pattern_address", mame_formula) try: return real(self, line, column) finally: monkeypatch.setattr(T, "graphics2_pattern_address", plain) monkeypatch.setattr(chip_module.TMS9918, "_draw_group", draw) r = mame_replay.replay(path) assert (r.vram_differ, r.frame_pixels_differ) == (0, 0) def test_variant_rows_draw_the_same_picture(): # The parts differ in output and line count, not in the picture [TI 2.4]. pics = [] for variant in (TMS9918A, TMS9928A): vdp = TMS9918(variant, windows=False) set_reg(vdp, 1, 0x80) write_block(vdp, 0, bytes(range(256)) * 64) for reg, value in {1: 0xC2, 2: 0x06, 3: 0xFF, 4: 0x03, 0: 0x02, 7: 0x15}.items(): set_reg(vdp, reg, value) vdp.run(LINE * 262 * 2) pics.append(vdp.take_frame().pixels) assert pics[0] == pics[1]