change the IEmulator saveram interface.

i don't like doing this, but there were already two emus with special bandaid logic in MainForm.cs
i hope this doesn't break something, but if it does i'll fix it
This commit is contained in:
goyuken 2012-09-14 22:28:38 +00:00
parent feb503c502
commit 83e145c36f
13 changed files with 100 additions and 43 deletions

View File

@ -64,9 +64,12 @@ namespace BizHawk
private int _lagcount = 0; private int _lagcount = 0;
private int _frame = 0; private int _frame = 0;
public byte[] ReadSaveRam { get { return new byte[0]; } } public byte[] ReadSaveRam() { return null; }
public bool DeterministicEmulation { get; set; } public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { }
public bool SaveRamModified { get; set; } public bool SaveRamModified { get; set; }
public bool DeterministicEmulation { get; set; }
public void SaveStateText(TextWriter writer) { SyncState(Serializer.CreateTextWriter(writer)); } public void SaveStateText(TextWriter writer) { SyncState(Serializer.CreateTextWriter(writer)); }
public void LoadStateText(TextReader reader) { SyncState(Serializer.CreateTextReader(reader)); } public void LoadStateText(TextReader reader) { SyncState(Serializer.CreateTextReader(reader)); }
public void SaveStateBinary(BinaryWriter bw) { SyncState(Serializer.CreateBinaryWriter(bw)); } public void SaveStateBinary(BinaryWriter bw) { SyncState(Serializer.CreateBinaryWriter(bw)); }
@ -80,7 +83,7 @@ namespace BizHawk
bw.Flush(); bw.Flush();
return ms.ToArray(); return ms.ToArray();
} }
private IList<MemoryDomain> memoryDomains; private IList<MemoryDomain> memoryDomains;
public IList<MemoryDomain> MemoryDomains { get { return memoryDomains; } } public IList<MemoryDomain> MemoryDomains { get { return memoryDomains; } }
public MemoryDomain MainMemory { get { return memoryDomains[0]; } } public MemoryDomain MainMemory { get { return memoryDomains[0]; } }

View File

