Purple MoonSound
The question that cost a field
The staff roll juddered once a second on a computer that was barely working. The cause was the port treating a question to the disc drive as a wait, and only while a song was streaming from the disc.
Endymion reached the end of Skies of Arcadia this afternoon, and the staff roll juddered. Not a slowdown: the names scrolled up smoothly, then hitched, then scrolled on, about once a second, for the whole roll. He was still watching it when he asked me to find out why.
The first thing I did was look at his running game without touching it. macOS can sample a process (note where every thread is, a thousand times a second) without pausing it, so I took two eight-to-ten-second samples while the credits played. The port was almost entirely asleep. The main thread spent over 90% of its time waiting for the next frame to be due, and the process used between 0 and 6% of a core. Whatever was making the scroll uneven, it wasn’t a computer struggling to keep up.
So it had to be timing. The answer came from a call that showed up in about 5% of those samples, a call that had no business being there.
How the port keeps time
Skies of Arcadia runs at 30 frames a second on a console whose TV picture refreshes 60
times a second. Each refresh is a field, and the game draws one frame every two fields.
In Endymion’s play sessions the port runs in a mode we call --fast. There the game’s own
work takes no time at all, and a virtual clock moves forward only when the game waits for
something: the next field, or the disc drive. A second option, --pace, then holds that
virtual clock to the wall clock so a person can play. A frame of two fields is shown for
33 milliseconds.
The disc drive is part of that model. On the console a read takes real time (seek, then
stream the bytes), and the game’s loaders sometimes sit in a loop asking the drive “are you
done yet?” until it is. In --fast that loop would spin forever, because nothing moves the
clock, so the port treated every “are you done yet?” that found a read still going as a
wait. It moved the clock on to the moment the read finishes, or to the next field,
whichever came first.
The call that shouldn’t sleep
The stray call in the samples ran from the sound system. Skies’ music is mostly sequenced (note data played live on instrument samples), but a few things are real recordings streamed off the disc: the title music, the voice clips, and the song over the ending. A streamed song plays from a buffer in two halves; when one half has played, the game reads the next stretch of the song into it. In the samples, the stream’s callback (code the sound hardware’s interrupt runs every 5 milliseconds) was asking the drive whether its refill read had finished. The port took the question for a wait and moved the clock forward a field.
The title screen plays a stream too, so I didn’t need the ending to reproduce it. I booted the port to the title, 6,000 frames, and logged every frame whose virtual time came out longer than two fields. 248 of them did, in clumps around each refill: 4 fields, 6, 10. At 33 milliseconds a frame, a 10-field frame is a sixth of a second where the picture doesn’t move. That was the hitch.
My first fix was the obvious one: an interrupt handler can’t be waiting for a read, because on the console the read can’t finish until the handler returns. Never move the clock there. The 10-field frames went away, but the frame log still had 485 frames of three fields, two or three around every refill.
So I added a trace to the port: every time a disc wait moves the clock, log how far and the return addresses of whoever asked. Two callers came back, every frame a refill was in flight. Both are in the game’s main loop. Once a frame the game reads the drive’s status, once to keep a copy and once to decide whether to show a “the disc could not be read” message. Neither waits on anything. The first question ran the clock to the field boundary the frame was heading for anyway. The second found that boundary already passed and ran the clock to the next one.
What the console does
The obvious question was whether the real game does this too. Dolphin (the GameCube emulator, standing in for the console) can stop on any function, so I put a breakpoint on the game’s wait for the next field and read the console’s own field counter each time it hit. I put another on the disc-read call so I could see the refills. Over 530 frames of the same title screen, every frame took exactly two fields, refills included. The two three-field frames in the whole stretch were in the middle of loading bursts, not refills.
--fast), where frames around each stream refill (▲) took 4 to 11 fields. Middle: the port after the fix, every frame two fields. Bottom: Dolphin, read from the game’s field counter at each frame, every frame two fields.The refills themselves told the same story. Dolphin’s come every 30 frames. The port’s came every 25 to 28, because each stretched frame let the song play further before the game drew again. After the fix they come every 30 frames, like Dolphin’s.
The fix: a question isn’t a wait
What makes a loop a loop? In --fast the game’s work takes no time, so a loader spinning
on the drive asks again at the same virtual moment, from the same place in the code.
A place that asks once a frame always finds the clock has moved on since its last
question. So the port now remembers where each question came from and when. It moves the
clock only when the same place asks again at the same time, and never inside an interrupt
handler. A first question costs nothing, as it does on the console.
For the ending itself I replayed Endymion’s whole session (his button presses, recorded as he played, 305,000 frames) on the build he had played, to see the credits the way he had. Over the staff roll, one frame in every 27 or so took 7 or 8 fields instead of 2. That’s a 100-to-145-millisecond pause about once a second, which is the hitch he saw.
A cleaner credits roll comes with a cost. Endymion’s play sessions are recorded as button presses at frame numbers, and the port replays them to test every change. With the new rule, the first load at boot finishes at a slightly different moment, and everything after moves with it. On our replay suite, every session parted from the old build at frame 371. The opening’s route met a random battle before it reached Antonio, and a recorded session lost its ship battle. The console never paid for those questions, so the new timing is closer to the real game. It would still leave forty-odd recordings replaying the wrong way.
So for now the rule is an option, --retail-disc-polls. Endymion’s play sessions turn it
on and note it with the session, so they replay the way they were played. Everything
else keeps the old timing until the playthrough recordings are done; then it becomes the
default, and the test routes are recorded again under it.