apple2ix/src/timing.c

165 lines
4.2 KiB
C
Raw Normal View History

/*
2013-06-28 06:36:25 +00:00
* Apple // emulator for Linux
*
* CPU Timing Support.
*
* Mostly this adds support for specifically throttling the emulator speed to
* match a 1.02MHz Apple //e.
*
* Added 2013 by Aaron Culliney
*
2013-06-28 06:36:25 +00:00
*/
#include <stdlib.h>
#include <stdio.h>
2013-07-23 07:33:29 +00:00
#include <stdint.h>
2013-06-28 06:36:25 +00:00
#include <time.h>
#include <pthread.h>
#include <limits.h>
2013-06-28 06:36:25 +00:00
2013-07-23 07:33:29 +00:00
#include "timing.h"
#include "misc.h"
#include "cpu.h"
2013-06-28 06:36:25 +00:00
2013-07-23 07:33:29 +00:00
#define CALIBRATE_HZ 100
static unsigned long CPU_TARGET_HZ = APPLE2_HZ; // target clock speed
static unsigned long CALIBRATE_INTERVAL_NSECS = NANOSECONDS / CALIBRATE_HZ; // calibration interval for drifting
static float CYCLE_NSECS = NANOSECONDS / (float)APPLE2_HZ; // nanosecs per cycle
2013-07-23 07:33:29 +00:00
static struct timespec ti;
static float spin_ratio=0.0;
2013-06-28 06:36:25 +00:00
// -----------------------------------------------------------------------------
// assuming end > start, returns end - start
static inline struct timespec timespec_diff(struct timespec start, struct timespec end) {
2013-06-28 06:36:25 +00:00
struct timespec t;
// assuming time_t is signed ...
if (end.tv_nsec < start.tv_nsec)
{
2013-06-28 06:36:25 +00:00
t.tv_sec = end.tv_sec - start.tv_sec - 1;
t.tv_nsec = NANOSECONDS + end.tv_nsec - start.tv_nsec;
}
else
{
2013-06-28 06:36:25 +00:00
t.tv_sec = end.tv_sec - start.tv_sec;
t.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return t;
}
// spin loop to throttle to target CPU Hz
static inline void _spin_loop(unsigned long c)
{
static volatile unsigned int spinney=0; // volatile to prevent being optimized away
for (unsigned long i=0; i<c; i++)
{
++spinney;
}
}
static void _determine_initial_spinloop_counter()
{
2013-07-23 07:33:29 +00:00
struct timespec s0, s1, deltat;
// time the spinloop to determine a good starting value for the spin counter
unsigned long avg_spin_nsecs = 0;
unsigned int const samples = 5;
unsigned int i=0;
unsigned int spinloop_count = 250000000;
do
{
clock_gettime(CLOCK_MONOTONIC, &s0);
_spin_loop(spinloop_count);
clock_gettime(CLOCK_MONOTONIC, &s1);
deltat = timespec_diff(s0, s1);
if (deltat.tv_sec > 0)
{
LOG("oops long wait (>= %lu sec) adjusting loop count (%d -> %d)", deltat.tv_sec, spinloop_count, spinloop_count>>1);
spinloop_count >>= 1;
i = 0;
avg_spin_nsecs = 0;
continue;
}
LOG("spinloop = %lu nsec", deltat.tv_nsec);
avg_spin_nsecs += deltat.tv_nsec;
++i;
} while (i<samples);
avg_spin_nsecs = (avg_spin_nsecs / samples);
LOG("average = %lu nsec , spinloop_count = %u , samples = %u", avg_spin_nsecs, spinloop_count, samples);
// counter for 1 nsec
spin_ratio = avg_spin_nsecs / ((float)spinloop_count);
LOG("%fns cycle spins for average %f", CYCLE_NSECS, spin_ratio);
}
void timing_initialize() {
// should do this only on startup
_determine_initial_spinloop_counter();
2013-07-23 07:33:29 +00:00
clock_gettime(CLOCK_MONOTONIC, &ti);
2013-06-28 06:36:25 +00:00
}
void timing_set_cpu_scale(unsigned int scale)
{
// ...
2013-06-28 06:36:25 +00:00
}
/*
* Throttles 6502 CPU down to the target CPU frequency (default is speed of original Apple //e).
*
* This uses an adaptive spin loop to stay closer to the target CPU frequency.
2013-06-28 06:36:25 +00:00
*/
void timing_throttle()
{
2013-07-23 07:33:29 +00:00
struct timespec tj, deltat;
clock_gettime(CLOCK_MONOTONIC, &tj);
deltat = timespec_diff(ti, tj);
ti=tj;
2013-06-28 06:36:25 +00:00
static time_t severe_lag=0;
if (deltat.tv_sec != 0)
{
// severely lagging...
if (severe_lag < time(NULL))
{
severe_lag = time(NULL)+2;
LOG("Severe lag detected...");
}
return;
}
uint8_t opcycles = cpu65__opcycles[cpu65_debug.opcode] + cpu65_debug.opcycles;
unsigned long opcycles_nsecs = opcycles * (CYCLE_NSECS/2);
if (deltat.tv_nsec >= opcycles_nsecs)
{
// lagging
return;
}
2013-07-23 07:33:29 +00:00
unsigned long diff_nsec = opcycles_nsecs - deltat.tv_nsec;
2013-07-23 07:33:29 +00:00
static time_t sample_time=0;
if (sample_time < time(NULL))
{
sample_time = time(NULL)+1;
//LOG("sample diff_nsec : %lu", diff_nsec);
2013-06-28 06:36:25 +00:00
}
unsigned long spin_count = spin_ratio * diff_nsec;
// spin for the rest of the interval ...
_spin_loop(spin_count);
2013-06-28 06:36:25 +00:00
}