@ -478,7 +478,9 @@ namespace BizHawk.Emulation.Consoles.Calculator
public bool DeterministicEmulation { get { return true; } set { } } public bool DeterministicEmulation { get { return true; } set { } }
public byte[] ReadSaveRam { get { return null; } } public byte[] ReadSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { }
public bool SaveRamModified public bool SaveRamModified
{ {
get { return false; } get { return false; }

View File

@ -70,9 +70,12 @@ namespace BizHawk.Emulation.Consoles.Coleco
private int _lagcount = 0; private int _lagcount = 0;
private int _frame = 0; private int _frame = 0;
public byte[] ReadSaveRam { get { return new byte[0]; } } public byte[] ReadSaveRam() { return null; }
public bool DeterministicEmulation { get; set; } public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { }
public bool SaveRamModified { get; set; } public bool SaveRamModified { get; set; }
public bool DeterministicEmulation { get; set; }
public void SaveStateText(TextWriter writer) { SyncState(Serializer.CreateTextWriter(writer)); } public void SaveStateText(TextWriter writer) { SyncState(Serializer.CreateTextWriter(writer)); }
public void LoadStateText(TextReader reader) { SyncState(Serializer.CreateTextReader(reader)); } public void LoadStateText(TextReader reader) { SyncState(Serializer.CreateTextReader(reader)); }
public void SaveStateBinary(BinaryWriter bw) { SyncState(Serializer.CreateBinaryWriter(bw)); } public void SaveStateBinary(BinaryWriter bw) { SyncState(Serializer.CreateBinaryWriter(bw)); }

View File

@ -117,8 +117,10 @@ namespace BizHawk.Emulation.Consoles.Intellivision
public bool DeterministicEmulation { get; set; } public bool DeterministicEmulation { get; set; }
public byte[] ReadSaveRam { get { return null; } }
public byte[] ReadSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { }
public bool SaveRamModified public bool SaveRamModified
{ {
get { return false; } get { return false; }

View File

@ -137,21 +137,18 @@ namespace BizHawk.Emulation.Consoles.GB
public bool DeterministicEmulation { get; set; } public bool DeterministicEmulation { get; set; }
public byte[] ReadSaveRam public byte[] ReadSaveRam()
{ {
get int length = LibGambatte.gambatte_savesavedatalength(GambatteState);
{
int length = LibGambatte.gambatte_savesavedatalength(GambatteState);
if (length > 0) if (length > 0)
{ {
byte[] ret = new byte[length]; byte[] ret = new byte[length];
LibGambatte.gambatte_savesavedata(GambatteState, ret); LibGambatte.gambatte_savesavedata(GambatteState, ret);
return ret; return ret;
}
else
return new byte[0];
} }
else
return new byte[0];
} }
public void StoreSaveRam(byte[] data) public void StoreSaveRam(byte[] data)

View File

@ -303,14 +303,29 @@ namespace BizHawk.Emulation.Consoles.Nintendo
public bool DeterministicEmulation { get { return true; } set { } } public bool DeterministicEmulation { get { return true; } set { } }
public byte[] ReadSaveRam
public byte[] ReadSaveRam()
{ {
get if (board == null || board.SaveRam == null)
{ return null;
if (board == null) return null; return (byte[])board.SaveRam.Clone();
return board.SaveRam;
}
} }
public void StoreSaveRam(byte[] data)
{
if (board == null || board.SaveRam == null)
return;
Array.Copy(data, board.SaveRam, data.Length);
}
public void ClearSaveRam()
{
if (board == null || board.SaveRam == null)
return;
for (int i = 0; i < board.SaveRam.Length; i++)
board.SaveRam[i] = 0;
}
public bool SaveRamModified public bool SaveRamModified
{ {
get { if (board == null) return false; if (board.SaveRam == null) return false; return true; } get { if (board == null) return false; if (board.SaveRam == null) return false; return true; }

View File

@ -414,7 +414,11 @@ namespace BizHawk.Emulation.Consoles.Nintendo.SNES
} }
} }
public byte[] ReadSaveRam { get { return snes_get_memory_data_read(LibsnesDll.SNES_MEMORY.CARTRIDGE_RAM); } } public byte[] ReadSaveRam()
{
return snes_get_memory_data_read(LibsnesDll.SNES_MEMORY.CARTRIDGE_RAM);
}
public static byte[] snes_get_memory_data_read(LibsnesDll.SNES_MEMORY id) public static byte[] snes_get_memory_data_read(LibsnesDll.SNES_MEMORY id)
{ {
var size = (int)LibsnesDll.snes_get_memory_size(id); var size = (int)LibsnesDll.snes_get_memory_size(id);
@ -433,6 +437,12 @@ namespace BizHawk.Emulation.Consoles.Nintendo.SNES
Marshal.Copy(data, 0, emudata, size); Marshal.Copy(data, 0, emudata, size);
} }
public void ClearSaveRam()
{
byte[] cleardata = new byte[(int)LibsnesDll.snes_get_memory_size(LibsnesDll.SNES_MEMORY.CARTRIDGE_RAM)];
StoreSaveRam(cleardata);
}
public void ResetFrameCounter() { timeFrameCounter = 0; } public void ResetFrameCounter() { timeFrameCounter = 0; }
public void SaveStateText(TextWriter writer) public void SaveStateText(TextWriter writer)
{ {

View File

@ -275,11 +275,9 @@ namespace BizHawk.Emulation.Consoles.TurboGrafx
public string Region { get; set; } public string Region { get; set; }
public bool DeterministicEmulation { get; set; } public bool DeterministicEmulation { get; set; }
public byte[] ReadSaveRam public byte[] ReadSaveRam() { return (byte[])BRAM.Clone(); }
{ public void StoreSaveRam(byte[] data) { Array.Copy(data, BRAM, data.Length); }
get { return BRAM; } public void ClearSaveRam() { BRAM = new byte[BRAM.Length]; }
}
public bool SaveRamModified { get; set; } public bool SaveRamModified { get; set; }
public void SaveStateText(TextWriter writer) public void SaveStateText(TextWriter writer)

View File

@ -23,10 +23,9 @@ namespace BizHawk.Emulation.Consoles.Sega
} }
} }
public byte[] ReadSaveRam // TODO if you're going to rename this to ReadSaveRam, refactor it to be a method, not a property. public byte[] ReadSaveRam() { return (byte[])SaveRAM.Clone(); }
{ public void StoreSaveRam(byte[] data) { Array.Copy(data, SaveRAM, data.Length); }
get { return SaveRAM; } public void ClearSaveRam() { SaveRAM = new byte[SaveRAM.Length]; }
}
public bool SaveRamModified { get; set; } public bool SaveRamModified { get; set; }
} }

View File

@ -31,7 +31,9 @@ namespace BizHawk.Emulation.Consoles.Sega
public byte[] SaveRAM = new byte[BankSize * 2]; public byte[] SaveRAM = new byte[BankSize * 2];
public byte SaveRamBank; public byte SaveRamBank;
public byte[] ReadSaveRam { get { return SaveRAM; } } public byte[] ReadSaveRam() { return (byte[])SaveRAM.Clone(); }
public void StoreSaveRam(byte[] data) { Array.Copy(data, SaveRAM, data.Length); }
public void ClearSaveRam() { SaveRAM = new byte[SaveRAM.Length]; }
public bool SaveRamModified { get; set; } public bool SaveRamModified { get; set; }
// Machine resources // Machine resources

View File

