From 85d2da363a1119508d125649bf7b6c02b048938d Mon Sep 17 00:00:00 2001 From: acamilo Date: Thu, 1 Oct 2026 12:57:03 +0000 Subject: [PATCH] macros: the battle bag's cursor is the entry, not the window's row (row 71) The battle bag is a DisplayListMenuID list: the arrow walks rows 0..2 and the window scrolls under it. state::battle reported wCurrentMenuItem alone as the bag cursor, so THROW BALL and ITEM could never reach a bag entry past the third. Live on rung 15 a wild Oddish sat at low HP with the Poke Ball fifth in the bag; row 69 left THROW BALL alone on the pad and every start blocked. The cursor is now wCurrentMenuItem + wListScrollOffset, the sum the cartridge makes on A. wListScrollOffset ($CC36) is pinned from the operand of DisplayListMenuIDLoop's read and checked by a ROM test. The ROM test from the live trap checkpoint (FLY_ROW71_CHECKPOINT) blocks on main and throws and catches on this branch. --- .../src/pokemon_red/macros/state.rs | 3 + .../flybrain-gb/src/pokemon_red/state.rs | 24 +++++- .../src/pokemon_red/state/tests.rs | 12 +++ .../flysim/crates/flysim/tests/rom_catch.rs | 82 +++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/state.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/state.rs index 853c01d..2558e4b 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/state.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/macros/state.rs @@ -239,6 +239,9 @@ pub enum BattleMenu { /// `ITEM` and section 14's `THROW BALL` navigate by (2026-09-17). Then it needed to be the /// fly's *turn*: a bag reading as nobody's turn landed on the between-turns row, whose `NEXT` /// is the A that advances text and on an open bag is the A that uses an item (12.10). + /// + /// `cursor` is the 0-based bag entry the arrow is on -- the window's row plus the list's + /// scroll offset, so the bag's `count`th entry is CANCEL (row 71). Bag { cursor: u8, count: u8 }, } diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs index 860b7b8..a8b660d 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state.rs @@ -50,6 +50,18 @@ pub mod poke { /// reads it back from there (row 63, `docs/design/macros.md` 12.27). pub const H_JOY_LAST: u16 = 0xffb1; + /// `wListScrollOffset`, `$CC36`: how many entries of a `DisplayListMenuID` list are scrolled + /// off the top of its window (row 71, `docs/design/macros.md` 12.34). + /// + /// Not in the reviewed address list, so pinned the way [`H_JOY_LAST`] is: read from the + /// operands of the cartridge's own instructions and checked by the ROM-gated test. + /// `DisplayListMenuIDLoop.buttonAPressed` turns the cursor into the entry it selects with + /// `ld a, [wCurrentMenuItem] / ld c, a / ld a, [wListScrollOffset] / add c / ld c, a` + /// (`home/list_menu.asm`), whose second load is `$CC36`. The cursor walks the window's rows + /// `0, 1, 2` and the window scrolls under it, so the entry under the cursor is the row plus + /// this. + pub const LIST_SCROLL_OFFSET: u16 = 0xcc36; + /// `ram/hram.asm`: `hWhoseTurn`, "0 on player's turn, 1 on enemy's turn". Counted from the /// `hJoyLast` anchor above through the declarations that follow it (each `UNION` at its /// largest member) to `$FFF3`; the section's remaining nine bytes then end at `$FFFE`, the @@ -687,8 +699,18 @@ pub fn battle(memory: &mut dyn MemoryReader) -> Option { // Pokédex page, the nickname offer and the naming screen -- read as an open bag, the pad // was `BACK` and `THROW BALL`, and the ball's A presses typed the nickname: 6,900 frames // from the catch to the end of the battle, under a `THROW BALL` that threw nothing. + // + // **And the cursor is the bag entry, not the window's row** (row 71). `wCurrentMenuItem` + // is the row of the window the arrow is on, `0..=2`, and the window scrolls under it, so + // the entry is that row plus [`poke::LIST_SCROLL_OFFSET`] -- the sum the cartridge itself + // makes when A selects. Read as the row alone, a ball fourth or later in the bag was a + // target no cursor reading could ever equal: live on rung 15, a wild Oddish at 8 of 36, + // POKé BALL fifth in the bag, `THROW BALL` alone on the pad (row 69's throw at low + // HP) and every start `blocked` while the arrow sat on the ball itself, 131 of 131 in ten + // minutes. let count = bag(memory).len().min(usize::from(poke::BAG_CAPACITY)); - BattleMenu::Bag { cursor: cursor.current, count: u8::try_from(count).unwrap_or(0) } + let entry = cursor.current.saturating_add(read(memory, poke::LIST_SCROLL_OFFSET)); + BattleMenu::Bag { cursor: entry, count: u8::try_from(count).unwrap_or(0) } } else { BattleMenu::None }; diff --git a/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs b/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs index 6ec67ad..bb7af83 100644 --- a/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs +++ b/services/flysim/crates/flybrain-gb/src/pokemon_red/state/tests.rs @@ -420,6 +420,18 @@ fn a_battle_frame_with_a_cursor_accepting_input_is_the_flys_turn() { assert_eq!(fight.menu, BattleMenu::Bag { cursor: 0, count: 1 }); assert!(fight.own_turn, "the battle bag is a cursor accepting input"); + // Row 71: the window scrolls under the arrow, and the cursor is the entry, not the row. The + // live bag: TM34, HELIX FOSSIL, NUGGET and TM45 in front and the POKé BALL fifth, scrolled by + // three so the window shows TM45, the ball and CANCEL with the arrow on the ball. + use crate::pokemon_red::macros::cartridge::item; + wram.bag(&[(0xea, 1), (0x2a, 1), (0x31, 1), (0xf5, 1), (item::POKE_BALL, 2)]) + .set(ram::wCurrentMenuItem, 1) + .set(poke::LIST_SCROLL_OFFSET, 3); + let fight = battle(&mut wram).unwrap(); + assert_eq!(fight.menu, BattleMenu::Bag { cursor: 4, count: 5 }, "row 1 of a window scrolled by 3"); + wram.set(poke::LIST_SCROLL_OFFSET, 0); + assert_eq!(battle(&mut wram).unwrap().menu, BattleMenu::Bag { cursor: 1, count: 5 }); + // And the two frames that are not a choice. A move list whose cursor cannot be placed is a // battle's opening frames (row 30b), and no menu at all is text, an animation or a turn // resolving. diff --git a/services/flysim/crates/flysim/tests/rom_catch.rs b/services/flysim/crates/flysim/tests/rom_catch.rs index 80303a2..1003225 100644 --- a/services/flysim/crates/flysim/tests/rom_catch.rs +++ b/services/flysim/crates/flysim/tests/rom_catch.rs @@ -807,3 +807,85 @@ fn row69_the_naming_bytes_are_where_the_cartridge_stores_them() { assert!(rom.windows(submit.len()).any(|window| window == submit), "wNamingScreenSubmitName"); assert_eq!(poke::NAMING_SUBMIT, poke::NAMING_LENGTH + 1, "consecutive in the UNION"); } + +fn row71_checkpoint() -> Option { + let path = std::env::var_os("FLY_ROW71_CHECKPOINT")?; + Some( + flysim::store::load(std::path::Path::new(&path)) + .expect("the checkpoint should be a FLYSIM01 envelope"), + ) +} + +/// Row 71: a ball past the bag's window is thrown. +/// +/// The live trap (rung 15, Route 25): a wild Oddish at 8 of 36, the bag open and scrolled by +/// three, POKé BALL its fifth entry with the arrow on it. Row 69's low-HP rule leaves +/// `THROW BALL` alone on the pad, and on 390d118 its cursor step compared the bag index (4) with +/// the window's row (`wCurrentMenuItem`, `0..=2`), never met it, and every start was `blocked`: +/// 131 of 131 in ten minutes live. The stub leans on the one bound button, as the live fly could +/// only do. On the branch the first throw catches the Oddish. +#[test] +fn row71_a_ball_past_the_bags_window_is_thrown() { + let Some(rom) = rom() else { + eprintln!("skipped: FLY_ROM is not set"); + return; + }; + let Some(checkpoint) = row71_checkpoint() else { + eprintln!("skipped: no FLY_ROW71_CHECKPOINT"); + return; + }; + let mut run = Run::resume(&rom, &checkpoint); + assert_eq!(run.byte(ram::wIsInBattle), 1, "the trap is a wild battle, not a trainer's"); + let balls = run.balls(); + assert!(balls > 0, "the checkpoint's bag holds a ball"); + let enemy = run.byte(ram::wEnemyMonSpecies); + assert_ne!(run.byte(ram::wCapturedMonSpecies), enemy, "nothing caught yet"); + let mut pads: std::collections::BTreeSet> = Default::default(); + let mut thrown_at = None; + for frame in 0..3_000u32 { + let bound = run.layer.bound_channels(); + if run.layer.running().is_none() { + pads.insert(bound.clone()); + } + let hot = bound.first().cloned(); + let active = + run.decoder.decode_bound(&rates(hot.as_deref()), run.ms, false, None, Some(&bound)); + run.legacy.execute(Some(&mut run.layer), &active, 0, run.ms, &mut run.gb, &run.adapter); + run.ms += MS_PER_FRAME; + run.frame += 1; + let evaluated = run + .legacy + .stub_advance(Some(&mut run.layer), &mut run.gb, &mut run.adapter, run.ms) + .expect("a frame should complete"); + run.payouts.extend(evaluated.rewards); + // The cartridge takes the ball out of the bag only once the catch's screens are over (the + // nickname is the fly's own buttons, which this stub does not press), so the ball keeping + // the Oddish (`wCapturedMonSpecies`) counts as the throw too. + if run.balls() < balls || run.byte(ram::wCapturedMonSpecies) == enemy { + thrown_at = Some(frame); + break; + } + } + let counts = run.layer.counts(); + let caught = run.byte(ram::wCapturedMonSpecies) == enemy; + eprintln!("thrown at {thrown_at:?}; caught {caught}; {counts:?}; pads {pads:?}"); + assert!(thrown_at.is_some(), "the ball was never thrown: {counts:?}"); + assert_eq!(counts.blocked, 0, "a THROW BALL that blocked on the way: {counts:?}"); +} + +/// `wListScrollOffset` is where the cartridge reads it when a list entry is chosen (row 71): +/// `DisplayListMenuIDLoop.buttonAPressed`'s `ld a, [wCurrentMenuItem] / ld c, a / +/// ld a, [wListScrollOffset] / add c / ld c, a`, once in the cartridge. +#[test] +fn row71_the_list_scroll_offset_is_where_the_cartridge_reads_it() { + let Some(rom) = rom() else { + eprintln!("skipped: FLY_ROM is not set"); + return; + }; + use flybrain_gb::pokemon_red::state::poke; + let [lo, hi] = ram::wCurrentMenuItem.to_le_bytes(); + let [slo, shi] = poke::LIST_SCROLL_OFFSET.to_le_bytes(); + let selects = [0xfa, lo, hi, 0x4f, 0xfa, slo, shi, 0x81, 0x4f]; + let found = rom.windows(selects.len()).filter(|window| *window == selects).count(); + assert_eq!(found, 1, "wListScrollOffset's read in DisplayListMenuIDLoop"); +}