# SPDX-License-Identifier: MIT """Read .spc files: an S-SMP snapshot (ARAM, DSP registers, SPC700 registers, the RAM under the IPL). Written from the published "SPC File Format v0.30" description (snesmusic.org ``spc_file_format.txt``, pinned in scripts/dsp_pinned_files.json): the offsets below are its table. The ID666 tag is read only for the song and game titles (text form; the binary form has the same title fields). """ from __future__ import annotations from dataclasses import dataclass from pathlib import Path SIGNATURE = b"SNES-SPC700 Sound File Data" RAM_OFFSET = 0x100 DSP_OFFSET = 0x10100 EXTRA_RAM_OFFSET = 0x101C0 MINIMUM_SIZE = 0x10200 @dataclass(frozen=True) class SPCFile: pc: int a: int x: int y: int psw: int sp: int ram: bytes # 64 KiB dsp_registers: bytes # 128 extra_ram: bytes # 64: the RAM at $FFC0-$FFFF when the IPL ROM is mapped over it song: str game: str def _text(field: bytes) -> str: return field.split(b"\0", 1)[0].decode("latin-1").strip() def parse_spc(data: bytes) -> SPCFile: if len(data) < MINIMUM_SIZE or not data.startswith(SIGNATURE): raise ValueError("not an .spc file (signature or size)") has_tag = data[0x23] == 26 return SPCFile( pc=data[0x25] | data[0x26] << 8, a=data[0x27], x=data[0x28], y=data[0x29], psw=data[0x2A], sp=data[0x2B], ram=bytes(data[RAM_OFFSET : RAM_OFFSET + 0x10000]), dsp_registers=bytes(data[DSP_OFFSET : DSP_OFFSET + 128]), extra_ram=bytes(data[EXTRA_RAM_OFFSET : EXTRA_RAM_OFFSET + 64]), song=_text(data[0x2E:0x4E]) if has_tag else "", game=_text(data[0x4E:0x6E]) if has_tag else "", ) def read_spc(path: str | Path) -> SPCFile: return parse_spc(Path(path).read_bytes())