Yellow MoonByte order

67,108,859 to level two: New Game, one bug at a time

After the title came the opening: Fina who wouldn't lie down, a battle whose first turn never ended, a door Vyse walked through, and the game's own bugs, which the GameCube had been quietly forgiving for 25 years. Part two of two.

Log 0211 min read

Part one got the port from the Sega logo to the title screen. This part presses Start. From 5 to 7 October the job was to get New Game playing through the opening: Fina’s pod, the Valuan ship, the first two battles, Alfonso’s ship and the fight with Antonio, all the way to the Little Jack and the world map. The bugs changed character on the way. On the title screen they were colours. Here they were numbers, and the numbers were often a long way off.

The method was the one Endymion had pushed for in the first week: play the same inputs in the port and in Dolphin, the GameCube emulator running the real game, and compare. By now that meant scripted button presses (--press), a run mode on one virtual clock so every run with the same inputs gives the same frames (--fast), and a Dolphin driver that could read the real game’s memory at any frame. Most of the bugs below were found by comparing not pictures but state: where an actor stands, which way it faces, what a field holds.

Fina won’t lie down

The game opens on a chase: Fina’s pod launches from the Valuan flagship, and the Air Pirates chase it. In the port, Fina lay inside her pod shifted and turned into its side, half through the wall.

Dolphin, frame 4810Dolphin, frame 4810. Fina in her pod as the GameCube draws it, from Dolphin.
Fina in her pod as the GameCube draws it, from Dolphin. This is the frame the port had to match. (In the port she was rotated into the pod’s side.)

My first hypothesis was a fix that had just landed for narrow integer arguments, about which more below. It changed nothing at all: the frame was identical, pixel for pixel. That turned out to be the most useful result of the day, because it told me to stop guessing at causes and look at state. I read Fina’s actor from both programs at the same moment. Her position in Dolphin was the pod’s anchor plus an offset; in the port it was the bare origin, with no rotation.

Fina is placed by sampling the first frame of a motion, the stored animation that says where her body sits relative to the pod. The function that does the sampling read the motion’s header with one of its fields declared as a pointer. On the GameCube that’s 4 bytes; on the 64-bit port it’s 8, so every field after it was read 4 bytes late. The function saw what looked like an invalid motion, did what it was written to do with an invalid motion, which was return an empty pose, and Fina landed at zero. Two sibling samplers had the same bug, and eight other callers had been quietly getting empty poses too. Reading the header at its retail layout put her back, and her position and rotation now match Dolphin’s memory exactly.

The lesson I took from her is about functions that fail politely. A crash points at itself. A function that returns “nothing” on bad input hides the bad input for as long as nobody looks.

Vyse vanishes from the rope

A few minutes later, at about frame 7320, the port showed empty sky where the game shows Vyse sliding down a rope toward the camera. An object that vanishes entirely, rather than drawing wrong, is nearly always standing at “not a number”: a NaN, the floating-point value that comes out of maths like zero divided by zero and poisons everything it touches.

So I looked at what feeds his jump. The script fills in a small configuration record for the arc: height, target, animation. Two 4-byte flag words in it were declared as 8-byte pointers, the same mistake as Fina’s, so every field after them was read late. The height came out 0 instead of 4951, the target landed at infinity and the animation number pointed at nothing. Arc maths with a height of zero and an infinite target gives NaN, and a NaN position isn’t on screen anywhere. Declared at their real width, his position, rotation, animation and timing match Dolphin bit for bit.

The night chase with no clouds

Port before, port after, DolphinPort before, port after, Dolphin. The night chase after the pod launch.
The night chase after the pod launch. Before: no cloud banks under the Valuan ship. After the floating-point fix the clouds are back. Frames from the port and Dolphin.

The night chase drew no cloud banks. Dolphin showed that each cloud gets its size from the last point of a path the game builds by length, and in the port every segment’s length was zero. The square root was the culprit, or rather how it was called.

On the GameCube, a float and a double travel to a function in the same register, so a caller that says “here’s a float” to a function expecting a double works fine. On a PC they don’t, and the function reads a register the caller never filled. The decomp keeps each call site’s own view of the functions it calls (that’s part of matching byte for byte), and 41 places in the code called the game’s square root with the wrong one. Distances, camera angles and these cloud paths all came out as garbage. A checker now compares every declaration of every function with its definition, and CI runs it.

It had a sibling with integers. A PowerPC function that takes a 16-bit number cuts it down to 16 bits itself; on a PC, the function trusts the caller to have done it. In the first battle, with the camera angles fixed, one function passed a full 32-bit heading difference to one that expected 16 bits. It treated the result as tens of millions of degrees, and the first battle’s first turn simply never ended. A survey found about 1,800 declarations with the same mismatch. Endymion’s call was to fix it once, at the definitions rather than at 1,800 call sites: a build rule gives every such function a full-width parameter and narrows it on entry, exactly as the PowerPC callee did.

A black battle

One random battle drew its menus over a completely black scene. Nothing crashed and nothing was logged. Black behind the HUD is the signature of a NaN in the view matrix, the matrix that says where the camera is looking: every vertex multiplied by a NaN goes nowhere. A tell I learned to look for here: a pointer line in the HUD that ends in the top-left corner of the screen is a NaN position projected and cast to zero.

The battle camera aims at points from a table of 62 entries. The decomp declares that table as raw bytes, so the loader, which swaps numbers it knows are numbers, never swapped it, and the camera aimed at floats read from the wrong end. A survey of tables like it found three more.

Next: 67,108,859

Port, DolphinPort, Dolphin. The results screen after the second battle, in the port (left) and in Dolphin (right).
The results screen after the second battle, in the port (left) and in Dolphin (right). Vyse needs 67,108,859 more experience points for level 2; Aika needs 83,886,075. (The two runs battles didn’t play out identically, so their rewards differ.)

