//! Save states (state.rs): a saved and loaded run continues as the run that was not //! interrupted; the refusals; the snapshot pair. The reference's tests/test_state.py on its //! own synthetic card, so nothing here needs a ROM. use pcengine_rust::board::{Board, BoardOptions}; use pcengine_rust::cards::{PopulousCard, RomCard, Sf2Card, TennokoeCard}; use pcengine_rust::fake_devices::{LockstepCard, LockstepPort}; use pcengine_rust::machine::{Machine, PC_ENGINE, TURBOGRAFX_16}; use pcengine_rust::pads::{Pad2, Pad6}; use pcengine_rust::sockets::Card; use pcengine_rust::state::{load_snapshot, load_state, save_snapshot, save_state, MAGIC}; use pcengine_rust::system::System; use pcengine_rust::trace::{roundtrip, runahead, Setup}; fn system_with(card: Box, machine: Machine) -> System { System::new( Board::new(BoardOptions { machine, card: Some(card), pad: Some(Box::new(Pad2::new())), ..BoardOptions::default() }) .unwrap(), ) } /// A card image that is only its reset vector (to $E000) and a fill. fn image(size: usize) -> Vec { let mut data = vec![0xEA; size]; data[size - 2] = 0x00; data[size - 1] = 0xE0; data } /// A card that does something: counts in work RAM and writes the count to the VCE's colour /// table and a PSG register, for ever (so a state has RAM, a palette and sound to carry). fn program() -> Vec { let code: &[u8] = &[ 0x78, // SEI 0xA9, 0xFF, 0x53, 0x02, // LDA #$FF; TAM #$02: the hardware page at $2000 0xA9, 0xF8, 0x53, 0x04, // LDA #$F8; TAM #$04: work RAM at $4000 0xEE, 0x00, 0x40, // loop ($E009): INC $4000 0xAD, 0x00, 0x40, // LDA $4000 0x8D, 0x02, 0x24, // STA $2402 (VCE colour address, low) 0x8D, 0x04, 0x24, // STA $2404 (VCE colour data, low) 0x8D, 0x06, 0x28, // STA $2806 (PSG channel data) 0x4C, 0x09, 0xE0, // JMP loop ]; let mut data = image(0x2000); data[..code.len()].copy_from_slice(code); data } fn rom(data: &[u8]) -> Box { Box::new(RomCard::new(data).unwrap()) } #[test] fn write_v2_state() { let mut system = system_with(rom(&program()), PC_ENGINE); system.run_frame(); std::fs::write(std::env::var("OUT").unwrap(), save_state(&system)).unwrap(); }