//Save states and rom loading private void HandleIO() { if (queueLoadState) { queueLoadState = false; using (FileStream FS = new FileStream(saveStatePath, FileMode.Open)) { ReadStateFromStream(FS); } RewindManager.Reset(); } else if (queueSaveState) { queueSaveState = false; using (FileStream FS = new FileStream(saveStatePath, FileMode.OpenOrCreate)) { WriteStateToStream(FS); } } else if (queueLoad) { queueLoad = false; GamePak = new GamePak(romPath); Mapper = Mapper.Create(this); CPU.Reset(); PPU.Reset(); APU.Reset(); RewindManager.Reset(); State = EmulatorState.Running; } }
public Emulator() { //Set emulation settings Turbo = false; Speed = 1; Pause = false; DisableAPU = false; TrackHistory = false; State = EmulatorState.Halted; queueLoad = false; romPath = ""; queueSaveState = false; queueLoadState = false; saveStatePath = ""; //Init emulator CPU = new CPU(this); PPU = new PPU(this); APU = new APU(this); Controllers = new Controller[] { new Controller(), new Controller() }; NetPlayServer = new NetPlayServer(this); GameGenie = new GameGenie(); RewindManager = new RewindManager(this); //Init emulation thread EmuThread = new Thread(EmulationLoop); EmuThread.IsBackground = true; EmuThread.Start(); }
private void HandleFrame() { if (!Pause && Running) { Stopwatch SW = Stopwatch.StartNew(); if (State == EmulatorState.Rewinding) { var frame = RewindManager.PopFrame(); PPU.FrameBuffer = frame.frame; if (frame.startReached) { Array.ForEach(Controllers, x => x.BlockInput = false); State = EmulatorState.Running; } } else if (State == EmulatorState.CatchingUp) { //Catchup to current frame num int numFrames = RewindManager.FrameOffset; for (int n = 0; n < numFrames; n++) { DoFrame(); var frame = RewindManager.GetFrameRelative(n); for (int i = 0; i < 2; i++) { Controllers[0].LoadInputByte(frame.input[0]); Controllers[1].LoadInputByte(frame.input[1]); } } Array.ForEach(Controllers, x => x.BlockInput = false); State = EmulatorState.Running; } else { DoFrame(); if (TrackHistory) { RewindManager.PushFrame(new byte[] { Controllers[0].GetInputByte(), Controllers[1].GetInputByte() }, (byte[])PPU.FrameBuffer.Clone()); } } SW.Stop(); if (!Turbo) { Thread.Sleep(Math.Max((int)(((1000.0 / 60) - SW.ElapsedMilliseconds) / Speed), 0)); } } }