From fccf9f06a9f1f8e8f2f28ab9fc3dde468f133b61 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 8 Mar 2015 03:10:20 +0000 Subject: [PATCH] Apple II - wire up savestates (binary only, for now) --- .../Computers/AppleII/AppleII.cs | 54 ++++++++++++++++++- .../Computers/AppleII/Virtu/Machine.cs | 29 ++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs index 57064ff8dc..b4fee4c92f 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/AppleII.cs @@ -10,7 +10,7 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII isPorted: true, isReleased: false )] - public partial class AppleII : IEmulator + public partial class AppleII : IEmulator, IStatable { [CoreConstructor("AppleII")] public AppleII(CoreComm comm, GameInfo game, byte[] rom, object Settings) @@ -250,6 +250,56 @@ namespace BizHawk.Emulation.Cores.Computers.AppleII } #endregion - + + #region IStatable + + public bool BinarySaveStatesPreferred { get { return false; } } + + [FeatureNotImplemented] + public void SaveStateText(TextWriter writer) + { + + } + + [FeatureNotImplemented] + public void LoadStateText(TextReader reader) + { + + } + + public void SaveStateBinary(BinaryWriter writer) + { + _machine.SaveState(writer); + } + + public void LoadStateBinary(BinaryReader reader) + { + _machine.LoadState(reader); + } + + public byte[] SaveStateBinary() + { + if (_stateBuffer == null) + { + var stream = new MemoryStream(); + var writer = new BinaryWriter(stream); + SaveStateBinary(writer); + _stateBuffer = stream.ToArray(); + writer.Close(); + return _stateBuffer; + } + else + { + var stream = new MemoryStream(_stateBuffer); + var writer = new BinaryWriter(stream); + SaveStateBinary(writer); + writer.Close(); + return _stateBuffer; + } + } + + private byte[] _stateBuffer; + + #endregion } } diff --git a/BizHawk.Emulation.Cores/Computers/AppleII/Virtu/Machine.cs b/BizHawk.Emulation.Cores/Computers/AppleII/Virtu/Machine.cs index 88790ec3d5..99f9d59207 100644 --- a/BizHawk.Emulation.Cores/Computers/AppleII/Virtu/Machine.cs +++ b/BizHawk.Emulation.Cores/Computers/AppleII/Virtu/Machine.cs @@ -204,6 +204,23 @@ namespace Jellyfish.Virtu } } + // TODO: don't copy paste + public void LoadState(BinaryReader reader) + { + string signature = reader.ReadString(); + var version = new Version(reader.ReadString()); + if ((signature != StateSignature) || (version != new Version(Machine.Version))) // avoid state version mismatch (for now) + { + throw new InvalidOperationException(); + } + foreach (var component in Components) + { + _debugService.WriteMessage("Loading machine '{0}'", component.GetType().Name); + component.LoadState(reader, version); + //_debugService.WriteMessage("Loaded machine '{0}'", component.GetType().Name); + } + } + private void LoadState(Stream stream) { using (var reader = new BinaryReader(stream)) @@ -228,6 +245,18 @@ namespace Jellyfish.Virtu _storageService.Save(Machine.StateFileName, stream => SaveState(stream)); } + public void SaveState(BinaryWriter writer) + { + writer.Write(StateSignature); + writer.Write(Machine.Version); + foreach (var component in Components) + { + _debugService.WriteMessage("Saving machine '{0}'", component.GetType().Name); + component.SaveState(writer); + //_debugService.WriteMessage("Saved machine '{0}'", component.GetType().Name); + } + } + private void SaveState(Stream stream) { using (var writer = new BinaryWriter(stream))