2013-07-06 04:37:13 +00:00
|
|
|
/*
|
2015-09-06 21:03:02 +00:00
|
|
|
* Apple // emulator for *nix
|
2013-06-28 06:36:25 +00:00
|
|
|
*
|
2013-10-06 06:22:08 +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
|
|
|
*
|
2013-10-06 06:22:08 +00:00
|
|
|
* THERE ARE NO WARRANTIES WHATSOEVER.
|
2013-07-06 04:37:13 +00:00
|
|
|
*
|
2013-06-28 06:36:25 +00:00
|
|
|
*/
|
|
|
|
|
2013-10-06 06:22:08 +00:00
|
|
|
/*
|
2015-01-31 21:57:10 +00:00
|
|
|
* 65c02 CPU timing support. Source inspired/derived from AppleWin.
|
2013-10-06 06:22:08 +00:00
|
|
|
*
|
2015-01-31 21:57:10 +00:00
|
|
|
* Simplified timing loop for each execution period:
|
|
|
|
*
|
|
|
|
* ..{...+....[....|..................|.........]....^....|....^....^....}......
|
|
|
|
* ti MBB CHK CHK MBE CHX SPK MBX tj ZZZ
|
2015-09-06 21:03:02 +00:00
|
|
|
*
|
2015-01-31 21:57:10 +00:00
|
|
|
* - ti : timing sample begin (lock out interface thread)
|
|
|
|
* - tj : timing sample end (unlock interface thread)
|
|
|
|
* - [ : cpu65_run()
|
|
|
|
* - ] : cpu65_run() finished
|
|
|
|
* - CHK : incoming timing_checkpoint_cycles() call from IO (bumps cycles_count_total)
|
|
|
|
* - CHX : update remainder of timing_checkpoint_cycles() for execution period
|
|
|
|
* - MBB : Mockingboard begin
|
|
|
|
* - MBE : Mockingboard end/flush (output)
|
|
|
|
* - MBX : Mockingboard end video frame (output)
|
|
|
|
* - SPK : Speaker output
|
|
|
|
* - ZZZ : housekeeping+sleep (or not)
|
2013-10-06 06:22:08 +00:00
|
|
|
*
|
|
|
|
*/
|
2013-06-28 06:36:25 +00:00
|
|
|
|
2014-01-23 04:42:34 +00:00
|
|
|
#include "common.h"
|
2013-10-06 06:22:08 +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
|
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
#define _LOCK_CPU_THREAD() \
|
|
|
|
if (pthread_self() != cpu_thread_id) { \
|
|
|
|
pthread_mutex_lock(&interface_mutex); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define _UNLOCK_CPU_THREAD() \
|
|
|
|
if (pthread_self() != cpu_thread_id) { \
|
|
|
|
pthread_mutex_unlock(&interface_mutex); \
|
|
|
|
}
|
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
// VBL constants?
|
|
|
|
#define uCyclesPerLine 65 // 25 cycles of HBL & 40 cycles of HBL'
|
|
|
|
#define uVisibleLinesPerFrame (64*3) // 192
|
|
|
|
#define uLinesPerFrame (262) // 64 in each third of the screen & 70 in VBL
|
|
|
|
#define dwClksPerFrame (uCyclesPerLine * uLinesPerFrame) // 17030
|
|
|
|
|
|
|
|
// cycle counting
|
|
|
|
double cycles_persec_target = CLK_6502;
|
|
|
|
unsigned long long cycles_count_total = 0;
|
|
|
|
int cycles_speaker_feedback = 0;
|
2015-02-04 06:10:47 +00:00
|
|
|
int32_t cpu65_cycles_to_execute = 0; // cycles-to-execute by cpu65_run()
|
|
|
|
int32_t cpu65_cycle_count = 0; // cycles currently excuted by cpu65_run()
|
|
|
|
static int32_t cycles_checkpoint_count = 0;
|
2015-01-10 17:53:38 +00:00
|
|
|
static unsigned int g_dwCyclesThisFrame = 0;
|
2013-10-06 08:31:58 +00:00
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
// scaling and speed adjustments
|
2015-03-23 02:14:35 +00:00
|
|
|
#if MOBILE_DEVICE
|
2015-07-26 20:38:43 +00:00
|
|
|
static bool is_paused = true;
|
2015-03-23 02:14:35 +00:00
|
|
|
#else
|
2015-07-26 20:38:43 +00:00
|
|
|
static bool is_paused = false;
|
2015-01-31 21:57:10 +00:00
|
|
|
static bool auto_adjust_speed = true;
|
2015-03-23 02:14:35 +00:00
|
|
|
#endif
|
2013-10-06 06:22:08 +00:00
|
|
|
double cpu_scale_factor = 1.0;
|
2013-10-06 08:31:58 +00:00
|
|
|
double cpu_altscale_factor = 1.0;
|
2015-01-31 21:57:10 +00:00
|
|
|
bool is_fullspeed = false;
|
2015-03-23 01:53:13 +00:00
|
|
|
bool alt_speed_enabled = false;
|
2013-12-21 21:55:40 +00:00
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
// misc
|
2015-07-26 20:38:43 +00:00
|
|
|
volatile uint8_t emul_reinitialize = 1;
|
2015-09-07 00:24:48 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
|
|
|
bool emul_reinitialize_audio = true;
|
|
|
|
#endif
|
2015-07-28 05:36:39 +00:00
|
|
|
#if MOBILE_DEVICE
|
|
|
|
static bool emul_reinitialize_background = true;
|
|
|
|
#endif
|
2015-09-11 07:00:04 +00:00
|
|
|
static bool cpu_shutting_down = false;
|
2014-03-30 17:57:56 +00:00
|
|
|
pthread_t cpu_thread_id = 0;
|
2015-02-07 22:23:16 +00:00
|
|
|
pthread_mutex_t interface_mutex = { 0 };
|
2015-02-16 16:46:29 +00:00
|
|
|
pthread_cond_t dbg_thread_cond = PTHREAD_COND_INITIALIZER;
|
2015-02-07 22:23:16 +00:00
|
|
|
pthread_cond_t cpu_thread_cond = PTHREAD_COND_INITIALIZER;
|
2013-11-06 06:11:27 +00:00
|
|
|
|
2013-06-28 06:36:25 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
2015-04-18 05:12:13 +00:00
|
|
|
__attribute__((constructor(CTOR_PRIORITY_LATE)))
|
2015-02-16 16:46:29 +00:00
|
|
|
static void _init_timing(void) {
|
2015-02-07 22:23:16 +00:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init(&attr);
|
|
|
|
pthread_mutex_init(&interface_mutex, &attr);
|
|
|
|
}
|
|
|
|
|
2013-10-06 06:22:08 +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)
|
2013-10-06 06:22:08 +00:00
|
|
|
{
|
|
|
|
*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 ...
|
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;
|
2013-10-06 06:22:08 +00:00
|
|
|
t.tv_nsec = 1000000000 + 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-10-06 06:22:08 +00:00
|
|
|
static inline struct timespec timespec_add(struct timespec start, unsigned long nsecs) {
|
|
|
|
|
|
|
|
start.tv_nsec += nsecs;
|
2015-01-31 21:57:10 +00:00
|
|
|
if (start.tv_nsec > NANOSECONDS_PER_SECOND)
|
2013-07-22 00:20:03 +00:00
|
|
|
{
|
2015-01-31 21:57:10 +00:00
|
|
|
start.tv_sec += (start.tv_nsec / NANOSECONDS_PER_SECOND);
|
|
|
|
start.tv_nsec %= NANOSECONDS_PER_SECOND;
|
2013-07-22 00:20:03 +00:00
|
|
|
}
|
2013-10-06 06:22:08 +00:00
|
|
|
|
|
|
|
return start;
|
2013-07-22 00:20:03 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
static void _timing_initialize(double scale) {
|
|
|
|
is_fullspeed = (scale >= CPU_SCALE_FASTEST);
|
|
|
|
if (!is_fullspeed) {
|
|
|
|
cycles_persec_target = CLK_6502 * scale;
|
2013-10-06 08:31:58 +00:00
|
|
|
}
|
2014-03-23 21:25:28 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-01-31 21:57:10 +00:00
|
|
|
speaker_reset();
|
2015-07-12 20:00:39 +00:00
|
|
|
//TIMING_LOG("ClockRate:%0.2lf ClockCyclesPerSpeakerSample:%0.2lf", cycles_persec_target, speaker_cyclesPerSample());
|
2014-03-23 21:25:28 +00:00
|
|
|
#endif
|
2013-07-22 00:20:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 21:03:02 +00:00
|
|
|
#if !TESTING
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
void reinitialize(void) {
|
|
|
|
#if !TESTING
|
|
|
|
assert(pthread_self() == cpu_thread_id);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cycles_count_total = 0;
|
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
vm_initialize();
|
2015-09-06 21:03:02 +00:00
|
|
|
|
|
|
|
softswitches = SS_TEXT | SS_IOUDIS | SS_C3ROM | SS_LCWRT | SS_LCSEC;
|
|
|
|
|
|
|
|
video_setpage( 0 );
|
|
|
|
|
|
|
|
video_redraw();
|
|
|
|
|
|
|
|
cpu65_init();
|
|
|
|
|
|
|
|
timing_initialize();
|
|
|
|
|
|
|
|
#ifdef AUDIO_ENABLED
|
|
|
|
MB_Reset();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
void timing_initialize(void) {
|
2015-09-07 04:03:59 +00:00
|
|
|
#if !TESTING
|
2015-07-26 20:38:43 +00:00
|
|
|
assert(cpu_isPaused() || (pthread_self() == cpu_thread_id));
|
2015-09-07 04:03:59 +00:00
|
|
|
#endif
|
2015-01-31 21:57:10 +00:00
|
|
|
_timing_initialize(alt_speed_enabled ? cpu_altscale_factor : cpu_scale_factor);
|
2013-06-28 06:36:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
void timing_toggleCPUSpeed(void) {
|
|
|
|
assert(cpu_isPaused() || (pthread_self() == cpu_thread_id));
|
2015-01-31 21:57:10 +00:00
|
|
|
alt_speed_enabled = !alt_speed_enabled;
|
|
|
|
timing_initialize();
|
2013-06-28 06:36:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 00:24:48 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-07-26 20:38:43 +00:00
|
|
|
void timing_reinitializeAudio(void) {
|
|
|
|
assert(cpu_isPaused() || (pthread_self() == cpu_thread_id));
|
2015-07-28 05:36:39 +00:00
|
|
|
emul_reinitialize_audio = true;
|
2013-10-06 08:31:58 +00:00
|
|
|
}
|
2015-09-07 00:24:48 +00:00
|
|
|
#endif
|
2013-07-26 05:58:31 +00:00
|
|
|
|
2015-07-05 01:17:04 +00:00
|
|
|
void cpu_pause(void) {
|
2015-07-28 05:36:39 +00:00
|
|
|
|
|
|
|
assert(pthread_self() != cpu_thread_id);
|
2015-07-26 20:38:43 +00:00
|
|
|
_LOCK_CPU_THREAD();
|
2015-07-28 05:36:39 +00:00
|
|
|
#if MOBILE_DEVICE
|
|
|
|
if (emul_reinitialize_background) {
|
|
|
|
RELEASE_LOG("CPU thread already paused ...");
|
|
|
|
_UNLOCK_CPU_THREAD();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-07-05 01:17:04 +00:00
|
|
|
audio_pause();
|
2015-07-26 20:38:43 +00:00
|
|
|
#endif
|
|
|
|
is_paused = true;
|
|
|
|
}
|
|
|
|
|
2015-07-28 05:36:39 +00:00
|
|
|
#if MOBILE_DEVICE
|
|
|
|
void cpu_pauseBackground(void) {
|
|
|
|
assert(pthread_self() != cpu_thread_id);
|
|
|
|
_LOCK_CPU_THREAD();
|
|
|
|
emul_reinitialize_background = true;
|
|
|
|
_UNLOCK_CPU_THREAD();
|
2015-07-05 01:17:04 +00:00
|
|
|
}
|
2015-07-28 05:36:39 +00:00
|
|
|
#endif
|
2015-07-05 01:17:04 +00:00
|
|
|
|
|
|
|
void cpu_resume(void) {
|
2015-07-28 05:36:39 +00:00
|
|
|
assert(pthread_self() != cpu_thread_id);
|
2015-07-26 20:38:43 +00:00
|
|
|
assert(cpu_isPaused());
|
|
|
|
is_paused = false;
|
|
|
|
|
2015-07-28 05:36:39 +00:00
|
|
|
#if MOBILE_DEVICE
|
|
|
|
int err = pthread_cond_signal(&cpu_thread_cond);
|
|
|
|
if (err) {
|
|
|
|
RELEASE_ERRLOG("pthread_cond_signal : %d", err);
|
|
|
|
RELEASE_BREAK();
|
2015-07-26 20:38:43 +00:00
|
|
|
}
|
2015-07-28 05:36:39 +00:00
|
|
|
#endif
|
2015-07-26 20:38:43 +00:00
|
|
|
|
|
|
|
#ifdef AUDIO_ENABLED
|
2015-07-05 01:17:04 +00:00
|
|
|
audio_resume();
|
2015-07-26 20:38:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_UNLOCK_CPU_THREAD();
|
2015-07-05 01:17:04 +00:00
|
|
|
}
|
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
bool cpu_isPaused(void) {
|
|
|
|
return is_paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !MOBILE_DEVICE
|
|
|
|
bool timing_shouldAutoAdjustSpeed(void) {
|
2015-01-31 21:57:10 +00:00
|
|
|
double speed = alt_speed_enabled ? cpu_altscale_factor : cpu_scale_factor;
|
|
|
|
return auto_adjust_speed && (speed < CPU_SCALE_FASTEST);
|
2013-10-06 06:22:08 +00:00
|
|
|
}
|
2015-07-26 20:38:43 +00:00
|
|
|
#endif
|
2013-10-06 06:22:08 +00:00
|
|
|
|
2015-09-06 21:03:02 +00:00
|
|
|
static void *cpu_thread(void *dummyptr) {
|
2014-03-30 17:57:56 +00:00
|
|
|
|
|
|
|
assert(pthread_self() == cpu_thread_id);
|
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
LOG("cpu_thread : initialized...");
|
2015-01-31 21:57:10 +00:00
|
|
|
|
2013-10-06 06:22:08 +00:00
|
|
|
struct timespec deltat;
|
2015-07-26 20:38:43 +00:00
|
|
|
#if !MOBILE_DEVICE
|
2015-01-31 21:57:10 +00:00
|
|
|
struct timespec disk_motor_time;
|
2015-07-26 20:38:43 +00:00
|
|
|
#endif
|
2013-10-06 06:22:08 +00:00
|
|
|
struct timespec t0; // the target timer
|
|
|
|
struct timespec ti, tj; // actual time samples
|
|
|
|
bool negative = false;
|
2014-03-22 18:24:57 +00:00
|
|
|
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
|
2013-10-06 06:22:08 +00:00
|
|
|
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
|
|
|
|
2013-10-06 06:22:08 +00:00
|
|
|
do
|
2013-07-22 00:20:03 +00:00
|
|
|
{
|
2015-09-07 04:03:59 +00:00
|
|
|
#if MOBILE_DEVICE
|
|
|
|
#if TESTING
|
|
|
|
emul_reinitialize_background = false;
|
|
|
|
#else
|
2015-07-28 05:36:39 +00:00
|
|
|
if (emul_reinitialize_background) {
|
|
|
|
|
|
|
|
speaker_destroy();
|
|
|
|
MB_Destroy();
|
|
|
|
audio_shutdown();
|
|
|
|
|
|
|
|
int err = TEMP_FAILURE_RETRY(pthread_mutex_lock(&interface_mutex));
|
|
|
|
if (err) {
|
|
|
|
RELEASE_LOG("Error locking CPU mutex : %d", err);
|
|
|
|
RELEASE_BREAK();
|
|
|
|
}
|
|
|
|
|
|
|
|
is_paused = true;
|
|
|
|
emul_reinitialize_background = false;
|
|
|
|
|
|
|
|
LOG("cpu_thread : waiting for splash screen completion...");
|
|
|
|
err = pthread_cond_wait(&cpu_thread_cond, &interface_mutex);
|
|
|
|
if (err) {
|
|
|
|
RELEASE_LOG("Error waiting for CPU condition : %d", err);
|
|
|
|
RELEASE_BREAK();
|
|
|
|
}
|
|
|
|
|
|
|
|
err = TEMP_FAILURE_RETRY(pthread_mutex_unlock(&interface_mutex));
|
|
|
|
if (err) {
|
|
|
|
RELEASE_LOG("Error unlocking CPU mutex : %d", err);
|
|
|
|
RELEASE_BREAK();
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("cpu_thread : starting...");
|
|
|
|
emul_reinitialize_audio = true;
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-07 04:03:59 +00:00
|
|
|
#endif
|
2015-07-28 05:36:39 +00:00
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-07-28 05:36:39 +00:00
|
|
|
if (emul_reinitialize_audio) {
|
|
|
|
emul_reinitialize_audio = false;
|
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
speaker_destroy();
|
|
|
|
MB_Destroy();
|
|
|
|
audio_shutdown();
|
|
|
|
|
|
|
|
audio_init();
|
|
|
|
speaker_init();
|
|
|
|
MB_Initialize();
|
|
|
|
}
|
|
|
|
#endif
|
2015-07-28 05:36:39 +00:00
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
if (emul_reinitialize) {
|
|
|
|
reinitialize();
|
|
|
|
}
|
|
|
|
|
2013-10-07 04:01:00 +00:00
|
|
|
LOG("cpu_thread : begin main loop ...");
|
2013-10-06 06:22:08 +00:00
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &t0);
|
2013-11-06 06:11:27 +00:00
|
|
|
|
2013-10-06 06:22:08 +00:00
|
|
|
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-31 21:57:10 +00:00
|
|
|
if (!is_fullspeed) {
|
|
|
|
TIMING_LOG("NOTE : serious divergence from target time ...");
|
|
|
|
}
|
2013-10-06 06:22:08 +00:00
|
|
|
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
|
2015-01-31 21:57:10 +00:00
|
|
|
cpu65_cycles_to_execute = (cycles_persec_target / 1000); // cycles_persec_target * EXECUTION_PERIOD_NSECS / NANOSECONDS_PER_SECOND
|
|
|
|
if (!is_fullspeed) {
|
|
|
|
cpu65_cycles_to_execute += cycles_speaker_feedback;
|
|
|
|
}
|
2013-10-06 06:22:08 +00:00
|
|
|
if (cpu65_cycles_to_execute < 0)
|
|
|
|
{
|
|
|
|
cpu65_cycles_to_execute = 0;
|
|
|
|
}
|
|
|
|
|
2014-01-23 04:42:34 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2013-11-06 06:11:27 +00:00
|
|
|
MB_StartOfCpuExecute();
|
2014-01-23 04:42:34 +00:00
|
|
|
#endif
|
2014-03-22 18:24:57 +00:00
|
|
|
if (is_debugging) {
|
|
|
|
debugging_cycles0 = cpu65_cycles_to_execute;
|
|
|
|
debugging_cycles = cpu65_cycles_to_execute;
|
|
|
|
}
|
2013-10-06 06:22:08 +00:00
|
|
|
|
2014-03-22 18:24:57 +00:00
|
|
|
do {
|
|
|
|
if (is_debugging) {
|
|
|
|
cpu65_cycles_to_execute = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpu65_cycle_count = 0;
|
2015-01-31 21:57:10 +00:00
|
|
|
cycles_checkpoint_count = 0;
|
2014-03-22 18:24:57 +00:00
|
|
|
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;
|
2015-02-16 16:46:29 +00:00
|
|
|
if ((err = pthread_cond_signal(&dbg_thread_cond))) {
|
2014-03-22 18:24:57 +00:00
|
|
|
ERRLOG("pthread_cond_signal : %d", err);
|
|
|
|
}
|
2014-03-30 18:17:12 +00:00
|
|
|
if ((err = pthread_cond_wait(&cpu_thread_cond, &interface_mutex))) {
|
2014-03-22 18:24:57 +00:00
|
|
|
ERRLOG("pthread_cond_wait : %d", err);
|
|
|
|
}
|
|
|
|
if (debugging_cycles <= 0) {
|
|
|
|
cpu65_cycle_count = debugging_cycles0 - debugging_cycles/*<=0*/;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-28 18:40:51 +00:00
|
|
|
if (emul_reinitialize) {
|
|
|
|
reinitialize();
|
|
|
|
}
|
2014-03-22 18:24:57 +00:00
|
|
|
} 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
|
2015-01-31 21:57:10 +00:00
|
|
|
g_dwCyclesThisFrame += cpu65_cycle_count;
|
2013-10-06 06:22:08 +00:00
|
|
|
|
2015-01-11 20:27:39 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-01-31 21:57:10 +00:00
|
|
|
MB_UpdateCycles(); // update 6522s (NOTE: do this before updating cycles_count_total)
|
2015-01-11 20:27:39 +00:00
|
|
|
#endif
|
2013-10-06 06:22:08 +00:00
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
timing_checkpoint_cycles();
|
2015-01-12 02:37:57 +00:00
|
|
|
#if CPU_TRACING
|
|
|
|
cpu65_trace_checkpoint();
|
|
|
|
#endif
|
2013-10-06 06:22:08 +00:00
|
|
|
|
2015-01-11 20:27:39 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-01-31 21:57:10 +00:00
|
|
|
speaker_flush(); // play audio
|
2015-01-10 17:53:38 +00:00
|
|
|
#endif
|
2013-10-06 06:22:08 +00:00
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
if (g_dwCyclesThisFrame >= dwClksPerFrame) {
|
2015-01-10 17:53:38 +00:00
|
|
|
g_dwCyclesThisFrame -= dwClksPerFrame;
|
|
|
|
#ifdef AUDIO_ENABLED
|
|
|
|
MB_EndOfVideoFrame();
|
2014-01-23 04:42:34 +00:00
|
|
|
#endif
|
2015-01-10 17:53:38 +00:00
|
|
|
}
|
2013-11-06 06:11:27 +00:00
|
|
|
|
2013-10-06 06:22:08 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &tj);
|
|
|
|
pthread_mutex_unlock(&interface_mutex);
|
|
|
|
// -UNLOCK--------------------------------------------------------------------------------------- SAMPLE tj
|
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
#if !MOBILE_DEVICE
|
|
|
|
if (timing_shouldAutoAdjustSpeed()) {
|
2015-01-31 21:57:10 +00:00
|
|
|
disk_motor_time = timespec_diff(disk6.motor_time, tj, &negative);
|
2015-01-11 20:27:39 +00:00
|
|
|
assert(!negative);
|
2015-01-31 21:57:10 +00:00
|
|
|
if (!is_fullspeed &&
|
2015-01-11 20:27:39 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-07-12 20:00:39 +00:00
|
|
|
!speaker_isActive() &&
|
2015-01-11 20:27:39 +00:00
|
|
|
#endif
|
2015-07-12 20:59:44 +00:00
|
|
|
!video_isDirty() && (!disk6.motor_off && (disk_motor_time.tv_sec || disk_motor_time.tv_nsec > DISK_MOTOR_QUIET_NSECS)) )
|
2015-01-11 20:27:39 +00:00
|
|
|
{
|
|
|
|
TIMING_LOG("auto switching to full speed");
|
2015-01-31 21:57:10 +00:00
|
|
|
_timing_initialize(CPU_SCALE_FASTEST);
|
2015-01-11 20:27:39 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-26 20:38:43 +00:00
|
|
|
#endif
|
2015-01-11 20:27:39 +00:00
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
if (!is_fullspeed) {
|
2015-01-11 20:12:51 +00:00
|
|
|
deltat = timespec_diff(ti, tj, &negative);
|
|
|
|
assert(!negative);
|
|
|
|
long sleepfor = 0;
|
|
|
|
if (!deltat.tv_sec)
|
2013-10-06 06:22:08 +00:00
|
|
|
{
|
2015-01-11 20:12:51 +00:00
|
|
|
sleepfor = EXECUTION_PERIOD_NSECS - drift_adj_nsecs - deltat.tv_nsec;
|
2013-10-06 06:22:08 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2015-07-05 20:17:41 +00:00
|
|
|
TIMING_LOG("not sleeping to catch up ... %ld . %ld", deltat.tv_sec, deltat.tv_nsec);
|
2015-01-11 20:12:51 +00:00
|
|
|
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
|
2015-01-31 21:57:10 +00:00
|
|
|
if (speaker_neg_feedback > cycles_speaker_feedback)
|
2015-01-11 20:12:51 +00:00
|
|
|
{
|
2015-01-31 21:57:10 +00:00
|
|
|
speaker_neg_feedback = cycles_speaker_feedback;
|
2015-01-11 20:12:51 +00:00
|
|
|
}
|
2015-01-31 21:57:10 +00:00
|
|
|
if (speaker_pos_feedback < cycles_speaker_feedback)
|
2015-01-11 20:12:51 +00:00
|
|
|
{
|
2015-01-31 21:57:10 +00:00
|
|
|
speaker_pos_feedback = cycles_speaker_feedback;
|
2015-01-11 20:12:51 +00:00
|
|
|
}
|
2013-11-17 23:01:23 +00:00
|
|
|
|
2015-01-11 20:12:51 +00:00
|
|
|
dbg_ticks += EXECUTION_PERIOD_NSECS;
|
2015-01-31 21:57:10 +00:00
|
|
|
if ((dbg_ticks % NANOSECONDS_PER_SECOND) == 0)
|
2015-01-11 20:12:51 +00:00
|
|
|
{
|
2015-01-31 21:57:10 +00:00
|
|
|
TIMING_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);
|
2015-01-11 20:12:51 +00:00
|
|
|
dbg_cycles_executed = 0;
|
|
|
|
dbg_ticks = 0;
|
|
|
|
speaker_neg_feedback = 0;
|
|
|
|
speaker_pos_feedback = 0;
|
|
|
|
}
|
2013-10-06 06:22:08 +00:00
|
|
|
#endif
|
2015-01-11 20:12:51 +00:00
|
|
|
}
|
2015-01-31 21:57:10 +00:00
|
|
|
|
2015-07-26 20:38:43 +00:00
|
|
|
#if !MOBILE_DEVICE
|
|
|
|
if (timing_shouldAutoAdjustSpeed()) {
|
2015-01-31 21:57:10 +00:00
|
|
|
if (is_fullspeed && (
|
|
|
|
#ifdef AUDIO_ENABLED
|
2015-07-12 20:00:39 +00:00
|
|
|
speaker_isActive() ||
|
2015-01-31 21:57:10 +00:00
|
|
|
#endif
|
2015-07-12 20:59:44 +00:00
|
|
|
video_isDirty() || (disk6.motor_off && (disk_motor_time.tv_sec || disk_motor_time.tv_nsec > DISK_MOTOR_QUIET_NSECS))) )
|
2015-01-31 21:57:10 +00:00
|
|
|
{
|
|
|
|
double speed = alt_speed_enabled ? cpu_altscale_factor : cpu_scale_factor;
|
|
|
|
if (speed < CPU_SCALE_FASTEST) {
|
|
|
|
TIMING_LOG("auto switching to configured speed");
|
|
|
|
_timing_initialize(speed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-26 20:38:43 +00:00
|
|
|
#endif
|
2015-05-31 19:59:26 +00:00
|
|
|
|
|
|
|
if (UNLIKELY(emul_reinitialize)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-07 00:24:48 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
2015-07-26 20:38:43 +00:00
|
|
|
if (UNLIKELY(emul_reinitialize_audio)) {
|
|
|
|
break;
|
|
|
|
}
|
2015-09-07 00:24:48 +00:00
|
|
|
#endif
|
2015-07-26 20:38:43 +00:00
|
|
|
|
2015-07-28 05:36:39 +00:00
|
|
|
#if MOBILE_DEVICE
|
|
|
|
if (UNLIKELY(emul_reinitialize_background)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
if (UNLIKELY(cpu_shutting_down)) {
|
2015-05-31 19:59:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
if (UNLIKELY(cpu_shutting_down)) {
|
2015-05-31 19:59:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-10-06 06:22:08 +00:00
|
|
|
} while (1);
|
2014-09-17 03:39:08 +00:00
|
|
|
|
2015-05-31 19:59:26 +00:00
|
|
|
#ifdef AUDIO_ENABLED
|
|
|
|
speaker_destroy();
|
|
|
|
MB_Destroy();
|
2015-06-14 22:05:44 +00:00
|
|
|
audio_shutdown();
|
2015-05-31 19:59:26 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-17 03:39:08 +00:00
|
|
|
return NULL;
|
2013-10-06 06:22:08 +00:00
|
|
|
}
|
2013-07-26 05:58:31 +00:00
|
|
|
|
2015-09-06 21:03:02 +00:00
|
|
|
void timing_startCPU(void) {
|
2015-09-11 07:00:04 +00:00
|
|
|
cpu_shutting_down = false;
|
2015-09-06 21:03:02 +00:00
|
|
|
video_init();
|
|
|
|
pthread_create(&cpu_thread_id, NULL, (void *)&cpu_thread, (void *)NULL);
|
|
|
|
}
|
|
|
|
|
2015-09-11 07:00:04 +00:00
|
|
|
void timing_stopCPU(void) {
|
|
|
|
cpu_shutting_down = true;
|
|
|
|
|
|
|
|
LOG("Emulator waiting for CPU thread clean up...");
|
|
|
|
if (pthread_join(cpu_thread_id, NULL)) {
|
|
|
|
ERRLOG("OOPS: pthread_join of CPU thread ...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
unsigned int CpuGetCyclesThisVideoFrame(void) {
|
|
|
|
timing_checkpoint_cycles();
|
|
|
|
return g_dwCyclesThisFrame + cycles_checkpoint_count;
|
2015-01-10 17:53:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 21:57:10 +00:00
|
|
|
// Called when an IO-reg is accessed & accurate global cycle count info is needed
|
|
|
|
void timing_checkpoint_cycles(void) {
|
2015-02-07 22:23:16 +00:00
|
|
|
assert(pthread_self() == cpu_thread_id);
|
|
|
|
|
2015-02-04 06:10:47 +00:00
|
|
|
const int32_t d = cpu65_cycle_count - cycles_checkpoint_count;
|
2015-01-31 21:57:10 +00:00
|
|
|
assert(d >= 0);
|
|
|
|
cycles_count_total += d;
|
|
|
|
cycles_checkpoint_count = cpu65_cycle_count;
|
2013-06-28 06:36:25 +00:00
|
|
|
}
|
2013-07-22 00:20:03 +00:00
|
|
|
|