184 lines
6.7 KiB
C#
184 lines
6.7 KiB
C#
using System.Runtime.InteropServices;
|
|
using Zio;
|
|
using Zio.FileSystems;
|
|
|
|
internal static class Program
|
|
{
|
|
private static readonly IFileSystem SdFileSystem = new PhysicalFileSystem();
|
|
private static readonly UPath SdRoot = new UPath(Path.Combine(AppContext.BaseDirectory, "sd").Replace('\\', '/').Replace("C:", "/mnt/c"));
|
|
|
|
private static UPath GetSlotFilePath(uint slotIdx, string filePath)
|
|
{
|
|
var normalizedPath = (filePath ?? string.Empty).Replace('\\', '/').TrimStart('/');
|
|
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) =>
|
|
{
|
|
var path = GetSlotFilePath(slotIdx, filePath);
|
|
Console.WriteLine($"[Core] SD read request: slot {slotIdx}, file '{filePath}', resolved path '{path}' destination length {destinationLength}");
|
|
|
|
if (destinationBuffer == IntPtr.Zero)
|
|
{
|
|
Console.WriteLine("[Core] SD read request failed: destination buffer is null.");
|
|
return -1;
|
|
}
|
|
|
|
if (!SdFileSystem.FileExists(path))
|
|
{
|
|
Console.WriteLine($"[Core] SD read request failed: file not found '{path}'");
|
|
return -1;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var stream = SdFileSystem.OpenFile(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
var bytesToRead = (int)Math.Min(destinationLength, stream.Length);
|
|
var buffer = new byte[bytesToRead];
|
|
var bytesRead = stream.Read(buffer, 0, bytesToRead);
|
|
Marshal.Copy(buffer, 0, destinationBuffer, bytesRead);
|
|
return bytesRead;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[Core] SD read request failed: {ex.Message}");
|
|
return -1;
|
|
}
|
|
};
|
|
private static readonly Nova64Core.SdGetFileSizeDelegate SdGetFileSize = (slotIdx, filePath) =>
|
|
{
|
|
var path = GetSlotFilePath(slotIdx, filePath);
|
|
Console.WriteLine($"[Core] SD get file size request: slot {slotIdx}, file '{filePath}', resolved path '{path}'");
|
|
|
|
if (!SdFileSystem.FileExists(path))
|
|
{
|
|
Console.WriteLine($"[Core] SD get file size failed: file not found '{path}'");
|
|
return -1;
|
|
}
|
|
|
|
try
|
|
{
|
|
var length = SdFileSystem.GetFileLength(path);
|
|
return length > int.MaxValue ? -1 : (int)length;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[Core] SD get file size failed: {ex.Message}");
|
|
return -1;
|
|
}
|
|
};
|
|
private static readonly Nova64Core.CartridgeStatusDelegate CartridgeStatus = (slotIdx, isInserted) => { };
|
|
private static readonly Nova64Core.LogMessageDelegate LogMessage = (message) =>
|
|
{
|
|
Console.Write($"[Core] {message}");
|
|
Console.Out.Flush();
|
|
};
|
|
|
|
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_file = Marshal.GetFunctionPointerForDelegate(SdReadFile),
|
|
sd_get_file_size = Marshal.GetFunctionPointerForDelegate(SdGetFileSize),
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|