# megadrive-python and m68000-python: two things a save state works around (PR notes) **Resolved 2026-11-07.** Raised 2026-11-06 as megadrive-python PR #11 (branch `socket-notes`, with the socket 7 and 8 docstring notes of docs/worklog.md Sessions 5, 6 and 9) and m68000-python PR #6 (branch `table-at-class`); both merged (d75b6a4, c827cca), and megadrive-python PR #12 moved its m68000-python pin to c827cca. Dropping `_prime` showed that it had covered a third case: sh2-python's `SH7604` built its dispatch table in `__init__` too, so a fresh process's first SH-2 step failed the same way. That is sh2-python PR #2, the same fix, merged as d4665d5; megadrive-python #12 merged as 5c6d1b7. The board repinned (docs/worklog.md, Session 16) and `savestate.py` dropped both workarounds below: the id remapping and `_prime`. The rest of this page is the record of what they were. Written by sega32x-python's save states (`src/sega32x_python/savestate.py`; docs/worklog.md, Session 10). Nothing here blocks the board: both are handled on the board's side without changing either core, and the board's determinism runs pass with the workarounds (docs/validation.md, "Save states"). They are written up because a save state of megadrive-python alone, or of any other board on these cores, would meet them too. ## megadrive-python (44ab15d): socket-8 leftovers keyed by `id(source)` ### Title audio: key socket-8 sources' leftover samples by the source, not by `id()` ### What the board does now `AudioPath._add_sources` keeps each extra source's leftover samples and its fractional position in `self._sources[id(source)]`. An `id` is an address in one process: a machine pickled and loaded in another process has the same source objects with new ids, so the lookup misses, the leftovers (a few samples at a frame boundary) are dropped and the source restarts from silence. A freed source's id can also be reused by a new one, which would then inherit its leftovers. ### The change Key the dict by the source object itself (sources are plain objects with identity hashing), or hold the leftovers on a small per-source record in a list parallel to `System.audio_sources`. Either survives pickling, since pickle keeps the object graph's identity. No behaviour changes within a process. ### Until then `savestate.py` saves the sources' old ids beside the machine and moves the keys to the new ids after loading. ## m68000-python (38dbd0f): the dispatch table is made by the first constructor ### Title cpu: build the dispatch table at class creation (or on first `step`), not in `__init__` ### What the core does now `M68000CPU.__init__` ends with `if "_table" not in cls.__dict__: cls._table = build_table(cls)`. A process that unpickles a CPU never runs `__init__`, so its first `step` fails with `AttributeError: '_table'`. ### The change Build it in `__init_subclass__` and once for the class itself at import, or fall back in `step` (`self._table` resolved through a class property that builds it). Either keeps subclasses with their own tables. ### Until then `savestate.py` builds one throwaway console before unpickling (`_prime`).