src/emulation/emulation.cc
/*
 * Runs the NES emulation for 1/60th of its clock rate.
 */
Runs the NES emulation for 1/60th of its clock rate.
Runs the NES emulation for 1 / 60th of its clock rate .
void Emulation::RunEmulationCycle(void) {
  size_t cycles_remaining = EMU_CYCLE_SIZE;
  size_t sync_cycles = 0;
  size_t scheduled_cycles = 0;
  size_t cpu_cycles = 0;
  size_t apu_cycles = 0;
  size_t ppu_cycles = 0;
  while (cycles_remaining > 0) {
    sync_cycles = MIN(sync_cycles, cycles_remaining);
    for (size_t i = 0; i < sync_cycles; i++) {
      cpu_->RunCycle();
      apu_->RunCycle();
      ppu_->RunSchedule(3U);
    }
    cycles_remaining -= sync_cycles;
    if (cycles_remaining <= 0) { return; }
    ppu_cycles = ppu_->Schedule();
    apu_cycles = apu_->Schedule();
    scheduled_cycles = MIN(ppu_cycles, apu_cycles);
    cpu_cycles = cpu_->RunSchedule(MIN(scheduled_cycles, cycles_remaining),
                                   sync_cycles);
    for (size_t i = 0; i < cpu_cycles; i++) { apu_->RunCycle(); }
    ppu_->RunSchedule(cpu_cycles * 3U);
    cycles_remaining -= cpu_cycles;
  }
  return;
}
void Emulation :: RunEmulationCycle ( void ) { size_t cycles_remaining = EMU_CYCLE_SIZE ; size_t sync_cycles = 0 ; size_t scheduled_cycles = 0 ; size_t cpu_cycles = 0 ; size_t apu_cycles = 0 ; size_t ppu_cycles = 0 ; while ( cycles_remaining > 0 ) { sync_cycles = MIN ( sync_cycles , cycles_remaining ) ; for ( size_t i = 0 ; i < sync_cycles ; i ++ ) { cpu_ -> RunCycle ( ) ; apu_ -> RunCycle ( ) ; ppu_ -> RunSchedule ( 3U ) ; } cycles_remaining -= sync_cycles ; if ( cycles_remaining <= 0 ) { return ; } ppu_cycles = ppu_ -> Schedule ( ) ; apu_cycles = apu_ -> Schedule ( ) ; scheduled_cycles = MIN ( ppu_cycles , apu_cycles ) ; cpu_cycles = cpu_ -> RunSchedule ( MIN ( scheduled_cycles , cycles_remaining ) , sync_cycles ) ; for ( size_t i = 0 ; i < cpu_cycles ; i ++ ) { apu_ -> RunCycle ( ) ; } ppu_ -> RunSchedule ( cpu_cycles * 3U ) ; cycles_remaining -= cpu_cycles ; } return ; }
Runs the NES emulation for 1/60th of its clock rate.
Runs the NES emulation for 1 / 60th of its clock rate .
/*
   * In order to increase cache hits during emulation, some math is done to
   * determine how long the individual chips can be run in isolation. Running
   * them this way prevents the memory systems of the other chips from causing
   * cache misses as often.
   */ // Emulate the system with all cycles synced. // The PPU is clocked at 3x the rate of the CPU. // Check if the synchronized execution finished this emulation cycle. // Determine how long the emulation can run out of sync. // Run the CPU, then catch up the APU and PPU.