# Tetris Battle Gaiden (J) — M0 reverse-engineering notes ROM: `rom/tbg_jp.sfc` (copy of `~/Downloads/Tetris Battle Gaiden (Japan).sfc`) SHA1 `e455607b32b0adf14bf4af917cbf6561eccd1d0d`, 1 MiB, LoROM, FastROM ($30), no copier header, checksum valid ($7B41). Maker $8B (BPS), 1993. Addresses below are SNES CPU addresses (`BB:AAAA`); WRAM as `7E:xxxx` (low 8K mirrored at `00:0000`). ## Tooling | Tool | Where | Notes | |---|---|---| | Ghidra 12.1.2 | `/opt/ghidra`, project `ghidra/tbg.gpr` | 65816 support via `third_party/ghidra-snes` (built against 12.1.2 with Gradle 9.8.2 — 9.3.1 can't run on JDK 27), installed to `~/.config/ghidra/ghidra_12.1.2_DEV/Extensions/ghidra-snes` | | Seed/export scripts | `re/ghidra_scripts/` | `SeedEntries.java` (entry points + CPU mode context + jump tables), `ExportAll.java `, `Stats.java` | | Export | `re/export/asm/bank_XX.asm` | Disassembly with callers and data refs. `re/export/decomp/` is decompiler output — **mostly garbage** (65816 stack/DBR modelling is broken); trust the asm | | Quick disassembler | `re/dis65816.py ROM ADDR [N] [--m16] [--x16]` | Standalone, tracks REP/SEP | | Mesen2 2.1.1 | `~/.local/opt/mesen2/Mesen`, wrapper `re/mesen.sh ` | Headless `--testrunner`. Needs `LD_PRELOAD=/usr/lib/libstdc++.so.6` (static libstdc++ clash → `std::bad_cast`) and an X display (KDE Xwayland `:1`). `~/.config/Mesen2/settings.json` must set `FirstRun:false`, Lua IO access, and SNES Port1/Port2 = SnesController or input does nothing | | Input/snapshot driver | `re/lua/drive.lua` | env: `TBG_OUT`, `TBG_PLAN="frame:btn+btn,..."`, `TBG_SHOTS`, `TBG_END`, `TBG_SAVE_AT`, `TBG_LOAD` | | REA | `npx rea-agents@6.7.0`, MCP registered in `~/.claude.json` | Ghidra provider healthy. MCP tools appear after a Claude Code restart | Regenerate the Ghidra project + export: ``` /opt/ghidra/support/analyzeHeadless ghidra tbg -import rom/tbg_jp.sfc -overwrite \ -scriptPath re/ghidra_scripts -preScript SeedEntries.java -postScript ExportAll.java $PWD/re/export ``` ## Boot / frame structure - Reset (emulation vector) `00:8028`: SEI, native mode, D=0, S=$1FFF, DBR=K, NMI off, FastROM on (`$420D=1`), `JML 80:8047` into the fast mirror. - `80:8047` main init: repeated `JSL 88:8000` with A=command — `88:801E` dispatches through a 26-entry `JMP (abs,X)` table at `88:803D` (labelled `snd_cmd_XX`; believed to be the APU/sound driver interface — unconfirmed). - Native NMI `00:81B6`: `JML [$0080]` — the per-frame handler is a 24-bit pointer at `7E:0080`, swapped per game mode: - `81:BCCB` (installed at `80:856D`) - `80:8AC8` (installed at `80:88D0`) - `84:A23D` (installed at `84:84E8` and `84:87C3`) All other vectors → `81:B9` infinite loop. - Second indirect vector: `JML [$0124]` at `81:A6BE`, `81:A6E3`, `82:883C` (written at `81:A6AD`, `81:A6D2`, `82:8829`) — not yet traced. ## Game flow (observed in Mesen, no-input boot + Start/A) frame ~200 BPS logo → ~600 copyright → ~1000 title menu (1P battle / 2P battle / options) → Start → character select (8 chars, 4 powers each shown) → A → Battle World map (1P vs COM) → stage intro → battle ("READY") ~3800. Savestate at battle start: `re/runs/battle1/state.mss` (frame 4000, Halloween vs Mirurun). Characters (map/English labels): Halloween, Mirurun, Shaman, Aladdin, Princess, Bit, Ninja, Wolfman. Char-select page also lists numbered powers (e.g. Halloween: 1. フライ 2. ダーク 3. バンパイア 4. ボム?). ## Board (CONFIRMED by WRAM diff vs screenshots) - Two boards, 10 columns × 22 rows (20 visible + 2 hidden above), row-major, **top row first**, 2 bytes per cell (row stride 20). - P1: `7E:2280`–`7E:2437` - P2: `7E:2438`–`7E:25EF` - Cell word: low byte = block (0 empty, 01 = locked block seen so far), high byte = BG tilemap attribute (P1 `$24`, P2 `$28` — palette/priority), i.e. the board is laid out like tilemap entries. - `7E:2208`–`7E:227F`: 6 rows of `$FF`-attribute filled cells immediately before P1 — sentinel/guard rows (collision?). Check whether P2 has its own guard. - Shared piece queue: both players draw from one queue shown in the centre column (3 previews visible). ## Detailed notes (M0 agents, supersede the sections above where they differ) - `re/notes/mechanics.md` — player DP struct, logic boards (`$0282`/`$04E2`, the `7E:2280` boards are display copies), shapes, rotation, speeds, queue, RNG - `re/notes/battle.md` — garbage/attack, rounds, power meter + casting, all 43 power effects - `re/notes/flow_chars_ai.md` — scene flow, character/power tables, CPU AI, 1P progression - Mesen savestates only work inside an exec callback: use `re/runs/battle_state/state.mss`, `re/runs/battle_mid/state.mss`, `re/runs/flow_st_select/state.mss` ## Open leads (from the first pass) - `7E:05A0`–`05CF`: byte grid that grows during play (1 byte/cell, nonzero = 01) — maybe CPU AI working copy or column profile. - `7E:01A0`–`01F8`, `7E:0154`–`016A`: change as pieces lock — likely per-player state (score/lines/power meter?). - Find: piece shape/rotation tables in ROM, RNG + queue generator, gravity/DAS timers, line-clear + garbage, power meter fill, power effects, CPU AI.