Skip to Content
The Alchemy Project

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 ip so the caller keeps lr. TLA drops both the macro and helper: its compiler emits mov lr, rN followed by the second half of a bl. 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 allows syMatrixBuildLookAt to hold the addresses of fixed-point multiply, divide, and dot-product in r11, r9, and r10 by name.
  • Updated unit-matrix macro: system/ builds 1.0 first when initialising an identity matrix, while battle/ 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

Felix walks north into a house door in Daila, triggering an overlay load from the resource directory
Felix walks north into a house door in Daila, triggering an overlay load from 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, and shop) shift base addresses per language edition. Cross-module calls jump through 8-byte stubs in each module's stubs.s, with module 0 (system/) as the exception. Other resident modules call system/ directly, while only the 114 compressed overlays go through StubsSystem (two hops via each overlay's import table).
  • 2,048-row resource directory (resource_directory.s): Maps IDs 0..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.hParent translation unit #includeing the system_*.c parts in ROM order
entry.s, system_start_up.c, irq.s, irq_table.sROM entry vector, syInit, IWRAM setup, and ARM IRQ dispatcher
system_frame.cCaller-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.c24-entry EventBuf with swapped passes (EVENT_PASS_VBLANK = 0xC80, EVENT_PASS_TICK = 0x480)
system_heap.c, heap_release.sIWRAM and EWRAM bump allocators and 62 slots accessed via fixed-address RamHeapSlots
system_flags.cgmUnitGet 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.incFixed 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.cClamped 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.s16.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.incAsset and overlay decompressors
system_bup.c, bupdata.cAtomic 12 KB copy-on-write flash saving (vendor drivers in games/common/sdk/flash/)
system_serial.cPer-VBlank link-cable packets and 20-byte chunked transfers (Battle Arena lobby and TBS Clear Data transfer)
system_debug_text.c, number_text.cBG0 cursor-based debug printer (syPrint*)
rom_start.c, rom_header.s, resource_directory.s192-byte header (AGFJ–AGFI), 2,048-row resource directory, and ResourceBuildStamp
stubs.s, stub.inc, overlay/overlay.ld, overlay/imports.inc, overlay/overlays.listStubsSystem, stub macro, and linker rules for all 114 -Os compressed overlays
channel_update_entry.s, resident_mixer.s, bios.sCamelot'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:

CallBehaviourSGL and TBS note
gmUnitGetReturns UNIT * for a character or active enemy (NULL out of range)Moved from TBS game/ (dead return-address read removed)
syGameFlagGet, syGameFlagSet, syGameFlagClear, syGameFlagToggleRead, set, clear, or flip one of 4,096 game flags (wraps modulo 4,096; Toggle unused)Moved from TBS game/
syGameFlagGetByte, syGameFlagSetByte, syGameFlagIncrementByte, syGameFlagDecrementByteRead, write, or clamped increment or decrement of an 8-bit counter inside GameFlags (Increment and Decrement unused)New in TLA
syGameFlagGetNibble, syGameFlagSetNibbleRead or write a 4-bit counter inside GameFlags (unused)New in TLA
syFindEventIndex 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
sySoundLoadReverbCodeRange-checks reverb code and returns 0 without copying mixer codeLoads IWRAM reverb mixer code in TBS; no-op in TLA
syVRAMCacheCountFreeBlocksCount free sprite VRAM cache blocksNew in TLA
syOAMAddSpriteAtDepthClampedSubmit sprite clamped below UI window depth (OAM_DEPTH_WINDOW)Replaces syOAMAddSpriteAtDepth in StubsSystem
syResourceReservedNoOperationNo-op taking a resource IDReplaces syDummyNoOperationHook in StubsSystem
syMatrixSetRotateYOverwrite current matrix with Y rotation (clearing translation)slRotY multiplies instead
syMatrixSetTranslationOverwrite translation column while keeping rotation (battle setup only)New in TLA
syTranslate, syScale, syMatrixScaleUniformTranslate by 3 scalars, scale by 3 scalars, or uniform scaleslTranslate and slScale

Source files

src/system/

FileDescription
bupdata.cThe save signature and sector header template, and test data nothing reads.
irq_table.cThe interrupt handlers: the table the IWRAM dispatcher calls through, and the handlers start-up copies into it.
math_table.cThe tangent table syAtan searches.
number_text.cThe digits the number text is written with.
rom_start.cThe 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.cThe 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.cSaving: flash sectors, parts and the save workspace.
system_debug_text.cNumber text, the debug text screen and the window graphics.
system_decompress.cResource decoding, through RAM copies of ARM code.
system_event.cEvents: per-tick and VBlank functions in priority order.
system_flags.cgmUnitGet (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.cInterrupt handlers, the tick wait, key repeat and the VBlank handler.
system_heap.cThe heap: its two bump allocators and heap slots.
system_internal.hWhat every part of the system library's object shares: its includes.
system_io_write_queue.cThe register write queue that VBlank carries out after each finished tick.
system_math.cRandom numbers, angles and square roots.
system_matrix.cThe 3D transform, its stack, the look-at matrix and the projection.
system_oam.cThe tick's sprite lists: affine matrices and depth slots.
system_screen_fade.cScreen fades through the blend registers.
system_serial.c
system_start_up.cPower-up, the cartridge's language, the build stamp and code loading.
system_vram_cache.cThe 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.

include/system/

FileDescription
asm.hThe matching devices: the asm statements the C holds for the compiler, not for the program, each a fake match.
definitions.hThe 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.hThe system library's variables, as SGL keeps its system variables and work areas: each group's variables in IWRAM, EWRAM and ROM.
mul_fx.hsyMulFXArm, 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.hThe two places of the cartridge header the code reads: the cartridge's start and the language letter of its game code.