# Save states A save state of a Mega Drive with the Sega CD on its expansion port. Since 2026-11-23 it is built on megadrive-python's save states (its `docs/save-states.md`, **state version 2**, megadrive-python efd80fc), which are the specification: what a state is, where one may be taken, the format, what is not saved, all-or-nothing loading. This document says what the CD adds. The implementation is `src/segacd_python/state.py`; the tests are `tests/test_state.py` (seconds) and `tests/test_save_state.py` (`pytest -m slow`). Before 2026-11-23 a state was a pickle of the whole machine (2026-11-21, docs/worklog.md). Pickles of the old format are not read. ## The property A console saved between two main-68000 steps and loaded into a freshly built console continues exactly as the run that was never interrupted. The check compares the whole state (every value a state holds, `state.machine_state`) right after the load and after every frame, so a value a load fails to restore shows at once, not only when it next matters. ## Where a state may be taken Between two main-68000 steps (`System.step`), as megadrive-python allows. The sub CPU runs whole instructions (megacd.py), so it then stands between two of its own; the gate array, the decoder, the drive, the graphics unit and the PCM are saved with their clocks and queues as they are (a drive part way through its 1/75 s exchange, a transfer part way, samples not yet taken). A hung sub CPU and its held write (megacd.py, "The hang") are saved too. ## The format ``` MAGIC 8 bytes b"MCDPYSTA" LENGTH 4 bytes the header's length, big-endian HEADER JSON {"format": "segacd-python state", "version": 1, "megadrive": , "bios_sha1": ..., "disc": , "host": {...}} BODY zlib JSON: {"m68k": {...}, "z80": {...}, "sub68k": {...}, "system": } ``` Refused up front (`StateError`), before the body is decompressed: another magic or format; another version of this format (`state.VERSION`, raised whenever an attribute of this package's objects is added, removed, renamed or changes meaning); another megadrive-python state version (its `state.VERSION`, which moves with the Mega Drive's graph and the cores'); anything megadrive-python's `check_header` refuses (another cartridge, machine or wiring); another boot ROM (your BIOS or mcd-verificator, by SHA-1); another disc in the drive, or a disc where the state had none, or none where it had one. `host` is the host's own data, not the console's: the command line keeps its frame number there (`--load-state` then counts on from it). ## What the CD adds to megadrive-python's walk - **The device's objects are state.** The walk saves the objects of the packages in megadrive-python's `state.MODULES`; for the length of a save or a load `segacd_python` is one of them. So the gate array, PRG RAM, word RAM, the backup RAM, the decoder and its 16 KB buffer, the drive, the CD-DA queue, the graphics unit and the PCM with its wave RAM are walked as the Mega Drive's chips are. The device is reached through `board.port` (and `System.clocked`, `System.audio_sources`: saved as references). - **The sub CPU** is saved by m68000-python's `capture_state` as `sub68k` and restored by `restore_state`, as the main CPUs are; the walk skips the CPU object. - **The disc is a medium.** Written as its identity, never by content: the SHA-1 of its track table and its first 16 sectors (the system area, where the disc's header is). On load the drive must hold a disc of the same identity, and the loading console's own disc object is kept. The lid can be opened and the disc changed at run time, so the identity is that of the disc in the drive when the state was saved: to load a state saved after a swap, build the console with the second disc. - **The boot ROM** is a byte string, so the walk writes it as an image of its length and keeps the loader's; the header's SHA-1 has said it is the same. - **The host's settings** are megadrive-python's `state.HOST`: the output sample rate, the FM generator's switch, and the drawing switches `VDP.rendering` and `VDP.undrawn_frame`, which since its state version 2 gate only the pixels. The loading console keeps its own. The device has no host settings of its own (`gain` and `rate` are class constants). ## Not supported - **A 32X in the slot** (`--with-32x`): sega32x-python saves its SH-2 side its own way; `save_state` refuses a console with a device in the slot. - Smaller units than a main-68000 step, as in megadrive-python. ## What it borrows, and why megadrive-python has no socket through which an expansion device adds itself to a state (docs/sockets.md ยง12, with the proposal). `state.py` therefore uses that module's private `_Encoder`, `_Decoder`, `_header` and `MODULES` at the pinned revision; nothing is copied. `tests/test_state.py` checks they are there and that `MODULES` is put back. ## Results Recorded in docs/status.md ("Save states") with the commit they were measured at.