# sms-python A readable, pure-Python emulator of **Sega's 8-bit Z80 machines**: the SG-1000, the SC-3000 computer (with its keyboard and the SF-7000 disk unit), the Mark III and Master System (SMS1 and SMS2 video chips), the Game Gear, and a Mega Drive running Master System cartridges through a Power Base Converter. It is a board built on [z80-python](https://github.com/alewman/z80-python): z80-python owns the instruction set, this repository owns everything around it. That means the memory map and cartridge mappers, the I/O ports, the video chip (TMS9918A modes and the Sega Mode 4), the SN76489 sound chip, the joypads, pause and reset, and the clock that ties them together. **Status (2026-11-12): sixteen machines run, and every cartridge and disk image of the local romsets has a modelled board.** ZEXDOC and ZEXALL (ZEXALL-SMS v0.21) report 79 of 79 tests OK on z80-python 0.4.0. The whole system runs in lockstep with MAME 0.285, instruction for instruction: | MAME 0.285 run | Instructions identical | Port reads differing | Exit frame pixels | | --- | ---: | ---: | ---: | | Flicky, `sg1000`, 20 s | 7,703,804 | 0 | 49,152 / 49,152 | | Sonic the Hedgehog, `sms` (through the BIOS), 20 s | 7,904,561 | 0 | 49,152 / 49,152 | | SMS VDP Test v1.31, `sms`, 12 s | 3,556,264 | 0 | 49,152 / 49,152 | | Sonic the Hedgehog, `gamegear`, 20 s | 5,903,153 | 1 (power-on, open item 7) | 23,040 / 23,040 | Compatibility sweep (milestone 7): after 10 seconds, **1,157 of 1,170** runnable Master System cartridges, **823 of 874** Game Gear and **237 of 243** SG-1000 show exactly MAME's frame, and every remaining difference has been traced to MAME or to a documented open question (docs/validation.md has the breakdown). FluBBa's SMS VDP Test v1.31, whose expected results come from real SMS1, SMS2 and Mega Drive hardware, passes **55 of 55** on both the SMS1 and SMS2 VDP variants, including every H-counter timing test; its Mega Drive entry passes **55 of 55** on `--machine megadrive-pbc`, a Mega Drive with a Power Base Converter. MAME is a detector, not a judge (docs/validation.md has the tiers and the full certification record). Sega's SG-1000 M2 Check Program passes its RAM test and reaches its I/O screen. The SC-3000 runs BASIC Level III from its keyboard, and the SF-7000 boots Disk BASIC through its IPL ROM and saves and loads a program. The unit tests run on CPython and PyPy; `pytest -m slow` reruns the MAME replays and locksteps when their logs are recorded locally. ## Playing A desktop window (picture, sound, keyboard, game controllers) comes with the `frontend` extra, which adds pygame-ce. Run it on [PyPy](https://pypy.org): it plays at full speed there with room to spare, and at about a third of full speed on CPython. ```sh pypy3.11 -m venv .venv-pypy .venv-pypy/bin/pip install "sms-python[frontend] @ git+https://github.com/alewman/sms-python" .venv-pypy/bin/sms-python "Sonic The Hedgehog (USA, Europe, Brazil) (En).zip" ``` The machine follows the file: `.sg` runs on an SG-1000, `.gg` on a Game Gear, `.sc` on an SC-3000 (the PC's keyboard is the computer's), anything else on an export Master System II (`--machine` picks another, `--bios` boots through a BIOS image). An SF-7000 disk (`.sf`) needs the unit's IPL ROM as `--bios`; what a program saves stays in memory and the file is not changed. Zipped images work. | Key | | | --- | --- | | Arrows, Z (or J), X (or K) | D-pad, button 1, button 2 (a game controller works too) | | Enter | Start on the Game Gear, Pause on the Master System | | Backspace | the reset button (SMS1 and Mark III) | | Tab (hold), F3, F5 | fast forward, pause emulation, power cycle | | F6, F7, F8 (hold) | save a state, load it, rewind (about a minute is kept) | | F11, F12, Esc | fullscreen, screenshot, quit | The sound is the console's clock: the window runs a frame whenever the queue of sound (three frames, 50 ms) runs short, so the game keeps its own 59.92 Hz, or 49.70 Hz on a PAL machine. `--machine` takes one of: | Name | Machine | | --- | --- | | `sg1000` | SG-1000 | | `sc3000`, `sc3000pal` | SC-3000 computer, NTSC and PAL (50.16 Hz: it keeps the NTSC crystal) | | `sf7000` | SC-3000 with the SF-7000 disk unit | | `mark3`, `mark3fm` | Mark III, without and with the FM Unit | | `smsj` | Japanese Master System (FM built in) | | `sms1`, `sms1pal`, `sms2`, `sms2pal` | export Master System and Master System II | | `gamegear` | Game Gear | | `ggsms`, `ggsms1asic` | Game Gear running a Master System cartridge (2-ASIC and 1-ASIC models) | | `megadrive-pbc`, `megadrive-pbc-pal` | Mega Drive with a Power Base Converter | ## As a library ```python from pathlib import Path from sms_python.board import SMS2 from sms_python.cartridge import Cartridge from sms_python.system import System system = System(SMS2, Cartridge.from_file(Path("game.sms"))) frame = system.run_frame() # rows of RGB888 ints audio = system.board.take_samples() # (left, right) pairs, 0.0-1.0 from sms_python.state import load_state, save_state state = save_state(system) # the whole machine as bytes, between two instructions load_state(system, state) # ... and back: the run continues exactly as it would have ``` | Module | What | | --- | --- | | `system.py` | z80-python's `Z80CPU` on a board: INT from the VDP, pause as NMI | | `board.py` | sixteen machines as data; memory map, $3E slot switching, port decode; the FM unit's ports | | `vdp.py` | TMS9918A modes 0-3 and Mode 4; SMS1/SMS2/GG/TMS and Mega Drive variants as data | | `audio.py` | the PSG's and the FM unit's samples box-filtered to the host's rate (the chips are sn76489-python and ym2413-python, cores of their own) | | `cartridge.py` | the Sega mapper, Codemasters, Korean, MSX-style, SG-1000 RAM, EEPROM and other boards; board detection from MAME's software lists | | `io.py` | ports $3F/$DC/$DD, region, TH latch; Game Gear ports $00-$05; the SC-3000's keyboard | | `sf7000.py` | the SF-7000: IPL ROM switch, 8255, uPD765A disk controller and a turning disk | | `state.py` | save states: the whole machine as bytes and back, and a rewind buffer made of them | | `png.py`, `wav.py` | frames and audio out, stdlib only | | `frontend.py` | the desktop window (pygame-ce, the `frontend` extra) | The documents are indexed in [docs/README.md](docs/README.md); the newest handoff there says where the work stands. ## Why one repository for all of them The SG-1000 is not a prerequisite project: it is the first milestone. Its video chip, the TI TMS9918A, lives on inside the Master System's 315-5124 as modes 0 to 3, next to Sega's new Mode 4. That is how a Mark III or an SMS1 runs SG-1000 cartridges. The Game Gear keeps the same chip with a 12-bit palette and a smaller window. The SC-3000 is an SG-1000 with a keyboard, and the Mega Drive's video chip still has Mode 4 inside. So the machines share a VDP, a PSG and a Z80, and differ in tables: | | SG-1000, SC-3000 | Mark III / SMS1 | SMS2 | Game Gear | Mega Drive + converter | | --- | --- | --- | --- | --- | --- | | Video | TMS9918A (modes 0-3); TMS9929A on the PAL SC-3000 | 315-5124 (modes 0-3 + Mode 4) | 315-5246 (adds 224/240 lines, loses the SMS1 mask quirks) | inside the 315-5378 ASIC (Mode 4, 160x144 window, 12-bit CRAM) | 315-5313 in Mode 4 (no TMS modes, its own timing) | | Sound | SN76489AN, discrete, 15-bit noise LFSR | SN76489 inside the VDP, 16-bit LFSR | same | same, stereo (port $06) | the PSG inside the 315-5313 | | Z80 | NMOS | NMOS | NMOS | CMOS (inside the ASIC) | NMOS (the Mega Drive's sound CPU) | | RAM | 1 KB at $C000, mirrored; 2 KB on the SC-3000; 64 KB with the SF-7000 | 8 KB at $C000, mirrored | 8 KB | 8 KB | 8 KB | | Media | cartridges up to 48 KB, no mapper (a few boards bank-switch or add RAM); SF-7000 disks | Sega mapper; Codemasters and Korean boards | same | Sega mapper; Codemasters; EEPROM carts | Master System cartridges | ## What is here | Path | What it is | Committed | | --- | --- | --- | | `docs/` | The documents listed in `docs/README.md` | yes | | `scripts/fetch_references.py` | Rebuilds `reference/` and `tests/test_roms/` from the pins | yes | | `scripts/pinned_files.json` | 179 files: URL, SHA-256, size, kind, licence | yes | | `scripts/pinned_repos.json` | 11 git repositories at pinned revisions | yes | | `scripts/mame_trace.py` | The MAME 0.285 trace and I/O-log recipe, verified | yes | | `reference/` | Fetched manuals, SMS Power documents and wiki, forum threads, source trees (492 MB) | no | | `tests/test_roms/` | Fetched test and diagnostic cartridges from SMS Power | no | | `scripts/compat_sweep.py`, `romsets.py` | Every cartridge and disk of the local romsets on MAME and on ours, frames compared | yes | | `tests/mame_traces/` | MAME runs | no | | `tests/compat/` | The sweep's per-cartridge records and MAME's snapshots | no | python scripts/fetch_references.py # fetch everything missing python scripts/fetch_references.py --verify # check the pins, fetch nothing ## Licence MIT for what this repository contains. The fetched material keeps its own terms (recorded per file in `scripts/pinned_files.json` and per repository in `docs/validation.md`) and is never committed. Behaviour taken from MAME's BSD-3-Clause source files is credited in [NOTICE](NOTICE). GPL, LGPL and non-commercial emulators are never read while writing code; they may be run as closed boxes to find differences. The sound chips are cores of their own, sn76489-python and ym2413-python, both MIT and written from documents ([docs/provenance.md](docs/provenance.md) has the licence audit).