wow that was hard

This commit is contained in:
nattthebear 2020-07-26 15:06:44 -04:00
parent 98da2ebb85
commit 105250f60d
3 changed files with 75 additions and 65 deletions

View File

@ -1,10 +1,11 @@
using System;
using System.Runtime.InteropServices;
using BizHawk.BizInvoke;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Nintendo.GBA
{
public static class LibmGBA
public abstract class LibmGBA
{
[Flags]
public enum Buttons : int
@ -34,7 +35,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
return ret;
}
const string dll = "mgba.dll";
const CallingConvention cc = CallingConvention.Cdecl;
public enum SaveType : int
@ -114,30 +114,30 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
public int sram_size;
}
[DllImport(dll, CallingConvention = cc)]
public static extern void BizDestroy(IntPtr ctx);
[BizImport(cc, Compatibility = true)]
public abstract void BizDestroy(IntPtr ctx);
[DllImport(dll, CallingConvention = cc)]
public static extern IntPtr BizCreate(byte[] bios, byte[] data, int length, [In]OverrideInfo dbinfo, bool skipBios);
[BizImport(cc, Compatibility = true)]
public abstract IntPtr BizCreate(byte[] bios, byte[] data, int length, [In]OverrideInfo dbinfo, bool skipBios);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizReset(IntPtr ctx);
[BizImport(cc, Compatibility = true)]
public abstract void BizReset(IntPtr ctx);
[DllImport(dll, CallingConvention = cc)]
public static extern bool BizAdvance(IntPtr ctx, Buttons keys, int[] vbuff, ref int nsamp, short[] sbuff,
[BizImport(cc, Compatibility = true)]
public abstract bool BizAdvance(IntPtr ctx, Buttons keys, int[] vbuff, ref int nsamp, short[] sbuff,
long time, short gyrox, short gyroy, short gyroz, byte luma);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizSetPalette(IntPtr ctx, int[] palette);
[BizImport(cc, Compatibility = true)]
public abstract void BizSetPalette(IntPtr ctx, int[] palette);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizGetMemoryAreas(IntPtr ctx, [Out]MemoryAreas dst);
[BizImport(cc, Compatibility = true)]
public abstract void BizGetMemoryAreas(IntPtr ctx, [Out]MemoryAreas dst);
[DllImport(dll, CallingConvention = cc)]
public static extern int BizGetSaveRam(IntPtr ctx, byte[] dest, int maxsize);
[BizImport(cc, Compatibility = true)]
public abstract int BizGetSaveRam(IntPtr ctx, byte[] dest, int maxsize);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizPutSaveRam(IntPtr ctx, byte[] src, int size);
[BizImport(cc, Compatibility = true)]
public abstract void BizPutSaveRam(IntPtr ctx, byte[] src, int size);
/// <summary>
/// start a savestate operation
@ -145,8 +145,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
/// <param name="p">private parameter to be passed to BizFinishGetState</param>
/// <param name="size">size of buffer to be allocated for BizFinishGetState</param>
/// <returns>if false, operation failed and BizFinishGetState should not be called</returns>
[DllImport(dll, CallingConvention = cc)]
public static extern bool BizStartGetState(IntPtr ctx, ref IntPtr p, ref int size);
[BizImport(cc, Compatibility = true)]
public abstract bool BizStartGetState(IntPtr ctx, ref IntPtr p, ref int size);
/// <summary>
/// finish a savestate operation. if StartGetState returned true, this must be called else memory leaks
@ -154,52 +154,52 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
/// <param name="p">returned by BizStartGetState</param>
/// <param name="dest">buffer of length size</param>
/// <param name="size">returned by BizStartGetState</param>
[DllImport(dll, CallingConvention = cc)]
public static extern void BizFinishGetState(IntPtr p, byte[] dest, int size);
[BizImport(cc, Compatibility = true)]
public abstract void BizFinishGetState(IntPtr p, byte[] dest, int size);
[DllImport(dll, CallingConvention = cc)]
public static extern bool BizPutState(IntPtr ctx, byte[] src, int size);
[BizImport(cc, Compatibility = true)]
public abstract bool BizPutState(IntPtr ctx, byte[] src, int size);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizSetLayerMask(IntPtr ctx, Layers mask);
[BizImport(cc, Compatibility = true)]
public abstract void BizSetLayerMask(IntPtr ctx, Layers mask);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizSetSoundMask(IntPtr ctx, Sounds mask);
[BizImport(cc, Compatibility = true)]
public abstract void BizSetSoundMask(IntPtr ctx, Sounds mask);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizGetRegisters(IntPtr ctx, int[] dest);
[BizImport(cc, Compatibility = true)]
public abstract void BizGetRegisters(IntPtr ctx, int[] dest);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizSetRegister(IntPtr ctx, int index, int value);
[BizImport(cc, Compatibility = true)]
public abstract void BizSetRegister(IntPtr ctx, int index, int value);
[DllImport(dll, CallingConvention = cc)]
public static extern ulong BizGetGlobalTime(IntPtr ctx);
[BizImport(cc, Compatibility = true)]
public abstract ulong BizGetGlobalTime(IntPtr ctx);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizWriteBus(IntPtr ctx, uint addr, byte val);
[BizImport(cc, Compatibility = true)]
public abstract void BizWriteBus(IntPtr ctx, uint addr, byte val);
[DllImport(dll, CallingConvention = cc)]
public static extern byte BizReadBus(IntPtr ctx, uint addr);
[BizImport(cc, Compatibility = true)]
public abstract byte BizReadBus(IntPtr ctx, uint addr);
[UnmanagedFunctionPointer(cc)]
public delegate void TraceCallback(string msg);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizSetTraceCallback(TraceCallback cb);
[BizImport(cc, Compatibility = true)]
public abstract void BizSetTraceCallback(TraceCallback cb);
[UnmanagedFunctionPointer(cc)]
public delegate void MemCallback(uint addr, mWatchpointType type, uint oldValue, uint newValue);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizSetMemCallback(MemCallback cb);
[BizImport(cc, Compatibility = true)]
public abstract void BizSetMemCallback(MemCallback cb);
[UnmanagedFunctionPointer(cc)]
public delegate void ExecCallback(uint pc);
[DllImport(dll, CallingConvention = cc)]
public static extern void BizSetExecCallback(ExecCallback cb);
[BizImport(cc, Compatibility = true)]
public abstract void BizSetExecCallback(ExecCallback cb);
[DllImport(dll, CallingConvention = cc)]
public static extern int BizSetWatchpoint(IntPtr ctx, uint addr, mWatchpointType type);
[BizImport(cc, Compatibility = true)]
public abstract int BizSetWatchpoint(IntPtr ctx, uint addr, mWatchpointType type);
[DllImport(dll, CallingConvention = cc)]
public static extern bool BizClearWatchpoint(IntPtr ctx, int id);
[BizImport(cc, Compatibility = true)]
public abstract bool BizClearWatchpoint(IntPtr ctx, int id);
}
}

