Skip to main content

VsDualSystem

Struct VsDualSystem 

Source
pub struct VsDualSystem { /* private fields */ }
Expand description

Two complete NES systems + the cabinet’s cross-wiring. See the module docs for the architecture and protocol.

Implementations§

Source§

impl VsDualSystem

Source

pub fn from_rom(bytes: &[u8]) -> Result<Self, RomError>

Construct the dual system from one ROM image. A proper DualSystem dump carries BOTH CPUs’ programs (64 KiB PRG: main half then sub half — MAME’s prg + sub regions; Mesen2’s prgOuter split); the sub console’s mapper banks the second half. A 32 KiB (main-half-only) dump constructs and runs, but its boot handshake cannot complete — the sub-CPU program is simply absent.

The caller has already determined the cart is a DualSystem board (the SHA-keyed vs_db dual_system flag — see crate::Emu).

§Errors

Returns the underlying [RomError] if the bytes don’t parse.

Source

pub fn from_rom_with_sample_rate( bytes: &[u8], sample_rate: u32, ) -> Result<Self, RomError>

Construct the dual system with an explicit audio sample rate (the frontend’s output-device rate), applied to BOTH consoles.

The sample rate is baked into each Nes at construction (there is no runtime setter), so the desktop present path — which knows the cpal device rate — must build the pair through this constructor for the main console’s audio to resample correctly. Otherwise identical to Self::from_rom.

§Errors

Returns the underlying [RomError] if the bytes don’t parse.

Source

pub fn run_frame(&mut self)

Run one MAIN-console frame with the sub console soft-locksteped to within a 5-CPU-cycle gap (Mesen2 RunFrame + RunVsSubConsole).

Returns when the main console’s PPU completes a frame (or its CPU jams / the frame budget trips — mirroring Nes::run_frame’s guards).

Source

pub fn main_framebuffer(&self) -> &[u8] ⓘ

The main console’s 256x240 RGBA8 framebuffer (the left screen).

Source

pub fn sub_framebuffer(&self) -> &[u8] ⓘ

The sub console’s 256x240 RGBA8 framebuffer (the right screen).

Source

pub const fn set_buttons(&mut self, port: usize, buttons: Buttons)

Route controller input: ports 0/1 (P1/P2) → the main console’s ports 0/1; ports 2/3 (P3/P4) → the sub console’s ports 0/1.

Source

pub const fn insert_coin(&mut self, acceptor: u8)

Coin routing (Mesen2 VsControlManager): acceptors 0/1 latch on the MAIN console, 2/3 on the SUB console.

Source

pub const fn clear_coin(&mut self)

Clear both consoles’ latched coin signals.

Source

pub const fn set_vs_service(&mut self, panel: u8, pressed: bool)

The service button (main panel) / service-2 (sub panel).

Source

pub const fn main(&self) -> &Nes

Borrow the main console (read-only diagnostics).

Source

pub const fn sub(&self) -> &Nes

Borrow the sub console (read-only diagnostics).

Source

pub const fn main_mut(&mut self) -> &mut Nes

Mutably borrow the main console (debugger / diagnostics — e.g. the side-effect-free debug_peek_cpu, which needs &mut for the mapper’s banked lookups). Cross-wiring stays wrapper-owned; don’t drive $4016 writes through this handle.

Source

pub const fn sub_mut(&mut self) -> &mut Nes

Mutably borrow the sub console (debugger / diagnostics; see Self::main_mut).

Source

pub const fn split_mut(&mut self) -> (&mut Nes, &mut Nes)

Simultaneously borrow both consoles (diagnostics — e.g. a tracing harness replicating the lockstep loop with instrumented pumping).

Source

pub fn power_cycle(&mut self)

v2.9.8 — power-cycle the whole cabinet: both consoles cold-boot and the cabinet wiring is installed again, so the result is the cabinet Self::from_rom builds (with each console’s settings and battery RAM kept, as Nes::power_cycle keeps them).

Power-cycling the two consoles alone is NOT enough, and that is why this exists. Nes::power_cycle rebuilds each mapper from the ROM, which drops the wiring construction installed on it: the sub console’s second-half PRG / upper-CHR banking (set_vs_dual_sub) and both consoles’ shared 2 KiB WRAM window (enable_vs_dual_wram). The sub then runs the MAIN program, fails its $4016 identity check, and the boot handshake never completes. The wrapper’s latches (the two bit-1 levels, and through them each console’s external /IRQ) are re-seeded to their reset values, and the scratch buffers emptied.

Source

pub fn snapshot(&self) -> Vec<u8> ⓘ

