# SPDX-License-Identifier: MIT """A Mega Drive with a 32X in its slot, built the one way every caller should build it. ``build(machine, cartridge, boot_roms)`` makes the slot device, plugs it in at power-on (``System(..., slot=device)``, socket 1) and gives it the VDP's line records (``System.attach_video_timing``, socket 6), which its VDP status bits and the SH-2s' VINT/HINT need, makes it the picture's last stage (``System.attach_picture``, socket 7: the composite, ``composite.py``) and adds its PWM to the sound (``System.audio_sources``, socket 8: ``pwm.py``). The user's boot ROMs are required (Aubrey's decision 3) and checked by SHA-1 (:class:`~sega32x_python.bootrom.BootROMs`). """ from __future__ import annotations from pathlib import Path from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.system import System from .bootrom import BootROMs from .machine import MACHINES as MACHINES_32X from .slot import Sega32X class AcclaimEEPROMCartridge(Cartridge): """An Acclaim cartridge with a serial EEPROM, as a 32X title has it (``Quirks.cartridge_boards``). The board's wiring (/SDA at $200001 bit 0, /SCL at $200000 bit 0, a 24C02) is megadrive-python's ``rom_nbajamte`` board at the pin; this class adds one rule: - /SDA reads the line, not only what the chip drives. DOC: the I2C bus's SDA is open-drain, a wired AND of every device driving it (NXP UM10204, the I2C-bus specification, section 3.1.1; Microchip's 24LC02B data sheet, "SDA ... open-drain terminal"). So a line the 68000 holds low reads low whatever the EEPROM drives. HYPOTHESIS: that the cartridge feeds the 68000's /SDA latch onto the line through an open-drain stage (no schematic of the board is to hand). The EEPROM answers at $200000-$200001 from power-on, as MAME's ``bus/megadrive/eeprom.cpp`` (BSD-3-Clause, ``md_eeprom_nbajamte_device``) has it (OBSERVED, an emulator's model). Before 2026-11-19 this class also switched the ROM and the EEPROM at $200000 by a write's bit 0; that rule came from a non-commercial emulator's source and was removed unread (docs/provenance.md, wave 2). """ def _decode_read(self, address: int) -> int | None: value = super()._decode_read(address) return None if value is None else value & self._sda def cartridge_for(image: bytes, model: str = "genesis-32x") -> Cartridge: """The Cartridge of a 32X image: megadrive-python's own choice of board, unless its serial is one of ``Quirks.cartridge_boards``.""" serial = image[0x183:0x18B].decode("ascii", "replace").strip() slot = MACHINES_32X[model].quirks.cartridge_boards.value.get(serial) if slot is None: return Cartridge.from_image(image) return AcclaimEEPROMCartridge(image, slot=slot) def build(machine: str, cartridge: Cartridge | None, boot_roms: BootROMs | str | Path, *, model: str = "genesis-32x") -> tuple[System, Sega32X]: # fmt: skip """A System of megadrive-python's ``machine`` with a 32X (``model``) holding ``cartridge``; ``boot_roms`` is a :class:`BootROMs` or the directory holding them.""" if not isinstance(boot_roms, BootROMs): boot_roms = BootROMs.load(boot_roms) device = Sega32X(cartridge, MACHINES_32X[model], boot_roms=boot_roms) system = System(MACHINES[machine], None, slot=device) # type: ignore[arg-type] system.attach_video_timing(device) system.attach_picture(device) system.audio_sources.append(device.pwm) return system, device __all__ = ["build", "cartridge_for"]