View File

@ -1,6 +1,6 @@
using System;
using System.Text;
using BizHawk.BizInvoke;
using BizHawk.Common;
using BizHawk.Emulation.Common;
@ -12,6 +12,16 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
ISaveRam, IStatable, IInputPollable, ISettable<MGBAHawk.Settings, MGBAHawk.SyncSettings>,
IDebuggable
{
private static readonly LibmGBA LibmGBA;
public static LibmGBA ZZHacky => LibmGBA;
static MGBAHawk()
{
var resolver = new DynamicLibraryImportResolver(
OSTailoredCode.IsUnixHost ? "libmgba.dll.so" : "mgba.dll", eternal: true);
LibmGBA = BizInvoker.GetInvoker<LibmGBA>(resolver, CallingConventionAdapters.Native);
}
[CoreConstructor("GBA")]
public MGBAHawk(byte[] file, CoreComm comm, SyncSettings syncSettings, Settings settings, bool deterministic, GameInfo game)
{

View File

@ -56,12 +56,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
{
_executeCallback = RunExecCallback;
_execPcs[callback.Address.Value] = callback;
LibmGBA.BizSetExecCallback(_executeCallback);
MGBAHawk.ZZHacky.BizSetExecCallback(_executeCallback);
}
else
{
LibmGBA.BizSetMemCallback(container.CallDelegate);
container.ID = LibmGBA.BizSetWatchpoint(_mgba.Core, callback.Address.Value, container.WatchPointType);
MGBAHawk.ZZHacky.BizSetMemCallback(container.CallDelegate);
container.ID = MGBAHawk.ZZHacky.BizSetWatchpoint(_mgba.Core, callback.Address.Value, container.WatchPointType);
}
_callbacks.Add(container);
@ -79,10 +79,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
if (_callbacks.All(cb => cb.Callback.Type != MemoryCallbackType.Execute))
{
_executeCallback = null;
LibmGBA.BizSetExecCallback(null);
MGBAHawk.ZZHacky.BizSetExecCallback(null);
}
}
else if (LibmGBA.BizClearWatchpoint(_mgba.Core, cbToRemove.ID))
else if (MGBAHawk.ZZHacky.BizClearWatchpoint(_mgba.Core, cbToRemove.ID))
{
_callbacks.Remove(cbToRemove);
}
@ -90,21 +90,21 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
}
public void RemoveAll(IEnumerable<MemoryCallbackDelegate> actions)
{
foreach (var action in actions)
{
Remove(action);
}
{
foreach (var action in actions)
{
Remove(action);
}
}
public void Clear()
{
foreach (var cb in _callbacks)
{
if (LibmGBA.BizClearWatchpoint(_mgba.Core, cb.ID))
{
_callbacks.Remove(cb);
}
foreach (var cb in _callbacks)
{
if (MGBAHawk.ZZHacky.BizClearWatchpoint(_mgba.Core, cb.ID))
{
_callbacks.Remove(cb);
}
}
}