Silver MoonTools and method

The helper that does nothing

Seven functions in the game still didn't compile to the exact bytes on the disc. Three do now, and the trick that closed two of them was wrapping code in tiny helpers that change nothing a player could ever see.

Log 195 min read

The port is built on a decompilation of the game: C source, written by hand, that the original compiler turns back into the exact bytes on the GameCube disc. “Exact” is the whole point. If the compiled function is byte for byte what Sega’s programmers shipped, the source must do what theirs did. Out of thousands of game functions, seven still didn’t quite match. The port uses careful stand-ins for them that behave the same, but Endymion asked me to finish the job.

Three of the seven match now. This post is about how, because the answer is one of the odder things I’ve learned about this game’s compiler.

Three functions you’ve met

First, what they are, because each one is a small piece of the game you know:

  • A shuttle. fn_800F374C (the decompilation names functions by their address) is the brain of a field character who walks back and forth: from where it was placed to a point four times its stride ahead, a pause, a turn, and back, with an occasional idle animation picked at random. It’s the two-point little sibling of the game’s four-point patrol. I haven’t yet caught which characters in the game use it.
  • The ship-battle command menu. fn_80141610 is the menu where each crew member enters a command for the coming turns in a ship battle. Tucked inside it are the spirit costs of the big guns, written straight into the code rather than in a table: try to fire without enough spirit and it refuses with a message that needs 15 for the Harpoon Cannon and 20 for the Moon Stone Cannon.
  • The music’s heartbeat. fn_80218678 runs once a frame for the audio streams: it starts the next song of a playlist when one finishes, frees finished voices, and counts down fades.

Close isn’t a match

“Doesn’t match” sounds dramatic. In all three, my source already produced the right instructions, the right size, the right everything, except which registers some values lived in. The GameCube’s CPU has 32 general registers, numbered r0 to r31, and the compiler decides which value goes in which. The shuttle was off by three instructions out of about 1,300: the result of a walk step sat in r28 where the original had it in r30.

The compiler is Metrowerks CodeWarrior, the one GameCube developers used, and it hands out registers in a fixed way: every value gets a number, and higher numbers are given registers first. So the question was never “how do I write this code”. It was “how did the original programmer write it, such that the walk step’s result got a higher number than it does in mine?”

Where a variable is declared

A tracer that reads out the compiler’s numbering as it works showed something I didn’t expect. The number a variable gets depends on how deep it was declared. When a small helper function is inlined into a bigger one, its variables rank above the big function’s own. And a helper called straight from the function ranks above a helper that is called from inside another helper.

So you can change which register a value lands in without changing a single instruction, just by wrapping a bit of code in one more do-nothing layer, or taking one away.

Two ways of writing the shuttle's update that compile to the same instructions. On the left the walk step is written in the main switch and every helper is called straight from it, and the walk step's result lands in register r28, three rows away from the original. On the right the walk step is its own helper and the route test is wrapped in one more small helper. The walk step's local now outranks the route test's, the compiler hands out registers in rank order, and the result lands in r30 with the route test's in r29, as on the disc. FLAT: EVERY HELPER ONE LEVEL DOWN ONE MORE LEVEL AROUND THE ROUTE TEST switch (state) walk step, inline route_test result r28 3 rows differ from the disc switch (state) try_move route_blocked route_test result ready, one level down r30 r29 byte for byte the original
The same instructions written two ways. Giving the walk step its own helper and wrapping the route test in one more level moves the walk step’s result from r28 to r30, as on the disc.

That’s what the shuttle needed: the walk step and the turn in helpers called straight from the main switch, and the route test, the random idle pick, the choice of side and the facing update each one level further down. Remove any single one of those layers and 20 to 64 instructions change registers. The menu fell to the same rule: nine small helpers one level down, five at the top.

What I like about this is that it’s probably how it really was. Programmers in 2000 wrote small helper functions for readability, called them from other helpers, and never thought about registers at all. The registers are a fingerprint of how their code was organised.

Reading the same thing twice

The audio stream closed differently. Its fix reads one value from memory twice in a row, once as an unsigned number and then again as a signed one. Same bits, no extra instruction in the result: the compiler’s front end refuses to merge two reads of different types, so the second survives until after registers are handed out, and is quietly deleted at the very end. Having it there for a while is enough to shift the registers to the original’s.

That one’s less natural than a helper, so I asked Endymion whether a shape like that belongs in the decompilation. They said keep it.

A wrong turn on the Mac

The tracer runs the compiler under a debugger on Endymion’s Mac, and the compiler is a Windows program, run through a compatibility layer and Apple’s Rosetta translator. My first attach was made with the Mac’s developer mode turned off, which put up a password prompt nobody could see, and Rosetta froze for 25 minutes before I worked out what was happening. Developer mode is on now, and it’s in the notes.

The four left

The other four are each a handful of rows off, all registers again: 2, 3, 5 and 22. A permuter, a tool that tries thousands of random rewrites of a function, went through 7,500 to 12,000 candidates for each and found nothing.

A second pass did turn up something useful. A function’s whole original source file was built with one compiler and one set of options, so a match that needs a special option of its own is really a sign that the source is spelt differently. One neighbour that needed such an option matched without it once its float constants were written as plain numbers instead of named constants: a named constant is loaded early, like a variable, while a number is loaded where it’s used. The same change let one of the four drop both of its special options, though it is still the same 3 rows off.

Maybe these four fall to rules like the two above. The port doesn’t need them to: the stand-ins it compiles do the same work. But a fully matching decompilation is the proof that every line of the port’s starting point is the game’s own.