public static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; Console.SetWindowSize(Chip8Emu.DisplayColumns + Chip8EmuView.LogWidth, Chip8Emu.DisplayRows); Console.CursorVisible = false; Console.CancelKeyPress += Console_CancelKeyPress; #if DEBUG LogLevel = LogLevel.DebugInfo; #endif try { // Parse args bool? shiftQuirkEnabled = null; bool? loadStoreQuirkEnabled = null; string romFile = ""; bool turbo = false; for (int i = 0; i < args.Length; i++) { switch (args[i].ToLower()) { case "-loadstore": case "/loadstore": case "-loadstorequirk": case "/loadstorequirk": loadStoreQuirkEnabled = true; break; case "-shift": case "/shift": case "-shiftquirk": case "/shiftquirk": shiftQuirkEnabled = true; break; case "-turbo": case "/turbo": turbo = true; break; default: romFile = args[i]; break; } } if (string.IsNullOrWhiteSpace(romFile)) { Usage(); return; } View.Log($"Loading ROM: {romFile}", LogLevel.Info); byte[] romData = File.ReadAllBytes(romFile); Emulator = new Chip8Emu(View, romData); Emulator.TryConfigureQuirks(); if (loadStoreQuirkEnabled.HasValue) { View.Log("Forcing LoadStoreQuirkEnabled.", LogLevel.Info); Emulator.LoadStoreQuirkEnabled = loadStoreQuirkEnabled.Value; } if (shiftQuirkEnabled.HasValue) { View.Log("Forcing ShiftQuirkEnabled.", LogLevel.Info); Emulator.ShiftQuirkEnabled = shiftQuirkEnabled.Value; } if (turbo) { Emulator.CycleRateHz = Chip8Emu.TurboCycleRateHz; } View.Log("Ready.", LogLevel.Info); Task uiTask = Task.Factory.StartNew(() => { Stopwatch sw = new Stopwatch(); sw.Start(); while (!CTS.Token.IsCancellationRequested) { if (sw.Elapsed >= ViewDelay) { View.ReadKeys(); View.DrawScreen(); sw.Restart(); } Thread.Yield(); } }); Task emuTask = Emulator.Start(CTS); emuTask.Wait(CTS.Token); uiTask.Wait(CTS.Token); } catch (Exception ex) { Console.Error.WriteLine($"Error: {ex.Message}"); } }
public void LoadRom(string romFile, bool prompt) { try { CTS?.Cancel(); Emulator?.Stop(); DisplayTask?.Wait(); if (prompt) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Title = Resources.OpenTitle; if (File.Exists(romFile)) { dialog.InitialDirectory = Path.GetDirectoryName(romFile); dialog.FileName = Path.GetFileName(romFile); } romFile = dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : ""; } if (!string.IsNullOrWhiteSpace(romFile) && File.Exists(romFile)) { LastRomFile = romFile; Log($"Loading ROM: {romFile}", LogLevel.Info); byte[] romData = File.ReadAllBytes(romFile); Emulator = new Chip8Emu(this, romData); DisplayBuffer = new bool[Chip8Emu.DisplayColumns, Chip8Emu.DisplayRows]; Emulator.TryConfigureQuirks(); Log("Ready.", LogLevel.Info); } CTS = new CancellationTokenSource(); Emulator?.Start(CTS); DisplayTask = Task.Factory.StartNew(() => { Stopwatch sw = new Stopwatch(); sw.Start(); while (!CTS.Token.IsCancellationRequested) { if (sw.Elapsed >= ViewDelay) { DrawDisplay(); sw.Restart(); } Thread.Yield(); } }); } catch (Exception ex) { HandleException(ex); } finally { UpdateConfig(); } }