From 2fe2aae243d2c226b62a566d76f9d7ce6dd91529 Mon Sep 17 00:00:00 2001 From: zeromus Date: Mon, 21 Mar 2011 02:22:10 +0000 Subject: [PATCH] [NES] rewind for the masses --- BizHawk.Emulation/CPUs/MOS 6502/MOS6502.cs | 16 +++++ .../Consoles/Nintendo/NES/NES.cs | 10 +-- BizHawk.Emulation/Util.cs | 68 +++++++++++++++++++ 3 files changed, 86 insertions(+), 8 deletions(-) diff --git a/BizHawk.Emulation/CPUs/MOS 6502/MOS6502.cs b/BizHawk.Emulation/CPUs/MOS 6502/MOS6502.cs index d0df5d6a32..eb438c0a65 100644 --- a/BizHawk.Emulation/CPUs/MOS 6502/MOS6502.cs +++ b/BizHawk.Emulation/CPUs/MOS 6502/MOS6502.cs @@ -152,6 +152,22 @@ namespace BizHawk.Emulation.CPUs.M6502 } } + void SyncStateBinary(BinarySerializer ser) + { + ser.Sync(ref A); + ser.Sync(ref X); + ser.Sync(ref Y); + ser.Sync(ref P); + ser.Sync(ref PC); + ser.Sync(ref S); + ser.Sync(ref NMI); + ser.Sync(ref IRQ); + ser.Sync(ref TotalExecutedCycles); + ser.Sync(ref PendingCycles); + } + public void SaveStateBinary(BinaryWriter writer) { SyncStateBinary(BinarySerializer.CreateWriter(writer)); } + public void LoadStateBinary(BinaryReader reader) { SyncStateBinary(BinarySerializer.CreateReader(reader)); } + // ==== End State ==== /// Carry Flag diff --git a/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs index f50d0e6ccc..d33234c2ca 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/NES/NES.cs @@ -513,12 +513,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo public void SaveStateBinary(BinaryWriter bw) { - using (var sw = new StringWriter()) - { - cpu.SaveStateText(sw); - sw.Flush(); - Util.WriteByteBuffer(bw, System.Text.Encoding.ASCII.GetBytes(sw.ToString())); - } + cpu.SaveStateBinary(bw); Util.WriteByteBuffer(bw, ram); Util.WriteByteBuffer(bw, CIRAM); bw.Write(cpu_accumulate); @@ -529,8 +524,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo public void LoadStateBinary(BinaryReader br) { - using (var sr = new StringReader(System.Text.Encoding.ASCII.GetString(Util.ReadByteBuffer(br, false)))) - cpu.LoadStateText(sr); + cpu.LoadStateBinary(br); ram = Util.ReadByteBuffer(br, false); CIRAM = Util.ReadByteBuffer(br, false); cpu_accumulate = br.ReadInt32(); diff --git a/BizHawk.Emulation/Util.cs b/BizHawk.Emulation/Util.cs index 28a363eaec..1062ba7582 100644 --- a/BizHawk.Emulation/Util.cs +++ b/BizHawk.Emulation/Util.cs @@ -515,6 +515,74 @@ namespace BizHawk } + public class BinarySerializer + { + BinaryReader br; + BinaryWriter bw; + public BinarySerializer() { } + public BinarySerializer(BinaryWriter _bw) { StartWrite(_bw); } + public BinarySerializer(BinaryReader _br) { StartRead(_br); } + public void StartWrite(BinaryWriter _bw) { this.bw = _bw; } + public void StartRead(BinaryReader _br) { this.br = _br; } + public static BinarySerializer CreateWriter(BinaryWriter _bw) { return new BinarySerializer(_bw); } + public static BinarySerializer CreateReader(BinaryReader _br) { return new BinarySerializer(_br); } + + bool IsReader { get { return br != null; } } + + public void Sync(ref byte val) + { + if (IsReader) Read(ref val); + else Write(ref val); + } + public void Sync(ref ushort val) + { + if (IsReader) Read(ref val); + else Write(ref val); + } + public void Sync(ref uint val) + { + if (IsReader) Read(ref val); + else Write(ref val); + } + public void Sync(ref sbyte val) + { + if (IsReader) Read(ref val); + else Write(ref val); + } + public void Sync(ref short val) + { + if (IsReader) Read(ref val); + else Write(ref val); + } + public void Sync(ref int val) + { + if (IsReader) Read(ref val); + else Write(ref val); + } + public void Sync(ref bool val) + { + if (IsReader) Read(ref val); + else Write(ref val); + } + + + void Read(ref byte val) { val = br.ReadByte(); } + void Write(ref byte val) { bw.Write(val); } + void Read(ref ushort val) { val = br.ReadUInt16(); } + void Write(ref ushort val) { bw.Write(val); } + void Read(ref uint val) { val = br.ReadUInt32(); } + void Write(ref uint val) { bw.Write(val); } + void Read(ref sbyte val) { val = br.ReadSByte(); } + void Write(ref sbyte val) { bw.Write(val); } + void Read(ref short val) { val = br.ReadInt16(); } + void Write(ref short val) { bw.Write(val); } + void Read(ref int val) { val = br.ReadInt32(); } + void Write(ref int val) { bw.Write(val); } + + void Read(ref bool val) { val = br.ReadBoolean(); } + void Write(ref bool val) { bw.Write(val); } + } + public static class BITREV {