$ show pret/pokeruby
pret/pokeruby is a reverse-engineering project that reconstructs the complete source code of PokΓ©mon Ruby and Sapphire (Game Boy Advance) in C and ARM assembly. The build system compiles the entire game into byte-identical .gba ROMs, verified by pinned SHA1 checksums across a matrix of variants: Ruby/Sapphire, revisions 0/1/2, German localization, debug builds, and a modern-toolchain mode. The repository is the actual working source of the game engine, not documentation: it contains the battle system (turn logic, AI, animations via sprite templates and affine transforms), overworld/field state machines (weather, poison, cut, evolving, day care), PokΓ©mon storage and save/load serialization, contest/link multiplayer subsystems, and the m4a sound driver, all written directly against GBA hardware registers and ARM7TDMI constraints. The architecture is a monolithic single-binary ROM rather than a service or library. Entry points are AgbMain() and the CB2_* main-loop callback system; game systems are orchestrated by a 16-slot task scheduler and a sprite/OAM manager. The build is driven by a sophisticated Makefile that feeds custom C++ asset pipelines β gbagfx (PNGβGBA tile conversion, Huffman compression), preproc (charmap string embedding, INCBIN expansion), scaninc (include/incbin dependency resolution), mid2agb and wav2agb (MIDI/WAV to m4a data), mapjson/jsonproc (map JSON to assembly) β which generate C arrays and assembly that the hand-written sources consume. Data flows are: raw asset β tool β generated C/asm table β compile/link β .gba ROM β SHA1 comparison against the retail cartridge dump. Because this is a decompilation, the code carries inherent artifacts: sub_* function names, NONMATCHING stubs with NAKED asm fallbacks, unused/unreferenced data, and known UB comments. However, the project is professionally maintained with CI, organized headers, careful localization branching, and a clean separation between game code and build tooling. The tools/ directory is the most reusable asset β each utility is standalone and portable to any GBA homebrew or decomp effort β followed by small hardware-abstraction layers like include/gba/macro.h and src/trig.c.
The LLM identified 7 files worth highlighting for reuse:
Standalone C++ preprocessor that rewrites C/C++ game sources into GBA-compatible assembly: converts _("...") strings to chardump byte arrays using a charmap-driven encoding, and expands INCBIN_S8/U8/S16/U16/S32/U32 directives into C array initializers. It is the backbone of every GBA decomp project's text/asset embedding and can be lifted into a new GBA toolchain with zero game-code coupling.
Command-line converter between PNG and GBA-native 4bpp/8bpp planar tile formats, including palette extraction, tilemap encoding/decoding, and Huffman compression used by the m4a/GBA graphics pipeline. Fully self-contained, deterministic, and directly reusable for any tile-based GBA or retro-embedded graphics pipeline.
Parser and generator that reads annotated .sym files (with .include, .space, .align commands and language conditionals) and emits linker scripts for ROM layout. Demonstrates clean C++ file handling with move constructors and a structured RaiseError/RaiseWarning/ReportDiagnostic reporting pattern β a reusable template for any linker-script or symbol-map generator.
Converts MIDI sequences into assembly data for the GBA m4a sound driver, handling track parsing, note tracking, and conversion to the driver's bytecode format. A well-isolated converter whose parsing architecture (midi.cpp / agb.cpp split) applies to any MIDI-to-embedded-audio toolchain.
The core asynchronous task scheduler used by every game system: a 16-slot table of function pointers plus a 16-entry scratch data array, with CreateTask/DestroyTask/RunTasks/SwitchTaskToFollowupFunc and priority ordering. Although it references game globals, the scheduler pattern is small, self-contained, and the canonical approach for structuring GBA game logic; it can be extracted into any embedded/retro project with minimal adaptation.
Macro-level abstraction layer for ARM7TDMI GBA hardware: DMA 3-in-1 copy/fill macros, CPU fill/copy, and register access helpers. The clean parameterized design (DmaCopyLarge, Dma3FillLarge_, CpuFill16) is the standard pattern for bulk memory transfers on GBA and is directly reusable in any homebrew or emulation-adjacent project.
Implements Q8.8 and Q4.12 fixed-point sine tables with Sin/Cos/Sin2/Cos2 accessors for affine sprite/background transforms. Minimal dependencies, classic lookup-table optimization for a no-FPU target β the canonical pattern for any 2D game engine that needs rotation without floating point.