Skip to content

About

Complete byte-for-byte matching decompilation of Ape Escape (PS1, PAL) in C

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Latest commit

 

History

5,470 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ape Escape Decompilation

The source code of Ape Escape (PlayStation, Europe, SCES-01564), rebuilt in C.

This is a complete decompilation, made for preservation and study. Every function on the disc is accounted for: the game's own code is written in C that compiles back into the exact same bytes as the original, and the build checks every image byte for byte. The Psy-Q library code is linked from the SDK when you have it, and assembled from the disassembly when you don't. Six functions stay handwritten assembly, because no compiler produces their instructions.

The game's disc, graphics, music and levels are not included: you need your own copy of the game. The source does contain the small tables, text and debug fonts that were part of the original program's code.

This is an unofficial fan project.

Progress

Decompilation progress

file what it is functions decompiled Psy-Q SDK in C Psy-Q SDK handwritten asm done
SCES_015.64 Main game executable 1,908 1,472 0 435 1 100.0%
KKIIDDZZ.BNS Stages, menus and other game modes 2,204 2,162 0 38 4 100.0%
MINI1.EXE Ski Kidz Racing minigame 395 202 0 193 0 100.0%
MINI2.EXE Specter Boxing minigame 399 216 0 183 0 100.0%
MINI3.EXE Banana dance minigame (not launched by the PAL game) 308 81 138 88 1 100.0%
whole disc 5,214 4,133 138 937 6 100.0%

Names

How each name was settled: by testing it in the running game, from the Psy-Q SDK or the developers' own text, or from the code.

Names verified

names proven in game outside source proven by code seen running code reading only unnamed verified never referenced
functions 990 1,133 3,188 3 0 8 99.7% –
struct fields 69 416 2,300 0 10 103 96.1% 361
globals 235 13 2,259 0 3 229 91.5% –

What you need

  • macOS or Linux. On Linux the old compilers are 32-bit x86 programs: any x86-64 Linux runs them; on ARM Linux, install qemu-user-static first.
  • The basics:
    • macOS: xcode-select --install
    • Debian/Ubuntu: sudo apt install git curl python3 build-essential zlib1g-dev xz-utils m4
  • Your own PAL disc of Ape Escape, dumped as BIN/CUE.

Add your game

Put the dump in a folder called Ape Escape (Europe) at the top of the repository:

Ape Escape (Europe)/
├── Ape Escape (Europe).bin
└── Ape Escape (Europe).cue

The .bin should have this SHA-1: abe619ea253b6af1ceee54d31499f0db2919915d. The folder is ignored by git.

Install the tools (one time)

bash tools/setup_toolchain.sh

This installs the old PlayStation compilers, a MIPS assembler and linker, a disassembler and a MIPS debugger into ~/ape-tools (set APE_TOOLS to use another folder, for this script and for build.sh). It needs no sudo, takes 5 to 15 minutes (most of it building binutils and gdb) and about 300 MB. Every download is pinned to an exact version and checked against a hash before it is used.

Build

bash build.sh all

The first build extracts the game's code from your disc into orig/ and disassembles it into asm/; then it compiles every C file, links each code image and compares it with the original. It ends with:

every image is byte-identical

bash build.sh builds the main executable only; bash build.sh mini1 (or mini2, mini3, bns.<OFFSET>) builds one other image. Each build compares its output with the original disc file and with the retail hashes in tools/data/image_hashes.json, and fails on any byte that differs. python3 tools/ape.py image-hashes checks your disc dump and the files extracted from it against the same record.

The Psy-Q SDK is not included. Without it, its library code is assembled from the disassembly, and the result is still byte-identical. If you have the Psy-Q 4.5 SDK, put its LIB folder at ~/ape-tools/psyq/v45/LIB (or set PSYQ_LIBS) and the build links the SDK's own objects instead.

Play it

bash tools/run.sh

This copies your disc into build/Recomp Ape Escape (Europe)/ with the rebuilt executable in place of the original, and boots it in PCSX-Redux (on macOS, /Applications/PCSX-Redux.app; set REDUX if it lives elsewhere). python3 tools/ape.py build-disc makes the same disc copy without starting the emulator, for any emulator you like. The PAL disc is copy-protected (LibCrypt): for the copy to boot, keep your dump's .sbi file in the Ape Escape (Europe) folder, and it is copied along.

VS Code and debugging

Open the folder in VS Code with the C/C++ extension (ms-vscode.cpptools).

  • Cmd/Ctrl+Shift+B runs PS1: Build all images.
  • The PS1: Run in PCSX-Redux task builds the main executable and boots it.
  • F5 starts PS1: Ape Escape source debugger: it builds the debug symbols (APE_DEBUG=1 bash build.sh, written to build/debug/), starts PCSX-Redux with its GDB server (bash tools/debug.sh --server), and attaches gdb. Breakpoints set in src/game/ (the main executable) stop in the running game, and the debugger shows the source, locals and structs.

Source debugging needs macOS with PCSX-Redux in /Applications, and the toolchain in ~/ape-tools (the launch configuration looks for gdb there). From a terminal, bash tools/debug.sh does the same and leaves you at a gdb prompt.

Where things are

Folder What it holds
src/game/ The main executable SCES_015.64: the engine, by subsystem (player/, monkeys/, enemies/, camera/, collision/, ui/, sound/ ...)
src/overlays/ The 50 code blocks loaded from KKIIDDZZ.BNS: the title, menus, cutscenes and each stage's room code, one folder each
src/minigames/ The three minigame executables: ski_kidz_racing/ (MINI1), specter_boxing/ (MINI2), banana/ (MINI3, which the PAL game never launches)
include/ The shared headers: structs, constants, and the Psy-Q library declarations
tools/ setup_toolchain.sh, run.sh and debug.sh are the scripts to run; ape.py is the build's helper, and data/ and toolchain/ hold what it and the installer read
.vscode/ Build, run and debug tasks

A file named after a function (Ski_PlayBGM_8011E9FC.c) holds that one function. A file with a plain name (camera.c, dark_ruins.c) holds a whole run of functions in their original order: one of the developers' original source files, recovered from how the linker laid them out. Its header says what it covers and how it was told apart.

Spotted a wrong name or comment? Please open an issue with what you saw.

About

Complete byte-for-byte matching decompilation of Ape Escape (PS1, PAL) in C

Topics

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages