KI Recomp

ki-recomp is a native PC port of Killer Instinct for macOS and Linux that recompiles the game to C++ with sound and higher resolutions.

README

ki-recomp

Exploratory static recompilation of Killer Instinct (Rare/Midway arcade, 1994; MIPS R4600 little-endian) to C++, with macOS Apple Silicon as the first host and a portable runtime (C++20, CMake; SDL2 only for the windowed player).

Current state: the whole game is recompiled and plays natively, with sound, in a window or headless. It starts from the player's MAME set like the arcade board: the boot ROM is recompiled too and unpacks the game. Attract mode, coin, character select, fights, game over and high scores all work. The DCS sound board (an ADSP-2105 DSP) is emulated; its output matches MAME sample for sample. Real game code is translated automatically from MIPS to C++. The validated code is six functions (FPU code, a switch with a jump table, integer code) and the whole call subtree of a drawing routine, which writes to the framebuffer. All of it matches MAME bit for bit (CPU and FPU registers and all of RAM) on 52 captured calls, 35 of them made by the game itself. The translator handles 562 of the 567 functions reached by jal.

Play a release build (macOS, Apple Silicon)

  1. Download KIRecompiled-macOS-arm64.zip from the releases and unzip it.
  2. On first launch macOS blocks the unsigned app: open System Settings → Privacy & Security → Open Anyway, or run xattr -dr com.apple.quarantine /path/to/KIRecompiled.app.
  3. When asked, drag onto the window the folder with your MAME kinst set: kinst.zip (BIOS v1.5d) and kinst.chd, or a .zip that contains kinst.chd (extracted once into the preferences folder). The app checks every file against MAME's SHA1 and remembers the folder.

No game data is included: the app contains the recompiled program code and starts it from your ROM like the arcade board, from the reset vector. Settings and high scores the game writes to its disk are kept in ~/Library/Application Support/kilnan/KIRecompiled/. Keyboard and gamepad controls are listed under Play it.

Unofficial fan project, not affiliated with or endorsed by Rare, Midway, Nintendo or Microsoft.

Development

  • Findings, sources, open questions and next step: docs/TECHNICAL_NOTE.md
  • Proof results per case: poc/SUMMARY.md. The full report with the original instructions and the generated code is produced locally in poc/out/REPORT.md (ignored by Git).

Layout

pathwhat
tools/kirecomp/kirecomp.pyMIPS III (LE) -> C++ translator, decoder: rabbitizer
runtime/guest runtime: CPU context, memory map, arithmetic/unaligned helpers; board model (kinst_board), DCS sound board (dcs)
third_party/adsp2100/ADSP-2105 DSP interpreter derived from MAME (BSD-3-Clause)
src/ki_run.cpp, src/ki_play.cppheadless runner and SDL2 player of the whole recompiled program
tests/dcs_replay.cppDCS model against a MAME capture of sound commands and audio
tools/mame_capture.pycaptures CPU state and RAM around one call of a function in MAME
tests/compare_case.cppreplays a capture through the recompiled code and diffs everything
tests/mutation_check.pynegative control: injects translation bugs, checks they are caught
tests/runtime_selftest.cpphelper semantics on synthetic values (no game data)
poc/cases.txt, poc/run_captures.shthe capture cases of the proof
poc/make_report.pyregenerates poc/SUMMARY.md and the local poc/out/REPORT.md
tools/check_data.pyverifies your ROM files against MAME's hashes
tools/extract_game_code.shunpacks the game code from the U98 boot EPROM (kipack)
tools/mame_verify_extraction.pychecks the unpacked code against DRAM in MAME
tools/mame_code_coverage.pywhich guest code MAME executes (DRC block log), optionally while playing
tools/mame_inputs.pyscripted inputs (coin, start, random buttons), timed on emulated frames
tools/mame_run.pyruns MAME with a wall-clock timeout; never leaves orphaned emulators
data/, traces/, poc/out/, build/ignored by Git: game data, captures, game-derived reports and generated code stay local

Requirements

macOS (arm64)
xcode-select --install                 # Apple clang (C++20)
brew install cmake ninja python mame rom-tools   # rom-tools provides chdman
python3 -m venv .venv && .venv/bin/pip install rabbitizer==1.16.2

mips-linux-gnu-binutils (Homebrew) is optional, for objdump listings. Tested with Apple clang 21, CMake 4.4, MAME 0.289, rabbitizer 1.16.2.

Linux

Debian/Ubuntu: apt install g++ cmake ninja-build python3-venv libsdl2-dev, the same venv, and configure with -DKI_PYTHON=.venv/bin/python. Tested on Debian 12 arm64 (GCC 12, CMake 3.25) in Docker, image definition in tools/docker/Dockerfile.linux:

