Metadata-Version: 2.5 Name: sms-python Version: 0.1.0.dev0 Summary: Readable, pure-Python SG-1000, Master System and Game Gear emulator built on z80-python Author: alewman License: MIT License Copyright (c) 2026 alewman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. License-File: LICENSE Requires-Python: >=3.11 Requires-Dist: sn76489-python @ git+https://github.com/alewman/sn76489-python@b0acbc863c87259b3bb120df1a5886980462e25c Requires-Dist: ym2413-python @ git+https://github.com/alewman/ym2413-python@13df60c31cf6007a378e65d21e6a52a323c15d1c Requires-Dist: z80-python>=0.4.0 Provides-Extra: dev Requires-Dist: pytest>=8; extra == 'dev' Requires-Dist: ruff==0.16.8; extra == 'dev' Provides-Extra: frontend Requires-Dist: pygame-ce>=2.5; extra == 'frontend' Description-Content-Type: text/markdown # sms-python A readable, dependency-free, pure-Python emulator of **Sega's 8-bit Z80 consoles**: the SG-1000 (and SC-3000), the Mark III and Master System (SMS1 and SMS2 video chips), and the Game Gear. 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-09-21): the SG-1000, Master System and Game Gear run on z80-python 0.4.0.** Milestone 1 passes: ZEXDOC and ZEXALL (ZEXALL-SMS v0.21) report 79 of 79 tests OK on the SMS2 board. 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, **918 of 928** runnable Master System cartridges, **532 of 562** Game Gear and **203 of 209** 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. 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 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, anything else on an export Master System II (`--machine` picks another, `--bios` boots through a BIOS image). 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 | | 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. ## 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 ``` | Module | What | | --- | --- | | `system.py` | z80-python's `Z80CPU` on a board: INT from the VDP, pause as NMI | | `board.py` | nine 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 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](https://github.com/alewman/sn76489-python) and ym2413-python) | | `cartridge.py` | Sega and Codemasters boards; board detection from MAME's software lists | | `io.py` | ports $3F/$DC/$DD, region, TH latch; Game Gear ports $00-$05 | | `png.py`, `wav.py` | frames and audio out, stdlib only | | `frontend.py` | the desktop window (pygame-ce, the `frontend` extra) | What is here: - a primer on the three machines; - the display and interrupt timing, with where each number came from; - the hardware quirks that separate the chip revisions; - every oracle found, ranked by tier and pinned; - a fetch script that rebuilds the reference library from those pins; - a working, verified recipe for MAME traces of the Z80 and the I/O bus; - a handoff brief with milestones. See [docs/README.md](docs/README.md). ## Why one repository for three consoles 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. So the machines share a VDP, a PSG and a Z80, and differ in tables: | | SG-1000 | Mark III / SMS1 | SMS2 | Game Gear | | --- | --- | --- | --- | --- | | Video | TMS9918A (modes 0-3) | 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) | | Sound | SN76489AN, discrete, 15-bit noise LFSR | SN76489 inside the VDP, 16-bit LFSR | same | same, stereo (port $06) | | Z80 | NMOS | NMOS | NMOS | CMOS (inside the ASIC) | | RAM | 1 KB at $C000, mirrored | 8 KB at $C000, mirrored | 8 KB | 8 KB | | Cartridge | up to 48 KB, no mapper (a few boards bank-switch) | Sega mapper; Codemasters and Korean boards | same | Sega mapper; Codemasters; EEPROM carts | ## 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` | 175 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 (478 MB) | no | | `tests/test_roms/` | Fetched test and diagnostic cartridges from SMS Power | no | | `tests/mame_traces/` | MAME runs | 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. Genesis Plus GX's licence forbids commercial use: read it, cite it, do not copy from it.