# sega32x-python A pure-Python Sega 32X (Mega Drive 32X / Genesis 32X / Super 32X), as a **board that plugs into [megadrive-python](https://github.com/alewman/megadrive-python)'s cartridge-slot socket** and runs two SH-2s from the separate core **sh2-python**. The package is `sega32x_python` (a package cannot start with a digit). **Status: milestones 0-5b done, milestone 6 (games) under way** (2026-11-24; every rung with its count and commit in [docs/status.md](docs/status.md)). `src/sega32x_python` is megadrive-python's slot device: with ADEN = 0 it is passive and megadrive-python's gate runs unchanged through it; with the user's boot ROMs it runs the two SH-2s (sh2-python) on the 32X's bus, the 32X VDP draws its picture, the composite puts it over the Mega Drive's, and PWM makes its sound. testpico's twelve 32X tests pass, the Mars Check Program passes all 161 tests (its whole run, about 7,000 frames, last on 2026-11-25 at sh2-python 8824075; the gate's 760 frames reach test 51), Sega's samples and the 240p suite match the detectors' frames, and the composite, PWM and SH-2 bus costs rest on documents and detectors only ([docs/claims.md](docs/claims.md)). The 27 titles of the smoke list and the 33 other releases of the No-Intro set all run 1,200 frames without an exception or lockup, and every difference from PicoDrive and BlastEm is triaged ([tests/sweep_triage.txt](tests/sweep_triage.txt)): 0 untriaged of 60. The 149 betas, prototypes and demos (600 frames) are triaged too, 0 untriaged (2026-11-25): 19 early programs of the release and beta lists stall on the board, not on the detectors, because they run an SH-2 at interrupt mask 0 while the FRT's FTOA, wired to /IRL0 as the documents say, is low ([docs/status.md](docs/status.md), "The FTOA finding"). Pinned on megadrive-python efd80fc (save states v2, the drawing switch) and sh2-python 8824075 (complete save states, DMA address errors; 2026-11-25). No 32X hardware is at hand: nothing here is measured on one. ```python from megadrive_python.cartridge import Cartridge from sega32x_python.console import build # 32X_G/M/S_BIOS.BIN from your own directory, checked by SHA-1 system, device = build("genesis", Cartridge.from_image(image), "/path/to/your/bios") system.run_frame() ``` ## Run ```sh # to frame 600; the last picture (the composite) and the sound python -m sega32x_python game.32x --bios-dir /path/to/your/bios --frames 600 \ --png out.png --wav out.wav # save the whole machine after frame 140 (both SH-2s, the 32X, the Mega Drive) ... python -m sega32x_python game.32x --bios-dir DIR --frames 140 --save-state boot.state --save-at 140 # ... and start there: frames 141-200 python -m sega32x_python game.32x --bios-dir DIR --frames 200 --load-state boot.state --png out.png ``` `scripts/run_32x.py` takes the same `--save-state FILE --save-at N` and `--load-state FILE` with its other options (a PNG every frame, button presses, the reset button, the PWM log). A state holds no ROM or BIOS bytes: it names them by SHA-1 and needs the same cartridge, boot ROMs, board and core revisions to load (`src/sega32x_python/savestate.py`; [docs/validation.md](docs/validation.md), "Save states"). It is a pickle, so load only states you made. In Python, `savestate.save(path, system, device, frame=n)` and `savestate.load(path, cartridge_for(image), boot_roms=BootROMs.load(DIR))`. The debugger hook: `device.sh2.on_step = fn` is called as `fn(index, chip)` before each step of either SH-2; sh2-python's `chip.observe` sees each of a chip's accesses. ## The shape The design is megadrive-python's `docs/expansion-sockets.md` (branch `expansion-sockets`, 439b2ad): an add-on is its own board in its own repository, pinning megadrive-python by git revision as megadrive-python pins its cores. Nothing is copied from megadrive-python. The 32X is a **slot device that holds a `Cartridge`**: it sits in the Mega Drive's cartridge slot and the game sits in it. This repository holds the 32X's own hardware: - the system registers (`$A15100-$A1513F` on the 68000 side, `$20004000-$2000403F` on the SH-2 side), the communication ports, the DREQ FIFO, the CMD interrupts; - 68000/SH-2 sharing and arbitration of the cartridge, the frame buffer, the palette and the registers (FM and RV); - the 32X VDP: packed-pixel, direct-colour and run-length modes, the line table, frame-buffer swap, auto fill, screen shift, priority against the Mega Drive; - PWM sound; - the 68000's view of the cartridge after ADEN (vectors from the adapter at `$000000`, the cartridge at `$880000` and banked at `$900000`); - the composite with the Mega Drive's picture and sound; - later, the Sega CD 32X combination (a device in each socket). The two SH-2s (SH7095, Sega 315-0922/315-0998: an SH7604 core with on-die DMA, FRT, WDT, SCI, division unit, cache and bus state controller) come from **sh2-python**, a separate core with its own Phase 0. Their clock is the Mega Drive's: SH-2 = MCLK x 3/7 (a PLL locked to the 68000's VCLK, tripled). **Boot ROMs are bring-your-own.** Sega's 68000, master and slave boot ROMs are never fetched or committed here; `scripts/fetch_references.py` refuses them by hash. A free replacement is possible ([docs/start-here.md](docs/start-here.md), "Boot ROMs"). ## Read 1. [docs/start-here.md](docs/start-here.md): the machine, memory maps, registers, the VDP, PWM, boot ROMs, the CD 32X. 2. [docs/timing.md](docs/timing.md): clocks, the line, waits, interrupts, latches. 3. [docs/undocumented-behavior.md](docs/undocumented-behavior.md): quirks, contested values, known-wrong sources, open items. 4. [docs/validation.md](docs/validation.md): every oracle with its tier; what ran here. 5. [docs/mame-oracle.md](docs/mame-oracle.md): MAME's 32X as a detector. 6. [docs/handoff-brief.md](docs/handoff-brief.md): sockets, milestones, decisions. ## Fetch ```sh python scripts/fetch_references.py # documents, test programs, source trees (~50 MB) python scripts/fetch_references.py --large # also the marsdev toolchain (103 MB) python scripts/fetch_references.py --verify # hash what is present; fails on any Sega boot ROM python scripts/build_referees.py # toolchain, sjasm, testpico (the detectors are shipped cores) python scripts/libretro_frames.py reference/build/picodrive/picodrive_libretro.so \ reference/build/testpico/hardware/testpico-30a5783.bin out/ --frames 2400 ``` Everything lands in `reference/` (git-ignored; about 480 MB once the toolchain is unpacked). Sega's diagnostic and sample programs are copied from the user's own No-Intro set under `$NO_INTRO_DIR`. The detectors are shipped programs: a PicoDrive libretro core at `reference/build/picodrive/picodrive_libretro.so` (or `PICODRIVE_CORE`), a BlastEm one at `BLASTEM_CORE`, MAME as installed. ## Licence MIT (see `LICENSE`). Fetched references keep their own licences, listed per pin. The only emulator sources fetched are ares (ISC) and MAME (`mega32x.cpp`, BSD-3), which may be read. PicoDrive and BlastEm run as shipped libretro cores, closed boxes whose source is neither fetched nor opened; jgenesis, the MiSTer core and Gens are not used (Aubrey, 2026-11-24; the family's `sources.md`; audit in [docs/provenance.md](docs/provenance.md)).