@ -41,7 +41,9 @@ namespace BizHawk
public int LagCount { get { return 0; } set { return; } } public int LagCount { get { return 0; } set { return; } }
public bool IsLagFrame { get { return false; } } public bool IsLagFrame { get { return false; } }
public byte[] ReadSaveRam { get { return new byte[0]; } } public byte[] ReadSaveRam() { return null; }
public void StoreSaveRam(byte[] data) { }
public void ClearSaveRam() { }
public bool DeterministicEmulation { get; set; } public bool DeterministicEmulation { get; set; }
public bool SaveRamModified { get; set; } public bool SaveRamModified { get; set; }
public void SaveStateText(TextWriter writer) { } public void SaveStateText(TextWriter writer) { }

View File

@ -21,11 +21,21 @@ namespace BizHawk
bool DeterministicEmulation { get; set; } bool DeterministicEmulation { get; set; }
/// <summary> /// <summary>
/// Don't edit this data, it may be useless as it may be a copy or some compiled data. /// return a copy of the saveram. editing it won't do you any good unless you later call StoreSaveRam()
/// use StoreSaveRam if you want to store sram into the core.
/// Well, this rule is being violated. This comment is just a reminder that this is a bad architecture
/// </summary> /// </summary>
byte[] ReadSaveRam { get; } byte[] ReadSaveRam();
/// <summary>
/// store new saveram to the emu core. the data should be the same size as the return from ReadSaveRam()
/// </summary>
void StoreSaveRam(byte[] data);
/// <summary>
/// reset saveram to a standard initial state
/// </summary>
void ClearSaveRam();
bool SaveRamModified { get; set; } bool SaveRamModified { get; set; }
void ResetFrameCounter(); void ResetFrameCounter();

View File

@ -1555,6 +1555,7 @@ namespace BizHawk.MultiClient
//zero says: this is sort of sketchy... but this is no time for rearchitecting //zero says: this is sort of sketchy... but this is no time for rearchitecting
try try
{ {
/*
var sram = new byte[Global.Emulator.ReadSaveRam.Length]; var sram = new byte[Global.Emulator.ReadSaveRam.Length];
using (var reader = new BinaryReader(new FileStream(PathManager.SaveRamPath(Global.Game), FileMode.Open, FileAccess.Read))) using (var reader = new BinaryReader(new FileStream(PathManager.SaveRamPath(Global.Game), FileMode.Open, FileAccess.Read)))
reader.Read(sram, 0, Global.Emulator.ReadSaveRam.Length); reader.Read(sram, 0, Global.Emulator.ReadSaveRam.Length);
@ -1564,6 +1565,11 @@ namespace BizHawk.MultiClient
((Gameboy)Global.Emulator).StoreSaveRam(sram); ((Gameboy)Global.Emulator).StoreSaveRam(sram);
else else
Array.Copy(sram, Global.Emulator.ReadSaveRam, Global.Emulator.ReadSaveRam.Length); Array.Copy(sram, Global.Emulator.ReadSaveRam, Global.Emulator.ReadSaveRam.Length);
*/
var sram = new byte[Global.Emulator.ReadSaveRam().Length];
using (var reader = new BinaryReader(new FileStream(PathManager.SaveRamPath(Global.Game), FileMode.Open, FileAccess.Read)))
reader.Read(sram, 0, sram.Length);
Global.Emulator.StoreSaveRam(sram);
} }
catch { } catch { }
} }
@ -1590,8 +1596,13 @@ namespace BizHawk.MultiClient
f.Directory.Create(); f.Directory.Create();
var writer = new BinaryWriter(new FileStream(path, FileMode.Create, FileAccess.Write)); var writer = new BinaryWriter(new FileStream(path, FileMode.Create, FileAccess.Write));
int len = Util.SaveRamBytesUsed(Global.Emulator.ReadSaveRam);
writer.Write(Global.Emulator.ReadSaveRam, 0, len); var saveram = Global.Emulator.ReadSaveRam();
// this assumes that the default state of the core's sram is 0-filled, so don't do
// int len = Util.SaveRamBytesUsed(saveram);
int len = saveram.Length;
writer.Write(saveram, 0, len);
writer.Close(); writer.Close();
} }
@ -3432,6 +3443,7 @@ namespace BizHawk.MultiClient
try try
{ {
/*
var sram = new byte[Global.Emulator.ReadSaveRam.Length]; var sram = new byte[Global.Emulator.ReadSaveRam.Length];
if (Global.Emulator is LibsnesCore) if (Global.Emulator is LibsnesCore)
((LibsnesCore)Global.Emulator).StoreSaveRam(sram); ((LibsnesCore)Global.Emulator).StoreSaveRam(sram);
@ -3439,6 +3451,8 @@ namespace BizHawk.MultiClient
((Gameboy)Global.Emulator).ClearSaveRam(); ((Gameboy)Global.Emulator).ClearSaveRam();
else else
Array.Copy(sram, Global.Emulator.ReadSaveRam, Global.Emulator.ReadSaveRam.Length); Array.Copy(sram, Global.Emulator.ReadSaveRam, Global.Emulator.ReadSaveRam.Length);
*/
Global.Emulator.ClearSaveRam();
} }
catch { } catch { }
} }