System Library
This page is a guide to the games/tla/src/system/ folder of the Alchemy
project, where the runtime kernel of Golden Sun: The Lost Age is kept, handling
hardware boot, the frame loop, flash saving, and the resource directory.
Module 0 runs every tick of gameplay: startup, the caller-stack sySynch()
loop, 24 event slots, RamHeapSlots, OAM and VRAM, atomic 12 KB flash saves,
link-cable packets, and the 16 MB Game Pak layout. Over The Broken Seal, TLA
drops stack switching from sySynch(), adds four event slots, and moves
gmUnitGet and syGameFlag* here.
The sections below cover swapped event passes, compiler codegen changes, flash saving, the 15-module ROM layout, a file map, and every system call changed from TBS. The conceptual architecture is described in The system library, and the full base SGL call reference is given in The Broken Seal's system library.
Boot, sySynch(), and swapped event passes
In The Broken Seal, sySynch() relocates the stack before running events. The
Lost Age drops that step and runs tick events directly on the caller's
stack. EventBuf also grows by 4 slots to 24 entries, and the two pass
offsets swap (EVENT_PASS_VBLANK = 0xC80, EVENT_PASS_TICK = 0x480). Placing
VBlank callbacks at the front of the descending-priority table lets
syVBlankHandler exit early as soon as it reaches the first tick event instead
of scanning all 24 slots during VBlank.
As in TBS, gmMainRoutineRun clears the event list and bump heaps between
engines. Only fixed ewram.s blocks (MainRoutineStateWork, CharacterUnits,
SnapshotObjects) and GameFlags survive across engines. With DebugMode
enabled, gmMainRoutineRun also exposes four hidden boot shortcuts: Venus
Lighthouse entrance, post-Title screen, Mt. Aleph summit, and an alternate Title
entry.
Compiler codegen changes from The Broken Seal
The Lost Age inherits the same SGL-derived system library and -fcall-used-r4
register contract explained in
The Broken Seal's system library,
with three visible signs that Camelot appears to have continued developing both
the library and its compiler:
- Inline 4-byte indirect calls: TBS reaches fast IWRAM multiply through a
helper macro returning via
ipso the caller keepslr. TLA drops both the macro and helper: its compiler emitsmov lr, rNfollowed by the second half of abl. The sequence takes four bytes inline with no helper, and no released GCC produces it. - High-register function pointer caching: Thumb instructions rarely use
r8–r11, while the new indirect call accepts any register. This allowssyMatrixBuildLookAtto hold the addresses of fixed-point multiply, divide, and dot-product inr11,r9, andr10by name. - Updated unit-matrix macro:
system/builds1.0first when initialising an identity matrix, whilebattle/still carries The Broken Seal's original order.
Saving and flash
Save files are never overwritten in place. Each of the 3 save slots is a single 12 KB part filling three 4 KB flash sectors across 5 three-sector slots on the 64 KB chip, leaving two spare slots at all times.
syBupWritePart writes the new copy with an incremented sequence number,
verifies it by reading it back, and only then retires the older copy. Each copy
is tagged with a checksum and ResourceBuildStamp. Before every read or write,
the reconstructed flash routines
(games/common/sdk/flash/) probe which of the four
flash vendors' chips is on the cartridge and enforce hardware timeouts.
16 MB Game Pak, modules, and the resource directory

