2013-07-07 21:01:21 +00:00
|
|
|
/*
|
2015-10-22 05:13:26 +00:00
|
|
|
* Apple // emulator for *ix
|
2013-07-07 21:01:21 +00:00
|
|
|
*
|
|
|
|
* This software package is subject to the GNU General Public License
|
2015-10-22 05:13:26 +00:00
|
|
|
* version 3 or later (your choice) as published by the Free Software
|
2013-07-07 21:01:21 +00:00
|
|
|
* Foundation.
|
|
|
|
*
|
2015-10-22 05:13:26 +00:00
|
|
|
* Copyright 2013-2015 Aaron Culliney
|
2013-07-07 21:01:21 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _COMMON_H_
|
|
|
|
#define _COMMON_H_
|
|
|
|
|
2013-10-23 04:18:03 +00:00
|
|
|
#if defined(__GNUC__) && !defined(_GNU_SOURCE)
|
2015-11-14 07:13:21 +00:00
|
|
|
# define _GNU_SOURCE 1
|
|
|
|
#endif
|
|
|
|
|
2015-11-14 19:09:33 +00:00
|
|
|
#ifdef __APPLE__
|
2015-11-14 07:13:21 +00:00
|
|
|
# warning DEFINING CUSTOM TEMP_FAILURE_RETRY(x) macro
|
|
|
|
# define TEMP_FAILURE_RETRY(exp) ({ \
|
|
|
|
typeof (exp) _rc; \
|
|
|
|
do { \
|
|
|
|
_rc = (exp); \
|
|
|
|
if (_rc == -1 && (errno == EINTR || errno == EAGAIN) ) { \
|
|
|
|
usleep(10); \
|
|
|
|
} else { \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (1); \
|
|
|
|
_rc; })
|
2013-10-07 04:01:00 +00:00
|
|
|
#endif
|
|
|
|
|
2015-07-12 20:59:44 +00:00
|
|
|
// custom annotations
|
2016-03-26 05:34:33 +00:00
|
|
|
#define INOUT
|
2015-07-12 20:59:44 +00:00
|
|
|
#define INPARM
|
2016-03-26 05:34:33 +00:00
|
|
|
#define _NONNULL
|
|
|
|
#define _NULLABLE
|
2015-07-12 20:59:44 +00:00
|
|
|
#define OUTPARM
|
|
|
|
#define PRIVATE
|
|
|
|
#define PUBLIC
|
|
|
|
#define READONLY
|
|
|
|
|
2014-01-23 04:42:34 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-07 21:01:21 +00:00
|
|
|
#include <stdlib.h>
|
2014-01-23 04:42:34 +00:00
|
|
|
#include <stdio.h>
|
2013-07-07 21:01:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2013-10-06 06:13:24 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <pthread.h>
|
2013-10-07 04:01:00 +00:00
|
|
|
#include <ctype.h>
|
2013-12-01 04:00:34 +00:00
|
|
|
#include <dirent.h>
|
2014-01-23 04:42:34 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
2015-04-27 01:12:56 +00:00
|
|
|
#include <math.h>
|
2014-01-23 04:42:34 +00:00
|
|
|
|
|
|
|
#include <zlib.h>
|
2013-07-07 21:01:21 +00:00
|
|
|
|
2013-11-24 20:19:54 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2013-12-01 04:00:34 +00:00
|
|
|
#include <sys/stat.h>
|
2013-11-24 20:19:54 +00:00
|
|
|
|
2016-02-07 21:12:59 +00:00
|
|
|
#include "json_parse.h"
|
2016-03-26 05:34:33 +00:00
|
|
|
#include "misc.h"
|
2015-09-11 07:00:04 +00:00
|
|
|
#include "vm.h"
|
2014-01-23 04:42:34 +00:00
|
|
|
#include "timing.h"
|
|
|
|
#include "cpu.h"
|
2016-02-07 05:23:40 +00:00
|
|
|
#include "display.h"
|
2014-01-23 04:42:34 +00:00
|
|
|
#include "disk.h"
|
|
|
|
#include "interface.h"
|
|
|
|
#include "keys.h"
|
|
|
|
#include "joystick.h"
|
|
|
|
#include "glue.h"
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "zlib-helpers.h"
|
|
|
|
|
2015-10-04 21:21:28 +00:00
|
|
|
#include "meta/trace.h"
|
|
|
|
|
2015-01-17 05:10:45 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include "darwin-shim.h"
|
|
|
|
#import <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
2014-09-17 03:49:57 +00:00
|
|
|
|
|
|
|
#if VIDEO_OPENGL
|
2014-10-08 04:26:50 +00:00
|
|
|
#include "video_util/glUtil.h"
|
2015-04-11 07:30:23 +00:00
|
|
|
// 2015/04/01 ... early calls to glGetError()--before a context exists--causes segfaults on MacOS X
|
2014-11-29 21:31:21 +00:00
|
|
|
extern bool safe_to_do_opengl_logging;
|
|
|
|
static inline GLenum safeGLGetError(void) {
|
|
|
|
if (safe_to_do_opengl_logging) {
|
|
|
|
return glGetError();
|
|
|
|
}
|
|
|
|
return (GLenum)0;
|
|
|
|
}
|
2014-09-17 03:49:57 +00:00
|
|
|
#else
|
2014-09-27 18:03:51 +00:00
|
|
|
#define GLenum int
|
2014-12-06 06:31:12 +00:00
|
|
|
#define safeGLGetError() 0
|
2014-09-17 03:49:57 +00:00
|
|
|
#define glGetError() 0
|
|
|
|
#endif
|
|
|
|
|
2014-01-23 04:42:34 +00:00
|
|
|
#ifdef DEBUGGER
|
|
|
|
#include "meta/debug.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ENABLED
|
|
|
|
#include "audio/soundcore.h"
|
|
|
|
#include "audio/speaker.h"
|
|
|
|
#include "audio/mockingboard.h"
|
|
|
|
#endif
|
|
|
|
|
2015-06-28 19:49:26 +00:00
|
|
|
#ifdef ANDROID
|
2015-07-11 21:37:41 +00:00
|
|
|
# include "../Android/jni/android_globals.h"
|
|
|
|
# define USE_SIMD 1
|
|
|
|
# define SIMD_IS_AVAILABLE() (android_armNeonEnabled/* || android_x86SSSE3Enabled*/)
|
|
|
|
#else
|
|
|
|
# define SIMD_IS_AVAILABLE()
|
2015-06-28 19:49:26 +00:00
|
|
|
#endif
|
|
|
|
|
2015-06-02 05:25:29 +00:00
|
|
|
#define PATH_SEPARATOR "/" // =P
|
|
|
|
|
2014-03-30 17:39:45 +00:00
|
|
|
#if !defined(MIN)
|
|
|
|
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(MAX)
|
|
|
|
#define MAX(a,b) (((a) >= (b)) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2015-07-03 05:24:14 +00:00
|
|
|
#define SPINLOCK_INIT 0
|
|
|
|
#define SPINLOCK_ACQUIRED -1
|
|
|
|
#define SPINLOCK_ACQUIRE(x) \
|
|
|
|
do { \
|
|
|
|
long val = __sync_sub_and_fetch((x), 1); \
|
|
|
|
if (val == SPINLOCK_ACQUIRED) { \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
__sync_add_and_fetch((x), 1); \
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
#define SPINLOCK_RELINQUISH(x) \
|
|
|
|
__sync_add_and_fetch((x), 1);
|
|
|
|
|
|
|
|
|
2015-10-04 21:39:53 +00:00
|
|
|
// cribbed from AOSP and modified with usleep() and to also ignore EAGAIN (should this be a different errno than EINTR)
|
|
|
|
#define TEMP_FAILURE_RETRY_FOPEN(exp) ({ \
|
|
|
|
typeof (exp) _rc; \
|
|
|
|
do { \
|
|
|
|
_rc = (exp); \
|
|
|
|
if (_rc == NULL && (errno == EINTR || errno == EAGAIN) ) { \
|
|
|
|
usleep(10); \
|
|
|
|
} else { \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (1); \
|
|
|
|
_rc; })
|
|
|
|
|
|
|
|
|
2014-01-26 06:10:33 +00:00
|
|
|
extern bool do_logging;
|
2014-06-14 18:17:34 +00:00
|
|
|
|
2015-02-16 02:35:32 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
static const char *log_end = "";
|
|
|
|
# include <android/log.h>
|
2015-02-16 04:08:01 +00:00
|
|
|
# define QUIT_FUNCTION(x) exit(x)
|
2015-02-16 02:35:32 +00:00
|
|
|
# define _LOG_CMD(str) __android_log_print(ANDROID_LOG_ERROR, "apple2ix", "%s", str)
|
|
|
|
#else
|
|
|
|
extern FILE *error_log;
|
|
|
|
static const char *log_end = "\n";
|
|
|
|
# define QUIT_FUNCTION(x) exit(x)
|
2015-11-14 07:13:21 +00:00
|
|
|
# define _LOG_CMD(str) \
|
|
|
|
do { \
|
|
|
|
if (UNLIKELY(!error_log)) { \
|
|
|
|
error_log = stderr; \
|
|
|
|
} \
|
|
|
|
fprintf(error_log, "%s", str); \
|
|
|
|
} while (0);
|
2015-02-16 02:35:32 +00:00
|
|
|
#endif
|
2014-09-17 03:49:57 +00:00
|
|
|
|
2015-08-22 06:41:53 +00:00
|
|
|
#define _MYFILE_ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
|
|
|
|
2014-06-14 18:17:34 +00:00
|
|
|
#define _LOG(...) \
|
2015-02-16 02:35:32 +00:00
|
|
|
do { \
|
|
|
|
int _err = errno; \
|
|
|
|
errno = 0; \
|
|
|
|
\
|
|
|
|
char *syserr_str = NULL; \
|
|
|
|
char *glerr_str = NULL; \
|
|
|
|
if (_err) { \
|
|
|
|
asprintf(&syserr_str, " (syserr:%s)", strerror(_err)); \
|
|
|
|
} \
|
|
|
|
if (_glerr) { \
|
|
|
|
asprintf(&glerr_str, " (glerr:%04X)", _glerr); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
char *buf0 = NULL; \
|
|
|
|
asprintf(&buf0, __VA_ARGS__); \
|
|
|
|
\
|
|
|
|
char *buf = NULL; \
|
2015-08-22 06:41:53 +00:00
|
|
|
asprintf(&buf, "%s:%d (%s) -%s%s %s%s", _MYFILE_, __LINE__, __func__, syserr_str ? : "", glerr_str ? : "", buf0, log_end); \
|
2015-02-16 02:35:32 +00:00
|
|
|
\
|
|
|
|
_LOG_CMD(buf); \
|
|
|
|
\
|
|
|
|
free(buf0); \
|
|
|
|
free(buf); \
|
|
|
|
if (syserr_str) { \
|
|
|
|
free(syserr_str); \
|
|
|
|
} \
|
|
|
|
if (glerr_str) { \
|
|
|
|
free(glerr_str); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2014-06-14 18:17:34 +00:00
|
|
|
|
2016-01-06 05:39:04 +00:00
|
|
|
#if !defined(NDEBUG) || (defined(NDEBUG) && defined(ANDROID))
|
2014-06-14 18:17:34 +00:00
|
|
|
|
2015-07-05 01:08:43 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
// Apparently some non-conformant Android devices (ahem, Spamsung, ahem) do not actually let me see what the assert
|
|
|
|
// actually was before aborting/segfaulting ...
|
|
|
|
# undef assert
|
|
|
|
# define assert(e) \
|
|
|
|
do { \
|
|
|
|
if ((e)) { \
|
|
|
|
/* ... */ \
|
|
|
|
} else { \
|
|
|
|
LOG( "!!! ASSERT !!! : " #e ); \
|
|
|
|
sleep(1); \
|
2015-08-22 06:41:53 +00:00
|
|
|
__assert2(_MYFILE_, __LINE__, __func__, #e); \
|
2015-07-05 01:08:43 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
2014-09-17 03:49:57 +00:00
|
|
|
#define LOG(...) \
|
2014-06-14 18:17:34 +00:00
|
|
|
if (do_logging) { \
|
2014-09-17 03:49:57 +00:00
|
|
|
errno = 0; \
|
2014-09-27 18:03:51 +00:00
|
|
|
GLenum _glerr = 0; \
|
2014-06-14 18:17:34 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-10-25 18:38:28 +00:00
|
|
|
} //
|
2013-07-07 21:01:21 +00:00
|
|
|
|
2014-09-17 03:49:57 +00:00
|
|
|
#define ERRLOG(...) \
|
2014-01-26 06:10:33 +00:00
|
|
|
if (do_logging) { \
|
2014-11-29 21:31:21 +00:00
|
|
|
GLenum _glerr = safeGLGetError(); \
|
2014-06-14 18:17:34 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-11-29 21:31:21 +00:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-27 18:03:51 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
|
|
|
} \
|
2014-10-25 18:38:28 +00:00
|
|
|
} //
|
2014-06-14 18:17:34 +00:00
|
|
|
|
2014-09-17 03:49:57 +00:00
|
|
|
#define GL_ERRLOG(...) \
|
2014-06-14 18:17:34 +00:00
|
|
|
if (do_logging) { \
|
2014-09-27 18:03:51 +00:00
|
|
|
GLenum _glerr = 0; \
|
2014-11-29 21:31:21 +00:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-17 03:49:57 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
|
|
|
} \
|
2014-10-25 18:38:28 +00:00
|
|
|
} //
|
2013-09-13 03:47:00 +00:00
|
|
|
|
2014-09-17 03:49:57 +00:00
|
|
|
#define ERRQUIT(...) \
|
|
|
|
do { \
|
2014-11-29 21:31:21 +00:00
|
|
|
GLenum _glerr = safeGLGetError(); \
|
2014-09-17 03:49:57 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-11-29 21:31:21 +00:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-27 18:03:51 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
|
|
|
} \
|
2014-09-17 03:49:57 +00:00
|
|
|
QUIT_FUNCTION(1); \
|
2014-10-25 18:38:28 +00:00
|
|
|
} while (0)
|
2014-09-17 03:49:57 +00:00
|
|
|
|
|
|
|
#define GL_ERRQUIT(...) \
|
|
|
|
do { \
|
2014-09-27 18:03:51 +00:00
|
|
|
GLenum _glerr = 0; \
|
2014-11-29 21:31:21 +00:00
|
|
|
while ( (_glerr = safeGLGetError()) ) { \
|
2014-09-27 18:03:51 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-09-17 03:49:57 +00:00
|
|
|
QUIT_FUNCTION(_glerr); \
|
|
|
|
} \
|
2014-10-25 18:38:28 +00:00
|
|
|
} while (0)
|
2014-09-17 03:49:57 +00:00
|
|
|
|
2013-07-07 21:01:21 +00:00
|
|
|
#else // NDEBUG
|
|
|
|
|
2013-10-06 06:13:24 +00:00
|
|
|
#define ERRLOG(...) \
|
2014-10-25 18:38:28 +00:00
|
|
|
do { } while (0)
|
2013-07-07 21:01:21 +00:00
|
|
|
|
2014-06-14 18:17:34 +00:00
|
|
|
#define ERRQUIT(...) \
|
2014-10-25 18:38:28 +00:00
|
|
|
do { } while (0)
|
2013-07-07 21:01:21 +00:00
|
|
|
|
2013-10-06 06:13:24 +00:00
|
|
|
#define LOG(...) \
|
2014-10-25 18:38:28 +00:00
|
|
|
do { } while (0)
|
|
|
|
|
|
|
|
#define GL_ERRLOG(...) \
|
|
|
|
do { } while (0)
|
|
|
|
|
|
|
|
#define GL_ERRQUIT(...) \
|
|
|
|
do { } while (0)
|
2014-06-14 18:17:34 +00:00
|
|
|
|
2014-09-17 03:49:57 +00:00
|
|
|
#endif // NDEBUG
|
2014-06-14 18:17:34 +00:00
|
|
|
|
|
|
|
#define RELEASE_ERRLOG(...) \
|
|
|
|
do { \
|
2014-10-25 18:38:28 +00:00
|
|
|
GLenum _glerr = 0; \
|
2014-06-14 18:17:34 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-10-25 18:38:28 +00:00
|
|
|
} while (0)
|
2014-06-14 18:17:34 +00:00
|
|
|
|
|
|
|
#define RELEASE_LOG(...) \
|
2013-10-06 06:13:24 +00:00
|
|
|
do { \
|
2014-11-29 21:31:21 +00:00
|
|
|
GLenum _glerr = safeGLGetError(); \
|
2013-10-06 06:13:24 +00:00
|
|
|
errno = 0; \
|
2014-06-14 18:17:34 +00:00
|
|
|
_LOG(__VA_ARGS__); \
|
2014-10-25 18:38:28 +00:00
|
|
|
} while (0)
|
2013-07-07 21:01:21 +00:00
|
|
|
|
2015-07-28 05:36:39 +00:00
|
|
|
#define RELEASE_BREAK() \
|
|
|
|
do { \
|
|
|
|
/* BOOM */ \
|
|
|
|
char *ptr = (char *)0xABADF000; \
|
|
|
|
*ptr = '\0';\
|
|
|
|
/* or if that worked, just deref NULL */ \
|
|
|
|
ptr = (char *)0x0; \
|
|
|
|
*ptr = '\0'; \
|
|
|
|
} while (0);
|
|
|
|
|
2015-12-31 06:16:57 +00:00
|
|
|
// memory management
|
|
|
|
#include "memmngt.h"
|
2014-09-27 18:03:51 +00:00
|
|
|
|
2015-01-24 07:34:03 +00:00
|
|
|
// branch prediction
|
|
|
|
#define LIKELY(x) __builtin_expect((x), true)
|
|
|
|
#define UNLIKELY(x) __builtin_expect((x), false)
|
|
|
|
|
2013-07-07 21:01:21 +00:00
|
|
|
#endif // whole file
|