Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions browser-payloads.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"release": "browser-payloads-794c86516e68",
"sha256": "4e984c92353f45f0076401dfd63d4c874c4854141a936873f5ce91c5e26846cb"
"release": "browser-payloads-da7751d556b4",
"sha256": "e635d2f9feaf756ae20b0028193ca92df0be1bbf7912a03b991b4b590c77faa8"
}
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/romdev-core-fceumm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "romdev-core-fceumm",
"version": "0.14.0",
"version": "0.15.0",
"description": "fceumm libretro core (NES) for romdev, bundled as WASM.",
"type": "module",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/romdev-core-gambatte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "romdev-core-gambatte",
"version": "0.13.1",
"version": "0.14.0",
"description": "gambatte libretro core (Game Boy / Game Boy Color) for romdev, bundled as WASM.",
"type": "module",
"main": "index.js",
Expand Down
61 changes: 61 additions & 0 deletions packages/romdev-core-host/LibretroHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ export class LibretroHost {
* @returns {Promise<void>} resolves (undefined) on success; THROWS on failure
*/
async loadMedia(args) {
this.stopWorldObservation();
const mod = this._needMod();
const { platform } = args;
const topology = args.controllerTopology;
Expand Down Expand Up @@ -960,6 +961,7 @@ export class LibretroHost {

unloadMedia() {
const mod = this._needMod();
this.stopWorldObservation();
if (this.status.loaded) {
mod._retro_unload_game();
this.status.loaded = false;
Expand Down Expand Up @@ -995,6 +997,7 @@ export class LibretroHost {
dispose() {
const mod = this.mod;
if (mod) {
try { this.stopWorldObservation(); } catch { /* teardown */ }
try { if (this.status.loaded) mod._retro_unload_game(); } catch { /* ignore */ }
// Proxied cores run retro_deinit on the app thread; calling the main-thread
// export directly would race the worker. Their module teardown is the
Expand Down Expand Up @@ -1392,6 +1395,7 @@ export class LibretroHost {
);
}
const ptr = mod._malloc(blob.byteLength);
this.stopWorldObservation();
try {
mod.HEAPU8.set(blob, ptr);
const ok = mod._retro_unserialize(ptr, blob.byteLength);
Expand Down Expand Up @@ -1420,6 +1424,62 @@ export class LibretroHost {
} finally { mod._free(ptr); }
}

/** Diagnostic snapshots at the ROM's explicit world-publication write. They do
* not pause CPU/PPU or change emulated RAM. Supported by rebuilt GB/GBC/NES
* cores only; ticks and fields are physical RAM regions, not CPU aliases.
* The ROM updates tick (LE32), then writes trigger before drawing its view.
* Drain every native frame: >8 publications fail with truncation metadata.
* Restore/reset/media changes disarm; callers must rebind fresh RAM pointers. */
startWorldObservation({ trigger, tick, fields, value = 0xa5 }) {
const mod = this._needMod(); this._needMedia();
if (!['gb', 'gbc', 'nes'].includes(this.status.platform) || !['_romdev_observe_set', '_romdev_observe_add', '_romdev_observe_arm', '_romdev_observe_get'].every(k => typeof mod[k] === 'function')) throw new Error('world observation unsupported by this core');
const nes = this.status.platform === 'nes';
if (!Number.isInteger(value) || value < 0 || value > 255) throw new Error('invalid world publication value');
if (!Number.isInteger(trigger) || !(nes ? trigger >= 0 && trigger < 0x800 || trigger >= 0x6100 && trigger < 0x8000 : trigger >= 0xc000 && trigger < 0xd000)) throw new Error('world publication must be in stable writable RAM');
if (!Array.isArray(fields) || !fields.length || fields.length > 32) throw new Error('invalid world fields');
const pointer = (span, fixedLength) => {
if (!span || !['system_ram', 'save_ram'].includes(span.region) || !Number.isInteger(span.offset) || span.offset < 0 || !Number.isInteger(span.length) || span.length < 1 || fixedLength && span.length !== fixedLength || span.length > 1024) throw new Error('invalid observation RAM span');
const id = MemoryRegionToRetro[span.region], size = mod._retro_get_memory_size(id), base = mod._retro_get_memory_data(id);
if (!base || span.offset + span.length > size || base + span.offset + span.length > mod.HEAPU8.length) throw new Error('observation RAM span out of bounds');
return base + span.offset;
};
const tickPtr = pointer(tick, 4), pointers = fields.map(f => pointer(f));
// Tick lives in stable unbanked WRAM, or ordinary NES RAM/PRG-RAM.
if (nes ? tick.region === 'system_ram' ? tick.offset + 4 > 0x800 : tick.offset < 0x100 : tick.region !== 'system_ram' || tick.offset + 4 > 0x1000) throw new Error('world tick must be in stable RAM');
const bytes = fields.reduce((n, f) => n + f.length, 0);
if (bytes > 1024) throw new Error('observation world exceeds 1024 bytes');
this.stopWorldObservation();
mod._romdev_observe_set(trigger, tickPtr, value);
try {
fields.forEach((f, i) => { if (!mod._romdev_observe_add(pointers[i], f.length)) throw new Error('core rejected observation span'); });
if (!mod._romdev_observe_arm(1)) throw new Error('core rejected world observation');
this._worldObservation = { bytes };
} catch (e) { this.stopWorldObservation(); throw e; }
}

drainWorldObservation() {
const mod = this._needMod(), layout = this._worldObservation;
if (!layout) throw new Error('no world observation armed');
const stride = 8 + layout.bytes, ptr = mod._malloc(stride * 8), meta = mod._malloc(12);
if (!ptr || !meta) { if (ptr) mod._free(ptr); if (meta) mod._free(meta); throw new Error('observation allocation failed'); }
try {
const n = mod._romdev_observe_get(ptr, stride * 8, meta, 1), counts = new DataView(mod.HEAPU8.buffer, meta, 12);
const total = counts.getUint32(0, true), stored = counts.getUint32(4, true);
if (n > 8 || stored > 8 || counts.getUint32(8, true) !== stride) throw new Error('invalid observation ABI');
const events = [];
for (let i = 0; i < n; i++) {
const at = ptr + i * stride, row = new DataView(mod.HEAPU8.buffer, at, stride);
events.push({ tick: row.getUint32(0, true), pc: row.getUint32(4, true), bytes: mod.HEAPU8.slice(at + 8, at + stride) });
}
return { events, total, truncated: total > n };
} finally { mod._free(ptr); mod._free(meta); }
}

stopWorldObservation() {
this.mod?._romdev_observe_set?.(0, 0, 0);
this._worldObservation = null;
}

listStates() {
return Array.from(this.namedStates.keys());
}
Expand Down Expand Up @@ -1775,6 +1835,7 @@ export class LibretroHost {

reset() {
const mod = this._needMod();
this.stopWorldObservation();
mod._retro_reset();
this.status.frameCount = 0;
// A reset clears the core's active cheats (they live in volatile core
Expand Down
20 changes: 20 additions & 0 deletions packages/romdev-core-host/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,23 @@ for native 1–4 controllers (Four Score only when slot 3/4 is present), preserv
slot numbers across gaps. Deterministic input accepts the eight native buttons.
See `romdevtools/scripts/multiplayer/README.md` for the fixed session identities,
frame-boundary restrictions, compiler records, reproducible builds and tests.

## Logical world observation (0.16.0)

`startWorldObservation({trigger, tick, fields, value: 0xa5})` binds an explicit
ROM publication write on GB/GBC/NES. After updating the world, the engine writes
a four-byte little-endian logical tick to stable RAM, then writes `value` to
`trigger` before rendering its view. `tick` and each field are
`{region: "system_ram" | "save_ram", offset, length}` physical RAM spans; the
tick must be unbanked and have length 4. The trigger is a stable writable CPU
address. The observer copies the selected physical bytes at that bus write,
without pausing the CPU, stepping clocks or modifying emulated state.

`drainWorldObservation()` returns owned `{tick, pc, bytes}` records in publication
order, plus `total` and `truncated`, then clears the diagnostic buffer. Drain once
per native frame. The buffer holds eight publications, at most 32 fields and
1024 field bytes; truncation must fail the logical-world check rather than be
treated as agreement. Instrumentation is outside savestates and causal digests.
`stopWorldObservation()`, restore, reset, unload and load disarm it. Rebind after
those lifecycle changes. Older cores do not advertise these optional exports.
This is a diagnostic hook; it does not synchronize consoles or certify a room.
2 changes: 1 addition & 1 deletion packages/romdev-core-host/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "romdev-core-host",
"version": "0.15.0",
"version": "0.16.0",
"description": "The romdev libretro host runtime, now ISOMORPHIC: load a romdev-core-* emulator core from disk (Node) or from a factory + wasm bytes (browser/worker), run frames, read the framebuffer/audio/memory regions as typed arrays, drive input, save/load state. The same hardened host the romdev MCP server uses.",
"type": "module",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/romdev-core-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"url": "git+https://github.com/monteslu/romdev.git"
},
"dependencies": {
"romdev-core-host": "0.15.0"
"romdev-core-host": "0.16.0"
},
"optionalDependencies": {
"@kmamal/sdl": "^0.11.13"
Expand Down
2 changes: 1 addition & 1 deletion packages/romdevtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"romdev-core-geargrafx": "0.10.1",
"romdev-core-gpgx": "0.15.1",
"romdev-core-handy": "0.9.1",
"romdev-core-host": "0.15.0",
"romdev-core-host": "0.16.0",
"romdev-core-parallel-n64": "0.3.1",
"romdev-core-prosystem": "0.10.1",
"romdev-core-runner": "0.2.18",
Expand Down
2 changes: 1 addition & 1 deletion packages/romdevtools/scripts/build-fceumm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ RDBG_OBJ="$FCEUMM_DIR/src/romdev_debug.o"
emcc -c -O2 "$FCEUMM_DIR/src/romdev_debug.c" -o "$RDBG_OBJ"
emar rcs "$CORE_LIB" "$RDBG_OBJ"

EXPORTED_FUNCTIONS='["_retro_api_version","_retro_init","_retro_deinit","_retro_set_environment","_retro_set_video_refresh","_retro_set_audio_sample","_retro_set_audio_sample_batch","_retro_set_input_poll","_retro_set_input_state","_retro_get_system_info","_retro_get_system_av_info","_retro_load_game","_retro_unload_game","_retro_run","_retro_reset","_retro_serialize_size","_retro_serialize","_retro_unserialize","_retro_cheat_reset","_retro_cheat_set","_romdev_watchpoint_set","_romdev_watchpoint_set_cond","_romdev_watchpoint_get","_romdev_readwatch_set","_romdev_readwatch_get","_romdev_pcbreak_set","_romdev_pcbreak_get","_romdev_watchdog_set","_romdev_regsnap_get","_romdev_irqblock_set","_romdev_vramwatch_set","_romdev_vramwatch_get","_romdev_setreg","_romdev_getreg","_romdev_range_set","_romdev_range_get","_romdev_cov_set","_romdev_cov_get","_romdev_covbits_set","_romdev_covbits_get","_retro_get_memory_data","_retro_get_memory_size","_retro_get_region","_retro_set_controller_port_device","_romdev_deterministic_boot","_romdev_deterministic_disable","_romdev_state_schema","_romdev_state_digest","_malloc","_free"]'
EXPORTED_FUNCTIONS='["_retro_api_version","_retro_init","_retro_deinit","_retro_set_environment","_retro_set_video_refresh","_retro_set_audio_sample","_retro_set_audio_sample_batch","_retro_set_input_poll","_retro_set_input_state","_retro_get_system_info","_retro_get_system_av_info","_retro_load_game","_retro_unload_game","_retro_run","_retro_reset","_retro_serialize_size","_retro_serialize","_retro_unserialize","_retro_cheat_reset","_retro_cheat_set","_romdev_watchpoint_set","_romdev_watchpoint_set_cond","_romdev_watchpoint_get","_romdev_readwatch_set","_romdev_readwatch_get","_romdev_pcbreak_set","_romdev_pcbreak_get","_romdev_watchdog_set","_romdev_regsnap_get","_romdev_irqblock_set","_romdev_vramwatch_set","_romdev_vramwatch_get","_romdev_setreg","_romdev_getreg","_romdev_range_set","_romdev_range_get","_romdev_observe_set","_romdev_observe_add","_romdev_observe_arm","_romdev_observe_get","_romdev_cov_set","_romdev_cov_get","_romdev_covbits_set","_romdev_covbits_get","_retro_get_memory_data","_retro_get_memory_size","_retro_get_region","_retro_set_controller_port_device","_romdev_deterministic_boot","_romdev_deterministic_disable","_romdev_state_schema","_romdev_state_digest","_malloc","_free"]'
EXPORTED_RUNTIME='["ccall","cwrap","addFunction","removeFunction","HEAPU8","HEAPU16","HEAPU32","HEAP16","HEAP32","HEAPF32","UTF8ToString","stringToUTF8","lengthBytesUTF8","getValue","setValue","FS"]'

mkdir -p "$OUT"
Expand Down
2 changes: 1 addition & 1 deletion packages/romdevtools/scripts/build-gambatte.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ RDBG_OBJ="$GAMBATTE_DIR/libgambatte/src/romdev_debug.o"
emcc -c -O2 "$GAMBATTE_DIR/libgambatte/src/romdev_debug.c" -o "$RDBG_OBJ"
emar rcs "$CORE_LIB" "$RDBG_OBJ"

EXPORTED_FUNCTIONS='["_retro_api_version","_retro_init","_retro_deinit","_retro_set_environment","_retro_set_video_refresh","_retro_set_audio_sample","_retro_set_audio_sample_batch","_retro_set_input_poll","_retro_set_input_state","_retro_get_system_info","_retro_get_system_av_info","_retro_load_game","_retro_unload_game","_retro_run","_retro_reset","_retro_serialize_size","_retro_serialize","_retro_unserialize","_retro_cheat_reset","_retro_cheat_set","_romdev_watchpoint_set","_romdev_watchpoint_set_cond","_romdev_watchpoint_get","_romdev_readwatch_set","_romdev_readwatch_get","_romdev_pcbreak_set","_romdev_pcbreak_get","_romdev_watchdog_set","_romdev_regsnap_get","_romdev_irqblock_set","_romdev_setreg","_romdev_getreg","_romdev_range_set","_romdev_range_get","_romdev_cov_set","_romdev_cov_get","_romdev_covbits_set","_romdev_covbits_get","_retro_get_memory_data","_retro_get_memory_size","_retro_get_region","_retro_set_controller_port_device","_romdev_deterministic_boot","_romdev_deterministic_disable","_romdev_state_schema","_romdev_state_digest","_malloc","_free"]'
EXPORTED_FUNCTIONS='["_retro_api_version","_retro_init","_retro_deinit","_retro_set_environment","_retro_set_video_refresh","_retro_set_audio_sample","_retro_set_audio_sample_batch","_retro_set_input_poll","_retro_set_input_state","_retro_get_system_info","_retro_get_system_av_info","_retro_load_game","_retro_unload_game","_retro_run","_retro_reset","_retro_serialize_size","_retro_serialize","_retro_unserialize","_retro_cheat_reset","_retro_cheat_set","_romdev_watchpoint_set","_romdev_watchpoint_set_cond","_romdev_watchpoint_get","_romdev_readwatch_set","_romdev_readwatch_get","_romdev_pcbreak_set","_romdev_pcbreak_get","_romdev_watchdog_set","_romdev_regsnap_get","_romdev_irqblock_set","_romdev_setreg","_romdev_getreg","_romdev_range_set","_romdev_range_get","_romdev_observe_set","_romdev_observe_add","_romdev_observe_arm","_romdev_observe_get","_romdev_cov_set","_romdev_cov_get","_romdev_covbits_set","_romdev_covbits_get","_retro_get_memory_data","_retro_get_memory_size","_retro_get_region","_retro_set_controller_port_device","_romdev_deterministic_boot","_romdev_deterministic_disable","_romdev_state_schema","_romdev_state_digest","_malloc","_free"]'
EXPORTED_RUNTIME='["ccall","cwrap","addFunction","removeFunction","HEAPU8","HEAPU16","HEAPU32","HEAP16","HEAP32","HEAPF32","UTF8ToString","stringToUTF8","lengthBytesUTF8","getValue","setValue","FS"]'

mkdir -p "$OUT"
Expand Down
20 changes: 20 additions & 0 deletions packages/romdevtools/scripts/multiplayer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,23 @@ from their normal npm versions. This fork does not substitute upstream npm
binaries for its modified SDCC/gambatte/fceumm builds. CI uses this mode and
keys its payload cache on both the dependency lockfile and payload manifest.
The original npm-only mode remains available for upstream development.

## Pre-render world publications

The shared debug observer used by the GB/GBC/NES recipes records physical RAM
at a trusted engine's publication write, after world update and before view
rendering. It neither freezes nor advances the CPU and is not serialized.
The ROM owns an ordinary unbanked LE32 tick and a marker written with `0xa5`;
boot-time RAM clearing therefore does not publish spurious ticks. The host
binds the linked addresses and physical field ranges rather than interpreting
display-boundary RAM as a completed logical tick. Eight publications, 32 spans
and 1024 field bytes are hard bounds. Overflow is reported, never silently
accepted. Consumers must compare matching logical ticks and reject skipped or
repeated publications. Disarm/rebind around restore, reset and media changes.

Run `node --test packages/romdevtools/test/world-observation.test.js` after rebuilding
Gambatte and fceumm. Tests compare instrumented and uninstrumented causal state,
CPU registers, raw snapshots, pixels and audio for 130 frames on all three
platforms, retain pre-view bytes after later writes, and cover bounded overflow,
owned copies, lifecycle cleanup and the native C ABI. The engine publisher and
author world schema are separate upper-layer responsibilities.
Loading
Loading