This commit is contained in:
Lukas Höppner
2026-08-09 19:39:53 +02:00
parent 123a6e71b4
commit 93fc058120
3 changed files with 81 additions and 80 deletions
+43
View File
@@ -0,0 +1,43 @@
using System.Runtime.InteropServices;
internal static class Nova64Core
{
private const string LibName = "nova64_core";
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void DisplayFlushDelegate(IntPtr buffer, ushort width, ushort height);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void AudioOutputDelegate(IntPtr samples, uint count);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool SdReadSectorDelegate(uint sectorIdx, IntPtr destinationBuffer, uint sectorCount);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CartridgeStatusDelegate(uint slotIdx, bool isInserted);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate void LogMessageDelegate([MarshalAs(UnmanagedType.LPStr)] string message);
[StructLayout(LayoutKind.Sequential)]
public struct Nova64IoInterface
{
public IntPtr display_flush;
public IntPtr audio_output;
public IntPtr sd_read_sector;
public IntPtr cartridge_status;
public IntPtr log_message;
}
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool nova64_init(IntPtr io_interface);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int nova64_main();
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool nova64_shutdown();
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern uint nova64_get_version();
}
+16 -58
View File
@@ -2,48 +2,6 @@ using System;
using System.Runtime.InteropServices;
using System.Threading;
internal static class Nova64Core
{
private const string LibName = "nova64_core";
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void DisplayFlushDelegate(IntPtr buffer, ushort width, ushort height);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void AudioOutputDelegate(IntPtr samples, uint count);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool SdReadSectorDelegate(uint sectorIdx, IntPtr destinationBuffer, uint sectorCount);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void CartridgeStatusDelegate(uint slotIdx, bool isInserted);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate void LogMessageDelegate([MarshalAs(UnmanagedType.LPStr)] string message);
[StructLayout(LayoutKind.Sequential)]
public struct Nova64IoInterface
{
public IntPtr display_flush;
public IntPtr audio_output;
public IntPtr sd_read_sector;
public IntPtr cartridge_status;
public IntPtr log_message;
}
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool nova64_init(IntPtr io_interface);
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern int nova64_main();
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern bool nova64_shutdown();
[DllImport(LibName, CallingConvention = CallingConvention.Cdecl)]
public static extern uint nova64_get_version();
}
internal static class Program
{
private static readonly Nova64Core.DisplayFlushDelegate DisplayFlush = (buffer, width, height) => { };
@@ -52,7 +10,7 @@ internal static class Program
private static readonly Nova64Core.CartridgeStatusDelegate CartridgeStatus = (slotIdx, isInserted) => { };
private static readonly Nova64Core.LogMessageDelegate LogMessage = (message) =>
{
Console.Write($"[Nova64] {message}");
Console.Write($"[Core] {message}");
};
private static GCHandle? _ioHandle;
@@ -63,13 +21,13 @@ internal static class Program
{
if (_ioHandle == null)
{
Console.WriteLine("[Nova64] Core thread failed: IO handle missing.");
Console.WriteLine("[Sim] Core thread failed: IO handle missing.");
return;
}
if (!Nova64Core.nova64_init(_ioHandle.Value.AddrOfPinnedObject()))
{
Console.WriteLine("[Nova64] nova64_init failed in core thread.");
Console.WriteLine("[Sim] nova64_init failed in core thread.");
_coreStartedEvent.Set();
return;
}
@@ -77,17 +35,17 @@ internal static class Program
_coreStartedEvent.Set();
var result = Nova64Core.nova64_main();
Console.WriteLine($"[Nova64] nova64_main returned: {result}");
Console.WriteLine($"[Sim] nova64_main returned: {result}");
}
private static void Main()
{
Console.WriteLine("Nova64 simulator starting...");
Console.WriteLine("[Sim] simulator starting...");
try
{
var version = Nova64Core.nova64_get_version();
Console.WriteLine($"Loaded nova64_core native library. Version: 0x{version:X}");
Console.WriteLine($"[Sim] Loaded nova64_core native library. Version: 0x{version:X}");
var ioInterface = new Nova64Core.Nova64IoInterface
{
@@ -108,49 +66,49 @@ internal static class Program
if (_coreStartedEvent.Wait(TimeSpan.FromSeconds(5)))
{
Console.WriteLine("nova64 core thread started.");
Console.WriteLine("[Sim] core thread started.");
}
else
{
Console.WriteLine("nova64 core thread did not signal startup in time.");
Console.WriteLine("[Sim] Core thread did not signal startup in time.");
}
Console.WriteLine("Press Enter to stop the simulator.");
Console.WriteLine("[Sim] Press Enter to stop the simulator.");
Console.ReadLine();
if (_coreThread != null && _coreThread.IsAlive)
{
Console.WriteLine("[Nova64] Requesting core shutdown...");
Console.WriteLine("[Sim] Requesting core shutdown...");
if (Nova64Core.nova64_shutdown())
{
if (!_coreThread.Join(TimeSpan.FromSeconds(5)))
{
Console.WriteLine("[Nova64] Core thread did not exit in time.");
Console.WriteLine("[Sim] Core thread did not exit in time.");
}
}
else
{
Console.WriteLine("[Nova64] Core shutdown request failed.");
Console.WriteLine("[Sim] Core shutdown request failed.");
}
}
}
catch (DllNotFoundException ex)
{
Console.WriteLine($"Native library not found: {ex.Message}");
Console.WriteLine($"[Sim] Native library not found: {ex.Message}");
}
catch (EntryPointNotFoundException ex)
{
Console.WriteLine($"Native entry point missing: {ex.Message}");
Console.WriteLine($"[Sim] Native entry point missing: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to initialize native library: {ex.Message}");
Console.WriteLine($"[Sim] Failed to initialize native library: {ex.Message}");
}
finally
{
if (_coreThread != null && _coreThread.IsAlive)
{
Console.WriteLine("[Nova64] Core thread still running; leaving native handle pinned until process exit.");
Console.WriteLine("Core thread still running; leaving native handle pinned until process exit.");
}
else if (_ioHandle.HasValue && _ioHandle.Value.IsAllocated)
{