mgba 0.5 rough in

This commit is contained in:
nattthebear 2016-09-17 16:29:25 -04:00
parent 7d1da4bdaf
commit 5bd9a67f62
4 changed files with 45 additions and 66 deletions

View File

@ -16,7 +16,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public static extern void BizDestroy(IntPtr ctx); public static extern void BizDestroy(IntPtr ctx);
[DllImport(dll, CallingConvention = cc)] [DllImport(dll, CallingConvention = cc)]
public static extern IntPtr BizCreate(byte[] bios); public static extern IntPtr BizCreate(byte[] bios, byte[] data, int length, [In]OverrideInfo dbinfo);
[DllImport(dll, CallingConvention = cc)] [DllImport(dll, CallingConvention = cc)]
public static extern void BizReset(IntPtr ctx); public static extern void BizReset(IntPtr ctx);
@ -57,9 +57,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public const uint IDLE_LOOP_NONE = unchecked((uint)0xffffffff); public const uint IDLE_LOOP_NONE = unchecked((uint)0xffffffff);
} }
[DllImport(dll, CallingConvention = cc)]
public static extern bool BizLoad(IntPtr ctx, byte[] data, int length, [In]OverrideInfo dbinfo);
[DllImport(dll, CallingConvention = cc)] [DllImport(dll, CallingConvention = cc)]
public static extern bool BizAdvance(IntPtr ctx, LibVBANext.Buttons keys, int[] vbuff, ref int nsamp, short[] sbuff, public static extern bool BizAdvance(IntPtr ctx, LibVBANext.Buttons keys, int[] vbuff, ref int nsamp, short[] sbuff,
long time, short gyrox, short gyroy, short gyroz, byte luma); long time, short gyrox, short gyroy, short gyroz, byte luma);
@ -83,11 +80,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public static extern void BizGetMemoryAreas(IntPtr ctx, [Out]MemoryAreas dst); public static extern void BizGetMemoryAreas(IntPtr ctx, [Out]MemoryAreas dst);
[DllImport(dll, CallingConvention = cc)] [DllImport(dll, CallingConvention = cc)]
public static extern int BizGetSaveRamSize(IntPtr ctx); public static extern int BizGetSaveRam(IntPtr ctx, byte[] dest);
[DllImport(dll, CallingConvention = cc)] [DllImport(dll, CallingConvention = cc)]
public static extern void BizGetSaveRam(IntPtr ctx, byte[] dest); public static extern bool BizPutSaveRam(IntPtr ctx, byte[] src, int size);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizPutSaveRam(IntPtr ctx, byte[] src);
[DllImport(dll, CallingConvention = cc)] [DllImport(dll, CallingConvention = cc)]
public static extern int BizGetStateMaxSize(IntPtr ctx); public static extern int BizGetStateMaxSize(IntPtr ctx);

View File

