legacy_env: MEM-01's bulk read behind the image seam (ENV-01 review R1)

The environment's memory image is now Emulator::read_memory_image: the
register windows read $FF (legacy-gameboy-v1 section 8, MEM-01 amendment)
and the read is neutral by construction, so the export/import guard is
gone. The toy golden's image digests are regenerated (only 'memory'
changes), the neutrality test states MEM-01's rule, and the ENV-01 guard
amendment is struck in legacy-gameboy-v1 and implementation.md. The
service traces still match in all three modes (rollback 3,199, climb
3,156, r58 11,043).
This commit is contained in:
acamilo 2026-09-29 17:07:48 +00:00
parent ae878ede41
commit d034a26be5
5 changed files with 287 additions and 292 deletions

View file

@ -270,7 +270,7 @@ the FLYSIM01 export from the three halves (STATE-02; the world's half is
**Amendment, 2026-09-29 (ENV-01).** Four readings the contract left open, fixed by this slice:
- *The image read is not state-neutral, so it is guarded.* `emulator_read_mem` of OAM, VRAM,
- ~~*The image read is not state-neutral, so it is guarded.* `emulator_read_mem` of OAM, VRAM,
`FF01`/`FF02`, `FF04`-`FF06`, `IF`, `STAT` or `LY` runs binjgb's lazy synchronisation, and
`export_state` afterwards differs (never, over 17,398 replayed frames, a frame or WRAM). The
legacy loop reads none of these between a frame and the ratchet's capture, so an unguarded image
@ -278,7 +278,11 @@ the FLYSIM01 export from the three halves (STATE-02; the world's half is
between `export_state` and `import_state` of the same bytes, which leaves the emulator exactly as
the frame left it; `legacy-gameboy-v1` section 8's proof ("export before and after, compare")
therefore fails for a plain 65,536-read loop and holds for the guarded one. MEM-01's bulk read
must keep that guard (or equivalent).
must keep that guard (or equivalent).~~
*Struck 2026-09-29 (ENV-01 review, R1):* superseded by MEM-01 (legacy-gameboy-v1 section 8
amendment). The register windows are not captured, so `legacy_env::memory_image` is MEM-01's
`Emulator::read_memory_image` with no guard, neutral by construction; the service traces still
match, and the toy golden's image digests were regenerated.
- *A `FLYSIM01` world starts at `k = emulatorFrame - setupFrames`.* The legacy file has no
boundary, world clock or audio position. The frame counter advances once per transition and
never across a rollback, so `engineFrame = k + 1` from a fresh start on; `worldTime` is

View file

@ -249,14 +249,21 @@ is not the environment's `contentDigest`. The emulator answers its own bank read
completes before any `State.Capture` or FLYSIM01 export at that boundary (section 16).
**Amendment, 2026-09-29 (ENV-01).** Two consequences of building this section, recorded in the
[implementation guide](implementation.md) ENV-01 entry: the memory image cannot be taken with a
[implementation guide](implementation.md) ENV-01 entry: ~~the memory image cannot be taken with a
plain loop of `emulator_read_mem`, because reading OAM, VRAM, the serial and timer registers,
`IF`, `STAT` or `LY` advances binjgb's lazy synchronisation and changes what `export_state` writes
-- the environment reads it between an export and an import of the same bytes, so slots and
captures stay byte for byte the legacy loop's; and a `FLYSIM01` world is restored at boundary
captures stay byte for byte the legacy loop's;~~ and a `FLYSIM01` world is restored at boundary
`emulatorFrame - 1`, its world time and audio position derived from that boundary, with the
ratchet's snapshot as the slot `best`.
*Struck 2026-09-29 (ENV-01 review, R1).* The first consequence is superseded by section 8's MEM-01
amendment of the same day: the register windows are not captured and read `$FF`, so the image is
MEM-01's one bulk read with no export/import guard around it, read-only by construction
(`legacy_env::memory_image`). With it the service's traces still reproduce every frame, WRAM and
slot-state digest (rollback 3,199, climb 3,156, r58 11,043 transitions), because the legacy
adapter and macros never read VRAM, OAM or I/O.
## 10. Executor `pokered-macros-v1`
The Pokémon Red task (reward adapter, ladder, ratchet ledger) and its action executor (the

View file

@ -82,34 +82,22 @@ pub fn capabilities() -> Vec<Id> {
// -------------------------------------------------------------------------------------------
// The emulator seams
/// The boundary's memory image: byte `i` is `fly_gb_read_mem(i)` (`legacy-gameboy-v1` section 8),
/// read without leaving a trace in the emulator.
/// The boundary's memory image (`legacy-gameboy-v1` section 8 and its MEM-01 amendment of
/// 2026-09-29): MEM-01's one read-only bulk read, `Emulator::read_memory_image`.
///
/// **The MEM-01 seam.** MEM-01 adds one read-only bulk read to the binjgb shim; when it lands the
/// 65,536 reads below become that one call and nothing else in this worker changes. The interim
/// body is the same function called 65,536 times through the existing `read_uncached`, so the
/// bytes are already the specified ones; the per-frame read cache is neither consulted nor
/// filled.
/// Every memory byte (`flybrain_gb::captured`) is what `fly_gb_read_mem(i)` returns at this
/// boundary; the three register windows -- VRAM, OAM, I/O with the APU and wave RAM -- are not
/// captured and read `$FF` (`flybrain_gb::NOT_CAPTURED`). binjgb answers every captured address
/// straight out of an array, so the read is state-neutral by construction: nothing is saved or
/// imported around it, and `export_state` before and after is byte-identical
/// (`tests/legacy_env.rs`, `the_memory_image_leaves_the_emulator_as_it_found_it`).
///
/// **Why the save and the import.** `emulator_read_mem` is not state-neutral: reading OAM, VRAM,
/// the serial and timer registers, `IF`, `STAT` or `LY` runs binjgb's lazy synchronisation up to
/// the current tick, which changes the bytes `export_state` writes (never, in a 3,199-frame
/// replay, a frame or WRAM). The legacy loop reads none of them between a frame and the ratchet's
/// capture, so a slot saved after an unguarded image read would not be the slot the service
/// saves. The image is therefore read between an `export_state` and an `import_state` of the same
/// bytes, which leaves the emulator exactly as the frame left it (`tests/legacy_env.rs`,
/// `the_memory_image_leaves_the_emulator_as_it_found_it`).
/// (Until the ENV-01 review, 2026-09-29, R1, this body read all 65,536 addresses through
/// `read_uncached` between an `export_state` and an `import_state` of the same bytes, because a
/// read of a register window runs binjgb's lazy catch-up and moves the exported state. MEM-01's
/// rule removes the cause: a register window is never read.)
pub fn memory_image(emulator: &mut Emulator) -> Result<Vec<u8>, String> {
let saved = emulator
.export_state()
.map_err(|e| format!("saving before the image: {e}"))?;
let image = (0..=u16::MAX)
.map(|address| emulator.read_uncached(address))
.collect();
emulator
.import_state(&saved)
.map_err(|e| format!("restoring after the image: {e}"))?;
Ok(image)
Ok(emulator.read_memory_image().as_bytes().to_vec())
}
/// binjgb's unsigned 8-bit interleaved stereo as the declared `f32le-interleaved`, with binjgb's

View file

@ -506,25 +506,21 @@ fn the_memory_image_leaves_the_emulator_as_it_found_it() {
let before = read.export_state().unwrap();
let image = legacy_env::memory_image(&mut read).unwrap();
assert_eq!(image.len(), 65_536);
assert_eq!(
read.export_state().unwrap(),
before,
"the guarded read is neutral"
);
// MEM-01's bulk read is neutral by construction: no guard around it.
assert_eq!(read.export_state().unwrap(), before, "the bulk read is neutral");
for (address, byte) in image.iter().enumerate() {
assert_eq!(
*byte,
unread.read_uncached(address as u16),
"byte {address:04x} is fly_gb_read_mem's"
);
let address = address as u16;
if flybrain_gb::captured(address) {
assert_eq!(*byte, unread.read_uncached(address), "byte {address:04x} is fly_gb_read_mem's");
} else {
assert_eq!(*byte, flybrain_gb::NOT_CAPTURED, "register byte {address:04x} is not captured");
}
}
// Why the register windows are not captured: reading them through `emulator_read_mem` runs
// binjgb's lazy catch-up, which moves the exported state.
let mut fresh = boot();
let _: Vec<u8> = (0..=u16::MAX).map(|a| fresh.read_uncached(a)).collect();
assert_ne!(
fresh.export_state().unwrap(),
before,
"an unguarded read is not neutral"
);
assert_ne!(fresh.export_state().unwrap(), before, "a register read is not neutral");
let mut unread = boot();
for e in [&mut read, &mut unread] {
e.set_buttons(32);