Files

15 KiB
Raw Permalink Blame History

TBG (J) — game flow, characters, CPU AI

Scope: game-mode flow, character/power data, COM AI, 1P progression. Board/piece/power-effect internals are only touched where needed. CONFIRMED = observed in Mesen (exec/write hooks, RAM pokes, screenshots) or read unambiguously from code+data; INFERRED = read from code, not exercised.

WRAM addresses are bank-0 mirrors ($xxxx = 7E:xxxx). Code uses DBR=$80–$88, so a Mesen write watch on $00E0 misses writes — watch the 80:–88: mirrors too (see re/lua/flow_watch.lua).

Tooling added (all in this fork's namespace)

  • re/lua/flow_drive.lua — drive.lua clone whose savestates actually work. Mesen only allows createSavestate/loadSavestate inside an exec callback; drive.lua calls them from endFrame, so re/runs/battle1/state.mss is 0 bytes. flow_drive hooks the NMI stub 00:81B6.
  • re/lua/flow_covflags.lua — records every executed ROM PC with its M/X/E flags (fast: one getState per new PC; 12000 frames from boot ≈ 45 s).
  • re/lua/flow_trace.lua (exec-hit log), flow_cov.lua (write-PC watch + PC set), flow_watch.lua (write log incl. mirrors), flow_boss.lua (poke 1P progression), flow_diff.lua (poke difficulty).
  • re/ghidra_scripts/FlowSeedFromCov.java — seeds disassembly from a coverage dump with the observed context; FlowDumpListing.java — dumps all instructions (not just function bodies).
  • Private Ghidra project re/runs/flow_ghidra/flowtbg (shared ghidra/tbg untouched), seeded with re/runs/flow_ghidra/cov_all.txt (boot→title→select→map→2 battle rounds): 21,963 → 28,765 instructions. Full listing: re/runs/flow_ghidra/lst/bank_XX.lst. The AI, both per-player state machines and the scene code below are only present there.
  • Savestates: re/runs/flow_state/state.mss (battle, f4000, Halloween vs Mirurun), re/runs/flow_st_select/state.mss (char select, f1900).

1. Game flow

Top-level scene sequencer (CONFIRMED by exec hooks, re/runs/flow_1/trace.txt)

No single mode variable: 80:8047 is a straight-line loop of scene subroutines that return a status in A.

Address Scene First hit (boot+Start/A plan) NMI handler at [$0080]
80:8084 boot logos/copyright f73 80:8AC8 (installed 80:88D0)
83:ECE5 title menu; A=1 → 83:A756, else demo 83:B634 on timeout f840 —
83:8000 character select ("book"); returns 1 = back to title f1521
84:8000 1P Battle World map / VS intro (skipped when $DE&2=0) f2280 84:A23D (installed 84:84E8, 84:87C3)
80:8554,80:85BA,81:C4E7,80:8355,80:8275 battle setup (fades, VRAM, stage BG) f3305 81:BCCB (installed 80:856D) from f3440
81:A443 battle (rounds loop) f3758 81:BCCB
84:F25A results/continue; returns FF game over, FE continue, FA ending, FD win
83:B747 ending (after FA), then JMP 80:8084

After 84:F25A (80:812E): FF → sound cmd $1A, back to title; FA → ending; otherwise 1P ($DE&2) loops to 80:8105 (next map/battle), 2P loops to 80:80FD (char select again).

Mode flags

  • $DE (CONFIRMED): bit1 = vs COM (1P), bit0 = attract demo. Title menu 83:EF91: $DE = (cursor ^ 1) << 1 with cursor 7F:0000 (0 = ひとりでバトル → $DE=2, 1 = ふたりでバトル → 0). Demo 83:B634 sets $DE=3 and forces $D2=1 for the duration.
  • Settings block 7F:2108.. copied in by 80:90B8 (INFERRED = options screen storage): $F5 time level-up on/off, $170/$171, $D2 difficulty, $CF bits 4-5, $D9 stage override, P1 level $F6 (+$F8), P1 power-cost base → $22D..$230 = n,2n,3n,4n, P2 level $F7, P2 cost base → $48D... Defaults (80:815B, CONFIRMED in RAM): $D2=0, $F5=1, $170=$171=5→$171=1 in battle, costs 1/2/3/4.

Battle loop 81:A443 (CONFIRMED structure)

  • Round setup 81:A47D: P1/P2 init ($114 = 2/1/7/6 sprite setup), 82:BB49 loads level params, 81:A5D1 round start, shared queue $16F,$16B,$16C,$16D filled by 4× 81:D396 (piece RNG; board fork).
  • Per frame 81:A548: globals (82:91BF, 82:9233, 82:DD07, 82:8665, 82:842E, 82:B153, 82:BA34 level-up-by-time), then for each player: select context, 82:DC13, 86:E203, 86:F1FB, then the player state machine. Result in $0B (DP): FF → match over (RTL 0), FE → next round (81:A47D).
  • Player context = direct page: 81:81C3 sets D=$0202 (P1), 81:81CC D=$0462 (P2); $12A = current player 0/1. So P1 field at DP $xx = $0202+xx, P2 = $0462+xx (stride $260).
  • Player state machine: state byte DP $0B ($020D/$046D) indexes 24-bit pointer tables 81:A8CC (P1) / 81:B02E (P2); dispatched by JML [$0124] at 81:A6BE/81:A6E3 (this is what the $0124 writers 81:A6AD/81:A6D2 are). P1 and P2 have separate code copies of every state. First 24 entries (table may be longer, ≤42): P1 00:A9E6 01:AA1A 02:AA4F 03:AA66 04:AA7F 05:AB73 06:ACD0 07:ABF2 08:ABBE 09:ACDA 0A:AD00 0B:AD71 0C:ADEE 0D:AE03 0E:AE0C 0F:A94C 10:A9BC 11:AE21 12:AE2D 13:AC26 14:AF1F 15:AF55 16:AED6 17:AF90 (bank 81) P2 same layout at 81:B148…81:B6EB. State 05 = piece under control (CONFIRMED: AI/pad merge there); states 02/03 init (81:AA5B/81:B1BD set state 3, $169/$16A=16). Spawn state calls the AI planner (81:AB20/81:B282).
  • Third $0124 user: 82:8829 → power activation dispatch (80:D340 table, 2×24-bit per power), also 82:D3E6 (80:D2BE), 82:DA0A/DA49 (80:D340/D343), 82:DC10 (80:DB7B, AI power predicates). Power fork territory.
  • Round wins: DP $22 ($224 P1, $484 P2); 2 wins = match (CONFIRMED in 84:F36A, screenshots show round-win heart in centre column).
  • Battle clock $8A:$8B:$8C:$8D = h:m:s:frames (81:C196, from NMI). If $F5, at minutes 3..10 (82:BAE7 table) both players' level $F6/$F7 +1 (cap 7) — 82:BA34/82:BA60.
  • Joypads: auto-read copied to $12B (P1) / $12D (P2) by 81:D046 (in NMI). 82:B02E merges the opponent pad into yours when your status DP $27 = $21 (a control-swap power); in 1P the "opponent pad" is $1F7 (COM). Per-player configurable button masks at DP $5F..$6F (default A=0080 X=0040 L=0020 R=0010 B=8000 Y=4000 Sel=2000 Start=1000 Up=0800), consumed by 81:9C50 → edge/hold flags $12F/$131/$139/$13B/$141/$143,X (+$145..$157,X).

Level (speed) table — CONFIRMED data, field use INFERRED

$F6/$F7 are speed levels 0–7, not characters. 82:BAEF copies the 8×7 table 82:BB8A → RAM 7E:326D.. (7 arrays of 8); 82:BB49 loads level A into DP $77,$78,$79,$7A,$7F,$7C,$7B. <$77 → gravity frames via 81:A654 ($40/(n+1) style divide 81:80D7 → DP $14), <$78 → 81:A678 → DP $18.

lvl 77 78 79 7A 7F 7C 7B
0 0D 00 10 04 02 07 04
1 0D 01 20 04 02 07 04
2 0D 02 30 04 02 07 04
3 0D 04 3A 04 02 07 03
4 0F 07 3A 04 02 07 03
5 10 0C 30 04 03 07 03
6 13 15 20 04 03 07 03
7 15 1B 20 04 03 07 03

2. Characters and powers (CONFIRMED tables; names decoded half-width katakana via cp932)

Character IDs: $E0 = P1, $E1 = P2/opponent (char select writes $E1 = $80|cursor at 83:8F90, then $E0 = $E1&7F at 83:80D6). $EE = mirror match ($E0==$E1).

id character power list (83:A2B8 → FF-terminated)
0 Halloween (ハロウィン) 07 フライ, 29 ダーク, 09 バンパイア, 03 ボム
1 Mirurun (ミルルン) 00 ミルルン, 22 ハンテン, 05 ペンタリス, 1A スリトテ
2 Shaman 0F モーゼ, 14 カイテン, 08 ノロイ, 24 イノリ
3 Aladdin 0B シーソー, 1C シールド, 1B ファックス, 21 リモコン
4 Princess 19 ソル, 1D ミラー, 13 マヒ, 01 コピー
5 Bit 11 スターダスト, 26 フィーバー, 16 ギャング, 10 マンボ
6 Ninja 15 ヨセ, 04 マヤカシ, 06 ダイナシ, 02 メデューサ
7 Wolfman (オオカミオトコ) 0E カット, 20 ヌー, 23 ナカヨシ, 17 ドトン
8 Dragon (1P sub-boss; screenshot re/runs/flow_boss0) 18 プレス, 1F セレクト, 25 ルーレット, 27 チェンジ
9 Queen (1P final boss, crown/purple hair; re/runs/flow_boss1; name INFERRED) 2A ハイパーフライ, 12 リバース, 28 ダブルス, 0A クリアー
  • 43 power IDs 00..2A; 0C,0D empty (both → 80:DBFC), 1E ヘビー unused by any character.
  • Power name table 80:D442: 16 bytes/power = 8 words, low byte = JIS X 0201 kana, A5 pad; tile = byte+$BB.
  • Battle copies the list to DP $2F..$32 ($231.. P1 at 83:9379, $491.. P2 at 83:93C3). Power meter = DP $1F ($221/$481); level-k power is affordable when meter ≥ DP $2B+k ($22D../$48D.., default 1,2,3,4 — CONFIRMED in RAM). In 1P the COM copies P1's costs (81:A4BF) and P1's level (81:A470).
  • 83:A221 copies a 14-word name tilemap (83:A303, 8 entries only → chars 8/9 not selectable); 83:A255 builds the char-select power list from 80:D442.
  • Portrait mood index = char*3 + bucket (82:A976), bucket from stack height (82:AA0F: <10, <18, ≥18).
  • Stage/background = opponent $E1 unless $D9 override (80:8355, 80:8339).
  • No per-character AI parameter record found; characters differ to the AI only through their powers (per-power predicate table 80:DB7B, 43×24-bit). INFERRED.

3. CPU AI

Runs for any player in state 05/spawn when $DE&2 (P2) — P1's handlers call the same code, used by the demo. All AI state for the moving piece lives in the player DP.

Planner 81:D9BA (once per piece; CONFIRMED via write hooks: writes $19D..$1A6 at 81:DBED,

target at 81:DA82/DA87) Workspace: D=$019D.

  1. Logic board: byte boards $0282..$0371 (P1) / $04E2..$05D1 (P2), 10×24, 1 byte/cell (cleared by 81:D98B); planner reads from row 2 ($296/$4F6). The $05A0..$05CF lead is the bottom of P2's logic board. (Board fork: this is the authoritative collision board; $7E:2280 is its tilemap copy.)
  2. 81:DBB9: column heights → $19D..$1A6 (22 − first filled row).
  3. 81:DC02: columns sorted by height → $1A7.. heights asc, $1B1.. column ids, $1BB.. rank per column.
  4. 81:DC59: surface profile (neighbour height deltas) → $1C5.. (DP $28..$2B).
  5. Special checks 81:DE38 (per-piece table 81:E4E9) and 81:DFA5; flag DP $56 = handled. While searching, player status bit6 ($228/$488 |= $40) is set.
  6. If ≥5 of columns 1..8 have height ≥ 18 → panic search 81:DA9F: brute force rotation 0..3 × column 0..9 × candidate rows (table 81:EC1F per piece), reject via collision 81:A338, 81:E2EF/81:E3E3/81:E45C, keep the placement with the lowest resulting height (DP $41, best in $1F9..$1FB). Else pattern fit 81:DB40: for pattern set 0..4 (table 81:E7C2 indexed by piece DP $3D) × column × variant, 81:E0E9 matches the piece's underside against the surface profile; first valid fit wins. Fallback: lowest-ranked column, rotation 0 (81:DB7D).
  7. Result $1DD → player DP $25 (target column), $1DC → DP $24; target rotation in DP $07.

AI flags DP $26 ($228/$488), rebuilt each piece by 82:D1FB (INFERRED semantics)

  • bit0 "use power now": meter>0, status DP $27=FF (none), not searching; set if stack height (82:D159, highest block in cols 3–6) ≥ 21, or the predicate for the best affordable power (82:D2D7 → 82:DBEE → 80:DB7B[id]) says yes; $D2==3 also inspects the opponent (82:974A); $D2==0 otherwise never.
  • bit1: $D2≥2 always; $D2==1 random (82:D19C: sum of four RNG rolls ≥ 50); $D2==0 never.
  • bit2: $D2≥2. bit3: $D2≥2 and 82:D326. bit4: stack height ≥ 10.

Controller 81:8A52 (every frame in state 05; CONFIRMED it writes the virtual pad)

Emits one button word into $1F1; P2's state 05 copies it to $12D and $1F7 (81:B2E0–B2E6).

  • bit0 set → 81:8BCE (affordable & DP $23==0) → Up (0800) = activate power.
  • else 82:BD18 → L (0020) (INFERRED: power-select / stock key; not for piece types 05/0C).
  • rotation ≠ target → A (0080) or B (8000); direction picked from current rotation & column (wall side) and inverted when DP $0D is set (INFERRED: reversed-controls status).
  • else compare piece column $1DF (81:E1B6) with target → Left 0200 / Right 0100.
  • aligned → 81:8B5B: with bit2 (hard AI) and no danger → Down 0400 soft drop (81:8AD1/81:8BB9; at $D2==0 a countdown $D3 throttles it); otherwise nothing (gravity).
  • danger (any column 1..8 height > 21, 81:8A9E) uses a reduced shift-only path 81:8B2A.
  • Next-piece look-ahead: $16B/$16C (queue) checked for types 05/0C and ≥7 (81:8B91, 81:8C08).

Difficulty knobs

  • $D2 global difficulty 0–3 (options; default 0; demo 1). No per-opponent ramp found in 1P: only writers are init 80:816A, settings 80:90CD, demo 83:B660/B67E. INFERRED.
  • Indirect ramp: speed level by battle time ($F5), and later opponents' power sets.
  • Quick measurement (re/lua/flow_diff.lua, 1200 frames from battle state, poking $D2): pieces/soft-drop frames = D0 5/427, D1 8/1158, D2 5/486, D3 5/486 — behaviour changes but the bit meanings above need a longer controlled run to confirm.

4. 1P progression / 2P

  • 1P (CONFIRMED by write hooks + pokes): $E2 = beaten bitmask, own char's bit pre-set (83:80ED). Map 84:8AAD: next opponent = lowest char id with bit clear (84:8ADF) → Halloween player meets Mirurun, Shaman, Aladdin, Princess, Bit, Ninja, Wolfman in id order (first observed $E1=1 at 84:8AF1). Win 84:FCDB sets the bit; beating char 8 sets $E3=1 instead. 84:8000: $E2≠FF → map; $E2=FF,$E3=0 → Dragon intro 84:8128; $E3=1 → Queen intro 84:81AC. Beating Queen with 2 round wins → 84:F36A returns FA → ending.
  • Continue screen 84:FBxx (cursor 7F:F000): continue → FE and $E4[opponent]++ (cap 99); no → FF. For bosses, $EC/$ED ≠ 0 (already continued) skips the intro and replays the battle directly.
  • 2P ($DE=0): no AI, no map; 84:8000 returns immediately; after results the loop returns to char select (80:80FD). Separate costs/levels per player from the settings block (in 1P they are copied from P1 to COM).

Open questions

  • Exact meaning of player states 00–17+ (board fork) and AI flag bits 1–3 in the controller paths.
  • Candidate tables 81:EC1F, 81:E7C2, 81:E4E9, 81:E4F0 formats (needed to reimplement the AI byte-exactly).
  • 82:BD18 / 82:BCF5 (L press) semantics; DP $0D, $23, $27 status codes ($20,$21,$1F seen).
  • Options screen → 7F:2108 block layout (where $D2, $F5, $170 are set by the player).
  • Queen's official name; whether chars 8/9 are playable in 2P (cheat) — tables support it.
  • $170/$171 (1..5 cycling counter used at round start) purpose.