2015-01-16 18:37:42 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
using BizHawk.Emulation.Common;
|
|
|
|
|
|
|
|
|
|
namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
|
|
|
|
|
{
|
|
|
|
|
public partial class Gameboy : IStatable
|
|
|
|
|
{
|
2017-04-25 15:42:11 +00:00
|
|
|
|
public bool BinarySaveStatesPreferred => true;
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
public void SaveStateText(TextWriter writer)
|
2015-01-16 18:37:42 +00:00
|
|
|
|
{
|
|
|
|
|
var s = SaveState();
|
|
|
|
|
ser.Serialize(writer, s);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
public void LoadStateText(TextReader reader)
|
2015-01-16 18:37:42 +00:00
|
|
|
|
{
|
|
|
|
|
var s = (TextState<TextStateData>)ser.Deserialize(reader, typeof(TextState<TextStateData>));
|
|
|
|
|
LoadState(s);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
public void SaveStateBinary(BinaryWriter writer)
|
2015-01-16 18:37:42 +00:00
|
|
|
|
{
|
2017-04-25 15:42:11 +00:00
|
|
|
|
if (!LibGambatte.gambatte_newstatesave(GambatteState, _savebuff, _savebuff.Length))
|
|
|
|
|
{
|
2015-01-16 18:37:42 +00:00
|
|
|
|
throw new Exception("gambatte_newstatesave() returned false");
|
2017-04-25 15:42:11 +00:00
|
|
|
|
}
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
writer.Write(_savebuff.Length);
|
|
|
|
|
writer.Write(_savebuff);
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
|
|
|
|
// other variables
|
|
|
|
|
writer.Write(IsLagFrame);
|
|
|
|
|
writer.Write(LagCount);
|
|
|
|
|
writer.Write(Frame);
|
|
|
|
|
writer.Write(frameOverflow);
|
|
|
|
|
writer.Write(_cycleCount);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
public void LoadStateBinary(BinaryReader reader)
|
2015-01-16 18:37:42 +00:00
|
|
|
|
{
|
|
|
|
|
int length = reader.ReadInt32();
|
2017-04-25 15:42:11 +00:00
|
|
|
|
if (length != _savebuff.Length)
|
|
|
|
|
{
|
2015-01-16 18:37:42 +00:00
|
|
|
|
throw new InvalidOperationException("Savestate buffer size mismatch!");
|
2017-04-25 15:42:11 +00:00
|
|
|
|
}
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
reader.Read(_savebuff, 0, _savebuff.Length);
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
if (!LibGambatte.gambatte_newstateload(GambatteState, _savebuff, _savebuff.Length))
|
|
|
|
|
{
|
2015-01-16 18:37:42 +00:00
|
|
|
|
throw new Exception("gambatte_newstateload() returned false");
|
2017-04-25 15:42:11 +00:00
|
|
|
|
}
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
|
|
|
|
// other variables
|
|
|
|
|
IsLagFrame = reader.ReadBoolean();
|
|
|
|
|
LagCount = reader.ReadInt32();
|
|
|
|
|
Frame = reader.ReadInt32();
|
|
|
|
|
frameOverflow = reader.ReadUInt32();
|
|
|
|
|
_cycleCount = reader.ReadUInt64();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] SaveStateBinary()
|
|
|
|
|
{
|
2017-04-25 15:42:11 +00:00
|
|
|
|
MemoryStream ms = new MemoryStream(_savebuff2);
|
2015-01-16 18:37:42 +00:00
|
|
|
|
BinaryWriter bw = new BinaryWriter(ms);
|
|
|
|
|
SaveStateBinary(bw);
|
|
|
|
|
bw.Flush();
|
2017-04-25 15:42:11 +00:00
|
|
|
|
if (ms.Position != _savebuff2.Length)
|
|
|
|
|
{
|
2015-01-16 18:37:42 +00:00
|
|
|
|
throw new InvalidOperationException();
|
2017-04-25 15:42:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-16 18:37:42 +00:00
|
|
|
|
ms.Close();
|
2017-04-25 15:42:11 +00:00
|
|
|
|
return _savebuff2;
|
2015-01-16 18:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
private byte[] _savebuff;
|
|
|
|
|
private byte[] _savebuff2;
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
|
|
|
|
private void NewSaveCoreSetBuff()
|
|
|
|
|
{
|
2017-04-25 15:42:11 +00:00
|
|
|
|
_savebuff = new byte[LibGambatte.gambatte_newstatelen(GambatteState)];
|
|
|
|
|
_savebuff2 = new byte[_savebuff.Length + 4 + 21];
|
2015-01-16 18:37:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 15:42:11 +00:00
|
|
|
|
private readonly JsonSerializer ser = new JsonSerializer { Formatting = Formatting.Indented };
|
2015-01-16 18:37:42 +00:00
|
|
|
|
|
|
|
|
|
// other data in the text state besides core
|
|
|
|
|
internal class TextStateData
|
|
|
|
|
{
|
|
|
|
|
public int Frame;
|
|
|
|
|
public int LagCount;
|
|
|
|
|
public bool IsLagFrame;
|
|
|
|
|
public ulong _cycleCount;
|
|
|
|
|
public uint frameOverflow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal TextState<TextStateData> SaveState()
|
|
|
|
|
{
|
|
|
|
|
var s = new TextState<TextStateData>();
|
|
|
|
|
s.Prepare();
|
|
|
|
|
var ff = s.GetFunctionPointersSave();
|
|
|
|
|
LibGambatte.gambatte_newstatesave_ex(GambatteState, ref ff);
|
|
|
|
|
s.ExtraData.IsLagFrame = IsLagFrame;
|
|
|
|
|
s.ExtraData.LagCount = LagCount;
|
|
|
|
|
s.ExtraData.Frame = Frame;
|
|
|
|
|
s.ExtraData.frameOverflow = frameOverflow;
|
|
|
|
|
s.ExtraData._cycleCount = _cycleCount;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void LoadState(TextState<TextStateData> s)
|
|
|
|
|
{
|
|
|
|
|
s.Prepare();
|
|
|
|
|
var ff = s.GetFunctionPointersLoad();
|
|
|
|
|
LibGambatte.gambatte_newstateload_ex(GambatteState, ref ff);
|
|
|
|
|
IsLagFrame = s.ExtraData.IsLagFrame;
|
|
|
|
|
LagCount = s.ExtraData.LagCount;
|
|
|
|
|
Frame = s.ExtraData.Frame;
|
|
|
|
|
frameOverflow = s.ExtraData.frameOverflow;
|
|
|
|
|
_cycleCount = s.ExtraData._cycleCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|