- 15 fixed-address modules (
ld_script.ld): The linker splits the main image into 15 modules, adding Super Lucky Dice as its own module. Four of them (game,field_engine,menu, andshop) shift base addresses per language edition. Cross-module calls jump through 8-byte stubs in each module'sstubs.s, with module 0 (system/) as the exception. Other resident modules callsystem/directly, while only the 114 compressed overlays go throughStubsSystem(two hops via each overlay's import table). - 2,048-row resource directory (
resource_directory.s): Maps IDs0..2047(leaving 325 spare trailing rows) to UI and fonts, title art, battle backgrounds, minigames, summons, and battle effects. It also indexes the World Map, monsters, maps and tilesets, and the 114 overlays (Daila's overlay is row 1617).
File map
| File(s) | Role |
|---|---|
system.c, system_internal.h | Parent translation unit #includeing the system_*.c parts in ROM order |
entry.s, system_start_up.c, irq.s, irq_table.s | ROM entry vector, syInit, IWRAM setup, and ARM IRQ dispatcher |
system_frame.c | Caller-stack sySynch(), syRenderBuildOAMList, syWaitForVBlankInterrupt (proactive pre-VBlank audio render and busy-wait), syVBlankHandler, no-op sySoundLoadReverbCode, sleep (L + R + Select, Pause Sleep, or 3 min idle) |
system_event.c | 24-entry EventBuf with swapped passes (EVENT_PASS_VBLANK = 0xC80, EVENT_PASS_TICK = 0x480) |
system_heap.c, heap_release.s | IWRAM and EWRAM bump allocators and 62 slots accessed via fixed-address RamHeapSlots |
system_flags.c | gmUnitGet and syGameFlag* bit, byte, and nibble helpers (moved here from game/ in TLA; flag index wraps modulo 4,096) |
ewram.s, iwram.s, iwram_entry.s, iwram_high.s, iwram_routines.s, ram.inc | Fixed RAM layout (MainRoutineStateWork, CharacterUnits, SnapshotObjects) and hot IWRAM ARM routines |
system_oam.c, system_vram_cache.c, system_io_write_queue.c, system_screen_fade.c | Clamped OAM depth buckets (syOAMAddSpriteAtDepthClamped), sprite VRAM cache (syVRAMCacheCountFreeBlocks), VBlank register write queue, and screen fades |
system_math.c, system_matrix.c, math_table.c, trig.s, inner_product.s | 16.16 fixed-point math and 3×4 matrix transforms (syTranslate, syScale, syMatrixScaleUniform, syMatrixSetRotateY, syMatrixSetTranslation) |
system_decompress.c, decompress_byte_lz.s, decompress_tagged_lz.s, move_to_front.inc | Asset and overlay decompressors |
system_bup.c, bupdata.c | Atomic 12 KB copy-on-write flash saving (vendor drivers in games/common/sdk/flash/) |
system_serial.c | Per-VBlank link-cable packets and 20-byte chunked transfers (Battle Arena lobby and TBS Clear Data transfer) |
system_debug_text.c, number_text.c | BG0 cursor-based debug printer (syPrint*) |
rom_start.c, rom_header.s, resource_directory.s | 192-byte header (AGFJ–AGFI), 2,048-row resource directory, and ResourceBuildStamp |
stubs.s, stub.inc, overlay/overlay.ld, overlay/imports.inc, overlay/overlays.list | StubsSystem, stub macro, and linker rules for all 114 -Os compressed overlays |
channel_update_entry.s, resident_mixer.s, bios.s | Camelot's 528-sample-per-frame IWRAM audio mixer and BIOS wrappers |
System calls added or changed from The Broken Seal
All base calls match
The Broken Seal's SGL call reference.
TLA adds these calls to StubsSystem (moving gmUnitGet and syGameFlag* from
game/ into module 0) and drops TBS's syRandom16Below and syDMAWait:
| Call | Behaviour | SGL and TBS note |
|---|---|---|
gmUnitGet | Returns UNIT * for a character or active enemy (NULL out of range) | Moved from TBS game/ (dead return-address read removed) |
syGameFlagGet, syGameFlagSet, syGameFlagClear, syGameFlagToggle | Read, set, clear, or flip one of 4,096 game flags (wraps modulo 4,096; Toggle unused) | Moved from TBS game/ |
syGameFlagGetByte, syGameFlagSetByte, syGameFlagIncrementByte, syGameFlagDecrementByte | Read, write, or clamped increment or decrement of an 8-bit counter inside GameFlags (Increment and Decrement unused) | New in TLA |
syGameFlagGetNibble, syGameFlagSetNibble | Read or write a 4-bit counter inside GameFlags (unused) | New in TLA |
syFindEvent | Index of a callback in EventBuf (used by field_tile_animation.c and scene/ankohl_ruins.c) | Exists UNUSED in TBS C; newly exported in StubsSystem |
sySoundLoadReverbCode | Range-checks reverb code and returns 0 without copying mixer code | Loads IWRAM reverb mixer code in TBS; no-op in TLA |
syVRAMCacheCountFreeBlocks | Count free sprite VRAM cache blocks | New in TLA |
syOAMAddSpriteAtDepthClamped | Submit sprite clamped below UI window depth (OAM_DEPTH_WINDOW) | Replaces syOAMAddSpriteAtDepth in StubsSystem |
syResourceReservedNoOperation | No-op taking a resource ID | Replaces syDummyNoOperationHook in StubsSystem |
syMatrixSetRotateY | Overwrite current matrix with Y rotation (clearing translation) | slRotY multiplies instead |
syMatrixSetTranslation | Overwrite translation column while keeping rotation (battle setup only) | New in TLA |
syTranslate, syScale, syMatrixScaleUniform | Translate by 3 scalars, scale by 3 scalars, or uniform scale | slTranslate and slScale |
Source files
| File | Description |
|---|---|
bupdata.c | The save signature and sector header template, and test data nothing reads. |
irq_table.c | The interrupt handlers: the table the IWRAM dispatcher calls through, and the handlers start-up copies into it. |
math_table.c | The tangent table syAtan searches. |
number_text.c | The digits the number text is written with. |
rom_start.c | The cartridge's first instruction, where the BIOS starts the game: ahead of the cartridge header (rom_header.s), it goes on to the game's start-up. |
system.c | The system library's object: power-up and the interrupt handlers, the tick wait and VBlank, input, the register write queue, fades, the sprite lists and VRAM cache, events, maths, debug text, the heap, the 3D transform, decoding, saving, the link cable and the game flags. |
system_bup.c | Saving: flash sectors, parts and the save workspace. |
system_debug_text.c | Number text, the debug text screen and the window graphics. |
system_decompress.c | Resource decoding, through RAM copies of ARM code. |
system_event.c | Events: per-tick and VBlank functions in priority order. |
system_flags.c | gmUnitGet (moved here from TBS's game_units.c, keeping its gm prefix for TBS/TLA parity) and the game flags: bits, bytes and nibbles. |
system_frame.c | Interrupt handlers, the tick wait, key repeat and the VBlank handler. |
system_heap.c | The heap: its two bump allocators and heap slots. |
system_internal.h | What every part of the system library's object shares: its includes. |
system_io_write_queue.c | The register write queue that VBlank carries out after each finished tick. |
system_math.c | Random numbers, angles and square roots. |
system_matrix.c | The 3D transform, its stack, the look-at matrix and the projection. |
system_oam.c | The tick's sprite lists: affine matrices and depth slots. |
system_screen_fade.c | Screen fades through the blend registers. |
system_serial.c | |
system_start_up.c | Power-up, the cartridge's language, the build stamp and code loading. |
system_vram_cache.c | The sprite VRAM cache, which hands out sprite tile VRAM to the actors, icons and portraits on screen and loads their pictures into it; then the affine matrix builder that follows it. |
| File | Description |
|---|---|
asm.h | The matching devices: the asm statements the C holds for the compiler, not for the program, each a fake match. |
definitions.h | The system library's types and constants, as SGL's sl_def.h holds its own: the hardware's registers, and each group's structures, enums and fixed values. |
globals.h | The system library's variables, as SGL keeps its system variables and work areas: each group's variables in IWRAM, EWRAM and ROM. |
mul_fx.h | syMulFXArm, the 16.16 fixed-point multiply: SGL's slMulFX written out for the C compiled as ARM, where the Thumb C calls the same multiply in IWRAM. |
rom_header.h | The two places of the cartridge header the code reads: the cartridge's start and the language letter of its game code. |