"""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 of more than 16 Mbit with a serial EEPROM, as a 32X title needs it (``Quirks.cartridge_boards``; each difference from megadrive-python's Cartridge at the pin is a change that belongs there, written up in docs/worklog.md, Session 5): - /SDA reads the line, not only what the chip drives: an open-drain line the 68000 is holding low reads low. NFL Quarterback Club tests exactly that at start (Genesis Plus GX ``eeprom_i2c_out``: "return latched /SDA input by default"). - a word write to $200000 selects what answers at $200000: bit 0 set the ROM, clear the EEPROM (Genesis Plus GX ``mapper_acclaim_32M_write16``); it clocks nothing. The ROM after reset, as there. """ def __init__(self, image: bytes, *, slot: str) -> None: super().__init__(image, slot=slot) self.eeprom_mapped = False def _decode_read(self, address: int) -> int | None: if not self.eeprom_mapped: return None value = super()._decode_read(address) return None if value is None else value & self._sda def write_word(self, address: int, value: int) -> None: if address & ~1 == 0x200000: self.eeprom_mapped = not value & 1 return super().write_word(address, value) 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"]