Serialize the dual system: a versioned container nesting the two standard Nes snapshots plus the wrapper’s latch state.

Layout: RVSD magic, u16 version, latch byte (bit0 = main_bit1, bit1 = sub_bit1; bit 2 reserved — it carried a WRAM-ownership flag in a pre-release layout and is ignored on load), then two u32-length-prefixed Nes snapshots (main, sub).

Source

pub fn snapshot_into(&mut self, out: &mut Vec<u8>)

v2.9.1 (libretro re-audit NL-09) — Self::snapshot without the two consoles’ THM thumbnails, encoded into a caller-owned buffer that is reused across calls. The libretro core’s retro_serialize path.

Same container, same layout, and it restores through Self::restore like a full snapshot: each console block is a Nes::snapshot_core_into blob, and THM is optional by format. What it drops is the work nobody on this path reads. Self::snapshot builds a 128x120 RGBA thumbnail per console (the desktop’s slot picker shows one; RetroArch never does), then a fresh buffer per console and a third for the container, about 645 KB allocated on every call. v2.9.0 measured that at 314-1,630 us per call against 46-78 us for this shape (docs/performance.md); the A/B that adopted it is recorded there.

&mut self only for the pooled scratch buffer; nothing emulated changes.

Source

pub fn restore(&mut self, data: &[u8]) -> Result<(), SnapshotError>

Restore a dual-system snapshot produced by Self::snapshot or Self::snapshot_into.

§Errors

Returns SnapshotError on a bad container or when either nested console snapshot fails to restore. Both consoles are then unchanged (since v2.9.0; before it a rejected sub block left main restored).

A successful restore empties the cabinet’s rewind ring (v3.1.0): the entries describe the timeline the restore replaced, as Nes::restore treats its own ring.

Source

pub fn restore_quiet(&mut self, data: &[u8]) -> Result<(), SnapshotError>

v3.1.0 (T-PS-dual-runahead) — Self::restore without its side effects: the cabinet’s rewind ring is kept, and each console is restored with Nes::restore_quiet (no timeline-generation bump, no rewind clear). For restores that return to the cabinet’s own timeline rather than replace it: run-ahead’s rollback and a rewind step. Same container, same all-or-nothing contract.

§Errors

As Self::restore.

Source

pub fn enable_rewind(&mut self)

v3.1.0 (T-PS-dual-runahead) — enable the cabinet’s rewind ring with the default byte budget and keyframe period.

Source

pub fn enable_rewind_with(&mut self, max_bytes: usize, keyframe_period: u32)

Enable the cabinet’s rewind ring with an explicit byte budget and keyframe period. Replaces (and so empties) any ring already enabled.

Source

pub fn disable_rewind(&mut self)

Disable the cabinet’s rewind ring and free its memory.

Source

pub const fn rewind_enabled(&self) -> bool

true if the cabinet’s rewind ring is enabled.

Source

pub fn rewind_len(&self) -> usize

Number of buffered rewind entries (0 when rewind is disabled).

Source

pub const fn set_rewind_capture(&mut self, enabled: bool)

Turn the per-frame rewind capture in Self::run_frame on or off. Run-ahead turns it off across its hidden and visible frames, which are not the cabinet’s timeline.

Source

pub const fn rewind_capture_enabled(&self) -> bool

true while Self::run_frame captures into the rewind ring.

Source

pub fn rewind_capture(&mut self)

Push the cabinet’s current state into the rewind ring, keyed by the main console’s frame. Self::run_frame calls this after every frame while capture is on; a no-op while rewind is disabled.

Each entry is a whole RVSD container (Self::snapshot_into): both consoles, framebuffers included, and the bit-1 latch. Unlike the single console’s ring, which stores SLIM entries and re-renders the picture after a step back, the cabinet keeps the framebuffers in the entry. That is what makes a step back exact for BOTH screens without running the cabinet forward and back: a re-render would run the five-cycle soft lockstep for a frame and restore again, twice the work of a single console. The cost is two 245,760-byte framebuffers per entry, which the ring’s XOR delta and LZ4 reduce to the pixels that changed since the last keyframe.

Source

pub fn rewind_step_back(&mut self) -> bool

Pop the most recent rewind entry and restore the cabinet to it, both framebuffers included. Returns true on success and false when the ring is empty, rewind is disabled, or the entry fails to decode or restore (the cabinet is then unchanged, per Self::restore’s all-or-nothing contract).

The restore is quiet (Self::restore_quiet): the ring survives, since the user is mid-rewind.

Source

pub fn rewind_clear(&mut self)

Drop every buffered rewind entry (the ring stays enabled).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.