docker build -t ki-linux -f tools/docker/Dockerfile.linux tools/docker
docker run --rm -v "$PWD":/src:ro ki-linux sh -c \
  'cmake -S /src -B /build -G Ninja -DCMAKE_BUILD_TYPE=Release -DKI_PYTHON=/venv/bin/python && cmake --build /build && cd /build && ctest'

The Linux build gives the same audio and framebuffers as the macOS one, bit for bit (1200 frames of attract mode compared).

Game data (not included, never committed)

Put the MAME kinst set (BIOS v1.5d) in data/roms/kinst/:

filesizeCRC32SHA1needed now
ki-l15d.u98512 KiB7b65ca3d607394d4ba1713f38c2cb5159303cace9cde991eyes
u10-l1 ... u36-l1 (8 files)512 KiB eachsee tools/check_data.pyfor sound (and for MAME to start)
kinst.chd131 MB diskcontent SHA1 81d833236e994528d1482979261401b198d1ca53for the gameplay cases

Hashes are from MAME src/mame/rare/kinst.cpp (ROM_START(kinst)). Verify the CHD with chdman info -i kinst.chd. tools/make_blank_chd.sh creates an empty disk with the right geometry. MAME boots it, with a checksum warning, far enough for the boot-time cases, which are marked --blank-disk in poc/cases.txt and always use it.

python3 tools/check_data.py            # verify the files you supplied
tools/extract_game_code.sh             # -> data/kipack-out/rom-{0,1,2}.bin
tools/make_blank_chd.sh                # needed by the --blank-disk cases
python3 tools/mame_verify_extraction.py   # optional: rom-0 == DRAM in MAME

Build and run the proof

cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build build                     # recompiles the functions in KI_FUNCTIONS
poc/run_captures.sh                     # MAME captures -> traces/poc/ (see note on timing)
cmake -S . -B build && ctest --test-dir build   # one test per capture + self-test
python3 tests/mutation_check.py build traces/poc
python3 poc/make_report.py              # -> poc/SUMMARY.md, poc/out/REPORT.md

Capture times:

  • Boot-time DRC cases: seconds each.
  • Gameplay cases (--play, real disk): 1-5 minutes each.
  • Interpreter cases: cases that set registers (syn-*, except syn-fpu-*, which inject memory only) and the *-interp cross-checks use MAME's MIPS interpreter, whose boot is much slower.

The whole set takes roughly 30-40 minutes. ONLY=<regex> selects cases, JOBS sets parallelism (default 3; each MAME uses a full core) and OUT_DIR writes elsewhere, for example to check that captures reproduce. The function list is the CMake cache variable KI_FUNCTIONS.

Code reached by MAME during scripted play (needs the real kinst.chd):

.venv/bin/python tools/mame_code_coverage.py --out traces/coverage --seconds 180

Survey of the whole code image (translation coverage, no validation):

.venv/bin/python tools/kirecomp/kirecomp.py --image data/kipack-out/rom-0.bin \
    --vram 88000000 --discover --out build/survey/all.cpp

Run the whole program (headless)

cmake --build build also builds ki_run, the whole game recompiled (every entry point the translator finds) on a model of the board. It covers I/O, the IDE drive on a raw disk image, VBLANK, the COP0 timer and interrupts. It starts from the state the original boot ROM leaves when it enters the game, captured once in MAME:

.venv/bin/python tools/mame_capture.py --func 88000000 --seconds 30 capture --hit 1 \
    --entry-only --out traces/bootstate
chdman extracthd -i data/roms/kinst/kinst.chd -o data/kinst.img
build/ki_run --boot traces/bootstate --disk data/kinst.img --rom data/roms/kinst/ki-l15d.u98 \
    --frames 600 --dump-every 60 --out traces/run     # framebuffers as PPM

--boot rom instead starts at the reset vector: the recompiled boot ROM unpacks the game itself (no snapshot needed). Its state at the game's entry equals the MAME snapshot byte for byte. KI_TRACE_IDE=1 logs disk commands, KI_TRACE_VRAM=1 framebuffer flips.

Play it (SDL2 window)
build/ki_play --data data/roms/kinst     # folder with kinst.zip or its files, and kinst.chd

This is the release mode: start at the reset vector from the MAME set, with sound. Without --data, the last folder is used, or the window asks for one to be dropped onto it. For development, the old form still works: ki_play --boot traces/bootstate|rom --disk data/kinst.img --rom data/roms/kinst/ki-l15d.u98 [--sound-roms DIR] [--mute]. ki_run --sound-roms DIR --wav-out FILE records the audio of a headless run.

