"""The Game Pak: ROM behind an address latch, a save chip, and (later) the GPIO port. docs/cartridge.md. The bus side (wait states, the prefetch buffer) is the board's; this package answers what the cartridge puts on the bus: - **ROM** is linear at 8000000 (and its WS1/WS2 mirrors). The cartridge latches the address on an N access and counts it up on S accesses (NanoBoyAdvance ``rom.hpp``, after Reiner Ziegler's notes); reads past the end of the ROM return the latched address's halfword number, ``(address / 2) & FFFF`` (GBATEK "GBA Unpredictable Things"; quirk 7, C5). - **The save chip** is detected from the ID string Nintendo's library links into the ROM (GBATEK "GBA Cart Backup IDs"), Flash first, then EEPROM, then SRAM, as the census counts them. An EEPROM sits on the ROM bus at the top of the region (D000000 for a ROM of 16 MiB or less, DFFFF00 above); SRAM and Flash at E000000. - **The GPIO port** at 80000C4-80000C9 carries the Seiko RTC when the ROM names ``SIIRTC_V`` (60 games in the census, the Pokémon ones among them; ``gpio.py``). """ from __future__ import annotations from datetime import datetime from gba_python.cartridge.backup import EEPROM, SRAM, Flash from gba_python.cartridge.gpio import GPIO, RTC from gba_python.scheduler import Scheduler ROM_MASK = 0x01FFFFFF #: save ID strings in detection order, and the chip each one means SAVE_IDS = ( (b"FLASH1M_V", "flash128"), (b"FLASH512_V", "flash64"), (b"FLASH_V", "flash64"), (b"EEPROM_V", "eeprom"), (b"SRAM_F_V", "sram"), (b"SRAM_V", "sram"), ) def detect_save(rom: bytes) -> str | None: for marker, kind in SAVE_IDS: if marker in rom: return kind return None class Cartridge: def __init__( self, rom: bytes, scheduler: Scheduler, save: bytes | None = None, save_type: str | None = None, rtc_start: datetime | None = None, ) -> None: self.rom = bytes(rom) self.size = len(self.rom) self.latch = 0 # the ROM address the cartridge's counter holds kind = save_type or detect_save(self.rom) self.save_type = kind self.sram: SRAM | Flash | None = None self.eeprom: EEPROM | None = None if kind == "sram": self.sram = SRAM(save) elif kind == "flash64": self.sram = Flash(0x10000, save) elif kind == "flash128": self.sram = Flash(0x20000, save) elif kind == "eeprom": self.eeprom = EEPROM(scheduler, save) self.gpio: GPIO | None = None if b"SIIRTC_V" in self.rom: self.gpio = GPIO(RTC(scheduler, rtc_start)) # The EEPROM answers where every address bit the mask selects is set (NBA rom.hpp). self.eeprom_mask = 0x01FFFF00 if self.size > 0x01000000 else 0x01000000 @property def title(self) -> str: return self.rom[0xA0:0xAC].rstrip(b"\0").decode("ascii", "replace") def save_data(self) -> bytes | None: chip = self.sram or self.eeprom return bytes(chip.data) if chip else None def eeprom_size_hint(self, dma_length: int) -> None: if self.eeprom is not None: self.eeprom.size_hint(dma_length) # ----- the ROM bus -------------------------------------------------------------------- def _is_eeprom(self, address: int) -> bool: return self.eeprom is not None and address & self.eeprom_mask == self.eeprom_mask def read16(self, address: int, sequential: bool) -> int: address &= 0x01FFFFFE if self.eeprom is not None and address & self.eeprom_mask == self.eeprom_mask: if not sequential: self.eeprom.select() return self.eeprom.read() if self.gpio is not None and self.gpio.readable and 0xC4 <= address <= 0xC8: return self.gpio.read(address) if not sequential: self.latch = address latch = self.latch self.latch = (latch + 2) & ROM_MASK if latch < self.size: rom = self.rom return rom[latch] | rom[latch + 1] << 8 if latch + 1 < self.size else rom[latch] return latch >> 1 & 0xFFFF def read32(self, address: int, sequential: bool) -> int: address &= 0x01FFFFFC if self.eeprom is not None and address & self.eeprom_mask == self.eeprom_mask: if not sequential: self.eeprom.select() low = self.eeprom.read() return self.eeprom.read() << 16 | low if self.gpio is not None and self.gpio.readable and 0xC4 <= address <= 0xC8: return self.gpio.read(address + 2) << 16 | self.gpio.read(address) if not sequential: self.latch = address latch = self.latch self.latch = (latch + 4) & ROM_MASK if latch + 3 < self.size: return int.from_bytes(self.rom[latch : latch + 4], "little") if latch < self.size: data = self.rom[latch : latch + 4] return int.from_bytes(data + bytes(4 - len(data)), "little") low = latch >> 1 & 0xFFFF return ((low + 1) & 0xFFFF) << 16 | low def write16(self, address: int, value: int, sequential: bool) -> None: address &= 0x01FFFFFE if self.eeprom is not None and address & self.eeprom_mask == self.eeprom_mask: if not sequential: self.eeprom.select() self.eeprom.write(value) return if self.gpio is not None and 0xC4 <= address <= 0xC8: self.gpio.write(address, value) if not sequential: self.latch = address # A write is a Game Pak cycle too: the counter moves on. hw-test/dma/burst-into-tears # (hardware): a DMA that writes 8000000 and then reads "8000000" S gets 8000002's data. self.latch = (self.latch + 2) & ROM_MASK # ----- the SRAM bus (E000000-FFFFFFF, 8 bits) ----------------------------------------- def read_sram(self, address: int) -> int: return self.sram.read(address) if self.sram is not None else 0xFF def write_sram(self, address: int, value: int) -> None: if self.sram is not None: self.sram.write(address, value)