1 |
/* |
2 |
* sysdeps.h - System dependent definitions for Linux |
3 |
* |
4 |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#ifndef SYSDEPS_H |
22 |
#define SYSDEPS_H |
23 |
|
24 |
#ifndef __STDC__ |
25 |
#error "Your compiler is not ANSI. Get a real one." |
26 |
#endif |
27 |
|
28 |
#include "config.h" |
29 |
#include "user_strings_unix.h" |
30 |
|
31 |
#ifndef STDC_HEADERS |
32 |
#error "You don't have ANSI C header files." |
33 |
#endif |
34 |
|
35 |
#ifdef HAVE_UNISTD_H |
36 |
# include <sys/types.h> |
37 |
# include <unistd.h> |
38 |
#endif |
39 |
|
40 |
#include <netinet/in.h> |
41 |
#include <assert.h> |
42 |
#include <stdio.h> |
43 |
#include <stdlib.h> |
44 |
#include <string.h> |
45 |
#include <signal.h> |
46 |
|
47 |
#ifdef HAVE_FCNTL_H |
48 |
# include <fcntl.h> |
49 |
#endif |
50 |
|
51 |
#ifdef TIME_WITH_SYS_TIME |
52 |
# include <sys/time.h> |
53 |
# include <time.h> |
54 |
#else |
55 |
# ifdef HAVE_SYS_TIME_H |
56 |
# include <sys/time.h> |
57 |
# else |
58 |
# include <time.h> |
59 |
# endif |
60 |
#endif |
61 |
|
62 |
// Define for external components |
63 |
#define SHEEPSHAVER 1 |
64 |
|
65 |
// Mac and host address space are the same |
66 |
#define REAL_ADDRESSING 1 |
67 |
|
68 |
#define POWERPC_ROM 1 |
69 |
|
70 |
#if EMULATED_PPC |
71 |
// Handle interrupts asynchronously? |
72 |
#define ASYNC_IRQ 0 |
73 |
// Mac ROM is write protected when banked memory is used |
74 |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
75 |
# define ROM_IS_WRITE_PROTECTED 0 |
76 |
# define USE_SCRATCHMEM_SUBTERFUGE 1 |
77 |
#else |
78 |
# define ROM_IS_WRITE_PROTECTED 1 |
79 |
#endif |
80 |
#else |
81 |
// Mac ROM is write protected |
82 |
#define ROM_IS_WRITE_PROTECTED 1 |
83 |
#define USE_SCRATCHMEM_SUBTERFUGE 0 |
84 |
#endif |
85 |
|
86 |
// Data types |
87 |
typedef unsigned char uint8; |
88 |
typedef signed char int8; |
89 |
#if SIZEOF_SHORT == 2 |
90 |
typedef unsigned short uint16; |
91 |
typedef short int16; |
92 |
#elif SIZEOF_INT == 2 |
93 |
typedef unsigned int uint16; |
94 |
typedef int int16; |
95 |
#else |
96 |
#error "No 2 byte type, you lose." |
97 |
#endif |
98 |
#if SIZEOF_INT == 4 |
99 |
typedef unsigned int uint32; |
100 |
typedef int int32; |
101 |
#elif SIZEOF_LONG == 4 |
102 |
typedef unsigned long uint32; |
103 |
typedef long int32; |
104 |
#else |
105 |
#error "No 4 byte type, you lose." |
106 |
#endif |
107 |
#if SIZEOF_LONG == 8 |
108 |
typedef unsigned long uint64; |
109 |
typedef long int64; |
110 |
#define VAL64(a) (a ## l) |
111 |
#define UVAL64(a) (a ## ul) |
112 |
#elif SIZEOF_LONG_LONG == 8 |
113 |
typedef unsigned long long uint64; |
114 |
typedef long long int64; |
115 |
#define VAL64(a) (a ## LL) |
116 |
#define UVAL64(a) (a ## uLL) |
117 |
#else |
118 |
#error "No 8 byte type, you lose." |
119 |
#endif |
120 |
#if SIZEOF_VOID_P == 4 |
121 |
typedef uint32 uintptr; |
122 |
typedef int32 intptr; |
123 |
#elif SIZEOF_VOID_P == 8 |
124 |
typedef uint64 uintptr; |
125 |
typedef int64 intptr; |
126 |
#else |
127 |
#error "Unsupported size of pointer" |
128 |
#endif |
129 |
|
130 |
// Helper functions to byteswap data |
131 |
#ifdef HAVE_BYTESWAP_H |
132 |
#include <byteswap.h> |
133 |
#endif |
134 |
|
135 |
#ifndef bswap_16 |
136 |
#define bswap_16 generic_bswap_16 |
137 |
#endif |
138 |
|
139 |
static inline uint16 generic_bswap_16(uint16 x) |
140 |
{ |
141 |
return ((x & 0xff) << 8) | ((x >> 8) & 0xff); |
142 |
} |
143 |
|
144 |
#ifndef bswap_32 |
145 |
#define bswap_32 generic_bswap_32 |
146 |
#endif |
147 |
|
148 |
static inline uint32 generic_bswap_32(uint32 x) |
149 |
{ |
150 |
return (((x & 0xff000000) >> 24) | |
151 |
((x & 0x00ff0000) >> 8) | |
152 |
((x & 0x0000ff00) << 8) | |
153 |
((x & 0x000000ff) << 24) ); |
154 |
} |
155 |
|
156 |
#ifndef bswap_64 |
157 |
#define bswap_64 generic_bswap_64 |
158 |
#endif |
159 |
|
160 |
static inline uint64 generic_bswap_64(uint64 x) |
161 |
{ |
162 |
return (((x & UVAL64(0xff00000000000000)) >> 56) | |
163 |
((x & UVAL64(0x00ff000000000000)) >> 40) | |
164 |
((x & UVAL64(0x0000ff0000000000)) >> 24) | |
165 |
((x & UVAL64(0x000000ff00000000)) >> 8) | |
166 |
((x & UVAL64(0x00000000ff000000)) << 8) | |
167 |
((x & UVAL64(0x0000000000ff0000)) << 24) | |
168 |
((x & UVAL64(0x000000000000ff00)) << 40) | |
169 |
((x & UVAL64(0x00000000000000ff)) << 56) ); |
170 |
} |
171 |
|
172 |
#ifdef WORDS_BIGENDIAN |
173 |
static inline uint16 tswap16(uint16 x) { return x; } |
174 |
static inline uint32 tswap32(uint32 x) { return x; } |
175 |
static inline uint64 tswap64(uint64 x) { return x; } |
176 |
#else |
177 |
static inline uint16 tswap16(uint16 x) { return bswap_16(x); } |
178 |
static inline uint32 tswap32(uint32 x) { return bswap_32(x); } |
179 |
static inline uint64 tswap64(uint64 x) { return bswap_64(x); } |
180 |
#endif |
181 |
|
182 |
// spin locks |
183 |
#ifdef __GNUC__ |
184 |
|
185 |
#ifdef __powerpc__ |
186 |
#define HAVE_TEST_AND_SET 1 |
187 |
static inline int testandset(int *p) |
188 |
{ |
189 |
int ret; |
190 |
__asm__ __volatile__("0: lwarx %0,0,%1 ;" |
191 |
" xor. %0,%3,%0;" |
192 |
" bne 1f;" |
193 |
" stwcx. %2,0,%1;" |
194 |
" bne- 0b;" |
195 |
"1: " |
196 |
: "=&r" (ret) |
197 |
: "r" (p), "r" (1), "r" (0) |
198 |
: "cr0", "memory"); |
199 |
return ret; |
200 |
} |
201 |
#endif |
202 |
|
203 |
#ifdef __i386__ |
204 |
#define HAVE_TEST_AND_SET 1 |
205 |
static inline int testandset(int *p) |
206 |
{ |
207 |
char ret; |
208 |
long int readval; |
209 |
|
210 |
__asm__ __volatile__("lock; cmpxchgl %3, %1; sete %0" |
211 |
: "=q" (ret), "=m" (*p), "=a" (readval) |
212 |
: "r" (1), "m" (*p), "a" (0) |
213 |
: "memory"); |
214 |
return ret; |
215 |
} |
216 |
#endif |
217 |
|
218 |
#ifdef __s390__ |
219 |
#define HAVE_TEST_AND_SET 1 |
220 |
static inline int testandset(int *p) |
221 |
{ |
222 |
int ret; |
223 |
|
224 |
__asm__ __volatile__("0: cs %0,%1,0(%2)\n" |
225 |
" jl 0b" |
226 |
: "=&d" (ret) |
227 |
: "r" (1), "a" (p), "0" (*p) |
228 |
: "cc", "memory" ); |
229 |
return ret; |
230 |
} |
231 |
#endif |
232 |
|
233 |
#ifdef __alpha__ |
234 |
#define HAVE_TEST_AND_SET 1 |
235 |
static inline int testandset(int *p) |
236 |
{ |
237 |
int ret; |
238 |
unsigned long one; |
239 |
|
240 |
__asm__ __volatile__("0: mov 1,%2\n" |
241 |
" ldl_l %0,%1\n" |
242 |
" stl_c %2,%1\n" |
243 |
" beq %2,1f\n" |
244 |
".subsection 2\n" |
245 |
"1: br 0b\n" |
246 |
".previous" |
247 |
: "=r" (ret), "=m" (*p), "=r" (one) |
248 |
: "m" (*p)); |
249 |
return ret; |
250 |
} |
251 |
#endif |
252 |
|
253 |
#ifdef __sparc__ |
254 |
#define HAVE_TEST_AND_SET 1 |
255 |
static inline int testandset(int *p) |
256 |
{ |
257 |
int ret; |
258 |
|
259 |
__asm__ __volatile__("ldstub [%1], %0" |
260 |
: "=r" (ret) |
261 |
: "r" (p) |
262 |
: "memory"); |
263 |
|
264 |
return (ret ? 1 : 0); |
265 |
} |
266 |
#endif |
267 |
|
268 |
#ifdef __arm__ |
269 |
#define HAVE_TEST_AND_SET 1 |
270 |
static inline int testandset(int *p) |
271 |
{ |
272 |
register unsigned int ret; |
273 |
__asm__ __volatile__("swp %0, %1, [%2]" |
274 |
: "=r"(ret) |
275 |
: "0"(1), "r"(p)); |
276 |
|
277 |
return ret; |
278 |
} |
279 |
#endif |
280 |
|
281 |
#endif /* __GNUC__ */ |
282 |
|
283 |
#if HAVE_TEST_AND_SET |
284 |
#define HAVE_SPINLOCKS 1 |
285 |
typedef int spinlock_t; |
286 |
|
287 |
static const spinlock_t SPIN_LOCK_UNLOCKED = 0; |
288 |
|
289 |
static inline void spin_lock(spinlock_t *lock) |
290 |
{ |
291 |
while (testandset(lock)); |
292 |
} |
293 |
|
294 |
static inline void spin_unlock(spinlock_t *lock) |
295 |
{ |
296 |
*lock = 0; |
297 |
} |
298 |
|
299 |
static inline int spin_trylock(spinlock_t *lock) |
300 |
{ |
301 |
return !testandset(lock); |
302 |
} |
303 |
#endif |
304 |
|
305 |
// Time data type for Time Manager emulation |
306 |
#ifdef HAVE_CLOCK_GETTIME |
307 |
typedef struct timespec tm_time_t; |
308 |
#else |
309 |
typedef struct timeval tm_time_t; |
310 |
#endif |
311 |
|
312 |
// Setup pthread attributes |
313 |
extern void Set_pthread_attr(pthread_attr_t *attr, int priority); |
314 |
|
315 |
// Various definitions |
316 |
typedef struct rgb_color { |
317 |
uint8 red; |
318 |
uint8 green; |
319 |
uint8 blue; |
320 |
uint8 alpha; |
321 |
} rgb_color; |
322 |
|
323 |
// Macro for calling MacOS routines |
324 |
#define CallMacOS(type, tvect) call_macos((uint32)tvect) |
325 |
#define CallMacOS1(type, tvect, arg1) call_macos1((uint32)tvect, (uint32)arg1) |
326 |
#define CallMacOS2(type, tvect, arg1, arg2) call_macos2((uint32)tvect, (uint32)arg1, (uint32)arg2) |
327 |
#define CallMacOS3(type, tvect, arg1, arg2, arg3) call_macos3((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3) |
328 |
#define CallMacOS4(type, tvect, arg1, arg2, arg3, arg4) call_macos4((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4) |
329 |
#define CallMacOS5(type, tvect, arg1, arg2, arg3, arg4, arg5) call_macos5((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5) |
330 |
#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) |
331 |
#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) |
332 |
|
333 |
#ifdef __cplusplus |
334 |
extern "C" { |
335 |
#endif |
336 |
extern uint32 call_macos(uint32 tvect); |
337 |
extern uint32 call_macos1(uint32 tvect, uint32 arg1); |
338 |
extern uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2); |
339 |
extern uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3); |
340 |
extern uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4); |
341 |
extern uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5); |
342 |
extern uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6); |
343 |
extern uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7); |
344 |
#ifdef __cplusplus |
345 |
} |
346 |
#endif |
347 |
|
348 |
#endif |