Skip to content

Regions: all USA game code builds from C (1,237 -> 1,965 relocated functions) - #828

Open
emmkay440 wants to merge 236 commits into
MatyMeatBoy:claude/rom-decompilation-research-obsal6from
emmkay440:emm/region-relocated
Open

emmkay440 wants to merge 236 commits into
MatyMeatBoy:claude/rom-decompilation-research-obsal6from
emmkay440:emm/region-relocated

Conversation

@emmkay440

@emmkay440 emmkay440 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Continues the USA regional build on top of #827. All USA game code now builds from C: every EU C function with a USA counterpart is placed through tools/config/region_symbols_usa.json, apart from one kept as region-only code (below).

  • Relocated EU C functions in the verified USA build: 1,237 → 1,965. Compiler-backed USA C bytes: 165,148 → 753,274.
  • EU functions without a shared placement:
    • sub_0800B7DC, its wrapper sub_0800BCFC, and sub_08091F20 do not exist in USA (their USA neighbours are adjacent).
    • sub_080663A4 stays region-only: the USA version sits elsewhere in link order.
  • Two region-only functions remain in usa_only.c. The 16-bit-text number helper (USA 080813D8) is now the REGION_USA branch of sub_080850C0.
  • USA code ranges not built from C: crt0/scheduler, m4a routines, BIOS calls, libgcc/libc.

Not included: data is still taken from the USA ROM, the USA layout is not shiftable, and some USA bodies still use register pins or empty asm statements to match.

How

All source differences live behind #ifdef REGION_USA or in the layout macros of include/region.h, which are the identity in the EU build, so EU output is unchanged.

  • Layout macros applied to structs and raw offsets:
    • PLAYER_FIELD / SAVE_FIELD. The save block starts with a 16-bit name.
    • ENEMY_FIELD / ENEMY_DATA_FIELD. The enemy record embedded at +0x204, and the enemy-data/job tables, carry a 0x174-byte prefix in USA.
    • ITEM_FIELD / EQUIP_FIELD, plus EQUIP_E_FIELD (archive 0xE records shift by 0xE0).
    • RES_ID, TEXT_HEADER, TextChar / NameChar.
  • USA code differences: single-language resource selection, 16-bit text, an enemy-slot multiply instead of an offset accumulator, inline palette-field reads, signed kind reads, a 999 stat cap in sub_0801BA74, and an extra direction check in the player task's state 5 (sub_0800D368).
  • Autofix false positives: literal 0x14C / 0x2E0 values that an earlier pass had turned into PLAYER_SIZE / ENEMY_SIZE are restored.
  • Tools: region_symbols.py places functions with assembler-resolved references by pinning them to the function's own shift; the regression test that expected them to be rejected now checks this. New region_permute.py runs decomp-permuter against the regional ROM; region_autofix.py gains --addresses.

Some commits are partial: layout fixes that cut a function's diff without placing it yet. Their messages say so.

Checks

  • make compare: shiningsoul2_eu.gba: OK
  • make REGION=usa compare: shiningsoul2_usa.gba: OK
  • make nonmatching: builds
  • check_tree.py: 0 errors

🤖 Generated with Claude Code

emmkay440 and others added 30 commits November 21, 2026 20:56
region_symbols.py now retries every unplaced EU C function at the address
given by the shift of the placed functions on either side, which finds
functions that follow a run of regional differences.

make REGION=usa compare: OK, 1265 C intervals (1245 relocated), 165792 bytes.
make REGION=jp compare: OK, 1270 C intervals (1227 relocated), 164410 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The regional build now compiles src/*.c once more with -DREGION_<REGION>
(Makefile REGION_CPPFLAGS, build/regions/<region>/variant) and pairs and
relocates the functions of that build, translating keys back to EU
addresses by name. Without regional blocks the variant equals EU.

include/region.h describes the USA player record (0x158 bytes, fields from
EU +0x0C moved by 10, from +0x2A by 12) as PLAYER_SIZE/PLAYER_FIELD;
struct Player uses them, and sub_08015C5C now addresses the record through
them.

make compare: OK.
make REGION=usa compare: OK, 1329 C intervals (1309 relocated), 187424 bytes.
make REGION=jp compare: OK, 1272 C intervals (1229 relocated), 164414 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tools

Adds SAVE_FIELD for the USA character save record and writes player/save
offsets of shared EU functions and player-record struct views through
PLAYER_FIELD/SAVE_FIELD/PLAYER_SIZE. The macros are the identity in EU.

New tools: region_diff.py (rank/diff unplaced functions against the USA
ROM), region_autofix.py (macro search on literals), region_structs.py
(regional pad sizes for 0x14C-byte struct views).

make compare: OK.
make REGION=usa compare: OK, 1364 C intervals (1344 relocated), 197914 bytes.
make REGION=jp compare: OK (unchanged).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
region_autofix.py --max-diff 16 rewrote player/save offsets of 34 more
functions; adds ENEMY_SIZE (EU 0x2E0, USA 0x454) and lets autofix try it.

make compare: OK.
make REGION=usa compare: OK, 1398 C intervals (1378 relocated), 216232 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed targets

region_autofix.py --max-diff 48 rewrote 13 more functions, 12 of them with
ENEMY_SIZE. region_build.py now calls BL targets that are also stored as
.word data through a <name>__bl .thumb_set alias, so the data word keeps
the Thumb bit (a .word of a .thumb_set symbol drops it).

make compare: OK.
make REGION=usa compare: OK, 1411 C intervals (1391 relocated), 221274 bytes.
make REGION=jp compare: OK (unchanged).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
make compare: OK. make REGION=usa compare: OK, 1421 relocated EU functions (+30).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
make compare: OK. make REGION=usa compare: OK, 1445 C intervals (1425 relocated), 226872 bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ss search

region_diff guesses now also try addresses where a function's opening
non-relocated bytes recur near each neighbour guess (literal pool shifts);
region_autofix widens SAVE_FIELD to save offsets up to 0x1000; region_structs
resizes 0x2E0 enemy record views to ENEMY_SIZE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tions)

(T *)0x03XXXXXX literals become &gUnk_03XXXXXX so that the regional build
relocates them; EU bytes unchanged. region_autofix also tries field macros
on size-valued literals; region_diff lists the counted differing halfwords.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
USA inserts 0x174 bytes at enemy record +0x206 (0x2E0 + 0x174 = 0x454):
0x1CE/0x1DC/0x1FD/0x201/0x204 keep their offsets, 0x206..0x22F move.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tions)

region_structs --partial maps smaller structs as prefix views of a player, save or enemy
record (kept only with an exact gain); region_autofix tries every candidate literal of
one macro at once before single moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l literal 2s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…(10 more shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… autofix (12 more shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ze (10 more shared functions)

PLAYER_FIELD inserts the 2 extra bytes before EU +0x30 (EU +0x2E maps to USA +0x38).
Autofix logs its best partial rewrite; region_structs treats short byte arrays as pads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ore shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tart state (6 more shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ced functions (5 more shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
emmkay440 and others added 29 commits November 30, 2026 00:40
…d function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ore shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… more shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… more shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… more shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…unction)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…1 more shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ction)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…re shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…more shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… (2 more shared functions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… shared function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… function)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@emmkay440 emmkay440 changed the title Regions: place 677 more shared EU functions in the USA build (1,237 -> 1,914 relocated) Regions: all USA game code builds from C (1,237 -> 1,965 relocated functions) Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant