From 96c7c1e4fee261ecf386fb2fff2155f06a2f7973 Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 8 Jan 2018 00:32:32 +0800 Subject: support console state load/save (minor bugs still present) --- src/mos6502.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/mos6502.rs') diff --git a/src/mos6502.rs b/src/mos6502.rs index 848dd1f..e8c471f 100644 --- a/src/mos6502.rs +++ b/src/mos6502.rs @@ -1,7 +1,10 @@ #![allow(dead_code)] #![allow(unused_macros)] +use core::mem::size_of; use memory::{CPUMemory, VMem}; +use utils::{Read, Write, load_prefix, save_prefix}; + pub const CPU_FREQ: u32 = 1789773; #[macro_export] @@ -588,7 +591,9 @@ enum IntType { DelayedNMI } +#[repr(C)] pub struct CPU<'a> { + /*-- begin state --*/ /* registers */ a: u8, x: u8, @@ -596,17 +601,22 @@ pub struct CPU<'a> { status: u8, pc: u16, sp: u8, - /* internal state */ + /* internal latches */ acc: bool, opr: u16, ea: u16, /* effective address */ imm_val: u8, pub cycle: u32, - //pub elapsed: u32, int: Option, + /*-- end state --*/ + + /*-- begin sub-state --*/ pub mem: CPUMemory<'a>, + /*-- end sub-state --*/ } +const CPU_IGNORED_SIZE: usize = size_of::(); + macro_rules! make_int { ($f:ident, $v: expr) => ( #[inline(always)] @@ -654,10 +664,19 @@ impl<'a> CPU<'a> { int: None, acc: false, mem, - //elapsed: 0 } } + pub fn load(&mut self, reader: &mut Read) -> bool { + load_prefix(self, CPU_IGNORED_SIZE, reader) && + self.mem.load(reader) + } + + pub fn save(&self, writer: &mut Write) -> bool { + save_prefix(self, CPU_IGNORED_SIZE, writer) && + self.mem.save(writer) + } + pub fn powerup(&mut self) { self.cycle = 2; self.pc = read16!(self.mem, RESET_VECTOR as u16); @@ -693,8 +712,7 @@ impl<'a> CPU<'a> { } pub fn tick(&mut self) { - self.cycle -= 1; - //self.elapsed += 1; + self.cycle -= 1 } pub fn reset(&mut self) { -- cgit v1.2.3-70-g09d2