//! A complete, serialisable picture of the board at a frame boundary, for //! the page's save-and-resume: the three CPUs, every RAM and latch, the //! custom chips, the starfield, the sound generators. Taken and restored //! only between frames, when no register write is pending. use serde::{Deserialize, Serialize}; use z80_rust::state::CpuState; use super::bus::Cpu; use super::machine::Machine; use super::n06xx::N06xx; use super::n51xx::N51xx; use super::n54xx::N54xx; use super::sound::Sound; use super::video::{Starfield, Video}; pub const SNAPSHOT_VERSION: u32 = 2; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct CpuSnapshot { pub a: u8, pub f: u8, pub b: u8, pub c: u8, pub d: u8, pub e: u8, pub h: u8, pub l: u8, pub ix: u16, pub iy: u16, pub sp: u16, pub pc: u16, pub wz: u16, pub i: u8, pub r: u8, pub iff1: bool, pub iff2: bool, pub im: u8, pub af_alt: u16, pub bc_alt: u16, pub de_alt: u16, pub hl_alt: u16, pub q: u8, pub halted: bool, pub ei_delay: u8, pub reset_pending: bool, pub maskable_interrupt_vector: Option, pub non_maskable_interrupt_pending: bool, } impl From for CpuSnapshot { fn from(s: CpuState) -> Self { CpuSnapshot { a: s.a, f: s.f, b: s.b, c: s.c, d: s.d, e: s.e, h: s.h, l: s.l, ix: s.ix, iy: s.iy, sp: s.sp, pc: s.pc, wz: s.wz, i: s.i, r: s.r, iff1: s.iff1, iff2: s.iff2, im: s.im, af_alt: s.af_alt, bc_alt: s.bc_alt, de_alt: s.de_alt, hl_alt: s.hl_alt, q: s.q, halted: s.halted, ei_delay: s.ei_delay, reset_pending: s.reset_pending, maskable_interrupt_vector: s.maskable_interrupt_vector, non_maskable_interrupt_pending: s.non_maskable_interrupt_pending, } } } impl From<&CpuSnapshot> for CpuState { fn from(s: &CpuSnapshot) -> Self { CpuState { a: s.a, f: s.f, b: s.b, c: s.c, d: s.d, e: s.e, h: s.h, l: s.l, ix: s.ix, iy: s.iy, sp: s.sp, pc: s.pc, wz: s.wz, i: s.i, r: s.r, iff1: s.iff1, iff2: s.iff2, im: s.im, af_alt: s.af_alt, bc_alt: s.bc_alt, de_alt: s.de_alt, hl_alt: s.hl_alt, q: s.q, halted: s.halted, ei_delay: s.ei_delay, reset_pending: s.reset_pending, maskable_interrupt_vector: s.maskable_interrupt_vector, non_maskable_interrupt_pending: s.non_maskable_interrupt_pending, } } } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Snapshot { pub version: u32, pub frame: u64, pub t: [u64; 3], pub controls: [u8; 2], pub videolatch_at_vblank_end: u8, pub cpus: Vec, pub video_ram: Vec, pub ram1: Vec, pub ram2: Vec, pub ram3: Vec, pub dsw_a: u8, pub dsw_b: u8, pub wsg_regs: Vec, pub misclatch: u8, pub videolatch: u8, pub main_irq_mask: bool, pub sub_irq_mask: bool, pub sub2_nmi_on: bool, pub subs_running: bool, pub main_irq_line: bool, pub sub_irq_line: bool, pub main_t: u64, pub n06: N06xx, pub n51: N51xx, pub starfield: Starfield, pub wsg_acc: [u32; 3], pub n54: N54xx, /// MAME's scheduler state (`galaga::sched`). pub sched: crate::galaga::sched::Sched, } impl Machine { pub fn snapshot(&self, video: &Video, sound: &Sound) -> Snapshot { let s = self.shared.borrow(); Snapshot { version: SNAPSHOT_VERSION, frame: self.frame, t: self.t, controls: self.controls, videolatch_at_vblank_end: self.videolatch_at_vblank_end, cpus: [Cpu::Main, Cpu::Sub, Cpu::Sub2] .iter() .map(|&c| CpuSnapshot::from(self.cpu(c).capture_state())) .collect(), video_ram: s.video_ram.to_vec(), ram1: s.ram1.to_vec(), ram2: s.ram2.to_vec(), ram3: s.ram3.to_vec(), dsw_a: s.dsw_a, dsw_b: s.dsw_b, wsg_regs: sound.wsg.regs.to_vec(), misclatch: s.misclatch, videolatch: s.videolatch, main_irq_mask: s.main_irq_mask, sub_irq_mask: s.sub_irq_mask, sub2_nmi_on: s.sub2_nmi_on, subs_running: s.subs_running, main_irq_line: s.main_irq_line, sub_irq_line: s.sub_irq_line, main_t: s.main_t, n06: s.n06.clone(), n51: s.n51.clone(), starfield: video.starfield.clone(), wsg_acc: sound.wsg.acc, n54: sound.n54.clone(), sched: self.sched.clone(), } } pub fn restore( &mut self, snap: &Snapshot, video: &mut Video, sound: &mut Sound, ) -> Result<(), String> { if snap.version != SNAPSHOT_VERSION { return Err(format!( "snapshot version {} is not {SNAPSHOT_VERSION}", snap.version )); } if snap.cpus.len() != 3 || snap.video_ram.len() != 0x800 || snap.ram1.len() != 0x400 || snap.ram2.len() != 0x400 || snap.ram3.len() != 0x400 || snap.wsg_regs.len() != 0x20 { return Err("snapshot has the wrong shape".into()); } self.frame = snap.frame; self.t = snap.t; self.sched = snap.sched.clone(); self.controls = snap.controls; self.videolatch_at_vblank_end = snap.videolatch_at_vblank_end; for (i, c) in snap.cpus.iter().enumerate() { self.cpu_mut_by_index(i).restore_state(&CpuState::from(c)); } { let mut s = self.shared.borrow_mut(); s.video_ram.copy_from_slice(&snap.video_ram); s.ram1.copy_from_slice(&snap.ram1); s.ram2.copy_from_slice(&snap.ram2); s.ram3.copy_from_slice(&snap.ram3); s.dsw_a = snap.dsw_a; s.dsw_b = snap.dsw_b; s.wsg.copy_from_slice(&snap.wsg_regs); s.misclatch = snap.misclatch; s.videolatch = snap.videolatch; s.main_irq_mask = snap.main_irq_mask; s.sub_irq_mask = snap.sub_irq_mask; s.sub2_nmi_on = snap.sub2_nmi_on; s.subs_running = snap.subs_running; s.main_irq_line = snap.main_irq_line; s.sub_irq_line = snap.sub_irq_line; s.main_t = snap.main_t; s.now = snap.main_t; s.n06 = snap.n06.clone(); s.n51 = snap.n51.clone(); s.effects = 0; s.events.clear(); s.wsg_writes.clear(); s.n54_writes.clear(); s.frame = snap.frame; } video.starfield = snap.starfield.clone(); sound.wsg.regs.copy_from_slice(&snap.wsg_regs); sound.wsg.acc = snap.wsg_acc; sound.n54 = snap.n54.clone(); Ok(()) } }