2013-07-06 04:37:13 +00:00
|
|
|
/*
|
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-07-06 04:37:13 +00:00
|
|
|
*
|
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>
|
2013-07-02 08:10:57 +00:00
|
|
|
#include <pthread.h>
|
2013-07-22 00:20:03 +00:00
|
|
|
#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
|
2013-07-22 00:20:03 +00:00
|
|
|
|
2013-07-26 05:58:31 +00:00
|
|
|
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-22 00:20:03 +00:00
|
|
|
|
2013-07-23 07:33:29 +00:00
|
|
|
static struct timespec ti;
|
2013-07-26 05:58:31 +00:00
|
|
|
static float spin_ratio=0.0;
|
2013-07-02 08:10:57 +00:00
|
|
|
|
2013-06-28 06:36:25 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// assuming end > start, returns end - start
|
2013-07-06 04:37:13 +00:00
|
|
|
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 ...
|
2013-07-06 04:37:13 +00:00
|
|
|
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;
|
2013-07-06 04:37:13 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-22 00:20:03 +00:00
|
|
|
// 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;
|
2013-07-22 00:20:03 +00:00
|
|
|
|
|
|
|
// 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;
|
2013-07-26 05:58:31 +00:00
|
|
|
unsigned int spinloop_count = 250000000;
|
2013-07-22 00:20:03 +00:00
|
|
|
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)
|
|
|
|
{
|
2013-07-26 05:58:31 +00:00
|
|
|
LOG("oops long wait (>= %lu sec) adjusting loop count (%d -> %d)", deltat.tv_sec, spinloop_count, spinloop_count>>1);
|
2013-07-22 00:20:03 +00:00
|
|
|
spinloop_count >>= 1;
|
|
|
|
i = 0;
|
|
|
|
avg_spin_nsecs = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-26 05:58:31 +00:00
|
|
|
LOG("spinloop = %lu nsec", deltat.tv_nsec);
|
2013-07-22 00:20:03 +00:00
|
|
|
avg_spin_nsecs += deltat.tv_nsec;
|
|
|
|
++i;
|
|
|
|
} while (i<samples);
|
|
|
|
|
|
|
|
avg_spin_nsecs = (avg_spin_nsecs / samples);
|
2013-07-26 05:58:31 +00:00
|
|
|
LOG("average = %lu nsec , spinloop_count = %u , samples = %u", avg_spin_nsecs, spinloop_count, samples);
|
2013-07-22 00:20:03 +00:00
|
|
|
|
2013-07-26 05:58:31 +00:00
|
|
|
// counter for 1 nsec
|
|
|
|
spin_ratio = avg_spin_nsecs / ((float)spinloop_count);
|
2013-07-22 00:20:03 +00:00
|
|
|
|
2013-07-26 05:58:31 +00:00
|
|
|
LOG("%fns cycle spins for average %f", CYCLE_NSECS, spin_ratio);
|
2013-07-22 00:20:03 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 04:37:13 +00:00
|
|
|
void timing_initialize() {
|
2013-07-22 00:20:03 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2013-07-22 00:20:03 +00:00
|
|
|
void timing_set_cpu_scale(unsigned int scale)
|
|
|
|
{
|
|
|
|
// ...
|
2013-06-28 06:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-07-26 05:58:31 +00:00
|
|
|
* Throttles 6502 CPU down to the target CPU frequency (default is speed of original Apple //e).
|
2013-07-02 08:10:57 +00:00
|
|
|
*
|
2013-07-26 05:58:31 +00:00
|
|
|
* This uses an adaptive spin loop to stay closer to the target CPU frequency.
|
2013-06-28 06:36:25 +00:00
|
|
|
*/
|
2013-07-26 05:58:31 +00:00
|
|
|
void timing_throttle()
|
2013-07-22 00:20:03 +00:00
|
|
|
{
|
2013-07-23 07:33:29 +00:00
|
|
|
struct timespec tj, deltat;
|
2013-07-22 00:20:03 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &tj);
|
|
|
|
deltat = timespec_diff(ti, tj);
|
|
|
|
ti=tj;
|
2013-06-28 06:36:25 +00:00
|
|
|
|
2013-07-26 05:58:31 +00:00
|
|
|
static time_t severe_lag=0;
|
|
|
|
if (deltat.tv_sec != 0)
|
2013-07-22 00:20:03 +00:00
|
|
|
{
|
2013-07-26 05:58:31 +00:00
|
|
|
// severely lagging...
|
|
|
|
if (severe_lag < time(NULL))
|
|
|
|
{
|
|
|
|
severe_lag = time(NULL)+2;
|
|
|
|
LOG("Severe lag detected...");
|
|
|
|
}
|
|
|
|
return;
|
2013-07-22 00:20:03 +00:00
|
|
|
}
|
2013-07-26 05:58:31 +00:00
|
|
|
|
|
|
|
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)
|
2013-07-22 00:20:03 +00:00
|
|
|
{
|
2013-07-26 05:58:31 +00:00
|
|
|
// lagging
|
|
|
|
return;
|
2013-07-22 00:20:03 +00:00
|
|
|
}
|
2013-07-23 07:33:29 +00:00
|
|
|
|
2013-07-26 05:58:31 +00:00
|
|
|
unsigned long diff_nsec = opcycles_nsecs - deltat.tv_nsec;
|
2013-07-23 07:33:29 +00:00
|
|
|
|
2013-07-26 05:58:31 +00:00
|
|
|
static time_t sample_time=0;
|
|
|
|
if (sample_time < time(NULL))
|
2013-07-22 00:20:03 +00:00
|
|
|
{
|
2013-07-26 05:58:31 +00:00
|
|
|
sample_time = time(NULL)+1;
|
|
|
|
//LOG("sample diff_nsec : %lu", diff_nsec);
|
2013-06-28 06:36:25 +00:00
|
|
|
}
|
2013-07-26 05:58:31 +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
|
|
|
}
|
2013-07-22 00:20:03 +00:00
|
|
|
|