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(); }
//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; } }
internal void ReadStateFromStream(Stream stream) { BinaryFormatter BF = new BinaryFormatter(); object[] data = (object[])BF.Deserialize(stream); //Make sure to fix dead references from serialization CPU = (CPU)data[0]; CPU.RefreshEmulatorReference(this); CPU.Memory.RefreshEmulatorReference(this); PPU = (PPU)data[1]; PPU.RefreshEmulatorReference(this); PPU.Memory.RefreshEmulatorReference(this); APU = (APU)data[2]; APU.RefreshEmulatorReference(this); Mapper = (Mapper)data[3]; Mapper.RefreshEmulatorReference(this); if (GamePak.UsesCharRAM) { GamePak.LoadCharRAM((byte[])data[4]); } }
private void DoFrame() { bool PreFrame = PPU.OddFrame; while (PreFrame == PPU.OddFrame) { ulong ElapsedCycles = CPU.Step(); for (uint i = 0; i < ElapsedCycles && !DisableAPU; i++) { APU.Step(); } //3 PPU per CPU for (uint i = 0; i < ElapsedCycles * 3; i++) { PPU.Step(); //Inform mapper Mapper.Step(); } } }