apple2ix/src/timing.c

379 lines
12 KiB
C
Raw Normal View History

/*
* Apple // emulator for *nix
2013-06-28 06:36:25 +00:00
*
* This software package is subject to the GNU General Public License
* version 2 or later (your choice) as published by the Free Software
* Foundation.
2013-06-28 06:36:25 +00:00
*
* THERE ARE NO WARRANTIES WHATSOEVER.
*
2013-06-28 06:36:25 +00:00
*/
/*
* 65c02 CPU Timing Support. Some source derived from AppleWin.
*
* Copyleft 2013 Aaron Culliney
*
*/
2013-06-28 06:36:25 +00:00
#include "common.h"
#define EXECUTION_PERIOD_NSECS 1000000 // AppleWin: nExecutionPeriodUsec
2013-06-28 06:36:25 +00:00
2015-01-11 20:12:51 +00:00
#define DEBUG_TIMING (!defined(NDEBUG) && 0) // enable to print timing stats
#if DEBUG_TIMING
# define TIMING_LOG(...) LOG(__VA_ARGS__)
#else
# define TIMING_LOG(...)
#endif
#define DISK_MOTOR_QUIET_NSECS 2000000
static const unsigned int uCyclesPerLine = 65; // 25 cycles of HBL & 40 cycles of HBL'
static const unsigned int uVisibleLinesPerFrame = 64*3; // 192
static const unsigned int uLinesPerFrame = 262; // 64 in each third of the screen & 70 in VBL
static const unsigned int dwClksPerFrame = uCyclesPerLine * uLinesPerFrame; // 17030
double g_fCurrentCLK6502 = CLK_6502;
bool g_bFullSpeed = false; // HACK TODO FIXME : prolly shouldn't be global anymore -- don't think it's necessary for speaker/soundcore/etc anymore ...
uint64_t g_nCumulativeCycles = 0; // cumulative cycles since emulator (re)start
int g_nCpuCyclesFeedback = 0;
static unsigned int g_dwCyclesThisFrame = 0;
static bool alt_speed_enabled = false;
2015-01-11 20:12:51 +00:00
double cpu_scale_factor = 1.0;
double cpu_altscale_factor = 1.0;
bool auto_adjust_speed = true;
int gc_cycles_timer_0 = 0;
int gc_cycles_timer_1 = 0;
2015-01-11 20:12:51 +00:00
volatile uint8_t emul_reinitialize = 0;
2014-03-30 17:57:56 +00:00
pthread_t cpu_thread_id = 0;
static unsigned int g_nCyclesExecuted = 0; // # of cycles executed up to last IO access
2013-06-28 06:36:25 +00:00
// -----------------------------------------------------------------------------
struct timespec timespec_diff(struct timespec start, struct timespec end, bool *negative) {
2013-06-28 06:36:25 +00:00
struct timespec t;
2013-12-29 19:37:05 +00:00
if (negative)
{
*negative = false;
}
// if start > end, swizzle...
if ( (start.tv_sec > end.tv_sec) || ((start.tv_sec == end.tv_sec) && (start.tv_nsec > end.tv_nsec)) )
{
t=start;
start=end;
end=t;
if (negative)
{
*negative = true;
}
}
2013-06-28 06:36:25 +00:00
// 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 = 1000000000 + 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;
}
static inline struct timespec timespec_add(struct timespec start, unsigned long nsecs) {
start.tv_nsec += nsecs;
if (start.tv_nsec > NANOSECONDS)
{
start.tv_sec += (start.tv_nsec / NANOSECONDS);
start.tv_nsec %= NANOSECONDS;
}
return start;
}
static void _timing_initialize(double scale)
{
if (g_bFullSpeed)
{
2015-01-11 20:12:51 +00:00
TIMING_LOG("timing_initialize() emulation at fullspeed ...");
return;
}
g_fCurrentCLK6502 = CLK_6502 * scale;
// this is extracted out of SetClksPerSpkrSample -- speaker.c
#ifdef AUDIO_ENABLED
g_fClksPerSpkrSample = (double) (UINT) (g_fCurrentCLK6502 / (double)SPKR_SAMPLE_RATE);
SpkrReinitialize();
2015-01-11 20:12:51 +00:00
TIMING_LOG("timing_initialize() ... ClockRate:%0.2lf ClockCyclesPerSpeakerSample:%0.2lf", g_fCurrentCLK6502, g_fClksPerSpkrSample);
#endif
}
2015-01-11 20:12:51 +00:00
static inline void _switch_to_fullspeed(void)
{
2015-01-11 20:12:51 +00:00
g_bFullSpeed = true;
2013-06-28 06:36:25 +00:00
}
2015-01-11 20:12:51 +00:00
static inline void _switch_to_regular_speed(double scale)
{
2015-01-11 20:12:51 +00:00
g_bFullSpeed = false;
_timing_initialize(scale);
2013-06-28 06:36:25 +00:00
}
2015-01-11 20:12:51 +00:00
void timing_toggle_cpu_speed(void)
{
alt_speed_enabled = !alt_speed_enabled;
2015-01-11 20:12:51 +00:00
double scale_factor = alt_speed_enabled ? cpu_altscale_factor : cpu_scale_factor;
if (scale_factor >= CPU_SCALE_FASTEST)
{
2015-01-11 20:12:51 +00:00
_switch_to_fullspeed();
}
else
{
2015-01-11 20:12:51 +00:00
_switch_to_regular_speed(scale_factor);
}
}
2015-01-11 20:12:51 +00:00
void timing_initialize(void)
{
_timing_initialize(cpu_scale_factor);
}
void *cpu_thread(void *dummyptr) {
2014-03-30 17:57:56 +00:00
assert(pthread_self() == cpu_thread_id);
struct timespec deltat;
struct timespec t0; // the target timer
struct timespec ti, tj; // actual time samples
bool negative = false;
long drift_adj_nsecs = 0; // generic drift adjustment between target and actual
int debugging_cycles0 = 0;
int debugging_cycles = 0;
2013-07-23 07:33:29 +00:00
2015-01-11 20:12:51 +00:00
#if DEBUG_TIMING
unsigned long dbg_ticks = 0;
2013-11-17 23:01:23 +00:00
int speaker_neg_feedback = 0;
int speaker_pos_feedback = 0;
2013-12-29 19:39:34 +00:00
unsigned int dbg_cycles_executed = 0;
2013-11-17 23:01:23 +00:00
#endif
2013-07-23 07:33:29 +00:00
do
{
g_nCumulativeCycles = 0;
LOG("cpu_thread : begin main loop ...");
clock_gettime(CLOCK_MONOTONIC, &t0);
emul_reinitialize = 1;
do {
// -LOCK----------------------------------------------------------------------------------------- SAMPLE ti
pthread_mutex_lock(&interface_mutex);
clock_gettime(CLOCK_MONOTONIC, &ti);
deltat = timespec_diff(t0, ti, &negative);
if (deltat.tv_sec)
{
2015-01-11 20:12:51 +00:00
TIMING_LOG("NOTE : serious divergence from target time ...");
t0 = ti;
deltat = timespec_diff(t0, ti, &negative);
}
t0 = timespec_add(t0, EXECUTION_PERIOD_NSECS); // expected interval
drift_adj_nsecs = negative ? ~deltat.tv_nsec : deltat.tv_nsec;
// set up increment & decrement counters
cpu65_cycles_to_execute = (g_fCurrentCLK6502 / 1000); // g_fCurrentCLK6502 * EXECUTION_PERIOD_NSECS / NANOSECONDS
cpu65_cycles_to_execute += g_nCpuCyclesFeedback;
if (cpu65_cycles_to_execute < 0)
{
cpu65_cycles_to_execute = 0;
}
g_nCyclesExecuted = 0;
#ifdef AUDIO_ENABLED
MB_StartOfCpuExecute();
#endif
if (is_debugging) {
debugging_cycles0 = cpu65_cycles_to_execute;
debugging_cycles = cpu65_cycles_to_execute;
}
do {
if (is_debugging) {
cpu65_cycles_to_execute = 1;
}
cpu65_cycle_count = 0;
cpu65_run(); // run emulation for cpu65_cycles_to_execute cycles ...
if (is_debugging) {
debugging_cycles -= cpu65_cycle_count;
if (c_debugger_should_break() || (debugging_cycles <= 0)) {
int err = 0;
if ((err = pthread_cond_signal(&ui_thread_cond))) {
ERRLOG("pthread_cond_signal : %d", err);
}
if ((err = pthread_cond_wait(&cpu_thread_cond, &interface_mutex))) {
ERRLOG("pthread_cond_wait : %d", err);
}
if (debugging_cycles <= 0) {
cpu65_cycle_count = debugging_cycles0 - debugging_cycles/*<=0*/;
break;
}
}
}
if (emul_reinitialize) {
reinitialize();
}
} while (is_debugging);
2015-01-11 20:12:51 +00:00
#if DEBUG_TIMING
2013-12-29 19:39:34 +00:00
dbg_cycles_executed += cpu65_cycle_count;
#endif
unsigned int uActualCyclesExecuted = cpu65_cycle_count;
g_dwCyclesThisFrame += uActualCyclesExecuted;
#ifdef AUDIO_ENABLED
MB_UpdateCycles(uActualCyclesExecuted); // Update 6522s (NB. Do this before updating g_nCumulativeCycles below)
#endif
// N.B.: IO calls that depend on accurate timing will update g_nCyclesExecuted
const int nRemainingCycles = uActualCyclesExecuted - g_nCyclesExecuted;
assert(nRemainingCycles >= 0);
g_nCumulativeCycles += nRemainingCycles;
2015-01-12 02:37:57 +00:00
#if CPU_TRACING
cpu65_trace_checkpoint();
#endif
if (!g_bFullSpeed)
{
#ifdef AUDIO_ENABLED
SpkrUpdate(uActualCyclesExecuted); // play audio
#endif
}
if (g_dwCyclesThisFrame >= dwClksPerFrame)
{
g_dwCyclesThisFrame -= dwClksPerFrame;
//VideoEndOfVideoFrame();
#ifdef AUDIO_ENABLED
MB_EndOfVideoFrame();
#endif
}
clock_gettime(CLOCK_MONOTONIC, &tj);
pthread_mutex_unlock(&interface_mutex);
// -UNLOCK--------------------------------------------------------------------------------------- SAMPLE tj
if (auto_adjust_speed) {
deltat = timespec_diff(disk6.motor_time, tj, &negative);
assert(!negative);
if (!g_bFullSpeed &&
#ifdef AUDIO_ENABLED
!Spkr_IsActive() &&
#endif
!video_dirty() && (!disk6.motor_off && (deltat.tv_sec || deltat.tv_nsec > DISK_MOTOR_QUIET_NSECS)) )
{
TIMING_LOG("auto switching to full speed");
_switch_to_fullspeed();
} else if (g_bFullSpeed && (
#ifdef AUDIO_ENABLED
Spkr_IsActive() ||
#endif
video_dirty() || (disk6.motor_off && (deltat.tv_sec || deltat.tv_nsec > DISK_MOTOR_QUIET_NSECS))) )
{
TIMING_LOG("auto switching to regular speed");
_switch_to_regular_speed(alt_speed_enabled ? cpu_altscale_factor : cpu_scale_factor);
}
}
2015-01-11 20:12:51 +00:00
if (!g_bFullSpeed) {
deltat = timespec_diff(ti, tj, &negative);
assert(!negative);
long sleepfor = 0;
if (!deltat.tv_sec)
{
2015-01-11 20:12:51 +00:00
sleepfor = EXECUTION_PERIOD_NSECS - drift_adj_nsecs - deltat.tv_nsec;
}
2015-01-11 20:12:51 +00:00
if (sleepfor <= 0)
{
// lagging ...
static time_t throttle_warning = 0;
if (t0.tv_sec - throttle_warning > 0)
{
TIMING_LOG("lagging... %ld . %ld", deltat.tv_sec, deltat.tv_nsec);
throttle_warning = t0.tv_sec;
}
}
else
{
deltat.tv_sec = 0;
deltat.tv_nsec = sleepfor;
nanosleep(&deltat, NULL);
}
2013-11-17 23:01:23 +00:00
2015-01-11 20:12:51 +00:00
#if DEBUG_TIMING
// collect timing statistics
if (speaker_neg_feedback > g_nCpuCyclesFeedback)
{
speaker_neg_feedback = g_nCpuCyclesFeedback;
}
if (speaker_pos_feedback < g_nCpuCyclesFeedback)
{
speaker_pos_feedback = g_nCpuCyclesFeedback;
}
2013-11-17 23:01:23 +00:00
2015-01-11 20:12:51 +00:00
dbg_ticks += EXECUTION_PERIOD_NSECS;
if ((dbg_ticks % NANOSECONDS) == 0)
{
LOG("tick:(%ld.%ld) real:(%ld.%ld) cycles exe: %d ... speaker feedback: %d/%d", t0.tv_sec, t0.tv_nsec, ti.tv_sec, ti.tv_nsec, dbg_cycles_executed, speaker_neg_feedback, speaker_pos_feedback);
dbg_cycles_executed = 0;
dbg_ticks = 0;
speaker_neg_feedback = 0;
speaker_pos_feedback = 0;
}
#endif
2015-01-11 20:12:51 +00:00
}
} while (!emul_reinitialize);
reinitialize();
} while (1);
return NULL;
}
// From AppleWin...
unsigned int CpuGetCyclesThisVideoFrame(const unsigned int nExecutedCycles) {
CpuCalcCycles(nExecutedCycles);
return g_dwCyclesThisFrame + g_nCyclesExecuted;
}
// Called when an IO-reg is accessed & accurate cycle info is needed
void CpuCalcCycles(const unsigned long nExecutedCycles) {
// Calc # of cycles executed since this func was last called
const long nCycles = nExecutedCycles - g_nCyclesExecuted;
assert(nCycles >= 0);
g_nCumulativeCycles += nCycles;
2014-06-21 21:55:30 +00:00
// HACK FIXME TODO
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
g_nCyclesExecuted = nExecutedCycles;
2014-06-21 21:55:30 +00:00
#pragma clang diagnostic pop
2013-06-28 06:36:25 +00:00
}