tools/package_macos.sh VERSION builds the portable app bundle in dist/ (SDL3 and sdl2-compat built from source for macOS 11, bundled; ad-hoc signed) and checks that it loads only its own libraries.

Player 1

keyaction
arrowsmove
A / S / Dhigh attack: quick, medium, fierce
Z / X / Clow attack: quick, medium, fierce
1start
5coin

Player 2

keyaction
I / J / K / Lmove
Q / W / Ehigh attacks
T / Y / Ulow attacks
2start
6coin

Gamepads (default: Xbox layout; first pad = player 1, second = player 2; G opens the setup screen to see which pads are connected, test them and rebind any input; bindings are saved in pad_map.txt in the preferences folder)

buttonaction
d-pad / left stickmove
X / Y / RBhigh attack: quick, medium, fierce
A / B / RTlow attack: quick, medium, fierce
Start (Nimbus: Menu)start
Back or LBcoin (LB for pads without Back, like the SteelSeries Nimbus)

Other keys

keyaction
Gcontroller setup: connected pads, live test, rebind every input (saved)
V16:9 / 4:3 (remembered; also --aspect 16:9): the sides continue the picture's edges, and on stages drawn from a wide panorama (6, 7, 8) show the real background, in HD
Ppause (H still works, to compare)
Rreset (restarts like switching the board off and on)
F6 / F7opponent / stage of the next fight: the game's, then each in turn (title bar; on a Mac keyboard hold Fn)
HHD sprites on/off (with --hd-pack DIR)
F5save the state (taken at the next frame)
F9load the saved state
F2test switch
Tabunthrottled (no sound while on)
Escquit
HD sprites and stages

ki_play --hd-pack DIR draws the game with upscaled sprites and stages. How a pack is made (extraction, SeedVR2 upscaling, packing) and what is done so far: docs/HD_PIPELINE.md. Packs are derived from the game and are not distributed.

Choosing opponents and stages

ki_play --opponent orchid,eyedol --stage 10,1 sets the coming fights in turn: the first is against Orchid on stage 10, the second against Eyedol on stage 1, and so on. Opponents can be given by name or number:

NumberCharacter
0jago
1combo
2spinal
3thunder
4glacius
5fulgore
6cinder
7sabrewulf
8orchid
9riptor
10eyedol

Stages are numbered 0–15. Of these, 6–9 are night variants of the rooftop and the game uses 6 for all four. The opponent applies to the fighter the game controls. In a two-player game only the stage is changed.

Debugging against MAME
  • build/ki_run --inputs FILE replays a scripted session; generate one with tools/make_input_script.py.
  • --call-log-from N logs function entries.
  • --dump-at ADDR[:N] saves the state at the N-th entry of a function, in the snapshot format.
  • tools/mame_snapshot.py takes the same kind of snapshot in MAME, mid-game. ki_run --boot starts from either kind of snapshot.
  • With -DKI_BUILD_TRACE=ON, ki_run_trace --trace-out writes one PC per executed instruction, to diff against a MAME trace log.
  • tools/mame_dcs_capture.py --seconds 150 --out traces/dcs_play records the sound commands of a MAME session and its audio at 31250 Hz; build/dcs_replay traces/dcs_play data/roms/kinst replays the commands on the DCS model and compares the output with MAME's, sample by sample.

License and game data

The code in this repository is released under the MIT License.

It covers only the code written for this project. Killer Instinct and every file derived from it belong to their rights holders and are neither included nor licensed here. That covers ROMs, the disk image, extracted images, MAME captures, the generated C++ (build/gen/) and the full report (poc/out/). You need your own copy of the game. Release builds contain the compiled recompiled code, as other static recompilation projects do, but no game data.

Third-party projects:

  • MAME (BSD-3-Clause for the files consulted) is used as documentation and as an external reference emulator. The ADSP-2105 core in third_party/adsp2100/ is derived from MAME's ADSP-21xx core and keeps its BSD-3-Clause license and copyright notice (Aaron Giles); see the README there. The DCS board model (runtime/src/dcs.cpp) is written for this project, following MAME's dcs.cpp as its specification.
  • N64Recomp (MIT) is used as documentation.
  • rabbitizer (MIT) is installed with pip.
  • ki-rom (AGPL-3.0-only): its kipack tool is built and run as a separate program by tools/extract_game_code.sh at build time. Release builds do not use it: the recompiled boot ROM unpacks the game.
  • libchdr (BSD-3-Clause) and the miniz it bundles (MIT) are fetched by CMake at a fixed revision to read kinst.chd and kinst.zip.
  • SDL3 and sdl2-compat (zlib) are bundled with the macOS app.

Apart from third_party/adsp2100/, no source code from these projects is included. Code reused from them must keep its original copyright notice. Code from ki-rom cannot be included without changing this project's license.