# WWF No Mercy (decompilation) A matching decompilation of WWF No Mercy for the Nintendo 64: C source that the game's own compiler (KMC GCC 2.7.2) turns back into exactly the original machine code. All 3466 functions build from C files: the main program and the five overlays. 3426 of them (98.8%, 99.5% of the code by size) are C that the compiler turns into exactly the original code. The other 40 were written in assembly in the original; their files hold those instructions in an `asm()` block (see Notes). `build.py` rebuilds the game from this tree and checks it against your original, byte for byte. The project was started with [decompdecomp](https://github.com/n-delic/decompdecomp), which split the game and matched the first functions. The matching work is now carried on and will be finished by Claude (Anthropic's AI model), working function by function in this repository. decompdecomp is no longer used to match; the build still borrows its compiler, binutils and Python packages (see Build). ## Build 1. Put your own copy of the game here as `baserom.z64` (it is in `.gitignore`; never commit it). The tree matches the copy with SHA-1 `91cee3d096f4a76644d8b35b9aead6448909abd1`. 2. `python3 build.py`. It uses decompdecomp's pinned period compiler (KMC GCC 2.7.2), its binutils and its Python packages (`decompnet`), so decompdecomp must be installed. The system `python3` does not have those packages, so `build.py` re-runs itself under decompdecomp's own Python. It finds that Python from the `decomp` command (the first line of `~/.local/bin/decomp`, or the `python` it launches) and, if `decomp` isn't on `PATH`, from the `uv tool install` location `~/.local/share/uv/tools/decompnet/bin/python`. If neither exists it stops with "install decompdecomp first". If `build.py` can't find decompdecomp's Python, run it with that interpreter directly, for example `~/.local/share/uv/tools/decompnet/bin/python build.py`. ## The game's parts The game is a main program plus five overlays. An overlay is a block of code and data that stays on the cartridge until the game needs it, and is then copied into memory. **Why the game has them.** The console has 4 MB of memory (8 MB with the Expansion Pak), and the game's code alone is about 1.4 MB, before any wrestler, arena or sound is loaded. So only main, with the code every part of the game needs, is always in memory. The rest is loaded for the part of the game that is running, and the next part's code is later loaded over the same memory. **Why they matter here.** Because overlays take turns in the same memory, two functions in different overlays can have the same address. A tool that reads the game as one flat program gets this wrong: it files one overlay's code under another's addresses and cuts functions in pieces. The map below comes from the game's own table of overlays (ROM offset 0x539A0), and every function is built and checked inside its own overlay. | Part | ROM | Loads at | Functions | In C | | :-- | :-- | :-- | --: | --: | | main | 0x001060-0x057270 | 0x80000460 | 966 | 926, and 40 in `asm()` | | overlay 1 | 0x057310-0x081210 | 0x800D9960 | 404 | 404 | | overlay 2 | 0x081210-0x0AE390 | 0x80106760 | 213 | 213 | | overlay 3 | 0x0AE390-0x0FD250 | 0x80106760 | 656 | 656 | | overlay 5 | 0x0FD250-0x144BE0 | 0x80106760 | 327 | 327 | | overlay 4 | 0x144BE0-0x1BD150 | 0x800D9960 | 900 | 900 | There are two memory slots. Overlay 1 sits in the first (0x800D9960) and is loaded together with one of overlays 2, 3 or 5, which share the second (0x80106760). Overlay 4 is large and takes the place of both. ### What each part does This is read from the decompiled code and from the text each part holds. Some of it is still inferred, and most functions are still named only by their address. | Part | What it does | What shows it | | :-- | :-- | :-- | | main | Always in memory. Holds the code the other parts share, and the loop that chooses the next overlay. | `func_80026888` is the loop, `func_8000073C` loads an overlay, `func_800007AC` starts it. | | overlay 1 | The front end's framework. It runs the frame loop for whichever of overlays 2, 3 or 5 is loaded beside it, draws text, loads textures, and holds the shared wording (match types, championships, options, records). | `func_800D997C_ovl1` sets up, then runs `func_800DAAE8_ovl1` until that returns something other than 0. Each pass calls 0x80106798, where overlays 2, 3 and 5 each have a function. `func_800DCB14_ovl1` draws text. | | overlay 2 | The opening, and the scenes between matches (story and entrance scenes). It is the first to run at power-on and makes the power-on checks. | `func_80106944_ovl2` reads the console's TV type and reset type and tests whether Start is held. The text has wrestler names and the Rock's catchphrase. | | overlay 3 | The menus, the results page and the tournament brackets. It decides whether a match, a scene or the editor comes next. | `func_80108C54_ovl3` runs the menus (`D_800457A4` is the current one) and turns the exit code `D_800457A0` into the flags below. `func_80126624_ovl3` draws the bracket page. | | overlay 5 | Create-a-superstar (appearance, moves, parameters), the shop, and save data: the Controller Pak, and the Transfer Pak with the Game Boy version. | Its text names all of these. When it closes it hands back to the menus (`func_80106818_ovl5`). | | overlay 4 | The match itself, with the computer-controlled wrestlers. It has no text of its own. | `D_80159990` holds the wrestlers (0x358 bytes each) and `D_801596C0` a "brain" for each (0xB0 bytes). | **How the next overlay is chosen.** The main loop goes round forever. Each time, it loads overlay 1 and then looks at the flags in `D_800A10B0`, which the part that ran last has set. They are tested in this order: | Flag | Effect | | :-- | :-- | | 0x01 clear | run overlay 2 | | 0x08 clear | run overlay 3 | | 0x40 set | run overlay 5 | | 0x02 clear | run overlay 4 | The flags start at 0, so overlay 2 comes first at power-on. The menus (overlay 3) then leave one of these: | Value | What follows | | :-- | :-- | | 0x01 | the match, then the menus | | 0x18 | the match, then overlay 2 | | 0x02, 0x06, 0x12, 0x22 | overlay 2 (0x12: overlay 2, the match, and overlay 2 again) | | 0x40 | overlay 5, then the menus | | 0x03 | the menus again | **How a computer-controlled wrestler works.** `func_800F03F0` fills in what the brain knows (the distance and angle to the opponent, the distance to each corner, both wrestlers' states) and then calls one of the routines in the table `D_80145A10` (145 entries). The routine answers with button presses: `func_8010838C`, for example, walks toward the opponent by pressing the directional pad. `func_80118368` copies those presses to the place where the match code looks for the controller. `func_800FCEB4` starts another routine, and timers return a brain that is stuck to routine 0. A function in main or overlay 4 is named `func_
`. In the other overlays the name ends in `_ovl` (`func_80112920_ovl3`), so that no two functions share a name. Each overlay's data is still kept as one binary block. 43 functions in main have English names instead: mostly libultra names such as `bcopy`, `osGetCount` and `_Printf`, and a few others such as `main` and `bits_read`. The names came in with decompdecomp's export and are kept as it gave them. Each of those files has a comment with the function's `func_
` name, which decompdecomp's split assembly used for all of them but `main`. `splat.yaml` holds this map. After changing it, run `python3 tools/make_map.py` (with decompdecomp's Python, as for `build.py`): it runs splat and rewrites the overlay lines of `symbol_addrs.txt`, `build.json` and `objdiff.json`. Then `python3 build.py` must say *identical* again. `python3 tools/check_rodata.py`, run after a build, checks what *identical* cannot: the jump tables and constants that the compiler makes for a C function. It must report 0 not found. When it lists a function, its `switch` sends some value to the wrong body: `python3 tools/relabel_switch.py NAME` renumbers the cases from the game's own table. ## Layout | Path | What it is | | :-- | :-- | | `src//.c` (`main`, `ovl1` to `ovl5`) | Decompiled functions, one per file, each matching the original. | | `asm/nonmatchings/` | Assembly of every function (the target for the decompiled ones). | | `asm/data/`, `asm/*.s`, `assets/` | Data, start-up code and binary parts, still as assembly or binary. | | `include/common.h` | Types, structs and prototypes worked out so far. | | `game.ld` | Places every function and data block at its original address. | | `build.json`, `build.py` | How each file is built, and the build. | | `objdiff.json` | Per-function units for [objdiff](https://github.com/encounter/objdiff) (`python3 build.py targets` makes their targets). | To match a function: write it in C under `src//`, run `python3 tools/make_map.py` so that `build.json` lists it, then `python3 build.py`. When it says *identical*, the function matches. This is the base for ports and mods: the game can be rebuilt and changed. A port to another platform needs its own version of the 40 hand-written routines. The game's code and data belong to their rights holder: keep this tree to yourself unless you have the right to share it. ## Notes - **The 40 hand-written routines.** They are all in main: the exception handler and thread switching, the coprocessor, cache and TLB access, `bcopy`, `bzero` and `bcmp`, libultra's matrix routines and the compiler's 64-bit arithmetic helpers. They were written in assembly in the original, so there is no C to get back. Each file explains what the routine does, who calls it and what is inferred, and has two versions: under `#else`, the original instructions in an `asm()` block, which is what the build uses; under `#ifdef NON_MATCHING`, a C version for reading. The build never defines `NON_MATCHING`. Each C version was run against the game's own instructions in an interpreter, with random and edge-case inputs, and no test found a difference; nothing was run on a console, and a file says so where its C version has limits. Where an instruction has no C form (coprocessor, cache, TLB) the C version uses one-line inline assembly. Ten of the C versions also compile to the original bytes, but eight need that inline assembly and two need variables pinned to registers. To count real C, leave these 40 out (`grep -l '^asm(' src/main/*.c` lists them). - Some of the 40 have oddities, which the C versions keep. `func_8003FDF8` (64-bit division) gives a quotient one too small when the dividend is the divisor shifted left; `func_80040190` (float to 64-bit integer, never called) uses whatever is left in a register for very large values; `func_80040280` (64-bit integer to float) cuts off instead of rounding; and one path of `__osDisableInt`, which looks unreachable, seems to read from the wrong address. - **Cleanup still going on.** The C files were rewritten without `goto` and without the names left over from the first machine decompilation (`temp_*`, `var_*`); a file changed only when its compiled code stayed the same. No such names are left. 12 `goto` statements in 7 files are left: written without them, the compiler makes different code. Each has a comment that says what was tried and why the goto stays. - `func_80026BE0` (libultra's `osDriveRomInit`) was compiled with IDO 5.3, not KMC GCC: the register moves and delay slots in the ROM are ones KMC GCC cannot make. `sqrtf` needs `-ffast-math`. `build.json`'s `compiler_ranges` gives those two functions their own compiler or flags; everything else uses KMC GCC 2.7.2. - A source file can hold several functions that follow one another: give each the same `"c"` in `build.json`, and the object is placed once, at the first function's address. `src/ovl4/func_8012ECD4.c` also holds `func_8012ED0C`: compiled apart, the assembler pads the first `mul.s` of `func_8012ED0C` with a `nop` the game does not have.