// SPDX-License-Identifier: MIT //! Save states: the whole console (the reference has none: its docs/status.md //! "Capabilities" says "no `state.py`"; this is the board's own, as //! gameboy-rust's is, and the gate proves it with `--save-at-half`). //! //! A state is the CPU's [`CpuState`] (saved between instructions, where the //! core's in-step fields are idle), the instruction count, the board's time //! and floating bus, the whole TIA (sound channels and unread samples //! included), the whole RIOT and the cartridge's fields (its own //! `save_state`), bincode-encoded behind a header. Left out, and kept from the //! console the state is loaded into: the machine row (checked by name), the //! host's attachments (bus observers, the TIA's line, VSYNC and pad callbacks, //! the sound's `on_sample`, the RIOT's port A listener, a Supercharger's tape //! input). Devices on the //! ports belong to the host and are saved by it (the trace tool saves its //! SaveKey's chip and Driving controller beside the state). use serde::{Deserialize, Serialize}; use sixfiveohtwo::CpuState; use crate::riot::RIOT; use crate::system::System; use crate::tia::TIA; const MAGIC: &[u8; 8] = b"A26RST01"; #[derive(Serialize, Deserialize)] struct Cpu { a: u8, x: u8, y: u8, s: u8, pc: u16, p: u8, flags: [bool; 5], polled: [Option; 3], } impl Cpu { fn from(s: &CpuState) -> Cpu { Cpu { a: s.a, x: s.x, y: s.y, s: s.s, pc: s.pc, p: s.p, flags: [ s.halted, s.reset_pending, s.maskable_interrupt_pending, s.non_maskable_interrupt_pending, s.skipped_poll, ], polled: [s.polled_i, s.polled_maskable_interrupt, s.polled_non_maskable_interrupt], } } fn to(&self) -> CpuState { CpuState { a: self.a, x: self.x, y: self.y, s: self.s, pc: self.pc, p: self.p, halted: self.flags[0], reset_pending: self.flags[1], maskable_interrupt_pending: self.flags[2], non_maskable_interrupt_pending: self.flags[3], polled_i: self.polled[0], polled_maskable_interrupt: self.polled[1], polled_non_maskable_interrupt: self.polled[2], skipped_poll: self.flags[4], } } } #[derive(Serialize, Deserialize)] struct Whole { machine: String, cpu: Cpu, instructions: u64, cycles: u64, halted_cycles: u64, data_bus: u8, tia: Vec, riot: Vec, cartridge_name: String, cartridge: Vec, } /// The console's state between two instructions. pub fn save_state(system: &System) -> Vec { let board = system.board(); let whole = Whole { machine: system.machine.name.to_string(), cpu: Cpu::from(&system.cpu.capture_state()), instructions: system.instructions, cycles: board.cycles, halted_cycles: board.halted_cycles, data_bus: board.data_bus, tia: bincode::serialize(&board.tia).expect("TIA state"), riot: bincode::serialize(&*board.riot.borrow()).expect("RIOT state"), cartridge_name: board.cartridge.name().to_string(), cartridge: board.cartridge.save_state(), }; let mut out = MAGIC.to_vec(); out.extend(bincode::serialize(&whole).expect("state")); out } /// Load a state into `system`, a console on the same machine row with the same cartridge. pub fn load_state(system: &mut System, data: &[u8]) -> Result<(), String> { if data.len() < 8 || &data[..8] != MAGIC { return Err("not an a2600-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 )); } let board = system.board_mut(); if whole.cartridge_name != board.cartridge.name() { return Err(format!( "the state's cartridge is {}, not {}", whole.cartridge_name, board.cartridge.name() )); } 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 current = board.riot.borrow_mut(); riot.port_a_listener = current.port_a_listener.take(); *current = riot; } board.cartridge.load_state(&whole.cartridge)?; board.cycles = whole.cycles; board.halted_cycles = whole.halted_cycles; board.data_bus = whole.data_bus; system.cpu.restore_state(&whole.cpu.to())?; system.instructions = whole.instructions; Ok(()) }