# SPDX-License-Identifier: MIT """Battery files: what a cartridge keeps when the console is off (docs/cartridge.md "Battery files"). A board convention, not GBA behaviour. - ``NAME.sav``: the save chip's memory as a raw image, nothing else: 32,768 bytes for SRAM or FRAM, 65,536 or 131,072 for Flash (bank 0 then bank 1), 512 or 8,192 for EEPROM (64-bit blocks in address order, each block's bytes in the order the chip sends them, most significant bit first). This is the layout other emulators' ``.sav`` files commonly use, so a file can move between them; no file is written for a cartridge without a save chip. - ``NAME.rtc`` (only with the clock): JSON with the clock's date and time when the file was written, its control register, its weekday offset, and the host's time then. Loading adds the host time that has passed, as the cartridge's own battery would have kept counting. """ from __future__ import annotations import json from pathlib import Path def rtc_path(path: Path) -> Path: return path.with_suffix(".rtc") def read_save(path: Path) -> bytes | None: return path.read_bytes() if path.exists() else None def write_battery(board, path: Path) -> None: data = board.backup.save_data() if data: path.write_bytes(data) if board.rtc is not None: state = board.rtc.state(board.now) rtc_path(path).write_text(json.dumps(state, indent=1) + "\n") def load_rtc(board, path: Path) -> None: sidecar = rtc_path(path) if board.rtc is not None and sidecar.exists(): board.rtc.load_state(json.loads(sidecar.read_text()), board.now)