diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi.cs index 90e941f3d3..0efbb9286a 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi.cs @@ -5,51 +5,60 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using BizHawk.Common; +using BizHawk.Emulation.Cores.Waterbox; +using BizHawk.Common.BizInvoke; namespace BizHawk.Emulation.Cores.Nintendo.SNES { public unsafe partial class LibsnesApi : IDisposable { - InstanceDll instanceDll; - string InstanceName; + /*[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + public static unsafe extern void* CopyMemory(void* dest, void* src, ulong count);*/ - [DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] - public static unsafe extern void* CopyMemory(void* dest, void* src, ulong count); + private PeRunner _exe; + private CoreImpl _core; + private bool _disposed; + private CommStruct* _comm; - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - delegate IntPtr DllInit(); + private abstract class CoreImpl + { + [BizImport(CallingConvention.Cdecl, Compatibility = true)] + public abstract IntPtr DllInit(); + [BizImport(CallingConvention.Cdecl, Compatibility = true)] + public abstract void Message(eMessage msg); + [BizImport(CallingConvention.Cdecl, Compatibility = true)] + public abstract void CopyBuffer(int id, void* ptr, int size); + [BizImport(CallingConvention.Cdecl, Compatibility = true)] + public abstract void SetBuffer(int id, void* ptr, int size); + } - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - delegate void MessageApi(eMessage msg); - - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - delegate void BufferApi(int id, void* ptr, int size); - - CommStruct* comm; - MessageApi Message; - BufferApi _copyBuffer; //TODO: consider making private and wrapping - BufferApi _setBuffer; //TODO: consider making private and wrapping public LibsnesApi(string dllPath) { - InstanceName = "libsneshawk_" + Guid.NewGuid().ToString(); - instanceDll = new InstanceDll(dllPath); - var dllinit = (DllInit)Marshal.GetDelegateForFunctionPointer(instanceDll.GetProcAddress("DllInit"), typeof(DllInit)); - Message = (MessageApi)Marshal.GetDelegateForFunctionPointer(instanceDll.GetProcAddress("Message"), typeof(MessageApi)); - _copyBuffer = (BufferApi)Marshal.GetDelegateForFunctionPointer(instanceDll.GetProcAddress("CopyBuffer"), typeof(BufferApi)); - _setBuffer = (BufferApi)Marshal.GetDelegateForFunctionPointer(instanceDll.GetProcAddress("SetBuffer"), typeof(BufferApi)); + _exe = new PeRunner(new PeRunnerOptions + { + Filename = "libsnes.wbx", + Path = dllPath, + SbrkHeapSizeKB = 32 * 1024, + InvisibleHeapSizeKB = 32 * 1024, + MmapHeapSizeKB = 32 * 1024, + PlainHeapSizeKB = 32 * 1024, + SealedHeapSizeKB = 32 * 1024 + }); - comm = (CommStruct*)dllinit().ToPointer(); + _core = BizInvoker.GetInvoker(_exe, _exe); + _comm = (CommStruct*)_core.DllInit().ToPointer(); } public void Dispose() { - instanceDll.Dispose(); - - foreach (var smb in DeallocatedMemoryBlocks.Values) smb.Dispose(); - foreach (var smb in SharedMemoryBlocks.Values) smb.Dispose(); - SharedMemoryBlocks.Clear(); - DeallocatedMemoryBlocks.Clear(); + if (!_disposed) + { + _exe.Dispose(); + _exe = null; + _core = null; + _comm = null; + } } /// @@ -57,8 +66,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES /// public void CopyAscii(int id, string str) { - fixed (byte* cp = System.Text.Encoding.ASCII.GetBytes(str+"\0")) - _copyBuffer(id, cp, str.Length + 1); + fixed (byte* cp = System.Text.Encoding.ASCII.GetBytes(str + "\0")) + { + _core.CopyBuffer(id, cp, str.Length + 1); + } } /// @@ -67,7 +78,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES public void CopyBytes(int id, byte[] bytes) { fixed (byte* bp = bytes) - _copyBuffer(id, bp, bytes.Length); + { + _core.CopyBuffer(id, bp, bytes.Length); + } } /// @@ -81,7 +94,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { fixed (byte* bp = bytes) { - _setBuffer(id, bp, bytes.Length); + _core.SetBuffer(id, bp, bytes.Length); andThen(); } } @@ -91,9 +104,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES /// public void SetAscii(int id, string str, Action andThen) { - fixed (byte* cp = System.Text.Encoding.ASCII.GetBytes(str+"\0")) + fixed (byte* cp = System.Text.Encoding.ASCII.GetBytes(str + "\0")) { - _setBuffer(id, cp, str.Length + 1); + _core.SetBuffer(id, cp, str.Length + 1); andThen(); } } @@ -124,8 +137,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES BRR = 0x80, }; - Dictionary SharedMemoryBlocks = new Dictionary(); - Dictionary DeallocatedMemoryBlocks = new Dictionary(); + //Dictionary SharedMemoryBlocks = new Dictionary(); + //Dictionary DeallocatedMemoryBlocks = new Dictionary(); snes_video_refresh_t video_refresh; snes_input_poll_t input_poll; @@ -177,7 +190,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES public bool BG3_Prio1 { get { return _BG3_Prio1 != 0; } set { _BG3_Prio1 = (byte)(value ? 1 : 0); } } public bool BG4_Prio0 { get { return _BG4_Prio0 != 0; } set { _BG4_Prio0 = (byte)(value ? 1 : 0); } } public bool BG4_Prio1 { get { return _BG4_Prio1 != 0; } set { _BG4_Prio1 = (byte)(value ? 1 : 0); } } - + public bool Obj_Prio0 { get { return _Obj_Prio0 != 0; } set { _Obj_Prio0 = (byte)(value ? 1 : 0); } } public bool Obj_Prio1 { get { return _Obj_Prio1 != 0; } set { _Obj_Prio1 = (byte)(value ? 1 : 0); } } public bool Obj_Prio2 { get { return _Obj_Prio2 != 0; } set { _Obj_Prio2 = (byte)(value ? 1 : 0); } } @@ -226,27 +239,52 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES public unsafe string GetAscii() { return _getAscii(str); } public bool GetBool() { return value != 0; } - private unsafe string _getAscii(sbyte* ptr) { + private unsafe string _getAscii(sbyte* ptr) + { int len = 0; sbyte* junko = (sbyte*)ptr; - while(junko[len] != 0) len++; + while (junko[len] != 0) len++; return new string((sbyte*)str, 0, len, System.Text.Encoding.ASCII); } } - public SNES_REGION Region { get { return comm->region; } } - public SNES_MAPPER Mapper { get { return comm->mapper; } } + public SNES_REGION Region + { + get + { + using (_exe.EnterExit()) + { + return _comm->region; + } + } + } + public SNES_MAPPER Mapper + { + get + { + using (_exe.EnterExit()) + { + return _comm->mapper; + } + } + } + public void SetLayerEnables(ref LayerEnables enables) { - comm->layerEnables = enables; - QUERY_set_layer_enable(); + using (_exe.EnterExit()) + { + _comm->layerEnables = enables; + QUERY_set_layer_enable(); + } } public void SetInputPortBeforeInit(int port, SNES_INPUT_PORT type) { - comm->inports[port] = (int)type; + using (_exe.EnterExit()) + { + _comm->inports[port] = (int)type; + } } } - } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_BRK.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_BRK.cs index 6ac0ca9ae6..ea281c0586 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_BRK.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_BRK.cs @@ -8,42 +8,38 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { bool Handle_BRK(eMessage msg) { - switch (msg) + using (_exe.EnterExit()) { - default: - return false; + switch (msg) + { + default: + return false; - case eMessage.eMessage_BRK_hook_exec: - { - ExecHook(comm->addr); + case eMessage.eMessage_BRK_hook_exec: + ExecHook(_comm->addr); break; - } - case eMessage.eMessage_BRK_hook_read: - { - ReadHook(comm->addr); + case eMessage.eMessage_BRK_hook_read: + ReadHook(_comm->addr); break; - } - case eMessage.eMessage_BRK_hook_write: - { - WriteHook(comm->addr, (byte)comm->value); + case eMessage.eMessage_BRK_hook_write: + WriteHook(_comm->addr, (byte)_comm->value); break; - } - //not supported yet - case eMessage.eMessage_BRK_hook_nmi: - break; - case eMessage.eMessage_BRK_hook_irq: - break; + //not supported yet + case eMessage.eMessage_BRK_hook_nmi: + break; + case eMessage.eMessage_BRK_hook_irq: + break; - case eMessage.eMessage_BRK_scanlineStart: - if (scanlineStart != null) - scanlineStart(comm->scanline); - break; + case eMessage.eMessage_BRK_scanlineStart: + scanlineStart?.Invoke(_comm->scanline); + break; - } //switch(msg) + } //switch(msg) - Message(eMessage.eMessage_Resume); - return true; + _core.Message(eMessage.eMessage_Resume); + return true; + } } } -} \ No newline at end of file +} diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_CMD.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_CMD.cs index 076a4752c5..37a35f468d 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_CMD.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_CMD.cs @@ -6,98 +6,86 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { unsafe partial class LibsnesApi { - public bool CMD_serialize(IntPtr data, int size) - { - comm->buf[0] = (uint)data.ToInt32(); - comm->buf_size[0] = size; - Message(eMessage.eMessage_CMD_serialize); - WaitForCMD(); - bool ret = comm->GetBool(); - return ret; - } - void WaitForCMD() { - for (; ; ) + using (_exe.EnterExit()) { - if (comm->status == eStatus.eStatus_Idle) - break; - if (Handle_SIG(comm->reason)) continue; - if (Handle_BRK(comm->reason)) continue; + for (;;) + { + if (_comm->status == eStatus.eStatus_Idle) + break; + if (Handle_SIG(_comm->reason)) continue; + if (Handle_BRK(_comm->reason)) continue; + } } } - public bool CMD_unserialize(IntPtr data, int size) - { - comm->buf[0] = (uint)data.ToInt32(); - comm->buf_size[0] = size; - Message(eMessage.eMessage_CMD_unserialize); - WaitForCMD(); - bool ret = comm->GetBool(); - return ret; - } - public void CMD_init() { - Message(eMessage.eMessage_CMD_init); + _core.Message(eMessage.eMessage_CMD_init); WaitForCMD(); } public void CMD_power() { - Message(eMessage.eMessage_CMD_power); + _core.Message(eMessage.eMessage_CMD_power); WaitForCMD(); } public void CMD_reset() { - Message(eMessage.eMessage_CMD_reset); + _core.Message(eMessage.eMessage_CMD_reset); WaitForCMD(); } public void CMD_run() { - Message(eMessage.eMessage_CMD_run); + _core.Message(eMessage.eMessage_CMD_run); WaitForCMD(); } public bool CMD_load_cartridge_super_game_boy(string rom_xml, byte[] rom_data, uint rom_size, byte[] dmg_data) { - SetAscii(0, rom_xml ?? "", () => - SetBytes(1, rom_data, () => - SetBytes(2, dmg_data, () => - { - Message(eMessage.eMessage_CMD_load_cartridge_sgb); - WaitForCMD(); - }) - ) - ); - return comm->GetBool(); + using (_exe.EnterExit()) + { + SetAscii(0, rom_xml ?? "", () => + SetBytes(1, rom_data, () => + SetBytes(2, dmg_data, () => + { + _core.Message(eMessage.eMessage_CMD_load_cartridge_sgb); + WaitForCMD(); + }) + ) + ); + return _comm->GetBool(); + } } public bool CMD_load_cartridge_normal(byte[] rom_xml, byte[] rom_data) { - //why don't we need this for the other loads? I dont know, our XML handling is really confusing - string xml = rom_xml == null ? null : System.Text.Encoding.ASCII.GetString(rom_xml); + using (_exe.EnterExit()) + { + //why don't we need this for the other loads? I dont know, our XML handling is really confusing + string xml = rom_xml == null ? null : System.Text.Encoding.ASCII.GetString(rom_xml); - SetAscii(0, xml ?? "", () => - SetBytes(1, rom_data, () => - { - Message(eMessage.eMessage_CMD_load_cartridge_normal); - WaitForCMD(); - }) - ); - return comm->GetBool(); + SetAscii(0, xml ?? "", () => + SetBytes(1, rom_data, () => + { + _core.Message(eMessage.eMessage_CMD_load_cartridge_normal); + WaitForCMD(); + }) + ); + return _comm->GetBool(); + } } public void CMD_term() { - Message(eMessage.eMessage_CMD_term); + _core.Message(eMessage.eMessage_CMD_term); WaitForCMD(); } public void CMD_unload_cartridge() { - Message(eMessage.eMessage_CMD_unload_cartridge); + _core.Message(eMessage.eMessage_CMD_unload_cartridge); WaitForCMD(); } - } } \ No newline at end of file diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_QUERY.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_QUERY.cs index 2917493a1c..7fb7d1296c 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_QUERY.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesApi_QUERY.cs @@ -10,16 +10,22 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { public int QUERY_get_memory_size(SNES_MEMORY id) { - comm->value = (uint)id; - Message(eMessage.eMessage_QUERY_get_memory_size); - return (int)comm->value; + using (_exe.EnterExit()) + { + _comm->value = (uint)id; + _core.Message(eMessage.eMessage_QUERY_get_memory_size); + return (int)_comm->value; + } } string QUERY_MemoryNameForId(SNES_MEMORY id) { - comm->id = (uint)id; - Message(eMessage.eMessage_QUERY_GetMemoryIdName); - return comm->GetAscii(); + using (_exe.EnterExit()) + { + _comm->id = (uint)id; + _core.Message(eMessage.eMessage_QUERY_GetMemoryIdName); + return _comm->GetAscii(); + } } public byte* QUERY_get_memory_data(SNES_MEMORY id) @@ -32,147 +38,169 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES public byte QUERY_peek(SNES_MEMORY id, uint addr) { - comm->id = (uint)id; - comm->addr = addr; - Message(eMessage.eMessage_QUERY_peek); - return (byte)comm->value; + using (_exe.EnterExit()) + { + _comm->id = (uint)id; + _comm->addr = addr; + _core.Message(eMessage.eMessage_QUERY_peek); + return (byte)_comm->value; + } } public void QUERY_poke(SNES_MEMORY id, uint addr, byte val) { - comm->id = (uint)id; - comm->addr = addr; - comm->value = (byte)val; - Message(eMessage.eMessage_QUERY_poke); - } - - public int QUERY_serialize_size() - { - for (; ; ) + using (_exe.EnterExit()) { - Message(eMessage.eMessage_QUERY_serialize_size); - int ret = (int)comm->size; - if (ret > 100) - { - return ret; - } - else Console.WriteLine("WHY????????"); + _comm->id = (uint)id; + _comm->addr = addr; + _comm->value = (byte)val; + _core.Message(eMessage.eMessage_QUERY_poke); } } - - public void QUERY_set_color_lut(IntPtr colors) { - comm->ptr = colors.ToPointer(); - Message(eMessage.eMessage_QUERY_set_color_lut); + using (_exe.EnterExit()) + { + _comm->ptr = colors.ToPointer(); + _core.Message(eMessage.eMessage_QUERY_set_color_lut); + } } public void QUERY_set_state_hook_exec(bool state) { - comm->value = state ? 1u : 0u; - Message(eMessage.eMessage_QUERY_state_hook_exec); + using (_exe.EnterExit()) + { + _comm->value = state ? 1u : 0u; + _core.Message(eMessage.eMessage_QUERY_state_hook_exec); + } } public void QUERY_set_state_hook_read(bool state) { - comm->value = state ? 1u : 0u; - Message(eMessage.eMessage_QUERY_state_hook_read); + using (_exe.EnterExit()) + { + _comm->value = state ? 1u : 0u; + _core.Message(eMessage.eMessage_QUERY_state_hook_read); + } } public void QUERY_set_state_hook_write(bool state) { - comm->value = state ? 1u : 0u; - Message(eMessage.eMessage_QUERY_state_hook_write); + using (_exe.EnterExit()) + { + _comm->value = state ? 1u : 0u; + _core.Message(eMessage.eMessage_QUERY_state_hook_write); + } } public void QUERY_set_trace_callback(int mask, snes_trace_t callback) { - this.traceCallback = callback; - comm->value = (uint)mask; - Message(eMessage.eMessage_QUERY_enable_trace); + using (_exe.EnterExit()) + { + this.traceCallback = callback; + _comm->value = (uint)mask; + _core.Message(eMessage.eMessage_QUERY_enable_trace); + } } public void QUERY_set_scanlineStart(snes_scanlineStart_t scanlineStart) { - this.scanlineStart = scanlineStart; - comm->value = (scanlineStart != null) ? 1u : 0u; - Message(eMessage.eMessage_QUERY_enable_scanline); + using (_exe.EnterExit()) + { + this.scanlineStart = scanlineStart; + _comm->value = (scanlineStart != null) ? 1u : 0u; + _core.Message(eMessage.eMessage_QUERY_enable_scanline); + } } public void QUERY_set_audio_sample(snes_audio_sample_t audio_sample) { - this.audio_sample = audio_sample; - comm->value = (audio_sample!=null) ? 1u : 0u; - Message(eMessage.eMessage_QUERY_enable_audio); + using (_exe.EnterExit()) + { + this.audio_sample = audio_sample; + _comm->value = (audio_sample != null) ? 1u : 0u; + _core.Message(eMessage.eMessage_QUERY_enable_audio); + } } public void QUERY_set_layer_enable() { - Message(eMessage.eMessage_QUERY_set_layer_enable); + _core.Message(eMessage.eMessage_QUERY_set_layer_enable); } public void QUERY_set_backdropColor(int backdropColor) { - comm->value = (uint)backdropColor; - Message(eMessage.eMessage_QUERY_set_backdropColor); + using (_exe.EnterExit()) + { + _comm->value = (uint)backdropColor; + _core.Message(eMessage.eMessage_QUERY_set_backdropColor); + } } - + public int QUERY_peek_logical_register(SNES_REG reg) { - comm->id = (uint)reg; - Message(eMessage.eMessage_QUERY_peek_logical_register); - return (int)comm->value; + using (_exe.EnterExit()) + { + _comm->id = (uint)reg; + _core.Message(eMessage.eMessage_QUERY_peek_logical_register); + return (int)_comm->value; + } } public unsafe void QUERY_peek_cpu_regs(out CPURegs ret) { - Message(eMessage.eMessage_QUERY_peek_cpu_regs); - ret = comm->cpuregs; + using (_exe.EnterExit()) + { + _core.Message(eMessage.eMessage_QUERY_peek_cpu_regs); + ret = _comm->cpuregs; + } } public void QUERY_set_cdl(ICodeDataLog cdl) { - for (int i = 0; i < 8; i++) + using (_exe.EnterExit()) { - comm->cdl_ptr[i] = 0; - comm->cdl_size[i] = 0; - } - - if (cdl != null) - { - comm->cdl_ptr[0] = cdl.GetPin("CARTROM").ToInt64(); - comm->cdl_size[0] = cdl["CARTROM"].Length; - if (cdl.Has("CARTRAM")) + for (int i = 0; i < 8; i++) { - comm->cdl_ptr[1] = cdl.GetPin("CARTRAM").ToInt64(); - comm->cdl_size[1] = cdl["CARTRAM"].Length; + _comm->cdl_ptr[i] = 0; + _comm->cdl_size[i] = 0; } - comm->cdl_ptr[2] = cdl.GetPin("WRAM").ToInt64(); - comm->cdl_size[2] = cdl["WRAM"].Length; - - comm->cdl_ptr[3] = cdl.GetPin("APURAM").ToInt64(); - comm->cdl_size[3] = cdl["APURAM"].Length; - - if (cdl.Has("SGB_CARTROM")) + if (cdl != null) { - comm->cdl_ptr[4] = cdl.GetPin("SGB_CARTROM").ToInt64(); - comm->cdl_size[4] = cdl["SGB_CARTROM"].Length; - - if (cdl.Has("SGB_CARTRAM")) + _comm->cdl_ptr[0] = cdl.GetPin("CARTROM").ToInt64(); + _comm->cdl_size[0] = cdl["CARTROM"].Length; + if (cdl.Has("CARTRAM")) { - comm->cdl_ptr[5] = cdl.GetPin("SGB_CARTRAM").ToInt64(); - comm->cdl_size[5] = cdl["SGB_CARTRAM"].Length; + _comm->cdl_ptr[1] = cdl.GetPin("CARTRAM").ToInt64(); + _comm->cdl_size[1] = cdl["CARTRAM"].Length; } - comm->cdl_ptr[6] = cdl.GetPin("SGB_WRAM").ToInt64(); - comm->cdl_size[6] = cdl["SGB_WRAM"].Length; + _comm->cdl_ptr[2] = cdl.GetPin("WRAM").ToInt64(); + _comm->cdl_size[2] = cdl["WRAM"].Length; - comm->cdl_ptr[7] = cdl.GetPin("SGB_HRAM").ToInt64(); - comm->cdl_size[7] = cdl["SGB_HRAM"].Length; + _comm->cdl_ptr[3] = cdl.GetPin("APURAM").ToInt64(); + _comm->cdl_size[3] = cdl["APURAM"].Length; + + if (cdl.Has("SGB_CARTROM")) + { + _comm->cdl_ptr[4] = cdl.GetPin("SGB_CARTROM").ToInt64(); + _comm->cdl_size[4] = cdl["SGB_CARTROM"].Length; + + if (cdl.Has("SGB_CARTRAM")) + { + _comm->cdl_ptr[5] = cdl.GetPin("SGB_CARTRAM").ToInt64(); + _comm->cdl_size[5] = cdl["SGB_CARTRAM"].Length; + } + + _comm->cdl_ptr[6] = cdl.GetPin("SGB_WRAM").ToInt64(); + _comm->cdl_size[6] = cdl["SGB_WRAM"].Length; + + _comm->cdl_ptr[7] = cdl.GetPin("SGB_HRAM").ToInt64(); + _comm->cdl_size[7] = cdl["SGB_HRAM"].Length; + } } - } - Message(eMessage.eMessage_QUERY_set_cdl); + _core.Message(eMessage.eMessage_QUERY_set_cdl); + } } - } } diff --git a/waterbox/libsnes/.vscode/settings.json b/waterbox/libsnes/.vscode/settings.json new file mode 100644 index 0000000000..320669d7d4 --- /dev/null +++ b/waterbox/libsnes/.vscode/settings.json @@ -0,0 +1,42 @@ +{ + "files.associations": { + "ostream": "cpp", + "algorithm": "cpp", + "cmath": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "cwchar": "cpp", + "deque": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "memory": "cpp", + "new": "cpp", + "queue": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xlocnum": "cpp", + "xmemory": "cpp", + "xmemory0": "cpp", + "xstddef": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xutility": "cpp" + } +} \ No newline at end of file diff --git a/waterbox/libsnes/Makefile b/waterbox/libsnes/Makefile index 7958a74f03..e70c77c5cd 100644 --- a/waterbox/libsnes/Makefile +++ b/waterbox/libsnes/Makefile @@ -3,11 +3,15 @@ CC = x86_64-nt64-midipix-g++ # -Werror=pointer-to-int-cast -Werror=int-to-pointer-cast #-std=c99 -fomit-frame-pointer -fvisibility=hidden #-DPROFILE_PERFORMANCE + #-fno-exceptions -fno-rtti CCFLAGS:= -DHOOKS -DBIZHAWK -DPROFILE_COMPATIBILITY -DGAMEBOY \ + -D_GNU_SOURCE \ -Werror=pointer-to-int-cast -Werror=int-to-pointer-cast \ - -I../emulibc -I../libco \ + -I../emulibc -I../libco -I./bsnes \ -Wall -Werror=implicit-function-declaration \ - -fno-exceptions -fno-rtti -fvisibility=hidden \ + -Wno-parentheses -Wno-sign-compare \ + -Wno-unused-variable -Wno-unused-function \ + -fvisibility=hidden \ -std=c++0x \ -O0 -g @@ -16,16 +20,22 @@ TARGET = libsnes.wbx LDFLAGS = -Wl,--dynamicbase,--export-all-symbols ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) -SRCS:= \ -# $(ROOT_DIR)/bsnes/snes/alt/cpu/cpu.cpp \ #perf only -# $(ROOT_DIR)/bsnes/snes/alt/ppu-performance/ppu.cpp \ #perf only -# $(ROOT_DIR)/bsnes/snes/alt/smp/smp.cpp \ # perf only - $(ROOT_DIR)/bsnes/snes/alt/ppu-compatibility/ppu.cpp \ #compat only + +SRCS_PERF:= \ + $(ROOT_DIR)/bsnes/snes/alt/cpu/cpu.cpp \ + $(ROOT_DIR)/bsnes/snes/alt/ppu-performance/ppu.cpp \ + $(ROOT_DIR)/bsnes/snes/alt/smp/smp.cpp +SRCS_COMPAT:= \ + $(ROOT_DIR)/bsnes/snes/alt/ppu-compatibility/ppu.cpp \ + $(ROOT_DIR)/bsnes/snes/cpu/cpu.cpp \ + $(ROOT_DIR)/bsnes/snes/smp/smp.cpp +SRCS_ALL:= \ $(ROOT_DIR)/bsnes/base/base.cpp \ $(ROOT_DIR)/bsnes/gameboy/apu/apu.cpp \ $(ROOT_DIR)/bsnes/gameboy/cartridge/cartridge.cpp \ $(ROOT_DIR)/bsnes/gameboy/cheat/cheat.cpp \ $(ROOT_DIR)/bsnes/gameboy/cpu/cpu.cpp \ + $(ROOT_DIR)/bsnes/snes/alt/dsp/dsp.cpp \ $(ROOT_DIR)/bsnes/gameboy/interface/interface.cpp \ $(ROOT_DIR)/bsnes/gameboy/lcd/lcd.cpp \ $(ROOT_DIR)/bsnes/gameboy/memory/memory.cpp \ @@ -58,6 +68,7 @@ SRCS:= \ $(ROOT_DIR)/bsnes/snes/system/system.cpp \ $(ROOT_DIR)/bsnes/target-libsnes/libsnes.cpp \ $(ROOT_DIR)/bsnes/target-libsnes/libsnes_pwrap.cpp +SRCS:=$(SRCS_ALL) $(SRCS_COMPAT) OBJ_DIR:=$(ROOT_DIR)/obj diff --git a/waterbox/libsnes/bsnes/gameboy/gameboy.hpp b/waterbox/libsnes/bsnes/gameboy/gameboy.hpp index 142fa036ac..6397a72e2f 100644 --- a/waterbox/libsnes/bsnes/gameboy/gameboy.hpp +++ b/waterbox/libsnes/bsnes/gameboy/gameboy.hpp @@ -17,7 +17,7 @@ namespace GameBoy { project started: 2010-12-27 */ -#include +#include #include namespace GameBoy { diff --git a/waterbox/libsnes/bsnes/nall/dsp/resample/average.hpp b/waterbox/libsnes/bsnes/nall/dsp/resample/average.hpp index 68f24f9d86..f584e3731f 100644 --- a/waterbox/libsnes/bsnes/nall/dsp/resample/average.hpp +++ b/waterbox/libsnes/bsnes/nall/dsp/resample/average.hpp @@ -50,7 +50,7 @@ void ResampleAverage::sample() { void ResampleAverage::sampleLinear() { while(fraction <= 1.0) { - real *channel = (real*)alloca(dsp.settings.channels * sizeof(real)); + std::vector channel(dsp.settings.channels); for(unsigned n = 0; n < dsp.settings.channels; n++) { real a = dsp.buffer.read(n, -1); @@ -61,7 +61,7 @@ void ResampleAverage::sampleLinear() { channel[n] = a * (1.0 - mu) + b * mu; } - dsp.write(channel); + dsp.write(channel.data()); fraction += step; } diff --git a/waterbox/libsnes/bsnes/nall/dsp/resample/cosine.hpp b/waterbox/libsnes/bsnes/nall/dsp/resample/cosine.hpp index 7dcb4140c7..6120a734b3 100644 --- a/waterbox/libsnes/bsnes/nall/dsp/resample/cosine.hpp +++ b/waterbox/libsnes/bsnes/nall/dsp/resample/cosine.hpp @@ -21,7 +21,7 @@ void ResampleCosine::clear() { void ResampleCosine::sample() { while(fraction <= 1.0) { - real *channel = (real*)alloca(dsp.settings.channels * sizeof(real)); + std::vector channel(dsp.settings.channels); for(unsigned n = 0; n < dsp.settings.channels; n++) { @@ -34,7 +34,7 @@ void ResampleCosine::sample() { channel[n] = a * (1.0 - mu) + b * mu; } - dsp.write(channel); + dsp.write(channel.data()); fraction += step; } diff --git a/waterbox/libsnes/bsnes/nall/dsp/resample/cubic.hpp b/waterbox/libsnes/bsnes/nall/dsp/resample/cubic.hpp index affee3a1ee..f7f077961c 100644 --- a/waterbox/libsnes/bsnes/nall/dsp/resample/cubic.hpp +++ b/waterbox/libsnes/bsnes/nall/dsp/resample/cubic.hpp @@ -21,7 +21,7 @@ void ResampleCubic::clear() { void ResampleCubic::sample() { while(fraction <= 1.0) { - real *channel = (real*)alloca(dsp.settings.channels * sizeof(real)); + std::vector channel(dsp.settings.channels); for(unsigned n = 0; n < dsp.settings.channels; n++) { real a = dsp.buffer.read(n, -3); @@ -39,7 +39,7 @@ void ResampleCubic::sample() { channel[n] = A * (mu * 3) + B * (mu * 2) + C * mu + D; } - dsp.write(channel); + dsp.write(channel.data()); fraction += step; } diff --git a/waterbox/libsnes/bsnes/nall/dsp/resample/hermite.hpp b/waterbox/libsnes/bsnes/nall/dsp/resample/hermite.hpp index ffe86e42d8..04c57b85c9 100644 --- a/waterbox/libsnes/bsnes/nall/dsp/resample/hermite.hpp +++ b/waterbox/libsnes/bsnes/nall/dsp/resample/hermite.hpp @@ -21,7 +21,7 @@ void ResampleHermite::clear() { void ResampleHermite::sample() { while(fraction <= 1.0) { - real *channel = (real*)alloca(dsp.settings.channels * sizeof(real)); + std::vector channel(dsp.settings.channels); for(unsigned n = 0; n < dsp.settings.channels; n++) { real a = dsp.buffer.read(n, -3); @@ -51,7 +51,7 @@ void ResampleHermite::sample() { channel[n] = (a0 * b) + (a1 * m0) + (a2 * m1) + (a3 * c); } - dsp.write(channel); + dsp.write(channel.data()); fraction += step; } diff --git a/waterbox/libsnes/bsnes/nall/dsp/resample/linear.hpp b/waterbox/libsnes/bsnes/nall/dsp/resample/linear.hpp index 7845b73b7c..34e06ae440 100644 --- a/waterbox/libsnes/bsnes/nall/dsp/resample/linear.hpp +++ b/waterbox/libsnes/bsnes/nall/dsp/resample/linear.hpp @@ -21,7 +21,7 @@ void ResampleLinear::clear() { void ResampleLinear::sample() { while(fraction <= 1.0) { - real *channel = (real*)alloca(dsp.settings.channels * sizeof(real)); + std::vector channel(dsp.settings.channels); for(unsigned n = 0; n < dsp.settings.channels; n++) { real a = dsp.buffer.read(n, -1); @@ -32,7 +32,7 @@ void ResampleLinear::sample() { channel[n] = a * (1.0 - mu) + b * mu; } - dsp.write(channel); + dsp.write(channel.data()); fraction += step; } diff --git a/waterbox/libsnes/bsnes/nall/dsp/resample/nearest.hpp b/waterbox/libsnes/bsnes/nall/dsp/resample/nearest.hpp index e298d4174d..73df9f7d91 100644 --- a/waterbox/libsnes/bsnes/nall/dsp/resample/nearest.hpp +++ b/waterbox/libsnes/bsnes/nall/dsp/resample/nearest.hpp @@ -21,7 +21,7 @@ void ResampleNearest::clear() { void ResampleNearest::sample() { while(fraction <= 1.0) { - real *channel = (real*)alloca(dsp.settings.channels * sizeof(real)); + std::vector channel(dsp.settings.channels); for(unsigned n = 0; n < dsp.settings.channels; n++) { real a = dsp.buffer.read(n, -1); @@ -32,7 +32,7 @@ void ResampleNearest::sample() { channel[n] = mu < 0.5 ? a : b; } - dsp.write(channel); + dsp.write(channel.data()); fraction += step; } diff --git a/waterbox/libsnes/bsnes/nall/platform.hpp b/waterbox/libsnes/bsnes/nall/platform.hpp index b918bd3cb1..7c38cb4ab5 100644 --- a/waterbox/libsnes/bsnes/nall/platform.hpp +++ b/waterbox/libsnes/bsnes/nall/platform.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include diff --git a/waterbox/libsnes/bsnes/nall/string.hpp b/waterbox/libsnes/bsnes/nall/string.hpp index 1233dcfbbc..c29574552b 100644 --- a/waterbox/libsnes/bsnes/nall/string.hpp +++ b/waterbox/libsnes/bsnes/nall/string.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include diff --git a/waterbox/libsnes/bsnes/nall/string/utility.hpp b/waterbox/libsnes/bsnes/nall/string/utility.hpp index d78b90ce9f..945728ba23 100644 --- a/waterbox/libsnes/bsnes/nall/string/utility.hpp +++ b/waterbox/libsnes/bsnes/nall/string/utility.hpp @@ -166,15 +166,15 @@ template string decimal(uintmax_t value) { buffer[size] = 0; unsigned length = (length_ == 0 ? size : length_); - char* result = (char*)alloca(length + 1); - memset(result, padding, length); + std::vector result(length + 1); + memset(result.data(), padding, length); result[length] = 0; for(signed x = length - 1, y = 0; x >= 0 && y < size; x--, y++) { result[x] = buffer[y]; } - return (const char*)result; + return (const char*)result.data(); } template string ldecimal(uintmax_t value) { @@ -211,15 +211,15 @@ template string hex(uintmax_t value) { } while(value); unsigned length = (length_ == 0 ? size : length_); - char *result = (char*)alloca(length + 1); - memset(result, padding, length); + std::vector result(length + 1); + memset(result.data(), padding, length); result[length] = 0; for(signed x = length - 1, y = 0; x >= 0 && y < size; x--, y++) { result[x] = buffer[y]; } - return (const char*)result; + return (const char*)result.data(); } template string binary(uintmax_t value) { diff --git a/waterbox/libsnes/bsnes/snes/cartridge/markup.cpp b/waterbox/libsnes/bsnes/snes/cartridge/markup.cpp index a4ac63bb8c..e4cde6a3fa 100644 --- a/waterbox/libsnes/bsnes/snes/cartridge/markup.cpp +++ b/waterbox/libsnes/bsnes/snes/cartridge/markup.cpp @@ -259,11 +259,10 @@ void Cartridge::parse_markup_necdsp(XML::Node &root) { if(!sha256.empty()) { //XML file specified SHA256 sum for program. Verify file matches the hash. fp.seek(0); - //uint8_t data[filesize]; //test - uint8_t *data = (uint8_t*)alloca(filesize); - fp.read(data, filesize); + std::vector data(filesize); + fp.read(data.data(), filesize); - if(sha256 != nall::sha256(data, filesize)) { + if(sha256 != nall::sha256(data.data(), filesize)) { interface()->message({ "Warning: NEC DSP firmware ", firmware, " SHA256 sum is incorrect." }); } } diff --git a/waterbox/libsnes/bsnes/snes/controller/usart/usart.cpp b/waterbox/libsnes/bsnes/snes/controller/usart/usart.cpp index 617b2d696f..f7013e02ef 100644 --- a/waterbox/libsnes/bsnes/snes/controller/usart/usart.cpp +++ b/waterbox/libsnes/bsnes/snes/controller/usart/usart.cpp @@ -89,7 +89,7 @@ USART::USART(bool port) : Controller(port) { txdata = 0; string filename = interface()->path(Cartridge::Slot::Base, "usart.so"); - if(open_absolute(filename)) { + if(0 /*open_absolute(filename)*/) { init = sym("usart_init"); main = sym("usart_main"); if(init && main) create(Controller::Enter, 1000000); diff --git a/waterbox/libsnes/bsnes/snes/smp/core/memory.hpp b/waterbox/libsnes/bsnes/snes/smp/core/memory.hpp index 02813580c6..973a2d6840 100644 --- a/waterbox/libsnes/bsnes/snes/smp/core/memory.hpp +++ b/waterbox/libsnes/bsnes/snes/smp/core/memory.hpp @@ -24,6 +24,5 @@ alwaysinline void op_writedp(uint8 addr, uint8 data) { alwaysinline void op_next() { opcode = op_readpcfirst(); - uindex = -1; } diff --git a/waterbox/libsnes/bsnes/snes/snes.hpp b/waterbox/libsnes/bsnes/snes/snes.hpp index 29861846e9..1eb91d6eb1 100644 --- a/waterbox/libsnes/bsnes/snes/snes.hpp +++ b/waterbox/libsnes/bsnes/snes/snes.hpp @@ -17,7 +17,7 @@ namespace SNES { project started: 2004-10-14 */ -#include +#include #if defined(GAMEBOY) #include diff --git a/waterbox/libsnes/bsnes/target-libsnes/libsnes.cpp b/waterbox/libsnes/bsnes/target-libsnes/libsnes.cpp index cb7602cdec..61ca62e7b8 100644 --- a/waterbox/libsnes/bsnes/target-libsnes/libsnes.cpp +++ b/waterbox/libsnes/bsnes/target-libsnes/libsnes.cpp @@ -6,8 +6,6 @@ #include -#include - using namespace nall; struct Interface : public SNES::Interface { diff --git a/waterbox/libsnes/bsnes/target-libsnes/libsnes_pwrap.cpp b/waterbox/libsnes/bsnes/target-libsnes/libsnes_pwrap.cpp index b9f1a81b64..9250e1bee2 100644 --- a/waterbox/libsnes/bsnes/target-libsnes/libsnes_pwrap.cpp +++ b/waterbox/libsnes/bsnes/target-libsnes/libsnes_pwrap.cpp @@ -4,13 +4,11 @@ //sig: core->frontend: "core signal" a synchronous operation called from the emulation process which the frontend should handle immediately without issuing any calls into the core //brk: core->frontend: "core break" the emulation process has suspended. the frontend is free to do whatever it wishes. -#include - #define LIBSNES_IMPORT #include "snes/snes.hpp" #include "libsnes.hpp" -#include +#include #include #include @@ -570,12 +568,10 @@ void new_emuthread() //------------------------------------------------ //DLL INTERFACE -BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved) -{ - return TRUE; -} +#include +#define EXPORT extern "C" ECL_EXPORT -extern "C" dllexport void* __cdecl DllInit() +EXPORT void* DllInit() { memset(&comm,0,sizeof(comm)); @@ -586,7 +582,7 @@ extern "C" dllexport void* __cdecl DllInit() return &comm; } -extern "C" dllexport void __cdecl Message(eMessage msg) +EXPORT void Message(eMessage msg) { if (msg == eMessage_Resume) { @@ -625,13 +621,18 @@ extern "C" dllexport void __cdecl Message(eMessage msg) //receives the given buffer and COPIES it. use this for returning values from SIGs -extern "C" dllexport void __cdecl CopyBuffer(int id, void* ptr, int32 size) +EXPORT void CopyBuffer(int id, void* ptr, int32 size) { comm.CopyBuffer(id, ptr, size); } //receives the given buffer and STASHES IT. use this (carefully) for sending params for CMDs -extern "C" dllexport void __cdecl SetBuffer(int id, void* ptr, int32 size) +EXPORT void SetBuffer(int id, void* ptr, int32 size) { comm.SetBuffer(id, ptr, size); -} \ No newline at end of file +} + +int main() +{ + return 0; +}