Skip to content

Releases

v1.0.1

Choose a tag to compare

@cheviman cheviman released this 19 Oct 22:08

v1.0.1

Fixes how the extracted sprite sheets look. The game, its code and the bytes the assets build back into are the same as in v1.0.

Fixed

  • Bars across the players' sprites. The extractor showed every 4-bit frame as if the board drew it non-opaque. In that mode, a pen 15 at the edge of a word is an end code and cuts the row. The players, and many other objects, are drawn opaque (sprite entry bit 8). There pen 15 is an ordinary color, so the sheets had see-through bars wherever a 15 fell at a word's edge (for example sonic/alternate_run). The sprite census now records which frames the game draws opaque, and the sheets draw them whole.
  • Stripes on the 8-bit sheets. ending/big_explosion is drawn non-opaque, and its end codes (pen 255) showed as vertical stripes. They are see-through now.

Updating

Run python extract_assets.py again. The manifest version changed, so it re-extracts every asset, which overwrites anything you edited in assets/: keep a copy of your changes first. python extract_assets.py --check still gives all 18 chips identical.

v1

v1

Choose a tag to compare

@cheviman cheviman released this 19 Oct 21:38

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, no sub_ or unk_ 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 --sound runs 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,
    and docs/unreachable.txt says why no scenario reaches each of the other 625.
  • tools/check_structure.py checks 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 sonic set (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/sonic

Coin 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.