# SPDX-License-Identifier: MIT """The 32X VDP (milestone 3, ``vdp.py``): the three modes, the line table, the screen shift and Bulletin #14's $FF exception, FS and its swap at VBLANK, the auto fill and FEN, the line timing of HBLK/VBLK/PEN, the palette's wait, HINT's counter. Each test names the quirk (``sega32x_python.machine.Quirks``) or the source it pins. The 68000 is parked in a loop with RV = 1 (so its cartridge stays mapped) and the test reaches the 32X through the board's bus, as a 68000 program would.""" from __future__ import annotations from conftest import SYNTHETIC_VECTORS, image, run from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System from sega32x_python import Sega32X from sega32x_python.machine import Quirks from sega32x_python.sh2side import HINT FB = 0x840000 PALETTE = 0xA15200 def _system(machine: str = "genesis", reg12: int = 0x81) -> tuple[System, Sega32X]: # move.w #$8104,$C00004 (mode 5); move.w #$8C81,$C00004 (H40, as the 32X needs: # Manual 3.3; reg12 0 for H32); bra * code = f"33fc810400c00004 33fc8c{reg12:02x}00c00004 60fe" device = Sega32X(Cartridge(image(code)), vector_rom=SYNTHETIC_VECTORS) system = System(MACHINES[machine], None, slot=device) # type: ignore[arg-type] system.board.fm.enabled = False system.attach_video_timing(device) board = system.board board.write_word(0xA15106, 1) # RV = 1: the 68000's loop stays visible board.write_word(0xA15100, 1) # ADEN run(system, 10) system.run_frame() # to the first frame's start return system, device def _frames(system: System, count: int = 2) -> None: for _ in range(count): system.run_frame() def _line_table(system: System, rows: dict[int, int], default: int = 0x100) -> None: board = system.board for row in range(240): board.write_word(FB + 2 * row, rows.get(row, default)) def _swap(system: System, device: Sega32X) -> None: """Show the buffer just drawn: FS swaps at once in blank mode.""" system.board.write_word(0xA1518A, device.vdp.fs ^ 1) def test_packed_pixel_line_table_and_shift() -> None: """Manual 3.3: packed pixel, a palette index a byte, high byte first, from the row's line-table word address; SFT shifts one dot left (a byte), except on a row whose address's low byte is $FF (Bulletin #14; Quirks.screen_shift_ff_exception).""" system, device = _system() board = system.board _line_table(system, {1: 0x01FF}) for i in range(160): board.write_word(FB + 0x200 + 2 * i, (((2 * i) & 0xFF) << 8) | ((2 * i + 1) & 0xFF)) board.write_word(FB + 0x3FE + 2 * i, (((2 * i) & 0xFF) << 8) | ((2 * i + 1) & 0xFF)) for i in range(256): board.write_word(PALETTE + 2 * i, 0x8000 | i) _swap(system, device) board.write_word(0xA15180, 1) _frames(system) picture = device.vdp.picture assert picture is not None and len(picture) == 224 assert picture[0][:4] == [0x8000, 0x8001, 0x8002, 0x8003] assert picture[1][:4] == [0x8000, 0x8001, 0x8002, 0x8003] board.write_word(0xA15182, 1) # SFT _frames(system) picture = device.vdp.picture assert picture[0][:4] == [0x8001, 0x8002, 0x8003, 0x8004] assert picture[1][:4] == [0x8000, 0x8001, 0x8002, 0x8003] # $xxFF: no shift def test_direct_colour_and_shift() -> None: """Manual 3.3: direct colour, a word a pixel with its through bit; SFT does not move it (Quirks.screen_shift_modes: packed pixel only, OBSERVED on ares, provisional).""" system, device = _system() board = system.board _line_table(system, {}) for i in range(330): board.write_word(FB + 0x200 + 2 * i, 0x8000 | i) _swap(system, device) board.write_word(0xA15180, 2) _frames(system) assert device.vdp.picture[5][:3] == [0x8000, 0x8001, 0x8002] # type: ignore[index] assert device.vdp.picture[5][319] == 0x8000 | 319 # type: ignore[index] board.write_word(0xA15182, 1) _frames(system) assert device.vdp.picture[5][:2] == [0x8000, 0x8001] # type: ignore[index] def test_run_length_and_shift() -> None: """Manual 3.3: run length, (length - 1) in the high byte and a palette index in the low, 320 pixels then the rest ignored; SFT changes nothing (Quirks.screen_shift_modes).""" system, device = _system() board = system.board _line_table(system, {}) board.write_word(PALETTE + 2, 0x001F) board.write_word(PALETTE + 4, 0x03E0) board.write_word(FB + 0x200, 0x0201) # 3 x colour 1 board.write_word(FB + 0x202, 0xFF02) # 256 x colour 2 board.write_word(FB + 0x204, 0xFF01) # 256 x colour 1, cut at 320 _swap(system, device) board.write_word(0xA15180, 3) _frames(system) row = device.vdp.picture[0] # type: ignore[index] assert len(row) == 320 assert row[:4] == [0x001F, 0x001F, 0x001F, 0x03E0] assert row[258] == 0x03E0 and row[259] == 0x001F and row[319] == 0x001F board.write_word(0xA15182, 1) _frames(system) assert device.vdp.picture[0][:4] == [0x001F, 0x001F, 0x001F, 0x03E0] # type: ignore[index] def test_blank_mode_rows_are_transparent() -> None: """Quirks.blank_row (Manual 3.3, "Priority").""" system, device = _system() _frames(system) assert device.vdp.picture is not None and device.vdp.picture[0] is None assert device.vdp.frame_rgb()[0][0] == 0 def test_line_addresses_wrap_through_the_dram() -> None: """Manual 3.3: 'VDP mechanically displays 320 pixels worth of data from the address' -- past the end it runs on into the overwrite image's alias of the same DRAM, i.e. the word address wraps.""" system, device = _system() board = system.board _line_table(system, {0: 0xFFFF}) board.write_word(0x85FFFE, 0x8123) board.write_word(FB, 0x8456) # word 0: also the line table's row 0 entry board.write_word(FB, 0xFFFF) _swap(system, device) board.write_word(0xA15180, 2) _frames(system) assert device.vdp.picture[0][:2] == [0x8123, 0xFFFF] # type: ignore[index] def test_fs_swaps_at_vblank_during_display() -> None: """Quirks.fs_swap (Manual 3.2.3): in blank mode at once; during display the write waits for VBLANK and FS reads the displayed buffer until then.""" system, device = _system() board = system.board board.write_word(0xA1518A, 1) assert board.read_word(0xA1518A) & 1 == 1 # blank mode: at once board.write_word(0xA15180, 1) _frames(system, 1) vdp = device.vdp while vdp.status(system.mclk)[0]: # into the display run(system, 20) board.write_word(0xA1518A, 0) assert board.read_word(0xA1518A) & 1 == 1 and vdp.fs_pending == 0 while not vdp.status(system.mclk)[0]: run(system, 20) run(system, 20) assert board.read_word(0xA1518A) & 1 == 0 def test_auto_fill_wraps_and_holds_fen() -> None: """Quirks.auto_fill: length + 1 words in the page (A16-A9 fixed), at once; FEN 1 for 7 + 3 x words Sclk.""" system, device = _system() board = system.board board.write_word(0xA15184, 3) # four words board.write_word(0xA15186, 0x01FE) board.write_word(0xA15188, 0xABCD) buffer = device.framebuffers[device.vdp.fs ^ 1] assert [buffer[i] for i in (0x3FC, 0x3FD, 0x3FE, 0x3FF, 0x200, 0x203)] == [ 0xAB, 0xCD, 0xAB, 0xCD, 0xAB, 0xCD, ] # fmt: skip assert device.vdp.fill_start == 0x0102 assert device.vdp.fill_end - device.clock == (7 + 3 * 4) * 7 // 3 assert device.vdp.status(device.clock)[3] # FEN assert not device.vdp.status(device.vdp.fill_end)[3] or _in_refresh(device, device.vdp.fill_end) def _in_refresh(device: Sega32X, mclk: int) -> bool: timing = device.vdp.timing_at(mclk) return timing is not None and timing.rise <= mclk < timing.refresh_end def test_line_timing_from_the_manual() -> None: """Quirks.hblank_phase and hblank_timing: HBLANK from 12 dots before the Mega Drive's HBlank flag makes every span of Manual 3.3 figures 3.15-3.17 come out in the Manual's own MCLK on the H40 line: PEN 3 dots = 24, VBLANK 27 = 224, latch 76 = 668, blank 100 = 860; the refresh FEN 40 Sclk.""" system, device = _system() _frames(system, 1) run(system, 300) vdp = device.vdp timing = vdp.timings[-1] assert timing.fall - timing.rise == 860 assert timing.pen_rise - timing.rise == 24 assert timing.refresh_end - timing.rise == 40 * 7 // 3 assert timing.latch - timing.rise == 668 assert timing.vedge - timing.rise == 224 status = vdp.status assert status(timing.rise)[1] and not status(timing.rise - 1)[1] assert not status(timing.fall)[1] and status(timing.fall - 1)[1] assert status(timing.rise)[3] and not status(timing.refresh_end)[3] def test_h32_counts_edclk_dots_from_hsync() -> None: """Quirks.edclk_h32: in H32 the 32X still counts EDCLK's H40 dots (EDCLK is made outside the VDP: spritesmind t=519, t=851), so the Manual's spans come out in its own MCLK figures as in H40, HBLANK 23 dots (184 MCLK) before HSYNC's fall, taken as 368 MCLK into the H32 line (provisional) -- not the Mega Drive's 10-MCLK H32 dots.""" system, device = _system(reg12=0x00) _frames(system, 1) run(system, 300) timing = device.vdp.timings[-1] assert device.vdp.timings[-1].end - timing.start == 3420 assert timing.rise - timing.start == 368 - 184 == 184 assert timing.pen_rise - timing.rise == 24 assert timing.vedge - timing.rise == 224 assert timing.latch - timing.rise == 668 assert timing.fall - timing.rise == 860 assert Quirks().edclk_h32.value == 368 def test_vblank_rows_and_palette_wait() -> None: """224 rows (Manual 3.3 figure 3.16: 38 blank lines on NTSC); PEN 0 in display in packed pixel mode, and a palette access then waits for it (Quirks.palette_wait).""" system, device = _system() board = system.board board.write_word(0xA15180, 1) _frames(system, 1) vdp = device.vdp while True: run(system, 5) timing = vdp.timing_at(system.mclk) if timing is not None and 10 < timing.v < 200 and timing.end > system.mclk: break middle = timing.fall + 400 vblank, hblank, pen, _fen = vdp.status(middle) assert not vblank and not hblank and not pen assert vdp.palette_wait(middle) == timing.end - middle + (timing.pen_rise - timing.start) assert vdp.palette_wait(timing.pen_rise) == 0 assert vdp.status(timing.pen_rise)[2] def test_bitmap_register_reads_back_and_latches() -> None: """Quirks.bitmap_bits (the Manual's scan: PRI 7, 240 6); the display takes it at the latch (Manual 3.3), the register reads back what was written.""" system, device = _system() board = system.board board.write_word(0xA15180, 0xFFFF) assert board.read_word(0xA15180) == 0x80C3 # NTSC, PRI, 240, mode 3 assert device.vdp.latched_mode == 0 _frames(system, 1) assert device.vdp.latched_mode == 3 and device.vdp.latched_pri == 1 def test_hint_counts_lines() -> None: """Quirks.hint_counter: H count 0 without HEN fires once per display row (224 a frame; the Mars Check Program's tests 53-54 expect 6,496-6,943 in 30 frames); with HEN, every line (262; tests 55-56); count 5 with HEN, every sixth (tests 57-58).""" from test_sh2side import _start from test_sh2side import _system as sh2_system system, device = sh2_system() _start(system, device) sh2 = device.sh2 assert sh2 is not None fired = [0] def count() -> None: fired[0] += 1 sh2.raise_hint = count # type: ignore[method-assign] _frames(system, 2) for hen, hcount, expected in ((False, 0, 224), (True, 0, 262), (True, 5, (43, 44))): device.hen, device.hcount = hen, hcount _frames(system, 1) fired[0] = 0 _frames(system, 1) if isinstance(expected, tuple): assert expected[0] <= fired[0] <= expected[1] else: assert fired[0] == expected assert HINT == 4 def test_quirks_of_milestone_3_keep_three_records() -> None: """docs/validation.md, "Three records, kept apart": every VDP quirk of milestone 3 fills documents, hardware and unresolved.""" quirks = Quirks() for name, quirk in quirks.table(): if quirk.milestone == 3: assert quirk.documents and quirk.hardware and quirk.unresolved, name