Skip to content

The games API

A shipped game was built against eleven interfaces and 486 functions, imported by slot index. The only copy of that surface anywhere is the one in the firmware.

OpenGLES     170        Metadata     169        Audio         54
SoundEffect   42        AsyncFileIO  17         miscTBD       13
Users         10        DebugUtil     5         MemoryAlloc    3
InputEvents    2        Settings      1

One of them is called miscTBD, a name that suggests the header was not finished when it went out to studios.

The platform object

Every interface reaches its service the same way. It fetches the platform singleton, then reads a member of it.

The singleton is a C++ function-local static with the usual shape. Test bit 0 of a guard word, and if it is clear, acquire the guard, construct, register the destructor and release. The three Itanium ABI guard functions are named in the project too.

Twenty-five functions go through it, and the member offset tells which service a slot reaches. One offset is the block arena behind MemoryAlloc, another the file service behind AsyncFileIO. Read the offset each slot loads and the method it forwards into, and a whole interface collapses to a single class.

graph LR
    SLOT["a slot<br/>(15 of the 486)"]
    PLAT["<b>the platform object</b><br/>a C++ function-local static,<br/>guarded and constructed once"]
    ARENA["+0x294 the block arena<br/><i>MemoryAlloc</i>"]
    FILE["+0x298 the file service<br/><i>AsyncFileIO</i>"]
    SLOT --> PLAT
    PLAT --> ARENA
    PLAT --> FILE

Every slot has a state

A generated table holds all 486 rows, and each row carries one of four states:

NAMED     266   its behaviour is recovered from its body
RESOLVED  216   the implementing function is known, what it MEANS is not
INFERRED    4   a candidate class, with the reason it is not proof written down
OPEN        0

RESOLVED does not mean done. A slot that dispatches through a vtable offset of the track-reference class has a concrete address and no more meaning than before. What it gives is one function to open instead of a search.

The four INFERRED rows are the artwork list. Its class cannot be measured here. The list was empty in every dump taken, and the only slot that inserts into it takes the object from its caller, so those objects come in through the API and are not built in this image.

Slots by shape

Sorting the slots by shape makes 486 of them manageable:

own-body           334   its own code, has to be read
list+vtable        116   fetch from a list, dispatch through a vtable
forwarder           16   a single branch
platform-service    15   fetch a service off the platform object and forward into it
post-event           5   build an event and post it

The interfaces

MemoryAlloc

Three slots: allocate, free, reallocate. The arena behind them is no plain heap.

Allocate refuses anything larger than the arena's capacity, then splits by size. Below a threshold the request goes to a general allocator. Above it, the block comes out of a table of preallocated blocks tracked by a bitmask. Free mirrors that: it finds the pointer in the table and clears its bit, or tail-branches to the general free.

The arena is the game's own request capped at 5 MiB, plus 3 MiB always, so 3 to 8 MiB. A game is bounded by the arena's size, whatever the number of allocations.

DebugUtil

Four file operations over a ten-entry handle table, plus a formatted print. The print goes out over ARM semihosting, character by character, to whatever is debugging the device. That is where the interface gets its name, and it is also how a stuck game can be made to explain itself.

InputEvents

Two slots that go opposite ways.

The game reads input, and the wheel arrives as a delta, not a position. The delta is the accumulator minus the consumed mark. Then the mark advances and the pending byte clears, so each movement is delivered exactly once.

The game also posts events into the firmware's own queue, after a two-byte descriptor is validated.

Settings

A single slot, a lookup by name over three records: TimeFormat, Language and HoldSwitch. The match is exact, so Time does not find TimeFormat.

Each record's handler carries the signature. The count argument is in-out, capacity going in and length written coming out. There are five distinct error codes, and "buffer too small" is checked before anything is written.

The length field in each record is zero in the image and gets filled at init from the strings. The static data alone would suggest that no name can ever match.

AsyncFileIO

