// SPDX-License-Identifier: MIT //! Save states: the whole console in bytes, loaded into a fresh console of the same //! machine and cartridge. **Added here; the reference has none** (a7800-python d6b220d: //! "the board has no whole-machine save state yet"; only its cartridges have //! `snapshot()` / `restore()`, which this uses). Its proof is the gate's //! `--save-at-half`: a case saved half way and loaded into a fresh console must give the //! reference's uninterrupted trace, line for line (scripts/lockstep.py). //! //! What is saved: SALLY (registers, ANE/LXA constants and every field of 6502-rust's //! frame, so a request, a poll or a stall in flight survives), the board's latches and //! counters and its RAM, MARIA whole (registers, counters, line RAM, the pixel line and //! the DMA engine mid-run with its locals: maria.rs's `Engine`), the TIA and RIOT //! through their serde forms (a2600-rust's, as its own state.rs uses them), the //! cartridge's `snapshot()`, the ProLine's and the light gun's state. Callbacks (bus //! observers, `on_line`, the pad sources, the sound tap) belong to the host and stay //! the loading console's. Saving is allowed at any instruction boundary. use serde::{Deserialize, Serialize}; use a2600::riot::RIOT; use a2600::tia::TIA; use crate::maria::{Engine, Faults}; use crate::system::System; const MAGIC: &[u8; 8] = b"A78RST01"; #[derive(Serialize, Deserialize)] struct Cpu { a: u8, x: u8, y: u8, s: u8, pc: u16, p: u8, ane_constant: u8, lxa_constant: u8, reset_pending: bool, requests: u8, polled: Option, deferred: Option, stalled: Option, stalled_before: Option, polled_i: Option, halted: bool, dma_stalled: bool, } #[derive(Serialize, Deserialize)] struct MariaState { line: u32, clock_in_line: u32, frame: u64, vblank: bool, enabled: bool, registers: Vec, ctrl: u8, dma_on: bool, read_mode: u8, kangaroo: bool, rdy_low: bool, halt: bool, granted: bool, nmi: bool, dll: u32, dl: u32, zone_offset: u8, holey: u8, dli_pending: bool, write_mode: u8, dma: Option, wait: i32, hbs: bool, dma_clocks: u64, build: Vec, show: Vec, pixels: Vec, rendered: u32, colour_kill: bool, faults: Faults, } #[derive(Serialize, Deserialize)] struct Whole { machine: String, cartridge_name: String, cpu: Cpu, instructions: u64, inptctrl: u8, inptctrl_writes: u32, locked: bool, clocks: u64, periods: u64, slow_cycles: u64, halted_periods: u64, stalled_periods: u64, data_bus: u8, nmi_after: Option, released_at: u64, ram: Vec, maria: MariaState, tia: Vec, riot: Vec, cartridge: Vec, proline: (Vec, Vec<[bool; 2]>), gun: Option<(Option<(u32, u32)>, bool, u64, bool)>, } pub fn save_state(system: &System) -> Vec { let cpu = &system.cpu; let f = &cpu.frame; let board = system.board(); let m = &board.maria; let proline = system.controllers.state.borrow(); let whole = Whole { machine: system.machine.name.to_string(), cartridge_name: board.cartridge.name().to_string(), cpu: Cpu { a: cpu.a, x: cpu.x, y: cpu.y, s: cpu.s, pc: cpu.pc, p: cpu.p, ane_constant: cpu.ane_constant, lxa_constant: cpu.lxa_constant, reset_pending: f.reset_pending, requests: f.requests, polled: f.polled, deferred: f.deferred, stalled: f.stalled, stalled_before: f.stalled_before, polled_i: f.polled_i, halted: f.halted, dma_stalled: f.dma_stalled, }, instructions: system.instructions, inptctrl: board.inptctrl, inptctrl_writes: board.inptctrl_writes, locked: board.locked, clocks: board.clocks, periods: board.periods, slow_cycles: board.slow_cycles, halted_periods: board.halted_periods, stalled_periods: board.stalled_periods, data_bus: board.data_bus, nmi_after: board.nmi_after, released_at: board.released_at, ram: board.ram.clone(), maria: MariaState { line: m.line, clock_in_line: m.clock_in_line, frame: m.frame, vblank: m.vblank, enabled: m.enabled, registers: m.registers.to_vec(), ctrl: m.ctrl, dma_on: m.dma_on, read_mode: m.read_mode, kangaroo: m.kangaroo, rdy_low: m.rdy_low, halt: m.halt, granted: m.granted, nmi: m.nmi, dll: m.dll, dl: m.dl, zone_offset: m.zone_offset, holey: m.holey, dli_pending: m.dli_pending, write_mode: m.write_mode, dma: m.dma.clone(), wait: m.wait, hbs: m.hbs, dma_clocks: m.dma_clocks, build: m.build.to_vec(), show: m.show.to_vec(), pixels: m.pixels.to_vec(), rendered: m.rendered, colour_kill: m.colour_kill, faults: m.faults, }, tia: bincode::serialize(&board.tia).expect("TIA state"), riot: bincode::serialize(&*board.riot.borrow()).expect("RIOT state"), cartridge: board.cartridge.snapshot(), proline: (proline.directions.to_vec(), proline.buttons.to_vec()), gun: board .light_gun .as_ref() .map(|g| (g.aim, g.trigger, g.hits.get(), g.seen.get())), }; let mut out = MAGIC.to_vec(); out.extend(bincode::serialize(&whole).expect("state")); out } pub fn load_state(system: &mut System, data: &[u8]) -> Result<(), String> { if data.len() < 8 || &data[..8] != MAGIC { return Err("not an a7800-rust state (or an older version)".to_string()); } let whole: Whole = bincode::deserialize(&data[8..]).map_err(|e| e.to_string())?; if whole.machine != system.machine.name { return Err(format!( "the state is for {}, not {}", whole.machine, system.machine.name )); } if whole.cartridge_name != system.board().cartridge.name() { return Err(format!( "the state's cartridge is {}, not {}", whole.cartridge_name, system.board().cartridge.name() )); } system.instructions = whole.instructions; { let mut proline = system.controllers.state.borrow_mut(); proline.directions.copy_from_slice(&whole.proline.0); proline.buttons.copy_from_slice(&whole.proline.1); } let c = whole.cpu; let cpu = &mut system.cpu; cpu.a = c.a; cpu.x = c.x; cpu.y = c.y; cpu.s = c.s; cpu.pc = c.pc; cpu.p = c.p; cpu.ane_constant = c.ane_constant; cpu.lxa_constant = c.lxa_constant; let f = &mut cpu.frame; f.reset_pending = c.reset_pending; f.requests = c.requests; f.polled = c.polled; f.deferred = c.deferred; f.stalled = c.stalled; f.stalled_before = c.stalled_before; f.polled_i = c.polled_i; f.halted = c.halted; f.dma_stalled = c.dma_stalled; let board = &mut cpu.bus; board.inptctrl = whole.inptctrl; board.inptctrl_writes = whole.inptctrl_writes; board.locked = whole.locked; board.clocks = whole.clocks; board.periods = whole.periods; board.slow_cycles = whole.slow_cycles; board.halted_periods = whole.halted_periods; board.stalled_periods = whole.stalled_periods; board.data_bus = whole.data_bus; board.nmi_after = whole.nmi_after; board.released_at = whole.released_at; board.ram.copy_from_slice(&whole.ram); let s = whole.maria; let m = &mut board.maria; m.line = s.line; m.clock_in_line = s.clock_in_line; m.frame = s.frame; m.vblank = s.vblank; m.enabled = s.enabled; m.registers.copy_from_slice(&s.registers); m.ctrl = s.ctrl; m.dma_on = s.dma_on; m.read_mode = s.read_mode; m.kangaroo = s.kangaroo; m.rdy_low = s.rdy_low; m.halt = s.halt; m.granted = s.granted; m.nmi = s.nmi; m.dll = s.dll; m.dl = s.dl; m.zone_offset = s.zone_offset; m.holey = s.holey; m.dli_pending = s.dli_pending; m.write_mode = s.write_mode; m.dma = s.dma; m.wait = s.wait; m.hbs = s.hbs; m.dma_clocks = s.dma_clocks; m.build.copy_from_slice(&s.build); m.show.copy_from_slice(&s.show); m.pixels.copy_from_slice(&s.pixels); m.rendered = s.rendered; m.colour_kill = s.colour_kill; m.faults = s.faults; let mut tia: TIA = bincode::deserialize(&whole.tia).map_err(|e| e.to_string())?; tia.on_line = board.tia.on_line.take(); tia.on_vsync = board.tia.on_vsync.take(); tia.pad_source = board.tia.pad_source.take(); tia.audio.on_sample = board.tia.audio.on_sample.take(); board.tia = tia; let mut riot: RIOT = bincode::deserialize(&whole.riot).map_err(|e| e.to_string())?; { let mut live = board.riot.borrow_mut(); riot.port_a_listener = live.port_a_listener.take(); *live = riot; } board.cartridge.restore(&whole.cartridge)?; if let (Some(gun), Some((aim, trigger, hits, seen))) = (board.light_gun.as_mut(), whole.gun) { gun.aim = aim; gun.trigger = trigger; gun.hits.set(hits); gun.seen.set(seen); } Ok(()) }