Repository navigation
v1
SegaSonic The Hedgehog decompilation, v1.0
Sega's 1993 arcade game SegaSonic The Hedgehog (Sega System 32), decompiled whole into readable,
platform-agnostic C. The V60 game program and the Z80 sound driver are both C now, and they run natively,
with no CPU emulation. The decompiled game was run through 98 test scenarios, each also played by the
original program on MAME's V60 and Z80 cores. In all of them it matches the original at the end of every
frame: memory, picture (pixel for pixel) and sound.
As with n64decomp/sm64, there is no game data here. You bring your own
ROM set; the tools verify it, extract the assets and build your own executable.
What's in it
The game, as C (src/)
- 1,783 functions (game and sound driver), about 89,000 lines of C, in files by subject: system, video,
engine, stage, player, each stage's objects, scenes. - No
goto, nosub_orunk_names, and a comment above every function saying what it does in the
game's terms. - Every function, variable, table, structure field, parameter and board register has a name in
symbols.txt, with the evidence for it. Every byte of the program ROM is code or named data. - 46 structures in the game (objects, players and each object type's own variables, floor blocks,
animation steps...) and 7 in the sound driver. - The code nothing in the program calls is decompiled too (about 14 KB): an LZ unpacker, a packed picture
decoder, object types never spawned, earlier versions of live routines.
The sound driver, as C (src/sound/)
- The Z80 program, decompiled with the same decompiler and named the same way (
sound_symbols.txt). It
runs on the board's time, and the YM3438 and RF5C68 are libraries. Among its unused routines is a driver
for a MIDI port the System 32 board doesn't have.
Verified frame by frame
tools/check_scenarios.py --video --soundruns 98 control scripts (attract mode, games for one, two and
three players, every stage, the ending, the test mode) on the original program and on the C. Every frame
it compares work RAM, video RAM, sprites, palette and mixer, then the picture and the sound.- The scenarios run 43,042 of the program's 47,575 instructions. 3,908 of the rest are code nothing calls,
anddocs/unreachable.txtsays why no scenario reaches each of the other 625. tools/check_structure.pychecks that the C's control flow does what the original's jumps do, including
the code no scenario reaches.
The board (runtime/)
- A System 32 model: memory map, interrupts and timers, I/O, EEPROM, trackballs, and the FD1149 level-load
protection. - A video library for the four tilemaps, the text and bitmap layers, the sprites and the mixer. It matches
MAME's System 32 video pixel for pixel. - YM3438 and RF5C68 libraries that make the same samples as ymfm and MAME.
PC port (ports/sdl2/)
- The game in a window, with sound, on SDL2. The mouse is the trackball and keeps rolling when you let go,
as the arcade's does. The keyboard works too, and each game controller is a player. - Full screen, smooth or sharp pixels, and settings in
sonic.ini, which the game reloads while it runs.
Assets you can edit (extract_assets.py)
- 6,138 sprite frames in 291 PNG sheets, by character and object, and 16 tile sheets.
- The program's data as text: palettes, stage scripts, spawn lists, animations, texts, the demos' inputs.
- The music and every sound effect and voice as MIDI, and the 175 PCM samples as WAV.
- Everything builds back into your ROM byte for byte (
--check), and the build takes what you change. Edit
a sprite, a palette, a spawn list or a song, build again, and it's in the game.
Tools
- A V60 disassembler identical to MAME's at every byte offset of the program ROM, and a Z80 one identical to
MAME's for the sound program. - The decompilers (
tools/decompile.py,tools/decompile_sound.py) and the reference runner (s32ref). - Coverage, and
tools/noise_report.py, which counts what is left of the CPU in the C.
What's not in it
- No game data and no executable. You need MAME's
sonicset (Japan rev. C). The executable you build
carries data from your ROM: it's yours, not one to share. - Other platforms, not yet. It has been built and played on Windows. SDL2 should build it on Linux and
macOS, but that hasn't been tried. The next work goes in a repository of its own, so that this one stays
the faithful reference:- ports to Switch, Android and Linux ARM handhelds (Anbernic);
- a console version with no coins, a settings menu and a higher resolution.
- Some of the CPU is still in the C. These leftovers stay because taking them out would make the C do
something the original doesn't: results copied back, memory reached through computed addresses, partial
register writes. The README counts each kind and explains it under "What is left, and why".
Getting started
Requirements: Python 3.10+, CMake 3.20+, a C11 / C++17 compiler, and SDL2's development files.
python tools/verify_rom.py
python extract_assets.py
cmake -S . -B build/cmake
cmake --build build/cmake --config Release
build/bin/sonicCoin with 5, start with 1; the mouse is the trackball. The README has the details.
Thanks
- MAME: the reference tools are built on its V60 and Z80 cores, its
System 32 video and its RF5C68. ymfm provides the YM3438. - n64decomp/sm64, for showing how a decompilation can ship without the
game's data. - SDL2.
SegaSonic The Hedgehog is a trademark of SEGA. This project is not affiliated with or endorsed by SEGA.