# TBG (J) — battle system: attack/garbage, power meter, power effects Scope: line clears → attack, garbage rows, round end/top-out, power meter + casting, all 43 power effects. **CONFIRMED** = observed in Mesen (RAM recording, pokes, write traces, screenshots) or unambiguous code+data. **INFERRED** = read from code or screenshots only, not isolated by experiment. Character/power *names*, char→power lists, AI, level table: see `re/notes/flow_chars_ai.md` (flow fork) — not duplicated here. WRAM `$xxxx` = `7E:xxxx`. ## Tooling (this fork) | File | Purpose | |---|---| | `re/lua/battle_lib.lua` | helpers: `inExec` (savestate calls are only legal inside an exec callback), `save/load`, `dumpWram`, `shot`, `inputPlan` (`"frame:btn+btn[/hold]"`, `p2@frame:btn` for port 2) | | `re/lua/battle_drive.lua` | input plan + shots/dumps + savestates + `TBG_POKES="frame:addr=val+addr=val"` + `TBG_WATCHB` per-frame byte log | | `re/lua/battle_trace.lua` | unique PCs reading/writing watched ranges (**watch bank-00 addresses, e.g. `000296-000371`**; `7E:` misses DP/DBR=$00 accesses; see flow notes for $80–$88 mirrors). Reported PC = *next* instruction | | `re/lua/battle_cdl.lua` + `re/cdl_dis.py ROM OUTDIR CDL...` | dump Mesen CDL (code bytes + M/X flags) → mode-correct disassembly of executed code (`re/runs/battle_trace/cdl_asm/`, 15k instrs). Unexecuted branch arms are omitted — fill with `dis65816.py` | | `re/lua/battle_rec.lua` + `re/tools/rec_fields.py` | record WRAM `$0000–$0FFF` every frame; summarise per-byte behaviour | | `re/lua/battle_power.lua` + `re/tools/power_summary.py` | force P1 to cast any power ID, log state/status/queue per frame + both boards per frame (`boards.bin`), shots. Results: `re/runs/battle_pw//`, `summary.txt` | | `re/lua/battle_regs.lua` | log regs when given PCs execute | Savestates: `re/runs/battle_state/state.mss` (battle start, "GO", Halloween vs Mirurun), `re/runs/battle_mid/state.mss` (mid-battle, both boards populated; used for all power tests). Random-play recording: `re/runs/battle_play1/` (plan `re/runs/battle_trace/plan1.txt`, 7000 f). ## Per-player struct (direct page; P1 D=$0202, P2 D=$0462) — fields used by the battle code | DP | P1 | meaning | conf | |---|---|---|---| | $06/$07 | $208/$209 | current piece type (0–6, +7 = coin variant) / rotation 0–3 (`81:81F7` → shape ptr table `81:8218`, 4×4 byte grids) | CONFIRMED | | $08/$09 | $20A/$20B | piece x / y (used by `81:D004` stamp) | INFERRED | | $0B | $20D | player state (table `81:A8CC`/`81:B02E`); `FF` = match over, `FE` = next round | CONFIRMED | | $1F | $221 | **power meter** (coins), capped at min(20, DP $2E) | CONFIRMED | | $20 | $222 | **attack stock**: lines cleared not yet delivered (shown as 10-slot sprite row, `81:D4F4`) | CONFIRMED | | $22 | $224 | round wins | CONFIRMED (flow) | | $26 | $228 | status bits (bit1/bit0 tested by power code; bit6 = AI searching) | INFERRED | | $27 | $229 | **active power of this player** (FF none) — effects test the *caster's* byte | CONFIRMED | | $28 | $22A | power duration counter (pieces), e.g. Fever | CONFIRMED (Fever), INFERRED others | | $2B–$2E | $22D–$230 | level 1–4 cost thresholds (default 1,2,3,4) | CONFIRMED | | $2F–$32 | $231–$234 | the character's 4 power IDs | CONFIRMED | | $33–$36 | $235–$238 | rows found full by `81:D06B` (FF = none) | CONFIRMED | | $37–$4A | $239–$24C | 20 meter-gauge tile bytes built by `81:D3E5` | CONFIRMED | | $5F–$76 | | button masks (A,B,…, cast = DP $6F, default `$0800` = **Up**) | CONFIRMED | | $80–$15F | $282–$361… | logic board 10×24 bytes (row 0 at $80, top-out row 1 at $8A, playfield rows 2–23 at $94 = `$0296`) | CONFIRMED | Cell values: 0 empty, 1 block, **6 coin (crystal) block**, 4 stone/cursed block (Medusa/Noroi), 2 seen after Moses, 09–0C = line-clear flash frames (table `81:D150` = `00 00 00 0C 0B 0A 09 08 07`). Render copy: `81:C0C6` copies cells+attr (`$0386`/`$05E6`) to tilemap buffer `7E:2280`/`7E:2438` every frame. ## Shared piece queue (CONFIRMED) `$016F` = piece being dealt, `$016B,$016C,$016D` = 3 previews (shared by both players; `81:D36F` shifts). Generator `81:D396`: `rng & 7`, reroll 7, reroll if equal to `$016D` (no immediate repeat). Every `$0170` pieces (countdown `$0171`; default 5) the piece gets **+7 = coin variant** (sound $14/$11). `82:86F2` can override (Fever). RNG `81:81A2`: index `$0122` into `83:EBD1` + ROM bytes. (Piece shapes/rotation: board fork.) ## Line clear → attack → garbage 1. Lock: `81:CFAA` writes piece cells (STA $94,X at `81:CFC4`). 2. State 0A (`81:AD00`): `81:D06B` scans rows; each full row → DP $33+n and **`INC $20` (attack stock +1)**; if `86:CC4F` (caster has **Doubles**, `28`) → +1 more. CONFIRMED. - ≥4 lines: `81:92D9` big-clear effect + sound $14/$05; else per-count sound (`81:B029` table). - `81:D0E0`: flash cells; **coins (value 6) in cleared rows add to meter DP $1F**; cap 20 and DP $2E. CONFIRMED. - States B/C flash ↔ E, then `81:D462` collapses each cleared row (shift down, clear top row). 3. **Delivery**: at the *victim's* state 00 (`81:A9E6`, start of each of their piece cycles), while the **opponent's** stock (`$0482` for P1) > 0: sprite animation (`81:A976`), stock −1, state 10 (`81:A9BC`) inserts **one garbage row** (`86:E645` → `81:D48E`). Repeats until 0. **1 line cleared = 1 garbage row.** CONFIRMED (recording: each stock decrement = +1 row on victim). **Correction:** before that, every state 00 nets the two stocks (`81:D6AE`: the larger minus the smaller, the smaller → 0) — CONFIRMED in Mesen, see `anim_battle_extra.md` §5. (Earlier note said "no cancellation".) Blocked when the victim has **Shield** (`1C`) or **Nakayoshi** (`23`) active (`86:F0AC`, `86:E1B5`). 4. Garbage row (`81:D48E`): shift board up one row (`81:D442`), bottom row = 10× block value `$0114` (1 normally, **4 if opponent has Noroi**), one hole. Hole (`81:D4B9`): column = `(rng&7)+(rng&1)` → **0–8, never column 9**, triangular-ish distribution; the **same hole is reused for 10 consecutive rows** (global counter `$019C`, reset to 9). CONFIRMED (code + observed repeated hole column). ## Round end / top-out (CONFIRMED code; INFERRED trigger order) - In state 0A, if no lines cleared and `81:D9A7` finds any block in logic row 1 (DP $8A–$93, the row above the 22-row array) → that player **loses the round**: `$12A`=winner context, `81:A73B` (round-end sequence, `INC $22` winner). 2 wins → DP $0B=`FF` (match over), else `FE` (next round, `81:A47D` re-init). - Battle frame loop `81:A548`; P1 then P2 each frame. ## Power meter & casting (CONFIRMED by poke + Up press, `re/runs/battle_cast1`) - Meter DP $1F fills only from coins in cleared lines (`81:D0E0`), steals (Vampire), etc. - **Cast = press Up** (DP $6F mask; edge-latched into `$013D,X` by `81:9DA5`); in falling state `81:9F39` → `82:B02E` check → state **13**. - State 13 (`81:AC26` P1 / `81:B381` P2): requires DP $2B≠0, own DP $27 = FF (no power running), and neither player in line-clear states B/C. Picks the **highest affordable level**: meter ≥ $2E → lv4, ≥ $2D → lv3, ≥ $2C → lv2, ≥ $2B → lv1. **Meter is reset to 0** whatever level was cast. Power ID = DP $2F+level, sound unless ID `1D`, then `82:D3BD` → `80:D2BE[ID]` (3-byte ptr) via `JML [$0124]`. - During the cast cut-in both players sit in state 00; DP $27 = power ID for the effect's lifetime. ## Power handlers and effects Handler = `80:D2BE[ID]`. Test: P1 (Halloween board) forced to cast each ID from `battle_mid` state (`re/runs/battle_pw/`; boards compared at cast / effect end / +60f). "own" = caster's field, "opp" = opponent. Durations in frames are of DP $27 in this test (≥800 = still active at end of test). | ID | name | handler | effect | conf | |---|---|---|---|---| | 00 | ミルルン | 81:BB4A | removes **4 bottom rows of own field** (board drops 4) | CONFIRMED | | 01 | コピー Copy | 86:BE41 | own field becomes a copy of opp's field | CONFIRMED | | 02 | メデューサ Medusa | 86:D7B0 | opp's blocks turned to **stone (value 4)** | CONFIRMED (values); stone behaviour INFERRED | | 03 | ボム Bomb | 86:B600 | explosions remove ~8 random cells in opp's lower field | CONFIRMED | | 04 | マヤカシ | 81:BA85 | status: opp's piece types scrambled by RNG for display (`81:BACF`) — wrong preview | INFERRED | | 05 | ペンタリス | 81:C251 | opp receives **5 garbage rows** (same hole) | CONFIRMED (5 rows) | | 06 | ダイナシ | 86:C23B | status: spider web drawn over opp's field (predicate `86:C3B9`) | CONFIRMED visual | | 07 | フライ Fly | 86:CC84 | fills the holes of own bottom 4 rows → they clear (and count as attack) | CONFIRMED | | 08 | ノロイ Curse | 86:E5CF | status: every opp piece cycle a **cursed stone row (value 4) rises** (`86:E9AF`); opp's garbage also stone, new hole each row (`86:E645`) | CONFIRMED code | | 09 | バンパイア | 81:F362 | **steals 1 meter coin** from opp (opp $1F −1, own +1) | CONFIRMED | | 0A | クリアー Clear | 81:87D4 | **clears own entire field** | CONFIRMED | | 0B | シーソー Seesaw | 81:C68C | own bottom rows moved to opp (own −2 rows, opp +2 identical rows) | CONFIRMED | | 0C/0D | (empty) | 80:D33F | nothing (no cast) | CONFIRMED | | 0E | カット Cut | 81:8C1C | removes the **top 4 rows** of own stack | CONFIRMED | | 0F | モーゼ Moses | 86:DD6F | clears **columns 3–6** of own field (parting) | CONFIRMED | | 10 | マンボ Mambo | 81:B789 | status ~540f: opp's field shaken/rearranged | INFERRED | | 11 | スターダスト | 82:CA0D | removes 4 bottom rows of own field (incl. a coin) | CONFIRMED (in this board state) | | 12 | リバース Reverse | 81:C53D | status: opp's **controls reversed** (L/R masks $73/$75 and rotate masks $5F/$67 swapped at spawn, `81:C642`) | CONFIRMED code | | 13 | マヒ Mahi | 81:A106 | status: opp **paralysed** (predicate `81:A153`, piece-move/collision helper `81:A338`; checker tile overlay) | INFERRED | | 14 | カイテン Kaiten | 81:A038 | status: opp's piece **keeps rotating** (`81:A071`, swirl tiles; blocks soft drop path in `81:9DCF`) | INFERRED | | 15 | ヨセ Yose | 86:F996 | **pushes all own blocks to the left** (compacts rows) | CONFIRMED | | 16 | ギャング Gang | 81:97CD | **removes the coin blocks** from opp's field | CONFIRMED | | 17 | ドトン Doton | 86:C733 | status: on own line clears, bottom row (21) also forced cleared (`86:CA78` sets DP $33=$15) | CONFIRMED code | | 18 | プレス Press | 86:E9C8 | crushes own stack (top rows pressed down / removed) | CONFIRMED (shape) | | 19 | ソル Sol | 82:C33A | clears columns ~3–5 of own lower field | CONFIRMED (shape) | | 1A | スリトテ | 82:BD3C | **inverts opp's occupied rows** (filled↔empty, coins kept) | CONFIRMED | | 1B | ファックス Fax | 82:8000 | opp's field becomes a copy of own field | CONFIRMED | | 1C | シールド Shield | 86:F07D | status: own field **blocks incoming garbage** (`86:F0AC` in state 10) | CONFIRMED code | | 1D | ミラー Mirror | 82:8D2B | status: when opp casts, power is **reflected back** (`82:8E0A` re-dispatch) | INFERRED | | 1E | ヘビー Heavy (unused) | 82:8C8C | status: opp piece gravity/lock override (`82:8CA7` vs DP $79/$7A); piece gfx 7 | INFERRED | | 1F | セレクト Select | 82:BBD2 | status: caster's L/R (`82:BC01`) **changes opp's current piece type** | CONFIRMED code | | 20 | ヌー Nu | 82:8F30 | status: opp **speed level raised** (writes level `7E:3236/3238`, `82:BA8D`) | INFERRED | | 21 | リモコン Remocon | 82:AFFA | status: **caster's pad is ORed into opp's pad** (`82:B039`) — caster steers opp's piece | CONFIRMED code | | 22 | ハンテン | 82:8B92 | status: opp's button masks $6F/$5F/$67 zeroed (no cast/rotate) via `82:8C5F` | INFERRED | | 23 | ナカヨシ | 86:E042 | status: own field drops a row instead of receiving garbage (`86:E1B5`); lines relation INFERRED | INFERRED | | 24 | イノリ Inori | 86:CFB8 | observed same result as 1A (opp rows inverted) in this test | CONFIRMED observation, mechanism unknown | | 25 | ルーレット | 82:B48A | status: roulette; opp's masks cleared like 22 (`82:BA0D`); caster input bits $10D kept | INFERRED | | 26 | フィーバー Fever | 82:8361 | queue forced to one piece type (`7E:3237`, observed all `02`) for DP $28 pieces, palette cycling | CONFIRMED | | 27 | チェンジ Change | 86:BB2B | **swaps the two fields** | CONFIRMED | | 28 | ダブルス Doubles | 86:CB02 | status: own cleared lines count **double** for attack stock (`86:CC4F`) | CONFIRMED code | | 29 | ダーク Dark | 86:C3E6 | status: opp's field darkened (predicate `86:C6DB`) | INFERRED (visual) | | 2A | ハイパーフライ | 86:CE0A | like Fly: fills holes of own bottom rows → clear | CONFIRMED | Halloween's level-4 Bomb was also observed end-to-end with a real Up press (`re/runs/battle_cast1`). ## Open questions - Status-power durations (DP $28 piece counters?) and exact end conditions — only Fever confirmed. - Whether row-removal powers (00, 11, 0E, 18) remove fixed counts or "garbage rows"/"top N" generally — tested on one board. - Stone (4) behaviour: can a full row containing stone clear? (`81:D118` special-cases value 4 in the flash code.) - Mirror/Shield/Mahi/Kaiten/Heavy/Nu details need targeted experiments with a human-controlled victim. - Inori (24) handler logic (possible random effect). - `$0170` coin interval vs options/difficulty (flow notes: settings block).