# 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: milestone 3** (2026-10-29). Milestone 0's referee tables are in [docs/validation.md](docs/validation.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 and the 32X VDP draws its own picture: testpico's twelve 32X tests pass, the Mars Check Program passes all 161 tests, and Sega's samples and the 240p suite match the detectors' frames ([docs/worklog.md](docs/worklog.md), Sessions 1-4). The picture is the 32X's alone (`device.vdp.frame_rgb()`): the composite with the Mega Drive's is milestone 4; PWM makes no sound (milestone 5). ```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, PicoDrive core 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 515 MB once the toolchain is unpacked and PicoDrive built). Sega's diagnostic and sample programs are copied from the user's own No-Intro mirror (`file://` pins). ## Licence MIT (see `LICENSE`). Fetched references keep their own licences, listed per pin. Of the emulators fetched, only ares (ISC) and MAME's `mega32x.cpp` (BSD-3) may be read; PicoDrive, jgenesis, BlastEm, the MiSTer core and Gens are built and run as closed-box detectors, their source never opened (the family's `sources.md`; audit in [docs/provenance.md](docs/provenance.md)).