Files
Nova64/simulator/Program.cs
T
Lukas Höppner 93fc058120 Cleanup
2026-08-09 19:39:53 +02:00

120 lines
4.2 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Threading;
internal static class Program
{
private static readonly Nova64Core.DisplayFlushDelegate DisplayFlush = (buffer, width, height) => { };
private static readonly Nova64Core.AudioOutputDelegate AudioOutput = (samples, count) => { };
private static readonly Nova64Core.SdReadSectorDelegate SdReadSector = (sectorIdx, destinationBuffer, sectorCount) => false;
private static readonly Nova64Core.CartridgeStatusDelegate CartridgeStatus = (slotIdx, isInserted) => { };
private static readonly Nova64Core.LogMessageDelegate LogMessage = (message) =>
{
Console.Write($"[Core] {message}");
};
private static GCHandle? _ioHandle;
private static Thread? _coreThread;
private static readonly ManualResetEventSlim _coreStartedEvent = new(false);
private static void CoreThreadProc()
{
if (_ioHandle == null)
{
Console.WriteLine("[Sim] Core thread failed: IO handle missing.");
return;
}
if (!Nova64Core.nova64_init(_ioHandle.Value.AddrOfPinnedObject()))
{
Console.WriteLine("[Sim] nova64_init failed in core thread.");
_coreStartedEvent.Set();
return;
}
_coreStartedEvent.Set();
var result = Nova64Core.nova64_main();
Console.WriteLine($"[Sim] nova64_main returned: {result}");
}
private static void Main()
{
Console.WriteLine("[Sim] simulator starting...");
try
{
var version = Nova64Core.nova64_get_version();
Console.WriteLine($"[Sim] Loaded nova64_core native library. Version: 0x{version:X}");
var ioInterface = new Nova64Core.Nova64IoInterface
{
display_flush = Marshal.GetFunctionPointerForDelegate(DisplayFlush),
audio_output = Marshal.GetFunctionPointerForDelegate(AudioOutput),
sd_read_sector = Marshal.GetFunctionPointerForDelegate(SdReadSector),
cartridge_status = Marshal.GetFunctionPointerForDelegate(CartridgeStatus),
log_message = Marshal.GetFunctionPointerForDelegate(LogMessage)
};
_ioHandle = GCHandle.Alloc(ioInterface, GCHandleType.Pinned);
_coreThread = new Thread(CoreThreadProc)
{
IsBackground = true,
Name = "Nova64CoreThread"
};
_coreThread.Start();
if (_coreStartedEvent.Wait(TimeSpan.FromSeconds(5)))
{
Console.WriteLine("[Sim] core thread started.");
}
else
{
Console.WriteLine("[Sim] Core thread did not signal startup in time.");
}
Console.WriteLine("[Sim] Press Enter to stop the simulator.");
Console.ReadLine();
if (_coreThread != null && _coreThread.IsAlive)
{
Console.WriteLine("[Sim] Requesting core shutdown...");
if (Nova64Core.nova64_shutdown())
{
if (!_coreThread.Join(TimeSpan.FromSeconds(5)))
{
Console.WriteLine("[Sim] Core thread did not exit in time.");
}
}
else
{
Console.WriteLine("[Sim] Core shutdown request failed.");
}
}
}
catch (DllNotFoundException ex)
{
Console.WriteLine($"[Sim] Native library not found: {ex.Message}");
}
catch (EntryPointNotFoundException ex)
{
Console.WriteLine($"[Sim] Native entry point missing: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"[Sim] Failed to initialize native library: {ex.Message}");
}
finally
{
if (_coreThread != null && _coreThread.IsAlive)
{
Console.WriteLine("Core thread still running; leaving native handle pinned until process exit.");
}
else if (_ioHandle.HasValue && _ioHandle.Value.IsAllocated)
{
_ioHandle.Value.Free();
}
}
}
}