//------------------------------------------------- // postload - after loading a save state //------------------------------------------------- void postload() { // remove all timers and make a private list of permanent ones simple_list <emu_timer> private_list = new simple_list <emu_timer>(); while (m_timer_list != null) { emu_timer timer = m_timer_list; // temporary timers go away entirely (except our special never-expiring one) if (timer.temporary() && !timer.expire().is_never()) { m_timer_allocator.reclaim(timer.release()); } // permanent ones get added to our private list else { private_list.append(timer_list_remove(timer)); } } { // now re-insert them; this effectively re-sorts them by time emu_timer timer; while ((timer = private_list.detach_head()) != null) { timer_list_insert(timer); } } m_suspend_changes_pending = true; rebuild_execute_list(); // report the timer state after a log LOG("After resetting/reordering timers:\n"); #if VERBOSE dump_timers(); #endif }
// execution //------------------------------------------------- // timeslice - execute all devices for a single // timeslice //------------------------------------------------- public void timeslice() { bool call_debugger = (machine().debug_flags_get & machine_global.DEBUG_FLAG_ENABLED) != 0; // build the execution list if we don't have one yet //if (UNEXPECTED(m_execute_list == null)) if (m_execute_list == null) { rebuild_execute_list(); } // if the current quantum has expired, find a new one while (m_basetime >= m_quantum_list.first().expire()) { m_quantum_allocator.reclaim(m_quantum_list.detach_head()); } // loop until we hit the next timer while (m_basetime < m_timer_list.expire()) { // by default, assume our target is the end of the next quantum attotime target = m_basetime + new attotime(0, m_quantum_list.first().actual()); // however, if the next timer is going to fire before then, override if (m_timer_list.expire() < target) { target = m_timer_list.expire(); } if (machine().video().frame_update_count() % 1000 == 0) { //LOG(("------------------\n")); LOG("device_scheduler.timeslice() - cpu_timeslice: target = {0}, m_timer_list.expire: {1}\n", target.as_string(), m_timer_list.expire().as_string()); } // do we have pending suspension changes? if (m_suspend_changes_pending) { apply_suspend_changes(); } // loop over all CPUs for (device_execute_interface exec = m_execute_list; exec != null; exec = exec.nextexec) { // only process if this CPU is executing or truly halted (not yielding) // and if our target is later than the CPU's current time (coarse check) //if (EXPECTED((exec.m_suspend == 0 || exec.m_eatcycles) && target.seconds >= exec.m_localtime.seconds)) if ((exec.suspend_ == 0 || exec.eatcycles > 0) && target.seconds() >= exec.localtime.seconds()) { // compute how many attoseconds to execute this CPU attoseconds_t delta = target.attoseconds() - exec.localtime.attoseconds(); if (delta < 0 && target.seconds() > exec.localtime.seconds()) { delta += attotime.ATTOSECONDS_PER_SECOND; } assert(delta == (target - exec.localtime).as_attoseconds()); if (exec.attoseconds_per_cycle == 0) { exec.localtime = target; } // if we have enough for at least 1 cycle, do the math else if (delta >= exec.attoseconds_per_cycle) { // compute how many cycles we want to execute int ran = exec.cycles_running = (int)divu_64x32((UInt64)delta >> exec.divshift, (UInt32)exec.divisor); if (machine().video().frame_update_count() % 1000 == 0) { LOG("device_scheduler.timeslice() - cpu '{0}': {1} ({2} cycles)\n", exec.device().tag(), delta, exec.cycles_running); } // if we're not suspended, actually execute if (exec.suspend_ == 0) { profiler_global.g_profiler.start(exec.profiler); // note that this global variable cycles_stolen can be modified // via the call to cpu_execute exec.cycles_stolen = 0; m_executing_device = exec; exec.icount_set(exec.cycles_running); // *exec->m_icountptr = exec->m_cycles_running; if (!call_debugger) { exec.run(); } else { exec.debugger_start_cpu_hook(target); exec.run(); exec.debugger_stop_cpu_hook(); } // adjust for any cycles we took back /*assert(ran >= *exec->m_icountptr);*/ ran -= exec.icountptrRef.i; /*assert(ran >= exec->m_cycles_stolen);*/ ran -= exec.cycles_stolen; profiler_global.g_profiler.stop(); } // account for these cycles exec.totalcycles += (UInt64)ran; // update the local time for this CPU attotime deltatime; if (ran < exec.cycles_per_second) { deltatime = new attotime(0, exec.attoseconds_per_cycle * ran); } else { UInt32 remainder; int secs = (int)divu_64x32_rem((UInt64)ran, exec.cycles_per_second, out remainder); deltatime = new attotime(secs, remainder * exec.attoseconds_per_cycle); } assert(deltatime >= attotime.zero); exec.localtime += deltatime; if (machine().video().frame_update_count() % 100 == 0) { LOG("device_scheduler.timeslice() - {0} ran, {1} total, time = {2}\n", ran, exec.totalcycles, exec.localtime.as_string()); } // if the new local CPU time is less than our target, move the target up, but not before the base if (exec.localtime < target) { target = attotime.Max(exec.localtime, m_basetime); if (machine().video().frame_update_count() % 1000 == 0) { LOG("device_scheduler.timeslice() - (new target)\n"); } } } } } m_executing_device = null; // update the base time m_basetime = target; } // execute timers execute_timers(); }