@ -12,9 +12,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
{ {
[CoreAttributes("mGBA", "endrift", true, true, "0.4.1", "https://mgba.io/", false)] [CoreAttributes("mGBA", "endrift", true, true, "0.4.1", "https://mgba.io/", false)]
[ServiceNotApplicable(typeof(IDriveLight), typeof(IRegionable))] [ServiceNotApplicable(typeof(IDriveLight), typeof(IRegionable))]
public class MGBAHawk : IEmulator, IVideoProvider, ISyncSoundProvider, IGBAGPUViewable, ISaveRam, IStatable, IInputPollable, ISettable<MGBAHawk.Settings, MGBAHawk.SyncSettings> public class MGBAHawk : IEmulator, IVideoProvider, ISyncSoundProvider, IGBAGPUViewable,
ISaveRam, IStatable, IInputPollable, ISettable<MGBAHawk.Settings, MGBAHawk.SyncSettings>
{ {
IntPtr core; private IntPtr _core;
private byte[] _saveScratch = new byte[262144];
[CoreConstructor("GBA")] [CoreConstructor("GBA")]
public MGBAHawk(byte[] file, CoreComm comm, SyncSettings syncSettings, Settings settings, bool deterministic, GameInfo game) public MGBAHawk(byte[] file, CoreComm comm, SyncSettings syncSettings, Settings settings, bool deterministic, GameInfo game)
@ -40,24 +42,18 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
{ {
throw new InvalidOperationException("BIOS must be exactly 16384 bytes!"); throw new InvalidOperationException("BIOS must be exactly 16384 bytes!");
} }
core = LibmGBA.BizCreate(bios); _core = LibmGBA.BizCreate(bios, file, file.Length, GetOverrideInfo(game));
if (core == IntPtr.Zero) if (_core == IntPtr.Zero)
{ {
throw new InvalidOperationException("BizCreate() returned NULL! Bad BIOS?"); throw new InvalidOperationException("BizCreate() returned NULL! Bad BIOS? and/or ROM?");
} }
try try
{ {
if (!LibmGBA.BizLoad(core, file, file.Length, GetOverrideInfo(game)))
{
throw new InvalidOperationException("BizLoad() returned FALSE! Bad ROM?");
}
if (!DeterministicEmulation && _syncSettings.SkipBios) if (!DeterministicEmulation && _syncSettings.SkipBios)
{ {
LibmGBA.BizSkipBios(core); LibmGBA.BizSkipBios(_core);
} }
CreateMemoryDomains(file.Length); CreateMemoryDomains(file.Length);
var ser = new BasicServiceProvider(this); var ser = new BasicServiceProvider(this);
ser.Register<IDisassemblable>(new ArmV4Disassembler()); ser.Register<IDisassemblable>(new ArmV4Disassembler());
@ -75,7 +71,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
} }
catch catch
{ {
LibmGBA.BizDestroy(core); LibmGBA.BizDestroy(_core);
throw; throw;
} }
} }
@ -138,12 +134,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
Frame++; Frame++;
if (Controller["Power"]) if (Controller["Power"])
{ {
LibmGBA.BizReset(core); LibmGBA.BizReset(_core);
//BizReset caused memorydomain pointers to change. //BizReset caused memorydomain pointers to change.
WireMemoryDomainPointers(); WireMemoryDomainPointers();
} }
IsLagFrame = LibmGBA.BizAdvance(core, VBANext.GetButtons(Controller), videobuff, ref nsamp, soundbuff, IsLagFrame = LibmGBA.BizAdvance(_core, VBANext.GetButtons(Controller), videobuff, ref nsamp, soundbuff,
RTCTime(), RTCTime(),
(short)Controller.GetFloat("Tilt X"), (short)Controller.GetFloat("Tilt X"),
(short)Controller.GetFloat("Tilt Y"), (short)Controller.GetFloat("Tilt Y"),
@ -176,10 +172,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public void Dispose() public void Dispose()
{ {
if (core != IntPtr.Zero) if (_core != IntPtr.Zero)
{ {
LibmGBA.BizDestroy(core); LibmGBA.BizDestroy(_core);
core = IntPtr.Zero; _core = IntPtr.Zero;
} }
} }
@ -226,7 +222,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
void WireMemoryDomainPointers() void WireMemoryDomainPointers()
{ {
var s = new LibmGBA.MemoryAreas(); var s = new LibmGBA.MemoryAreas();
LibmGBA.BizGetMemoryAreas(core, s); LibmGBA.BizGetMemoryAreas(_core, s);
_iwram.Data = s.iwram; _iwram.Data = s.iwram;
_ewram.Data = s.wram; _ewram.Data = s.wram;
@ -243,7 +239,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
_cwram.Peek = _cwram.Peek =
delegate(long addr) delegate(long addr)
{ {
LibmGBA.BizGetMemoryAreas(core, s); LibmGBA.BizGetMemoryAreas(_core, s);
if (addr < 0 || addr >= (256 + 32) * 1024) if (addr < 0 || addr >= (256 + 32) * 1024)
throw new IndexOutOfRangeException(); throw new IndexOutOfRangeException();
if (addr >= 256 * 1024) if (addr >= 256 * 1024)
@ -321,16 +317,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public byte[] CloneSaveRam() public byte[] CloneSaveRam()
{ {
byte[] ret = new byte[LibmGBA.BizGetSaveRamSize(core)]; int len = LibmGBA.BizGetSaveRam(_core, _saveScratch);
if (ret.Length > 0) if (len == 0)
{
LibmGBA.BizGetSaveRam(core, ret);
return ret;
}
else
{
return null; return null;
}
var ret = new byte[len];
Array.Copy(_saveScratch, ret, len);
return ret;
} }
private static byte[] LegacyFix(byte[] saveram) private static byte[] LegacyFix(byte[] saveram)
@ -359,39 +352,30 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
{ {
data = LegacyFix(data); data = LegacyFix(data);
} }
if (!LibmGBA.BizPutSaveRam(_core, data, data.Length))
int len = LibmGBA.BizGetSaveRamSize(core);
if (len > data.Length)
{ {
byte[] _tmp = new byte[len]; throw new InvalidOperationException("BizPutSaveRam returned NULL!");
Array.Copy(data, _tmp, data.Length);
for (int i = data.Length; i < len; i++)
_tmp[i] = 0xff;
data = _tmp;
} }
else if (len < data.Length)
{
// we could continue from this, but we don't expect it
throw new InvalidOperationException("Saveram will be truncated!");
}
LibmGBA.BizPutSaveRam(core, data);
} }
public bool SaveRamModified public bool SaveRamModified
{ {
get { return LibmGBA.BizGetSaveRamSize(core) > 0; } get
{
return LibmGBA.BizGetSaveRam(_core, _saveScratch) > 0;
}
} }
#endregion #endregion
private void InitStates() private void InitStates()
{ {
savebuff = new byte[LibmGBA.BizGetStateMaxSize(core)]; _savebuff = new byte[LibmGBA.BizGetStateMaxSize(_core)];
savebuff2 = new byte[savebuff.Length + 13]; _savebuff2 = new byte[_savebuff.Length + 13];
} }
private byte[] savebuff; private byte[] _savebuff;
private byte[] savebuff2; private byte[] _savebuff2;
public bool BinarySaveStatesPreferred public bool BinarySaveStatesPreferred
{ {
@ -413,11 +397,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public void SaveStateBinary(BinaryWriter writer) public void SaveStateBinary(BinaryWriter writer)
{ {
int size = LibmGBA.BizGetState(core, savebuff, savebuff.Length); int size = LibmGBA.BizGetState(_core, _savebuff, _savebuff.Length);
if (size < 0) if (size < 0)
throw new InvalidOperationException("Core failed to save!"); throw new InvalidOperationException("Core failed to save!");
writer.Write(size); writer.Write(size);
writer.Write(savebuff, 0, size); writer.Write(_savebuff, 0, size);
// other variables // other variables
writer.Write(IsLagFrame); writer.Write(IsLagFrame);
@ -428,13 +412,13 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public void LoadStateBinary(BinaryReader reader) public void LoadStateBinary(BinaryReader reader)
{ {
int length = reader.ReadInt32(); int length = reader.ReadInt32();
if (length > savebuff.Length) if (length > _savebuff.Length)
{ {
savebuff = new byte[length]; _savebuff = new byte[length];
savebuff2 = new byte[length + 13]; _savebuff2 = new byte[length + 13];
} }
reader.Read(savebuff, 0, length); reader.Read(_savebuff, 0, length);
if (!LibmGBA.BizPutState(core, savebuff, length)) if (!LibmGBA.BizPutState(_core, _savebuff, length))
throw new InvalidOperationException("Core rejected the savestate!"); throw new InvalidOperationException("Core rejected the savestate!");
// other variables // other variables
@ -445,12 +429,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public byte[] SaveStateBinary() public byte[] SaveStateBinary()
{ {
var ms = new MemoryStream(savebuff2, true); var ms = new MemoryStream(_savebuff2, true);
var bw = new BinaryWriter(ms); var bw = new BinaryWriter(ms);
SaveStateBinary(bw); SaveStateBinary(bw);
bw.Flush(); bw.Flush();
ms.Close(); ms.Close();
return savebuff2; return _savebuff2;
} }
public int LagCount { get; set; } public int LagCount { get; set; }
@ -486,7 +470,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
if (o.DisplayBG2) mask |= LibmGBA.Layers.BG2; if (o.DisplayBG2) mask |= LibmGBA.Layers.BG2;
if (o.DisplayBG3) mask |= LibmGBA.Layers.BG3; if (o.DisplayBG3) mask |= LibmGBA.Layers.BG3;
if (o.DisplayOBJ) mask |= LibmGBA.Layers.OBJ; if (o.DisplayOBJ) mask |= LibmGBA.Layers.OBJ;
LibmGBA.BizSetLayerMask(core, mask); LibmGBA.BizSetLayerMask(_core, mask);
_settings = o; _settings = o;
return false; return false;
} }

Binary file not shown.

Binary file not shown.