# SPDX-License-Identifier: MIT """The slot device's decode and quirks (milestone 1). Each test names the quirk it pins (``sega32x_python.machine.Quirks``), whose field carries the source and tier.""" from __future__ import annotations import pytest from conftest import SYNTHETIC_VECTORS, image, run, system_with_32x from megadrive_python.board import Lockup from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.sockets import M68K, Z80, Reset from megadrive_python.system import System from sega32x_python import MACHINES as MACHINES_32X from sega32x_python import ( BootROMError, BootROMs, Quirk, ResetLanding, Sega32X, ) from sega32x_python.bootrom import check from sega32x_python.machine import ( ASSUMPTION, EMULATOR, HARDWARE_CORRECTED, RESEARCH, SPECIFICATION, ) def _pages(system: System) -> list[int]: pages = system.board._pages assert pages is not None return [index << 8 for index, device in enumerate(pages) if device is not None] def _aden(device: Sega32X, system: System, rv: bool) -> None: board = system.board board.write_word(0xA15106, int(rv)) board.write_word(0xA15100, 0x0003) assert device.aden and device.rv == rv # -- ADEN = 0: the Mega Drive's own map --------------------------------------------------- def test_aden_0_claims_only_time_and_the_registers() -> None: """Manual 3.5: with ADEN = 0 the 68000's map is the Mega Drive's. Nothing at $000000-$3FFFFF is claimed, so the board keeps the cartridge itself on the bus (its ROM hot path unchanged).""" system, device = system_with_32x() board = system.board assert board.cartridge is device.cartridge is board.game_cartridge assert _pages(system) == [0xA13000, 0xA15100, 0xA15200, 0xA15300] # Quirks.area_without_aden: unclaimed, so megadrive-python's own lock-up (inherited, # unverified with a 32X attached); the two mini-sweep cartridges' addresses. for address in (0x800000, 0x840000, 0x9FFFFE): with pytest.raises(Lockup): board.read_word(address) def test_mars_id_reads_with_aden_0() -> None: """Quirks.mars_id: "MARS" at $A130EC-$A130EF, word, byte and long, from either CPU.""" system, device = system_with_32x() board = system.board assert (board.read_word(0xA130EC), board.read_word(0xA130EE)) == (0x4D41, 0x5253) assert bytes(board.read_byte(0xA130EC + n) for n in range(4)) == b"MARS" assert bytes(device.read_byte(0xA130EC + n, 0, Z80) for n in range(4)) == b"MARS" assert device.peek_byte(0xA130EF) == ord("S") def _time_reads(system: System) -> list[tuple[int, int, int]]: board = system.board return [(board.read_word(a), board.read_byte(a), board.read_byte(a | 1)) for a in range(0xA13000, 0xA13100, 2)] # fmt: skip def test_the_rest_of_time_reads_as_without_a_32x() -> None: """The $A130xx page is claimed for "MARS" only; every other address of /TIME answers as megadrive-python answers it with no device: the cartridge's ``read_time`` on both halves, or the open bus's MSB.""" rom = image("60fe") plain = System(MACHINES["genesis"], Cartridge(rom)) system, _device = system_with_32x(rom=rom) run(plain, 3) run(system, 3) with_32x, without = _time_reads(system), _time_reads(plain) for index, (a, b) in enumerate(zip(with_32x, without, strict=True)): if 0xEC <= 2 * index < 0xF0: continue assert a == b, hex(0xA13000 + 2 * index) for address in (0xA13000, 0xA130F1, 0xA130FF): # the Z80's bank window: $FF either way assert system.board._z80_bank_read(address) == plain.board._z80_bank_read(address) def test_time_writes_reach_the_held_cartridge() -> None: """$A130F1 (the SRAM register) and the mapper's $A130F3-$A130FF are the cartridge's: a write through the 32X sets the held cartridge's bank as it would without it.""" rom = image("60fe", size=0x500000) cartridge = Cartridge(rom, slot="rom_ssf2") device = Sega32X(cartridge, vector_rom=SYNTHETIC_VECTORS) system = System(MACHINES["genesis"], None, slot=device) # type: ignore[arg-type] system.board.write_byte(0xA130F3, 9) assert cartridge.banks[1] == 9 system.board.write_word(0xA130F4, 5) assert cartridge.banks[2] == 5 device.write_byte(0xA130F5, 7, 0, Z80) assert cartridge.banks[2] == 7 class _TimeBoard(Cartridge): """A board with its own /TIME registers (megadrive-python's ``mappers.Board`` interface): every call logged.""" def __init__(self, rom: bytes) -> None: super().__init__(rom) self.calls: list[tuple] = [] def read_time_word(self, address: int) -> int | None: self.calls.append(("word", address)) return 0x1200 | (address & 0xFF) def read_time(self, address: int) -> int | None: self.calls.append(("byte", address)) return 0x34 def write_time_lanes(self, address: int, high: int, low: int, word: bool) -> None: self.calls.append(("lanes", address, high, low, word)) def write_time(self, address: int, value: int) -> None: self.calls.append(("write", address, value)) @pytest.mark.parametrize("aden", [False, True]) def test_time_reaches_a_board_as_without_a_32x(aden: bool) -> None: """A board that decodes /TIME itself (read_time_word, write_time_lanes: the pirate and protection boards, Pier Solar, Sonic & Knuckles) is reached through the 32X as the board reaches it with no device -- the same calls, words and lanes -- with ADEN = 0 and with ADEN = 1. Before 2026-11-25 the 32X passed the byte interface (the low byte of a write; a read's low byte on both halves), so a passive 32X broke such boards (found by sega32x-rust's ADEN = 0 rung: A Bug's Life, KOF99, Pier Solar ...).""" rom = image("60fe") plain_board = _TimeBoard(rom) plain = System(MACHINES["genesis"], plain_board) held = _TimeBoard(rom) device = Sega32X(held, vector_rom=SYNTHETIC_VECTORS) system = System(MACHINES["genesis"], None, slot=device) # type: ignore[arg-type] if aden: _aden(device, system, rv=False) for target in (plain, system): board = target.board assert board.read_word(0xA13010) == 0x1210 board.write_word(0xA130F2, 0xABCD) board.write_byte(0xA130F5, 0x77) board.write_byte(0xA130F8, 0x66) assert ( held.calls == plain_board.calls == [ ("word", 0xA13010), ("lanes", 0xA130F2, 0xAB, 0xCD, True), ("lanes", 0xA130F5, 0x77, 0x77, False), ("lanes", 0xA130F8, 0x66, 0x66, False), ] ) def test_registers_are_live_with_aden_0() -> None: """t_32x_init reads and writes $A15100-$A1512F before ADEN; the communication ports are plain registers (Quirks.comm_both_write is milestone 2's).""" system, device = system_with_32x() board = system.board board.write_word(0xA15120, 0x1234) board.write_byte(0xA1512F, 0x56) assert device.comm[0] == 0x1234 and device.comm[7] == 0x0056 assert board.read_byte(0xA15120) == 0x12 def test_z80_reaches_the_registers_through_its_bank_window() -> None: """Manual 3.4's access table ('V'): the Z80 can reach the adapter control register and the communication ports; the board hands it to the same decode.""" system, device = system_with_32x() board = system.board board.arbiter_enabled = False board.bank = 0xA15100 & 0xFF8000 board.z80_write(0x8000 | (0xA15121 & 0x7FFF), 0x42) assert device.comm[0] == 0x0042 assert board.z80_read(0x8000 | (0xA15101 & 0x7FFF)) == 0x82 def test_unused_register_space_reads_open_bus() -> None: """Quirks.unused_registers: $A15140-$A1517F as an undecoded $A1 address.""" system, _device = system_with_32x("4e71" * 4 + "60fe") run(system, 2) board = system.board assert board.read_word(0xA15140) == board.open_bus_msb() assert board.read_byte(0xA15141) == 0 board.write_word(0xA15140, 0xFFFF) # ignored def test_vdp_without_aden_locks_up() -> None: """Quirks.vdp_without_aden (testpico t_32x_init: "VDP; hangs without ADEN").""" system, _device = system_with_32x() with pytest.raises(Lockup): system.board.read_byte(0xA15181) with pytest.raises(Lockup): system.board.write_word(0xA15200, 0) def test_pwm_registers() -> None: """Quirks.pwm_registers: control (the 68000 writes the routing bits only), cycle, and three-deep pulse-width FIFOs reporting FULL/EMPTY (Manual 3.2.1); with ADEN = 0 the counter does not run (Quirks.pwm_needs_aden), so nothing drains.""" system, device = system_with_32x() board = system.board board.write_word(0xA15130, 0xFFFF) assert board.read_word(0xA15130) == 0x000F board.write_word(0xA15132, 0xFFFF) assert board.read_word(0xA15132) == 0x0FFF assert board.read_word(0xA15134) == 0x4000 # EMPTY for n in range(3): board.write_word(0xA15134, n) assert board.read_word(0xA15134) == 0x8000 # FULL board.write_word(0xA15134, 9) # the oldest is dropped assert device.pwm.fifos[0] == [1, 2, 9] board.write_word(0xA15138, 7) # mono: both assert device.pwm.fifos[1] == [7] # -- ADEN = 1 ---------------------------------------------------------------------------- def test_aden_claims_follow_rv() -> None: """ADEN = 1 claims the vector page and $840000-$9FFFFF; RV = 0 also the rest of $000000-$3FFFFF (Manual 3.1). ``Board.update_claims`` re-reads them on each change.""" system, device = system_with_32x() board = system.board board.write_word(0xA15100, 3) # RV = 0 pages = _pages(system) assert pages[0] == 0 and 0x3FFF00 in pages and 0x840000 in pages and 0x9FFF00 in pages assert 0x400000 not in pages and 0x83FF00 not in pages board.write_byte(0xA15107, 1) # RV = 1: the cartridge back at $000100 pages = _pages(system) assert pages[0] == 0 and 0x100 not in pages and 0x840000 in pages board.write_word(0xA15100, 2) # ADEN = 0 again (testpico x32x_disable) assert _pages(system) == [0xA13000, 0xA15100, 0xA15200, 0xA15300] assert board.cartridge is device.cartridge def test_vector_rom_and_hint_vector() -> None: """Quirks.vector_rom_with_rv and hint_vector_power_on: the vector page is the boot ROM's but for $70-$73, a register; byte writes reach it lane by lane.""" system, device = system_with_32x() board = system.board _aden(device, system, rv=True) assert board.read_word(0x000004) == 0x0088 and board.read_word(0x000006) == 0x0200 assert board.read_word(0x000070) == 0 and board.read_word(0x000072) == 0 board.write_byte(0x000071, 0x12) board.write_byte(0x000073, 0x34) assert device.hint_vector == 0x00120034 board.write_word(0x000070, 0xA5FF) assert (board.read_word(0x70), board.read_byte(0x73)) == (0xA5FF, 0x34) board.write_word(0x000004, 0) # ROM assert board.read_word(0x000006) == 0x0200 def test_aden_without_a_boot_rom_is_refused() -> None: system, _device = system_with_32x(vector_rom=None) with pytest.raises(BootROMError): system.board.write_word(0xA15100, 1) def test_low_map_without_rv_reads_open_bus() -> None: """Quirks.low_map_without_rv (an assumption: ares agrees).""" system, device = system_with_32x() board = system.board _aden(device, system, rv=False) assert board.read_word(0x000200) == board.open_bus_msb() assert board.read_byte(0x000201) == 0 board.write_word(0x000200, 0x1234) # ignored board.write_byte(0xA15107, 1) assert board.read_word(0x000200) == 0x60FE # the cartridge again def test_windows_and_the_bank_register() -> None: """$880000 = cartridge $000000-$07FFFF, $900000 = the 1 MB bank BK1:BK0 (Manual 3.1, 3.5), both with RV = 0 only.""" rom = bytearray(image("60fe", size=0x400000)) for bank in range(4): rom[bank << 20 : (bank << 20) + 2] = (0xB000 | bank).to_bytes(2, "big") rom[0x7FFFE:0x80000] = b"\x12\x34" system, device = system_with_32x(rom=bytes(rom)) board = system.board _aden(device, system, rv=False) assert board.read_word(0x880004) == 0x0000 and board.read_word(0x880006) == 0x0200 assert board.read_word(0x8FFFFE) == 0x1234 for bank in range(4): board.write_word(0xA15104, bank) assert board.read_word(0x900000) == 0xB000 | bank assert board.read_byte(0x900001) == bank assert board.read_word(0xA15104) == 3 def test_windows_with_rv_lock_up() -> None: """Quirks.windows_with_rv (testpico t_32x_md_rom).""" system, device = system_with_32x() _aden(device, system, rv=True) for address in (0x880000, 0x900000): with pytest.raises(Lockup): system.board.read_word(address) def test_vdp_side_with_fm_follows_the_manual() -> None: """Quirks.vdp_side_with_fm: with FM = 1 the frame buffer, overwrite image, VDP registers and palette read the board's open bus (undefined in Manual 3.1; the value is chosen) and drop writes; nothing hangs. testpico's "FM=0 (hangs without)" is recorded as an unexplained observation, not modelled.""" system, device = system_with_32x("4e71" * 4 + "60fe") run(system, 2) board = system.board _aden(device, system, rv=True) board.write_word(0x840000, 0x1234) board.write_byte(0xA15100, 0x80) # FM = 1 assert device.fm and board.read_word(0xA15100) == 0x8083 msb = board.open_bus_msb() for address in (0x840000, 0x860000, 0xA15180, 0xA1518A, 0xA15200, 0xA153FE): assert board.read_word(address) == msb, hex(address) assert board.read_byte(address) == msb >> 8 and board.read_byte(address | 1) == 0 board.write_word(address, 0x5A5A) board.write_byte(address | 1, 0x5A) board.write_byte(0xA15100, 0x00) # FM = 0: the write above never reached the DRAM assert board.read_word(0x840000) == 0x1234 def test_vdp_registers_and_palette_are_storage() -> None: """vdpregs.py: testpico t_32x_reset_prep / t_32x_reset_btn's writable bits (shift 1, fill length $FF, fill start $FFFF, $8C/$8E read 0), the palette as words, and FS written outside display taking effect at once.""" system, device = system_with_32x() board = system.board _aden(device, system, rv=True) board.write_byte(0xA15181, 1) for offset in (0x82, 0x84, 0x86, 0x8C, 0x8E): board.write_word(0xA15100 + offset, 0xFFFF) reads = [board.read_word(0xA15100 + o) for o in (0x82, 0x84, 0x86, 0x8C, 0x8E)] assert reads == [1, 0xFF, 0xFFFF, 0, 0] assert board.read_byte(0xA15181) == 1 board.write_word(0xA15200, 0x7FFF) board.write_word(0xA153FE, 0x8001) assert device.vdp.palette[0] == 0x7FFF and device.vdp.palette[255] == 0x8001 board.write_word(0xA1518A, 1) # no line published yet: counted as blanking assert board.read_word(0xA1518A) & 1 == 1 and device.vdp.fs == 1 def test_auto_fill_wraps_in_its_page() -> None: """Quirks.auto_fill; Manual 3.2.3: A16-A9 fixed, A8-A1 counting.""" system, device = system_with_32x() board = system.board _aden(device, system, rv=True) 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[0x3FC:0x400] == bytes.fromhex("abcdabcd") assert buffer[0x200:0x204] == bytes.fromhex("abcdabcd") assert device.vdp.fill_start == 0x0102 def test_dma_reads_what_the_68000_reads() -> None: system, device = system_with_32x() _aden(device, system, rv=False) system.board.write_word(0x840010, 0xBEEF) assert device.dma_read_word(0x840010, 0) == 0xBEEF assert device.dma_read_word(0x880006, 0) == 0x0200 def test_peek_has_no_side_effects_and_never_raises() -> None: _system, device = system_with_32x() clock = device.clock assert device.peek_byte(0xA15101) == 0x82 assert device.peek_byte(0xA15181) == 0xFF # the VDP with ADEN = 0: would lock up assert device.peek_byte(0xA13001) == 0xFF # as Board.z80_peek reads $A1xxxx assert device.clock == clock # -- clocks and resets ------------------------------------------------------------------ def test_accesses_and_steps_catch_the_device_up() -> None: system, device = system_with_32x("303900a15120" + "60fe") # move.w $A15120,d0 system.step() assert device.clock == system.mclk device.read_word(0xA15120, system.mclk + 100) assert device.clock == system.mclk + 100 device.read_byte(0xA15120, 5, M68K) assert device.clock == system.mclk + 100 # never backwards def test_registers_survive_the_reset_button() -> None: """Quirks.registers_survive_button (testpico t_32x_reset_btn); power-on resets them.""" system, device = system_with_32x() board = system.board _aden(device, system, rv=True) board.write_word(0xA15104, 3) board.write_word(0xA15120, 0x5A20) # testpico clears RV before it waits for the button ("run from 880xxx as the reset # vector points there"); with RV = 1 the vector's $880200 is the window that locks up # here (docs/undocumented-behavior.md: where hardware lands then is open). board.write_word(0xA15106, 0) system.press_reset() run(system, 5) assert device.aden and not device.rv and device.bank == 3 and device.comm[0] == 0x5A20 assert system.m68k.PC in range(0x880200, 0x880210) # the boot ROM's reset vector device.reset(Reset.POWER_ON, system.mclk) assert not device.aden and device.bank == 0 and device.dreq_length == 0xFFFF assert _pages(system) == [0xA13000, 0xA15100, 0xA15200, 0xA15300] def test_reset_with_rv_is_a_placeholder_stop() -> None: """Quirks.reset_with_rv: the button with ADEN = 1 and RV = 1 stops at the reset vector's $880200 with ``ResetLanding`` -- a placeholder for open item 10 (testpico saw the 68000 land in $2C8-$304), not a hardware claim. A peek passes through it.""" system, device = system_with_32x() _aden(device, system, rv=True) system.press_reset() assert device.peek_byte(0x880200) == 0xFF with pytest.raises(ResetLanding): run(system, 5) assert device.aden and device.rv # the registers are as they were def test_an_empty_32x_slot() -> None: """A 32X with no cartridge: /CART reads high through it (socket 1).""" device = Sega32X(None, vector_rom=SYNTHETIC_VECTORS) system = System(MACHINES["genesis"], None, slot=device) # type: ignore[arg-type] assert not system.board.cartridge_present assert system.board.read_word(0xA130EC) == 0x4D41 # -- data ------------------------------------------------------------------------------- def test_every_quirk_names_a_source_and_a_tier() -> None: tiers = {HARDWARE_CORRECTED, SPECIFICATION, RESEARCH, EMULATOR, ASSUMPTION} for machine in MACHINES_32X.values(): assert isinstance(machine.sh2_variant, Quirk) for name, quirk in machine.quirks.table(): assert isinstance(quirk, Quirk), name assert quirk.tier in tiers and len(quirk.source) > 40, name assert 1 <= quirk.milestone <= 7, name def test_the_three_records_are_kept_apart() -> None: """docs/validation.md, "Three records, kept apart": the quirks the rule was first applied to fill what the documents say, what hardware testing shows, and what is unresolved, apart from what the board implements.""" quirks = MACHINES_32X["genesis-32x"].quirks for name in ("low_map_without_rv", "vdp_side_with_fm", "area_without_aden", "reset_with_rv"): quirk = getattr(quirks, name) assert quirk.documents and quirk.hardware and quirk.unresolved, name assert "ares" in quirks.low_map_without_rv.unresolved assert "main.c:2185" in quirks.vdp_side_with_fm.hardware assert "gen-hw.txt note 2" in quirks.area_without_aden.documents assert "$2C8-$304" in quirks.reset_with_rv.hardware def test_boot_roms_are_checked() -> None: with pytest.raises(BootROMError): check("m68k", SYNTHETIC_VECTORS) with pytest.raises(BootROMError): Sega32X(None, vector_rom=bytes(255)) def test_the_users_boot_roms_verify(user_boot_roms: BootROMs) -> None: assert len(user_boot_roms.m68k) == 256 assert len(user_boot_roms.master) == 2048 and len(user_boot_roms.slave) == 1024