scaffolding for framebuffer functions
This commit is contained in:
+81
-5
@@ -1,9 +1,75 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
internal static class Nova64Core
|
||||
internal static class Nova64
|
||||
{
|
||||
private const string LibName = "nova64_core";
|
||||
|
||||
/* ========================================================================== */
|
||||
/* Enums */
|
||||
/* ========================================================================== */
|
||||
|
||||
[Flags]
|
||||
public enum Nova64Transform : uint
|
||||
{
|
||||
None = 0,
|
||||
FlipH = 1 << 0,
|
||||
FlipV = 1 << 1,
|
||||
Rot90 = 1 << 2,
|
||||
Rot180 = 1 << 3,
|
||||
Rot270 = 1 << 4
|
||||
}
|
||||
|
||||
/* ========================================================================== */
|
||||
/* PPU Delegates */
|
||||
/* ========================================================================== */
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void PpuFillRectDelegate(IntPtr dst, uint dstStride, int x, int y, int w, int h, ushort color);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void PpuBlitDelegate(IntPtr src, uint srcStride, IntPtr dst, uint dstStride, int srcX, int srcY, int dstX, int dstY, int w, int h);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void PpuBlitColorKeyDelegate(IntPtr src, uint srcStride, IntPtr dst, uint dstStride, int srcX, int srcY, int dstX, int dstY, int w, int h, ushort keyColor);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void PpuBlendDelegate(IntPtr src, uint srcStride, IntPtr dst, uint dstStride, int srcX, int srcY, int dstX, int dstY, int w, int h, byte globalAlpha);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void PpuBlitTransformDelegate(IntPtr src, uint srcStride, IntPtr dst, uint dstStride, int srcX, int srcY, int dstX, int dstY, int w, int h, Nova64Transform transform);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void PpuBlitScaleDelegate(IntPtr src, uint srcStride, int srcW, int srcH, IntPtr dst, uint dstStride, int dstX, int dstY, int dstW, int dstH);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void PpuWaitIdleDelegate();
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public delegate bool PpuIsBusyDelegate();
|
||||
|
||||
/* ========================================================================== */
|
||||
/* PPU Interface Struct */
|
||||
/* ========================================================================== */
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Nova64PpuInterface
|
||||
{
|
||||
public IntPtr fill_rect;
|
||||
public IntPtr blit;
|
||||
public IntPtr blit_colorkey;
|
||||
public IntPtr blend;
|
||||
public IntPtr blit_transform;
|
||||
public IntPtr blit_scale;
|
||||
public IntPtr wait_idle;
|
||||
public IntPtr is_busy;
|
||||
}
|
||||
|
||||
/* ========================================================================== */
|
||||
/* I/O Delegates */
|
||||
/* ========================================================================== */
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void DisplayFlushDelegate(IntPtr buffer, ushort width, ushort height);
|
||||
|
||||
@@ -17,15 +83,18 @@ internal static class Nova64Core
|
||||
public delegate int SdGetFileSizeDelegate(uint slotIdx, string filePath);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
public delegate void CartridgeStatusDelegate(uint slotIdx, bool isInserted);
|
||||
public delegate void CartridgeStatusDelegate(uint slotIdx, [MarshalAs(UnmanagedType.I1)] bool isInserted);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
|
||||
public delegate void LogMessageDelegate([MarshalAs(UnmanagedType.LPStr)] string message);
|
||||
|
||||
/* ========================================================================== */
|
||||
/* I/O Interface Struct */
|
||||
/* ========================================================================== */
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Nova64IoInterface
|
||||
{
|
||||
public IntPtr display_flush;
|
||||
public IntPtr audio_output;
|
||||
public IntPtr sd_read_file;
|
||||
public IntPtr sd_get_file_size;
|
||||
@@ -33,18 +102,25 @@ internal static class Nova64Core
|
||||
public IntPtr log_message;
|
||||
}
|
||||
|
||||
/* ========================================================================== */
|
||||
/* Native Imports */
|
||||
/* ========================================================================== */
|
||||
|
||||
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern bool nova64_init(IntPtr io_interface);
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool nova64_init(IntPtr io_interface, IntPtr ppu_interface);
|
||||
|
||||
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern int nova64_main();
|
||||
|
||||
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool nova64_shutdown();
|
||||
|
||||
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
|
||||
[return: MarshalAs(UnmanagedType.I1)]
|
||||
public static extern bool nova64_is_finalized();
|
||||
|
||||
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern uint nova64_get_version();
|
||||
}
|
||||
}
|
||||
+35
-15
@@ -13,9 +13,9 @@ internal static class Program
|
||||
return SdRoot / $"slot{slotIdx}" / normalizedPath;
|
||||
}
|
||||
|
||||
private static readonly Nova64Core.DisplayFlushDelegate DisplayFlush = (buffer, width, height) => { };
|
||||
private static readonly Nova64Core.AudioOutputDelegate AudioOutput = (samples, count) => { };
|
||||
private static readonly Nova64Core.SdReadFileDelegate SdReadFile = (slotIdx, filePath, destinationBuffer, destinationLength) =>
|
||||
private static readonly Nova64.DisplayFlushDelegate DisplayFlush = (buffer, width, height) => { };
|
||||
private static readonly Nova64.AudioOutputDelegate AudioOutput = (samples, count) => { };
|
||||
private static readonly Nova64.SdReadFileDelegate SdReadFile = (slotIdx, filePath, destinationBuffer, destinationLength) =>
|
||||
{
|
||||
var path = GetSlotFilePath(slotIdx, filePath);
|
||||
Console.WriteLine($"[Core] SD read request: slot {slotIdx}, file '{filePath}', resolved path '{path}' destination length {destinationLength}");
|
||||
@@ -47,7 +47,7 @@ internal static class Program
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
private static readonly Nova64Core.SdGetFileSizeDelegate SdGetFileSize = (slotIdx, filePath) =>
|
||||
private static readonly Nova64.SdGetFileSizeDelegate SdGetFileSize = (slotIdx, filePath) =>
|
||||
{
|
||||
var path = GetSlotFilePath(slotIdx, filePath);
|
||||
Console.WriteLine($"[Core] SD get file size request: slot {slotIdx}, file '{filePath}', resolved path '{path}'");
|
||||
@@ -69,14 +69,15 @@ internal static class Program
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
private static readonly Nova64Core.CartridgeStatusDelegate CartridgeStatus = (slotIdx, isInserted) => { };
|
||||
private static readonly Nova64Core.LogMessageDelegate LogMessage = (message) =>
|
||||
private static readonly Nova64.CartridgeStatusDelegate CartridgeStatus = (slotIdx, isInserted) => { };
|
||||
private static readonly Nova64.LogMessageDelegate LogMessage = (message) =>
|
||||
{
|
||||
Console.Write($"[Core] {message}");
|
||||
Console.Out.Flush();
|
||||
};
|
||||
|
||||
private static GCHandle? _ioHandle;
|
||||
private static GCHandle? _ppuHandle;
|
||||
private static Thread? _coreThread;
|
||||
private static readonly ManualResetEventSlim _coreStartedEvent = new(false);
|
||||
|
||||
@@ -87,8 +88,13 @@ internal static class Program
|
||||
Console.WriteLine("[Sim] Core thread failed: IO handle missing.");
|
||||
return;
|
||||
}
|
||||
if (_ppuHandle == null)
|
||||
{
|
||||
Console.WriteLine("[Sim] Core thread failed: PPU handle missing.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Nova64Core.nova64_init(_ioHandle.Value.AddrOfPinnedObject()))
|
||||
if (!Nova64.nova64_init(_ioHandle.Value.AddrOfPinnedObject(), _ppuHandle.Value.AddrOfPinnedObject()))
|
||||
{
|
||||
Console.WriteLine("[Sim] nova64_init failed in core thread.");
|
||||
_coreStartedEvent.Set();
|
||||
@@ -97,7 +103,7 @@ internal static class Program
|
||||
|
||||
_coreStartedEvent.Set();
|
||||
|
||||
var result = Nova64Core.nova64_main();
|
||||
var result = Nova64.nova64_main();
|
||||
Console.WriteLine($"[Sim] nova64_main returned: {result}");
|
||||
}
|
||||
|
||||
@@ -107,12 +113,11 @@ internal static class Program
|
||||
|
||||
try
|
||||
{
|
||||
var version = Nova64Core.nova64_get_version();
|
||||
var version = Nova64.nova64_get_version();
|
||||
Console.WriteLine($"[Sim] Loaded nova64_core native library. Version: 0x{version:X}");
|
||||
|
||||
var ioInterface = new Nova64Core.Nova64IoInterface
|
||||
var ioInterface = new Nova64.Nova64IoInterface
|
||||
{
|
||||
display_flush = Marshal.GetFunctionPointerForDelegate(DisplayFlush),
|
||||
audio_output = Marshal.GetFunctionPointerForDelegate(AudioOutput),
|
||||
sd_read_file = Marshal.GetFunctionPointerForDelegate(SdReadFile),
|
||||
sd_get_file_size = Marshal.GetFunctionPointerForDelegate(SdGetFileSize),
|
||||
@@ -121,6 +126,14 @@ internal static class Program
|
||||
};
|
||||
|
||||
_ioHandle = GCHandle.Alloc(ioInterface, GCHandleType.Pinned);
|
||||
|
||||
var ppuInterface = new Nova64.Nova64PpuInterface
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
_ppuHandle = GCHandle.Alloc(ppuInterface, GCHandleType.Pinned);
|
||||
|
||||
_coreThread = new Thread(CoreThreadProc)
|
||||
{
|
||||
IsBackground = true,
|
||||
@@ -143,10 +156,10 @@ internal static class Program
|
||||
if (_coreThread != null && _coreThread.IsAlive)
|
||||
{
|
||||
Console.WriteLine("[Sim] Requesting core shutdown...");
|
||||
if (Nova64Core.nova64_shutdown())
|
||||
if (Nova64.nova64_shutdown())
|
||||
{
|
||||
int timeoutMs = 1000;
|
||||
while (!Nova64Core.nova64_is_finalized() && timeoutMs > 0)
|
||||
while (!Nova64.nova64_is_finalized() && timeoutMs > 0)
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
timeoutMs -= 10;
|
||||
@@ -178,9 +191,16 @@ internal static class Program
|
||||
{
|
||||
Console.WriteLine("Core thread still running; leaving native handle pinned until process exit.");
|
||||
}
|
||||
else if (_ioHandle.HasValue && _ioHandle.Value.IsAllocated)
|
||||
else
|
||||
{
|
||||
_ioHandle.Value.Free();
|
||||
if (_ioHandle.HasValue && _ioHandle.Value.IsAllocated)
|
||||
{
|
||||
_ioHandle.Value.Free();
|
||||
}
|
||||
if (_ppuHandle.HasValue && _ppuHandle.Value.IsAllocated)
|
||||
{
|
||||
_ppuHandle.Value.Free();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user