# Core mechanics — board, pieces, queue, RNG, gravity Status legend: **[C]** confirmed in Mesen (WRAM logs / write traces / model check), **[I]** inferred from code. Code listing used: `re/runs/mech_1/battle.asm` — exact disassembly of every instruction executed during battle (`re/covdis.py` over Mesen coverage `re/runs/mech_1/cov_all.txt`, flags captured per PC). Range helper: `re/asmrange.py FILE LO HI`. Savestate (battle, Halloween vs Mirurun, level 0): `re/runs/battle1/state.mss` (regenerated with the exec-callback save — `re/lua/mech_lib.lua`). ## Player context All piece/board code runs with **D = player base**: P1 `$0202`, P2 `$0462` (set by `81:81C3` / `81:81CC`). `$012A` = current player index (0 = P1, 1 = P2); toggled by `81:81EC`. [C] Below, `DP+xx` = base + xx. | DP | meaning | | |---|---|---| | +00/01 | pointer (bank 81) to the active 4×4 shape matrix | [C] | | +02 | pixel X (screen) | [C] | | +04 | OAM attribute for the piece sprite (P1 `$22`, P2 `$24`, `$20` while in queue) | [C] | | +05 | pixel Y | [C] | | +06 | piece type 0–13 (7–13 = crystal variant of type−7); during queue animation holds a preview index | [C] | | +07 | rotation 0–3 | [C] | | +08 | column in screen tiles (`X>>3`); board col 0 = 4 (P1) / `$12` (P2) | [C] | | +09 | row (`(Y>>3)+1`); board row r ↔ offset `(r-1)*10` from DP+80 | [C] | | +0A | second row checked when Y is not cell-aligned (0 if aligned) | [C] | | +0B | piece state (see below) | [C] | | +0C | collision result of last test (nonzero = blocked) / grounded | [C] | | +10/11 | horizontal slide step / progress within cell (max 8) | [C] | | +12/13, +14/15 | horizontal 8.8 accumulator / speed | [C] | | +16/17, +18/19 | gravity 8.8 accumulator / speed; integer carry → +1A = px to fall this frame | [C] | | +1B | timer from level byte 2 (`DP+79`) — not observed changing; purpose open | [I] | | +1C | lock delay counter (from `DP+7A`, 4 at level 0), counts down while grounded | [C] | | +1D | spawn "entry" counter (from `DP+7C`, 7) — while nonzero, horizontal moves are instant (8 px) and lock delay uses `DP+7D` instead of `DP+7B` (`81:A02E`) | [C]/[I] | | +1F | crystal/power meter, +1 per cleared crystal cell, capped at 20 (`81:D137`); compared with +2E | [I] | | +33..36 | indices of full rows found by the clear scan (`$FF` = none) | [I] | | +77..7F | current speed-level parameters (see Gravity) | [C] | | +80..+16F | **board**: 24 rows × 10 bytes, top row first (P1 `$0282`, P2 `$04E2`) | [C] | | +94 | first of the 22 rows that are rendered (row 3) | [C] | Rendered copy: `81:C0C6` packs (tile = `DP+94+i`, attr = `$0386+i` / `$05E6+i`) into tilemap words at `7E:2280` / `7E:2438` (220 cells), DMA'd by `81:C0F7` / `81:C140` to VRAM `$7864` / `$7872`. [C] That buffer is display-only; game logic uses the byte boards. Cell values seen: `0` empty, `1` normal block, `6` crystal (counts toward meter when cleared), `4` checked specially during clear (`81:D118`; meaning open), garbage cells get the value passed in `$0114` to `81:D4B9`. [I] ### Piece state machine (DP+0B) [C] `05` falling → (`06` → `16` one frame each = rotate) / (`07`/`08` = sliding right/left) → grounded: lock delay DP+1C counts 4…0 → `09` → `0E`/`0A` (lock + clear check, a few frames) → `00` → `01`, `02`, `03` (next piece animates out of the centre queue, 17 / 17 / 16 frames; `$0169` counts) → `04` → `05` with the new piece at row 0. Dispatch tables: `81:A8CC` (P1) / `81:B02E` (P2), 3-byte entries. ## Collision, lock, line clear - **Collision test** `81:CEAD` [C]: for each of 16 cells i of the shape (`[DP+00],Y`) that is nonzero: col = DP+08 + (i & 3), row = DP+09 + (i >> 2); also tests row DP+0A + (i >> 2) if DP+0A ≠ 0. Cell test `81:CE6C`: wall if col < 4 or col == 14 (P1; `$12` / `$1C` for P2); floor if row ≥ 25; rows < 3 are never blocked; else board[`DP+94` + (row−3)*10 + (col−4)] ≠ 0. Any hit → `INC DP+0C`. - **Lock / write** `81:D004` + `81:CFAA`: writes each nonzero shape byte (its value, so crystals stay 6) into the board at DP+94-based index, or DP+80-based for rows 1–2 (the hidden rows). [C] - **Full-row scan** `81:D06B`: rows 0..21 of the DP+94 area; a row is full when all 10 cells ≠ 0; up to 4 full row indices stored in DP+33..36, count returned in A, `INC DP+20` per row (lines counter?). [I] - **Clear animation / crystal count** `81:D0E0`: for each full row overwrite cells with flash tile from `81:D150[$0114]`; counts cells == 6 → added to DP+1F (cap 20). [I] - **Collapse** `81:D159`: for each full row (DP+33..), copy every row above it down by one (`DP+8A+x → DP+94+x`, top to the row) then clear the top row. Standard naive gravity (no cascade). [I] ## Shapes & rotation Shape pointer table `81:8218`, word index `type*8 + rot*2` (`81:81F7`), pointing at 16-byte row-major 4×4 matrices in bank 81. [C] ``` type 0 O .XX. .XX. (all 4 rotations identical) type 1 L r0 ..X. XXX. r1 .X.. .X.. .XX. r2 .... XXX. X... r3 XX.. .X.. .X.. type 2 J r0 X... XXX. r1 .XX. .X.. .X.. r2 .... XXX. ..X. r3 .X.. .X.. XX.. type 3 S r0 .... .XX. XX.. r1 X... XX.. .X.. r2 .XX. XX.. r3 .X.. .XX. ..X. type 4 Z r0 .... XX.. .XX. r1 .X.. XX.. X... r2 XX.. .XX. r3 ..X. .XX. .X.. type 5 I r0 .... .... XXXX r1 ..X. ..X. ..X. ..X. r2 .... XXXX r3 .X.. .X.. .X.. .X.. type 6 T r0 .X.. XXX. r1 .X.. .XX. .X.. r2 .... XXX. .X.. r3 .X.. XX.. .X.. ``` (rows listed top→bottom, 4 chars each; missing trailing rows are empty.) Note this is **not SRS** — S/Z/I states shift around inside the box. Types 7–13 use the same shapes with one cell = `6` (crystal), e.g. type 7 r0 `.61.` / `.11.`; full dump in `re/runs/mech_1/` or regenerate from `81:8218`. Rotation [C]: A = `81:CCD7` rot+1 (mod 4), B = `81:CD11` rot−1. Rotated shape is collision-tested; on failure the old rotation is restored — **no wall kicks, no floor kicks**. Sound cmd `$14` on success. Horizontal moves `81:CD4B` / `81:CD70`: shift, test, revert on collision. [C] Spawn [C]: after the 3-stage queue animation, the piece starts at Y = 0 (row 0, hidden) with the 4×4 box's left edge at P1 pixel X = `$38` → screen col 7 → **board col 3** (box covers board cols 3–6). ## Speed levels / gravity Speed is a per-player **level 0–7** (`$00F6` P1, `$00F7` P2, `$00F8` also used; all 0 in the test match), applied by `82:BB49` from RAM tables `7E:326D..329D` (copied at boot by `82:BAEF` from ROM `82:BB9A`, 7 bytes/level). [C] | lvl | DP+77 h-spd | DP+78 grav | DP+79 | DP+7A lock | DP+7F | DP+7C entry | DP+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 | (ROM byte order is 77,78,79,7A,7F,7C,7B; DP+7D = 0, DP+7E = 8 always.) Speeds via 16×16 multiply `81:80D7`: **gravity = `$40 × (grav+1)` 8.8 px/frame**, i.e. 0.25 px/frame at level 0 (1 cell per 32 frames) up to 7 px/frame at level 7; **horizontal slide = `$40 × (hspd+1)`** (3.5 px/frame at level 0, capped at 8 px = one cell per move). [C at level 0, formula from code] Soft drop (Down): 8 px (1 row) per frame after a 2-frame delay. [C] DP+7F goes to `$0159` (soft-drop or DAS related, unconfirmed). Auto-repeat observed: first repeat 7 frames after the initial move, then every 3 frames (slide animation length). [C] No hard drop observed (Up produced a one-frame state `16`, no move). [C] ## Shared piece queue Single queue shared by both players — no per-player index; whichever player finishes locking first calls the shift. [C from code + logs] - Slots: `$016B` (next), `$016C`, `$016D` (newest); `$016E` temp; `$016F` = piece just handed out. - `81:D36F` shift: taken = `$016B`; `$016B←$016C`, `$016C←$016D`; generate (`81:D396`) into `$016F`; `$016D←$016F` (new); `$016F←taken`. - Generator `81:D396` [C]: unless `82:86F2` returns nonzero (power-related override) — `do { n = rng() & 7 } while (n == 7 || n == $016D /*previous newest*/)` → **no immediate repeats**, otherwise uniform-ish over 7. Then unless (`$00CF & $30) == $10` or `$0170 == 0`: `if (--$0171 == 0) { n += 7; $0171 = $0170; play sfx $14 }` → **every `$0170`-th generated piece is a crystal piece** (`$0170` = 5 in the test match; set at `81:A3D1`/`81:A531`, likely per-character/stage). - Preview drawing `81:D299` draws `$016F`, `$016B`, `$016C`, `$016D` at centre column X=`$78`, Y=`$20/$30/$40/$50` using 2-tile preview sprites from table `81:D261` (index DP+06). ## RNG `81:81A2` (shared by queue, garbage holes, AI `82:D19C..`, others in bank 83) State `i = $0122`, `h = $0123`; called with DBR = `$81`. Exact semantics (model verified against 18/18 logged calls) [C]: ``` X = h<<8 | i // 16-bit A1 = T[i] + MEM[0x81:0x8000 + X] // T = 256-byte table at 83:EBD1; 8-bit add, carry c X += 1 i = X & 0xFF h = (A1 + MEM[0x81:0x4000 + X] + c) & 0xFF // address may carry into bank 0x82 return A1 ``` MEM reads hit ROM (most of the time), open bus (returns the instruction's operand high byte `$80`/`$40` in Mesen), or — when the effective address lands in `82:0000-1FFF` — **low WRAM**, so the stream depends on game state (7/25 calls in a sample). No explicit seed write found; `$0122/$0123` are only written by the RNG. For a reimplementation: either emulate it with a RAM snapshot, or (recommended) replace with any good PRNG — nothing observed depends on exact values. ## Garbage rows (mechanics only) `81:D48E` push-up: shift whole board up one row (`81:D442`), fill bottom row from `$0192..$019B`. Row pattern `81:D4B9`: reuses the same pattern for 10 consecutive rows (`$019C` counter 9…0); new pattern = 10 cells of the garbage value with one hole at column `(rng()&7) + (rng()&1)` → columns 0–8 (0 and 8 at half probability, never column 9). [I] (How many rows are sent per clear is battle-system territory.) ## Open questions - Purpose of DP+1B (level byte 2: `$10..$3A`), DP+7F (→ `$0159`), DP+7B vs DP+7D lock-delay pair, cell value 4. - What Up / X / Y / L / R do during play (Up → state `16` for one frame only). - `82:86F2` queue override conditions (reads `$00EF`, `$0229`/`$0489` == `$26` — likely a power forcing pieces). - Level progression: who writes `$00F6/$00F7` mid-match (callers of `82:BB49`: `81:A3DA`, `81:A3FF`, `81:A48D`, `81:A4D1`, `82:916C`, `82:BAA0`, `82:BAD5`). - Scoring: none found in the board/queue code; DP+20 increments per cleared row (lines?).