Twelve of its seventeen slots fetch the file service and forward into one method. That method gates on a kind byte in the request, and the slot index selects the kind. The rest of the interface is the workers behind them.

Audio

A command API, with fifty-three distinct targets for fifty-four slots. Each slot allocates a small event, stamps it with its own class pointer and posts it. A slot's identity is the event class it stamps.

The interface is asynchronous by construction: a game asks, then waits. The events belong to the same 0x66 family as the game screen's lifecycle.

OpenGLES

At 170 slots, this is the Vincent ES 1.1 the firmware already carries, handed to games wholesale. Its tail is EGL. Telling which slot is which takes another method than the rest of the API: each slot is fingerprinted by the enum values it accepts.

Slots that do nothing

Some slots return a constant and nothing else. One is a bare bx lr, another returns a fixed number, five GL slots return 0 and two return 1. The original does nothing there either, so they are no gaps.

The frame contract

The runner fills two lists before every call to entry[4] and clears both when the frame returns:

A+0x2C   the finished ASYNC IO requests
A+0x30   the input events

A completion node, read from the game's own walk of that list:

+0x00  next            the link the OS overwrites
+0x04  in-flight byte  the walker clears it
+0x08  callback        the game's own function pointer
+0x0C  context         handed to it

The platform delivers the node, and the game calls its own callback.

The runner's stop condition, checked after every frame, is the game's own state word. The runner keeps going while the game has not written 5 or 6 into it. Writing 5 is a game quitting, so a port that only stops on a negative return keeps calling into a game that has already torn itself down.

sequenceDiagram
    participant G as the game
    participant H as the runner
    participant S as the file service
    G->>S: submit a request (kind byte selects the work)
    S-->>G: 1 = QUEUED (any non-zero reads as queued)
    Note over G: marks its request in flight
    S->>H: the finished request, linked through request+0
    H->>G: entry[4](A, B) with A+0x2C = completions, A+0x30 = input
    Note over G: walks A+0x2C, clears the in-flight byte,<br/>calls its OWN callback at node+0x08
    H->>H: clear both lists, then test:<br/>continue iff the game has not written 5 or 6

Three details that break a port

Detail What goes wrong
A submit answers "queued" any non-zero answer means the request is in flight. A refusal code read as success leaves the game waiting for a completion that never comes
A+0x2C has to be published if it stays zero, no submit ever completes, and the game sits in its loading state while its frames keep running
A pointer slot must answer a pointer one metadata slot returns the element, an object pointer, or zero when out of range. Answering index + 1 hands the game a small non-null integer to dereference. It is kept as a refcounted object and released through bx, and the low bit of the garbage pointer picks the instruction set. With nothing real to return, return the failure the device returns

Tracing what a game calls

A shipped game is unmodified, so a table entry pointing at the wrong function is quite possible and nothing else would catch it. The registry can be built with a naked trampoline per slot. It pushes the argument registers and the link register, logs the interface and slot, pops them back and branches through the real table. The callee sees exactly the registers and stack it would have seen.

The steady-state frame, from that trace:

GL nop . context attribute . glClear . a query . attribute . nop . eglSwapBuffers .
timer x2 . input poll

Two build notes for turning it on. ARMv5 has no movw, so the interface and slot go into two 8-bit immediates instead of a literal. And 486 trampolines put a literal pool out of reach, so each one needs its own .ltorg.

The trace also tells a starved game from a running one. A game the arena refuses memory allocates, uploads exactly one texture, then clears and presents an empty frame forever, with glDrawArrays at zero across a whole launch, 105 SDK calls in all. With the arena sized right, the same launch makes 200,745 calls and the title screen arrives on the panel.

Where the implementation is

chikuma/drivers/games/, with host tests that assert the original's behaviour:

  • a bounded arena that refuses instead of eating the firmware's heap, and ignores foreign pointers
  • a ten-handle ceiling
  • a wheel delta consumed on read
  • the five settings error codes staying distinct