--- SheepShaver/src/Unix/sysdeps.h 2002/02/21 15:12:12 1.2 +++ SheepShaver/src/Unix/sysdeps.h 2003/12/31 18:16:55 1.19 @@ -59,15 +59,37 @@ # endif #endif -// Are we using a PPC emulator or the real thing? -#ifdef __powerpc__ -#define EMULATED_PPC 0 -#else -#define EMULATED_PPC 1 -#endif +// Define for external components +#define SHEEPSHAVER 1 + +// Mac and host address space are the same +#define REAL_ADDRESSING 1 #define POWERPC_ROM 1 +#if EMULATED_PPC +// Handle interrupts asynchronously? +#define ASYNC_IRQ 0 +// Mac ROM is write protected when banked memory is used +#if REAL_ADDRESSING || DIRECT_ADDRESSING +# define ROM_IS_WRITE_PROTECTED 0 +# define USE_SCRATCHMEM_SUBTERFUGE 1 +#else +# define ROM_IS_WRITE_PROTECTED 1 +#endif +// Configure PowerPC emulator +#define PPC_CHECK_INTERRUPTS (ASYNC_IRQ ? 0 : 1) +#define PPC_DECODE_CACHE 1 +#define PPC_FLIGHT_RECORDER 1 +#define PPC_PROFILE_COMPILE_TIME 0 +#define PPC_PROFILE_GENERIC_CALLS 0 +#define KPX_MAX_CPUS 1 +#else +// Mac ROM is write protected +#define ROM_IS_WRITE_PROTECTED 1 +#define USE_SCRATCHMEM_SUBTERFUGE 0 +#endif + // Data types typedef unsigned char uint8; typedef signed char int8; @@ -92,12 +114,228 @@ typedef long int32; #if SIZEOF_LONG == 8 typedef unsigned long uint64; typedef long int64; +#define VAL64(a) (a ## l) +#define UVAL64(a) (a ## ul) #elif SIZEOF_LONG_LONG == 8 typedef unsigned long long uint64; typedef long long int64; +#define VAL64(a) (a ## LL) +#define UVAL64(a) (a ## uLL) #else #error "No 8 byte type, you lose." #endif +#if SIZEOF_VOID_P == 4 +typedef uint32 uintptr; +typedef int32 intptr; +#elif SIZEOF_VOID_P == 8 +typedef uint64 uintptr; +typedef int64 intptr; +#else +#error "Unsupported size of pointer" +#endif + +/** + * Helper functions to byteswap data + **/ + +#if defined(__GNUC__) +#if defined(__x86_64__) +// Linux/AMD64 currently has no asm optimized bswap_32() in +#define opt_bswap_32 do_opt_bswap_32 +static inline uint32 do_opt_bswap_32(uint32 x) +{ + uint32 v; + __asm__ __volatile__ ("bswap %0" : "=r" (v) : "0" (x)); + return v; +} +#endif +#endif + +#ifdef HAVE_BYTESWAP_H +#include +#endif + +#ifdef opt_bswap_16 +#undef bswap_16 +#define bswap_16 opt_bswap_16 +#endif +#ifndef bswap_16 +#define bswap_16 generic_bswap_16 +#endif + +static inline uint16 generic_bswap_16(uint16 x) +{ + return ((x & 0xff) << 8) | ((x >> 8) & 0xff); +} + +#ifdef opt_bswap_32 +#undef bswap_32 +#define bswap_32 opt_bswap_32 +#endif +#ifndef bswap_32 +#define bswap_32 generic_bswap_32 +#endif + +static inline uint32 generic_bswap_32(uint32 x) +{ + return (((x & 0xff000000) >> 24) | + ((x & 0x00ff0000) >> 8) | + ((x & 0x0000ff00) << 8) | + ((x & 0x000000ff) << 24) ); +} + +#ifdef opt_bswap_64 +#undef bswap_64 +#define bswap_64 opt_bswap_64 +#endif +#ifndef bswap_64 +#define bswap_64 generic_bswap_64 +#endif + +static inline uint64 generic_bswap_64(uint64 x) +{ + return (((x & UVAL64(0xff00000000000000)) >> 56) | + ((x & UVAL64(0x00ff000000000000)) >> 40) | + ((x & UVAL64(0x0000ff0000000000)) >> 24) | + ((x & UVAL64(0x000000ff00000000)) >> 8) | + ((x & UVAL64(0x00000000ff000000)) << 8) | + ((x & UVAL64(0x0000000000ff0000)) << 24) | + ((x & UVAL64(0x000000000000ff00)) << 40) | + ((x & UVAL64(0x00000000000000ff)) << 56) ); +} + +#ifdef WORDS_BIGENDIAN +static inline uint16 tswap16(uint16 x) { return x; } +static inline uint32 tswap32(uint32 x) { return x; } +static inline uint64 tswap64(uint64 x) { return x; } +#else +static inline uint16 tswap16(uint16 x) { return bswap_16(x); } +static inline uint32 tswap32(uint32 x) { return bswap_32(x); } +static inline uint64 tswap64(uint64 x) { return bswap_64(x); } +#endif + +// spin locks +#ifdef __GNUC__ + +#ifdef __powerpc__ +#define HAVE_TEST_AND_SET 1 +static inline int testandset(int *p) +{ + int ret; + __asm__ __volatile__("0: lwarx %0,0,%1 ;" + " xor. %0,%3,%0;" + " bne 1f;" + " stwcx. %2,0,%1;" + " bne- 0b;" + "1: " + : "=&r" (ret) + : "r" (p), "r" (1), "r" (0) + : "cr0", "memory"); + return ret; +} +#endif + +#ifdef __i386__ +#define HAVE_TEST_AND_SET 1 +static inline int testandset(int *p) +{ + char ret; + long int readval; + + __asm__ __volatile__("lock; cmpxchgl %3, %1; sete %0" + : "=q" (ret), "=m" (*p), "=a" (readval) + : "r" (1), "m" (*p), "a" (0) + : "memory"); + return ret; +} +#endif + +#ifdef __s390__ +#define HAVE_TEST_AND_SET 1 +static inline int testandset(int *p) +{ + int ret; + + __asm__ __volatile__("0: cs %0,%1,0(%2)\n" + " jl 0b" + : "=&d" (ret) + : "r" (1), "a" (p), "0" (*p) + : "cc", "memory" ); + return ret; +} +#endif + +#ifdef __alpha__ +#define HAVE_TEST_AND_SET 1 +static inline int testandset(int *p) +{ + int ret; + unsigned long one; + + __asm__ __volatile__("0: mov 1,%2\n" + " ldl_l %0,%1\n" + " stl_c %2,%1\n" + " beq %2,1f\n" + ".subsection 2\n" + "1: br 0b\n" + ".previous" + : "=r" (ret), "=m" (*p), "=r" (one) + : "m" (*p)); + return ret; +} +#endif + +#ifdef __sparc__ +#define HAVE_TEST_AND_SET 1 +static inline int testandset(int *p) +{ + int ret; + + __asm__ __volatile__("ldstub [%1], %0" + : "=r" (ret) + : "r" (p) + : "memory"); + + return (ret ? 1 : 0); +} +#endif + +#ifdef __arm__ +#define HAVE_TEST_AND_SET 1 +static inline int testandset(int *p) +{ + register unsigned int ret; + __asm__ __volatile__("swp %0, %1, [%2]" + : "=r"(ret) + : "0"(1), "r"(p)); + + return ret; +} +#endif + +#endif /* __GNUC__ */ + +#if HAVE_TEST_AND_SET +#define HAVE_SPINLOCKS 1 +typedef int spinlock_t; + +static const spinlock_t SPIN_LOCK_UNLOCKED = 0; + +static inline void spin_lock(spinlock_t *lock) +{ + while (testandset(lock)); +} + +static inline void spin_unlock(spinlock_t *lock) +{ + *lock = 0; +} + +static inline int spin_trylock(spinlock_t *lock) +{ + return !testandset(lock); +} +#endif // Time data type for Time Manager emulation #ifdef HAVE_CLOCK_GETTIME @@ -106,6 +344,10 @@ typedef struct timespec tm_time_t; typedef struct timeval tm_time_t; #endif +// Timing functions +extern uint64 GetTicks_usec(void); +extern void Delay_usec(uint32 usec); + // Setup pthread attributes extern void Set_pthread_attr(pthread_attr_t *attr, int priority); @@ -117,6 +359,25 @@ typedef struct rgb_color { uint8 alpha; } rgb_color; +// X11 display fast locks +#ifdef HAVE_SPINLOCKS +#define X11_LOCK_TYPE spinlock_t +#define X11_LOCK_INIT SPIN_LOCK_UNLOCKED +#define XDisplayLock() spin_lock(&x_display_lock) +#define XDisplayUnlock() spin_unlock(&x_display_lock) +#elif defined(HAVE_PTHREADS) +#define X11_LOCK_TYPE pthread_mutex_t +#define X11_LOCK_INIT PTHREAD_MUTEX_INITIALIZER +#define XDisplayLock() pthread_mutex_lock(&x_display_lock); +#define XDisplayUnlock() pthread_mutex_unlock(&x_display_lock); +#else +#define XDisplayLock() +#define XDisplayUnlock() +#endif +#ifdef X11_LOCK_TYPE +extern X11_LOCK_TYPE x_display_lock; +#endif + // Macro for calling MacOS routines #define CallMacOS(type, tvect) call_macos((uint32)tvect) #define CallMacOS1(type, tvect, arg1) call_macos1((uint32)tvect, (uint32)arg1) @@ -127,13 +388,19 @@ typedef struct rgb_color { #define CallMacOS6(type, tvect, arg1, arg2, arg3, arg4, arg5, arg6) call_macos6((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5, (uint32)arg6) #define CallMacOS7(type, tvect, arg1, arg2, arg3, arg4, arg5, arg6, arg7) call_macos7((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5, (uint32)arg6, (uint32)arg7) -extern "C" uint32 call_macos(uint32 tvect); -extern "C" uint32 call_macos1(uint32 tvect, uint32 arg1); -extern "C" uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2); -extern "C" uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3); -extern "C" uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4); -extern "C" uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5); -extern "C" uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6); -extern "C" uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7); +#ifdef __cplusplus +extern "C" { +#endif +extern uint32 call_macos(uint32 tvect); +extern uint32 call_macos1(uint32 tvect, uint32 arg1); +extern uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2); +extern uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3); +extern uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4); +extern uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5); +extern uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6); +extern uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7); +#ifdef __cplusplus +} +#endif #endif