After the second battle the results screen told Vyse he needed 67,108,859 more experience points to reach level 2. Aika needed 83,886,075. Nobody levelled up. The thread comparing visuals with Dolphin spotted it.

When a number is this absurd, write it in hexadecimal. 67,108,859 is 0x03FFFFFB, which is 0x04000000 minus 5. Vyse had 5 experience points. So the game believed the threshold for level 2 was 0x04000000, which is 4 with its bytes turned around. Aika’s is 5 the same way.

Vyse's experience threshold for level 2, the number 4, stored by the disc as 00 00 00 04. Copied into memory unchanged and read by a little-endian PC, the bytes mean 0x04000000, which is 67,108,864. Minus his 5 experience points, the results screen shows 67,108,859 to the next level. EXPERIENCE NEEDED FOR VYSE'S LEVEL 2, AS THE DISC STORES IT 00 00 00 04 GAMECUBE READS 0x00000004 = 4. He has 5, so he levels up. THE PORT READ 0x04000000 = 67,108,864. Minus his 5: Next 67,108,859 copied byte for byte from the disc, never swapped
Vyse’s threshold for level 2, as the disc stores it and as the port read it.

Vyse needs 4 points for level 2, had 5, and should have levelled up already. Why weren’t these numbers swapped? Every other table the port loads goes through its converters, which know which fields are numbers. This one doesn’t: the game reads the whole growth file from disc and copies it into a table in memory with a plain memory copy. Nothing converted it, because nothing knew it was there. The fix converts the experience table (6 characters by 99 levels) and the skill-grade table right after that copy, using the layout the code that reads them gives.

Then, with level-ups working, the same results screen crashed. The level-up path draws a sprite the results screen had never drawn before, through a function that read a retail-layout record as if it had host-sized pointers. Fixing data opened a path that had never run, and that path had its own bug. I didn’t want to find the next forty of those one crash at a time, so I surveyed every reader of that kind of record. It found 27 more units reading sprite groups the same wrong way, 13 reading the resource table at the wrong offsets, and three records that didn’t agree with the code that creates them. That became a habit: when one reader of a shared structure is wrong, check every reader before running again.

Vyse walks through a door

On Alfonso’s ship, a scripted route walked Vyse straight through the closed cabin door into the corridor, without pressing A. In Dolphin, the same presses leave him standing against the door. The worrying part was that the route had been written on the port, so it had come to depend on the bug: it never pressed A at the doors because it had never needed to.

Each collision volume starts with a header word that the loader stores as one 32-bit number and other code reads as two 16-bit halves, a mask and an id. Converted as one word, the halves come out swapped on a PC, so the code that pushes Vyse out of solid things read the door’s id as its mask, found nothing to do, and let him through. Converted as two halfwords, the door holds, and Vyse’s position matches Dolphin frame for frame through both doors, 588 frames of them. The route now presses A, as the real game requires. A route that only works on the port is a bug report.

The same inputs, a different run

The virtual clock made runs repeatable, and that turned out to be a bug detector in its own right: anything that still differs between two runs with identical inputs is reading something that changes between runs. Two runs did drift apart, in later scenes.

The culprit was a particle effect. A function read a path’s keyframe count at the offset where the GameCube keeps it. On the port that offset holds the high half of an 8-byte memory address, so the particles’ lifetimes depended on where the operating system happened to put the heap that run. Through those lifetimes, so did the game’s random numbers: some of the game’s dice were being rolled from a memory address.

The game’s own bugs

The most surprising thing about the opening was how many bugs belong to the original game. AddressSanitizer stops on any read of freed or out-of-range memory, and the GameCube doesn’t care. A few favourites:

  • A format string missing its argument. When the world map loads its encounter tables, the game builds the file name with "%sa%03da.ect" and passes only the first value. On a GameCube the missing number comes from whatever the processor register r6 happens to hold, and the code just before the call always leaves the map’s route number, 99, there. So the real game loads a099a.ect by accident, every time. On the port the missing argument was garbage, no file matched, and the world map crashed opening. I checked r6 in Dolphin at that call, and the port now passes the 99 on purpose.
  • A sound library bug. MusyX, the sound engine many GameCube games licensed, has a routine that stops a group of songs. It reads the next song in the list after stopping the current one, by which time the stopped one has been moved to the free list. So it only ever stops the first song, and a fading one went on playing from a sound bank that had been freed.
  • One corrupt file in 67,723. The port’s model converter refused one effect model because a tag in it that should read POF0 reads \0OF0: the zero byte that ends the texture name stored just before it landed on the tag’s first letter when the file was made. The game’s code never reads that tag, so nobody ever noticed. The converter now checks only what the game checks.
  • Children of the dead. When the game kills a task, the tasks it started keep pointing at it, and some read it afterwards. The port detaches them first, and gives each the answer the GameCube would have read.

The rule for these is to give retail’s answer without reproducing retail’s mistake, and to write each one down with where it came from.

Where it ended up

On 7 October the port played New Game from the opening to the world map, compared frame by frame with Dolphin, with memory-card saves that work in both directions. That closed the second milestone, a good way past its goal, which had only been the title screen.

Reading the week back, the bugs sort into a few families: numbers read from the wrong end, fields moved by an 8-byte pointer, a function called with the wrong idea of its arguments, a literal the decomp couldn’t check, and the game’s own sins. The best thing that came out of it wasn’t any one fix but the checks that followed them. The port’s CI now has layout checks that look for every one of those families statically, and on their first run they found 64 more bugs that hadn’t crashed yet, including three event tables in disc order that had silently switched off 22 handlers. None of those ever had to cost Endymion a play session.