# blastem-events A decoder for BlastEm's hardware event log, the file `blastem -e FILE` writes (and the libretro core writes when its host asks for one): it reads the log and writes every event as a JSON record in the family tool contract's `trace/events` format, with a run manifest, so an MIT repository can use the log knowing only this README and emu-tools `docs/contract.md` (version 1). Version 0.1.0 (`CHANGELOG.md`). It moved here from megadrive-python on 2026-11-26. The log's binary layout was first learned in 2026-09 from BlastEm's GPL-3.0 source at `542164d`, before the family's closed-box rule of 2026-11-14; megadrive-python's licence tidy (decision L4, its commit `9993a1d`) took every source name and line number out of its readers, and Aubrey then chose to move the decoding itself to the GPL side. megadrive-python now runs this tool and reads only its output. ## Where it is Set **`EMU_TOOLS_GPL`** to a checkout of this repository (emu-tools `docs/contract.md`, section 5). | Path | What | | --- | --- | | `$EMU_TOOLS_GPL/tools/blastem-events/run.py` | the decoder (Python 3.11 or newer, standard library only) | | `$EMU_TOOLS_GPL/tools/blastem-events/build.sh` | nothing to build: checks the interpreter and runs the smoke test | | `$EMU_TOOLS_GPL/tools/blastem-events/tests/smoke.py` | the smoke test (the command line end to end) | | `$EMU_TOOLS_GPL/tools/blastem-events/tests/test_decoder.py` | the decoder's unit tests (pytest, or run alone) | ## Upstream, licence and pin - **BlastEm** by Michael Pavone (Mask of Destiny), GPL-3.0 (emulator-lineage `docs/sources.md`: closed box only). Nothing of BlastEm is fetched, built, linked or committed here: the tool reads a file BlastEm wrote. The layout was learned from, and every rule is checked against logs of, revision **`542164d`** (libretro's git mirror), log version 512 (`0x0200`). A log of another revision may differ; one that does will normally fail the end-of-body check below. - **This tool: GPL-3.0-or-later** (`../../LICENSE`). `run.py` and `tests/test_decoder.py` are our code (alewman), first published under MIT as megadrive-python's `scripts/blastem_events.py` and `tests/test_blastem_events.py` (to 2026-11-25); they keep that MIT permission (`../../notices/MIT-emulator-family.txt`). ## Command line run.py --log FILE --out DIR [--gzip] run.py --version - `--log FILE`: the event log. - `--out DIR`: the output directory (created): `manifest.json` and `events.jsonl`. - `--gzip`: write `events.jsonl.gz` instead (a long log's records run to gigabytes plain). - `--version`: prints `blastem-events 0.1.0 (format of BlastEm 542164d, contract 1)`. Exit status as the contract says: `0` the log decoded to the last byte of its body; `1` it did not (not a BlastEm log, a payload this decoder does not know, a cut-off or desynchronised stream: the manifest's `status` is `failed` and `error` says why, and the records decoded before the failure are kept); `2` a usage error; `3` the log is missing. ## Outputs **`events.jsonl`**: one JSON object a line, one per event, in the log's order. Every record has `t` (an integer: master clocks from the log's start, absolute), `clock` (always `"mclk"`) and `event` (the name below), then the fields of that event: | `event` | Fields | What it is | | --- | --- | --- | | `vdp-reg` | `register`, `value` | a VDP register write (register number 0-23, the byte written) | | `vram-b`, `vram-bd`, `vram-b1`, `vram-ba` | `address`, `value` | a byte reaching VRAM: `address` is the 16-bit VRAM byte address as the VDP addresses it (no byte swap applied), `value` the byte. The four names are the log's four encodings of the address (given in full, as a step from the last one, as the next byte, or as a step of the VDP's auto-increment); the record always carries the resolved address | | `vram-w`, `vram-wd` | `address`, `value` | a word write in 128 KB mode: `address` is 17 bits, `value` the 16-bit word (the address given in full, or as a step from the last one) | | `intram` | `memory`, `index`, `value` | a write to the VDP's internal memories: `memory` is `"cram"` or `"vsram"`, `index` the word index (0-63), `value` the 16-bit word as logged (unmasked) | | `psg` | `value` | a byte written to the PSG | | `ym` | `part`, `register`, `value` | a YM2612 register write: `part` 0 or 1, the register, the byte | | `adjust` | `deduction` | BlastEm's counter adjustment (bookkeeping: `t` is not moved by it) | | `flush` | none | an output-buffer flush. **Not** a frame marker, although its count lands near the frame count | Times: `t` advances by each event's delta only, so it is continuous across the whole log; the log's own 32-bit counter, which wraps and which `adjust` deducts from, is reported once, as the manifest's `counter_end`. Several events may share a `t`. The frame an event falls in is `t // frame_mclk` and its line `(t % frame_mclk) // line_mclk` (manifest below); BlastEm's `-b N` runs N/2 frames headless at `542164d`, so use the frame of the last `t`, not the argument. **`manifest.json`**: the contract's run manifest (`contract` 1, `tool` `blastem-events`, `tool_version`, `tool_commit`, `tool_licence`, `upstream` `{"name": "BlastEm", "commit": "542164d", "licence": "GPL-3.0", "patches": []}`, `inputs` with the log's SHA-1 and size, `args`, `host`, `started`, `time_origin`, `outputs` with the events file, `format` `trace/events` and its record count, `status` `ok` or `failed`, `error` when failed, `evidence` `OBSERVED`), plus a `log` object from the log's start block and the decode: | `log` field | Meaning | | --- | --- | | `version` | the log's format version (512 at `542164d`) | | `system_type`, `video_std` | the start block's two bytes as logged; `video_std` bit 0 is 1 for PAL | | `name` | the game name in the start block (Latin-1) | | `line_mclk`, `frame_mclk` | 3,420 master clocks a line (DOC: the Mega Drive's figure), and a frame of 262 lines NTSC or 313 PAL | | `body_bytes`, `decoded_bytes` | the decompressed body's length and where decoding stopped: equal on a good decode | | `t_end` | the last event's `t` (0 for an empty log) | | `counter_end` | BlastEm's own 32-bit counter after the last event | | `counts` | records by `event`, most frequent first | ## Evidence and known limits Everything this tool writes is a closed-box observation: tag it **OBSERVED (BlastEm @ 542164d log format, via blastem-events 0.1.0)**, never a hardware fact; any document or hardware measurement that disagrees overrules it (`emu-tools/docs/wall.md`). BlastEm itself is a detector for the family: its log says where to look. - The check that the decode is right is that a whole log decodes to the last byte of its body: one wrong payload width desynchronises the stream and fails it. That checks widths and header forms, not meanings. - The `intram` split (CRAM below 128 in the log's address byte, VSRAM from 128) and the auto-increment step taken from register 15 come from the 2026-09 reading of the source and decode real logs to their last byte; the split's meaning is HYPOTHESIS until a log is replayed against a known CRAM and VSRAM. - What the log does not carry is not invented here. megadrive-python's `scripts/vdp_streams.py` documents the gaps it has met (a VRAM copy DMA is not logged; 64 KB-mode writes carry no address bit 16). - The `state` event type (12) is not decoded; a log that has one fails with an error naming it. ## Smoke test `python3 -I tests/smoke.py` (or `./build.sh`): `--version`; the usage (2) and missing-log (3) codes; a file without the log's magic and a log cut off mid-payload both fail (1) with the manifest saying so; a small log written by the test with every payload kind decodes to the expected 14 records, the same plain and gzipped, with the manifest's counts, `t_end` and `counter_end`. `python3 -I tests/test_decoder.py` runs the 15 decoder tests (14 moved from megadrive-python, one new for `intram`). No ROM, no BlastEm. ## For consumers (megadrive-python) Run `$EMU_TOOLS_GPL/tools/blastem-events/run.py --log FILE --out DIR` as a separate program and read `DIR/manifest.json` and the events file it names; never import it. With `EMU_TOOLS_GPL` unset or `run.py` absent, a consumer script stops with exit status 3 naming the variable and the path, and a test skips naming it. megadrive-python's `scripts/blastem_events.py` and `scripts/vdp_streams.py blastem` do so since 2026-11-26.