Program Flow

These diagrams document the complete architecture of Klonoa: Empire of Dreams, derived from reverse-engineering all 476 functions across 10 modules. The game uses a callback-driven state machine where a central callback block at 0x03003510 holds function pointers defining the current scene's behavior.

1. Main Program Flow

Boot sequence (AgbMain), main game loop, scene dispatch, and the transition state machine. Each frame: VBlank interrupt fires, input is read, timers update, and the current scene's update function runs. Transitions fade out over 16 frames, free resources, install new callbacks, and fade back in.

Main program flow

2. Entity System

Per-frame entity update pipeline (code_1 module). EntityUpdateDispatch iterates 99 entity slots (28 bytes each). Slot 0 is the player (with input handling and physics); slots 1-98 are NPCs, enemies, projectiles, and pickups dispatched through a behavior function table. Common pipeline: gravity, tile movement, scroll bounds, sprite updates, then state-specific handlers (carried, thrown, hit, death).

Entity system

3. Graphics Pipeline

Three parallel systems: VBlank rendering (OAM DMA, BG scroll, palette, level-specific tile DMA, HUD, dialog); scene setup (decompress tiles/tilemaps/palettes from ROM, DMA to VRAM charblocks); and the graphics stream command system (bytecode interpreter with 15+ opcodes for dynamic graphics control during cutscenes and transitions).

Graphics pipeline

4. Sound Engine

Nintendo's MusicPlayer2000 ("Sappy" / m4a): MIDI-like bytecode tracks mixed to PCM and DMA'd to the GBA's Direct Sound FIFOs. 4 music players, 39 BGM songs, 126 SFX, 3 voice/instrument tables, ~300KB of 8-bit PCM samples. Per-frame: decode bytecode events per track, mix all channels, DMA to audio FIFO.

Sound engine

5. World Map, Level Loading, and Save System

World map scene with 8 worlds (Breezegale, Jugpot, Forlock, Volk, Ishras Viel, Timber Tracks, The Dark, EX Levels), each with 2 visions. Level loading pipeline: transition fade → free buffers → load world/vision-specific data → decompress graphics → spawn entities → start music. Save system uses EEPROM with DMA-based sector write/verify/retry.

World map and level loading