protected void RunEmulator(IEmulator emulator, Func <bool> tickFunc) { IProcessorCore cpu = (IProcessorCore)emulator.Components.First(c => c.Name == "Ricoh 2A03 CPU"); IComponentWithClock ppu = (IComponentWithClock)emulator.Components.First(c => c.Name == "Ricoh 2C02 PPU"); IComponentWithClock apu = (IComponentWithClock)emulator.Components.First(c => c.Name == "NES APU"); while (true) { if (tickFunc != null && !tickFunc()) { break; } int cycles = cpu.Step(); if (cycles < 0) { Assert.Fail("CPU step failed!"); break; } for (int i = 0; i < cycles; i++) { // PPU ticks 3 times during each CPU cycle ppu.Tick(); ppu.Tick(); ppu.Tick(); } for (int i = 0; i < cycles; i++) { apu.Tick(); } } }
void IEmulator.Run() { int cycles; int cycleCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); this.run = true; IProcessorCore core = (IProcessorCore)this.cpu; IComponentWithClock ppuClock = (IComponentWithClock)this.ppu; IComponentWithClock apuClock = (IComponentWithClock)this.cpu.APU; while (this.run) { cycles = core.Step(); if (cycles < 0) { break; } for (int i = 0; i < cycles; i++) { // PPU ticks 3 times during each CPU cycle ppuClock.Tick(); ppuClock.Tick(); ppuClock.Tick(); } for (int i = 0; i < cycles; i++) { apuClock.Tick(); } cycleCount += cycles; if (sw.ElapsedMilliseconds >= 1000) { Trace.WriteLine(String.Format("Emulated clock speed: {0} MHz", cycleCount / (1000000f))); cycleCount = 0; sw.Restart(); } } stopEvent.Set(); }