1 #ifndef UAE_SYSDEPS_H
2 #define UAE_SYSDEPS_H
3 
4 /*
5   * UAE - The Un*x Amiga Emulator
6   *
7   * Try to include the right system headers and get other system-specific
8   * stuff right & other collected kludges.
9   *
10   * If you think about modifying this, think twice. Some systems rely on
11   * the exact order of the #include statements. That's also the reason
12   * why everything gets included unconditionally regardless of whether
13   * it's actually needed by the .c file.
14   *
15   * Copyright 1996, 1997 Bernd Schmidt
16   */
17 
18 #if defined(__cplusplus)
19 #include <cstddef>
20 #include <cstdbool>
21 #else
22 #include <stddef.h>
23 /* Note: stdbool.h has a __cplusplus section, but as it is stated in
24  * GNU gcc stdbool.h:
25  * "Supporting <stdbool.h> in C++ is a GCC extension."
26  */
27 #if defined(HAVE_STDBOOL_H)
28 #    include <stdbool.h>
29 #  else
30 #    ifndef HAVE__BOOL
31 #      define _Bool signed char
32 #    endif
33 #    define bool _Bool
34 #    define false 0
35 #    define true 1
36 #    define __bool_true_false_are_defined 1
37 #  endif // HAVE_STDBOOL_H
38 #endif // __cplusplus
39 
40 #define UAE_RAND_MAX RAND_MAX
41 #define ECS_DENISE
42 
43 #ifdef JIT
44 #define NATMEM_OFFSET natmem_offset
45 #else
46 #undef NATMEM_OFFSET
47 #  undef JIT_DEBUG
48 #  undef USE_UDIS86
49 #endif
50 
51 /* Add some useful macros for debugging. If JIT_DEBUG
52  * is not defined, these macros do nothing.
53  * Note: These are defined here, because some other parts
54  * like inputrecord.c do make use of them.
55  */
56 #if defined(JIT_DEBUG) && defined(__GNUC__)
57 # define JITLOG(fmt, ...) { \
58 	char trace_info[1024]; \
59 	snprintf(trace_info, 256, "[JIT] %s:%d - %s : %s\n", basename(__FILE__), __LINE__, __FUNCTION__, fmt); \
60 	write_log(trace_info, __VA_ARGS__); \
61 }
62 # if defined(USE_UDIS86)
63 #   include <udis86.h>
64 #   if defined(__x86_64__)
65 #	  define UD_MODE 64
66 #   else
67 #	  define UD_MODE 32
68 #   endif // __x86_64__
69 #   define UDISFN(udis_func, udis_end) { \
70 	int dSize = (int)((uaecptr)(udis_end) - (uaecptr)(udis_func)); \
71 	if (dSize > 0) { \
72 		uint8_t* p = (uint8_t*)(udis_func); \
73 		for ( ; dSize && (!p[dSize-1] || (0x90 == p[dSize-1])); --dSize) ; /* Find ending */ \
74 		JITLOG("Disassembling %s (size %u bytes) @ 0x%p:", #udis_func, dSize, p) \
75 		for (int i = 0; i < dSize; i += 0x10) { \
76 			write_log("%08x ", i); \
77 			for (int j = 0; j < 16; ++j) \
78 				write_log("%s%02x", 8==j ? "  " : " ", p[i + j]); \
79 			write_log("\n"); \
80 		} \
81 		ud_t ud_obj; \
82 		ud_init(&ud_obj); \
83 		ud_set_input_buffer(&ud_obj, p, dSize); \
84 		ud_set_mode(&ud_obj, UD_MODE); \
85 		ud_set_syntax(&ud_obj, UD_SYN_INTEL); \
86 		while (dSize > 0) { \
87 				dSize -= ud_disassemble(&ud_obj); \
88 				JITLOG("%s", ud_insn_asm(&ud_obj)); \
89 		} \
90 	} else \
91 		JITLOG("Can't dissassemble %s, start (0x%08lx) is larger than end (0x%08lx)", \
92 				#udis_func, (uaecptr)udis_func, (uaecptr)udis_end) \
93 }
94 # else
95 #   define UDISFN(...) {}
96 # endif // defined(USE_UDIS86)
97 #else
98 # define JITLOG(...) {}
99 # define UDISFN(...) {}
100 #endif
101 
102 #if defined __AMIGA__ || defined __amiga__
103 #include <devices/timer.h>
104 #endif
105 
106 #if defined(__cplusplus)
107 #include <cstdio>
108 #include <cstdlib>
109 #include <cerrno>
110 #include <cassert>
111 #include <climits>
112 #else
113 #include <stdio.h>
114 #include <stdlib.h>
115 #include <errno.h>
116 #include <assert.h>
117 #include <limits.h>
118 #endif // __cplusplus
119 
120 #ifndef __STDC__
121 #  ifdef _MSC_VER
122 #    error "M$ is no longer supported. Use WinUAE instead, it's great!"
123 #  else
124 #error "Your compiler is not ANSI. Get a real one."
125 #endif
126 #endif
127 
128 #if defined(__cplusplus)
129 #include <cstdarg>
130 #else
131 #include <stdarg.h>
132 #endif // __cplusplus
133 
134 #ifdef HAVE_SYS_TYPES_H
135 #include <sys/types.h>
136 #endif
137 
138 #ifdef HAVE_VALUES_H
139 #include <values.h>
140 #endif
141 
142 #include "uae_string.h"
143 
144 #ifdef HAVE_UNISTD_H
145 #include <unistd.h>
146 #endif
147 #ifdef HAVE_FCNTL_H
148 #include <fcntl.h>
149 #endif
150 
151 #ifdef HAVE_UTIME_H
152 #include <utime.h>
153 #endif
154 
155 #ifdef HAVE_SYS_STAT_H
156 #include <sys/stat.h>
157 #ifndef stat64
158 #define stat64 stat
159 #endif
160 #endif
161 
162 #if TIME_WITH_SYS_TIME
163 # include <sys/time.h>
164 #  if defined(__cplusplus)
165 #    include <ctime>
166 #  else
167 # include <time.h>
168 #  endif // __cplusplus
169 #else
170 # if HAVE_SYS_TIME_H
171 #  include <sys/time.h>
172 # else
173 #    if defined(__cplusplus)
174 #      include <ctime>
175 #    else
176 #  include <time.h>
177 #    endif // __cplusplus
178 # endif
179 #endif
180 
181 #if HAVE_DIRENT_H
182 # include <dirent.h>
183 #else
184 # define dirent direct
185 # if HAVE_SYS_NDIR_H
186 #  include <sys/ndir.h>
187 # endif
188 # if HAVE_SYS_DIR_H
189 #  include <sys/dir.h>
190 # endif
191 # if HAVE_NDIR_H
192 #  include <ndir.h>
193 # endif
194 #endif
195 
196 #ifdef HAVE_SYS_UTIME_H
197 # include <sys/utime.h>
198 #endif
199 
200 #if EEXIST == ENOTEMPTY
201 #define BROKEN_OS_PROBABLY_AIX
202 #endif
203 
204 #ifdef __NeXT__
205 #define S_IRUSR S_IREAD
206 #define S_IWUSR S_IWRITE
207 #define S_IXUSR S_IEXEC
208 #define S_ISDIR(val) (S_IFDIR & val)
209 struct utimbuf
210 {
211     time_t actime;
212     time_t modtime;
213 };
214 #endif
215 
216 #include "uae_types.h"
217 #include "uae_malloc.h"
218 #include "writelog.h"
219 
220 // Support macros for working with pointers of variable size -> 32bit.
221 #ifdef __x86_64__
222 #  if defined(JIT_DEBUG)
jit_debug_to_uae_u32(uaecptr ptr,const char * macro,const char * file,const int line,const char * func)223 static uae_u32 jit_debug_to_uae_u32(uaecptr ptr, const char* macro, const char* file, const int line, const char* func)
224 {
225 	uae_u32 result = (uae_u32)ptr;
226 	if ((uaecptr)result != ptr) {
227 		write_log("%s:%d - %s : %s - Pointer Truncated! 0x%08x %08x\n",
228 				file, line, func, macro, (uae_u32)(ptr >> 32), result);
229 	}
230 	return result;
231 }
232 #    define PTR_TO_UINT32(ptr) jit_debug_to_uae_u32((uaecptr)ptr, "PTR_TO_UINT32", \
233 										basename(__FILE__), __LINE__, __FUNCTION__)
234 #  else
235 #    define PTR_TO_UINT32(ptr) ((uae_u32)(uaecptr)(ptr))
236 #  endif
237 #else
238 #  define PTR_TO_UINT32(ptr) ((uae_u32)(ptr))
239 #endif // __x86_64__
240 #define VALUE_TO_PTR(val)  ((uaecptr)(val))
241 #define PTR_OFFSET(src, dst) (((uaecptr)(dst)) - ((uaecptr)(src)))
242 #define NATMEM_ADDRESS     PTR_TO_UINT32(NATMEM_OFFSET)
243 
244 /* The following macro allows to execute machine code directly
245  * without the need of (illegal) object<->function pointer conversions.
246  */
247 /// @todo : Add compiler/OS checks
248 #ifdef __GNUC__
249 #  define CALL_CODE_DIRECT(ptr) __asm__("call *%0" : : "a" (ptr));
250 #else
251 #  define CALL_CODE_DIRECT(ptr) ((compop_func*)ptr)(0);
252 #endif // __GNUC__
253 
254 
255 #ifdef __GNUC__
256 /* While we're here, make abort more useful.  */
257 #ifndef __MORPHOS__
258 /* This fails to compile on Morphos - not sure why yet */
259 #    define abort() { \
260 			write_log ("Internal error; file %s, line %d\n", __FILE__, __LINE__); \
261 			exit (0); \
262 		} // no need for a do-while!
263 #else
264 #define abort() exit(0)
265 #endif
266 #endif
267 
268 /*
269  * Porters to weird systems, look! This is the preferred way to get
270  * filesys.c (and other stuff) running on your system. Define the
271  * appropriate macros and implement wrappers in a machine-specific file.
272  *
273  * I guess the Mac port could use this (Ernesto?)
274  */
275 
276 #undef DONT_HAVE_POSIX
277 #undef DONT_HAVE_REAL_POSIX /* define if open+delete doesn't do what it should */
278 #undef DONT_HAVE_STDIO
279 #undef DONT_HAVE_MALLOC
280 
281 #if defined(WARPUP)
282 #define DONT_HAVE_POSIX
283 #endif
284 
285 #define FILEFLAG_DIR     0x1
286 #define FILEFLAG_ARCHIVE 0x2
287 #define FILEFLAG_WRITE   0x4
288 #define FILEFLAG_READ    0x8
289 #define FILEFLAG_EXECUTE 0x10
290 #define FILEFLAG_SCRIPT  0x20
291 #define FILEFLAG_PURE    0x40
292 
293 #  if defined(__cplusplus)
294 extern "C" {
295 #  endif
296 
297 #ifdef DONT_HAVE_POSIX
298 #define access posixemu_access
299 extern int posixemu_access (const char *, int);
300 #define open posixemu_open
301 extern int posixemu_open (const char *, int, int);
302 #define close posixemu_close
303 extern void posixemu_close (int);
304 #define read posixemu_read
305 extern int posixemu_read (int, char *, int);
306 #define write posixemu_write
307 extern int posixemu_write (int, const char *, int);
308 #undef lseek
309 #define lseek posixemu_seek
310 extern int posixemu_seek (int, int, int);
311 #define stat(a,b) posixemu_stat ((a), (b))
312 extern int posixemu_stat (const char *, STAT *);
313 #define mkdir posixemu_mkdir
314 extern int mkdir (const char *, int);
315 #define rmdir posixemu_rmdir
316 extern int posixemu_rmdir (const char *);
317 #define unlink posixemu_unlink
318 extern int posixemu_unlink (const char *);
319 #define truncate posixemu_truncate
320 extern int posixemu_truncate (const char *, long int);
321 #define rename posixemu_rename
322 extern int posixemu_rename (const char *, const char *);
323 #define chmod posixemu_chmod
324 extern int posixemu_chmod (const char *, int);
325 #define tmpnam posixemu_tmpnam
326 extern void posixemu_tmpnam (char *);
327 #define utime posixemu_utime
328 extern int posixemu_utime (const char *, struct utimbuf *);
329 #define opendir posixemu_opendir
330 extern DIR * posixemu_opendir (const char *);
331 #define readdir posixemu_readdir
332 extern struct dirent* readdir (DIR *);
333 #define closedir posixemu_closedir
334 extern void closedir (DIR *);
335 
336 /* This isn't the best place for this, but it fits reasonably well. The logic
337  * is that you probably don't have POSIX errnos if you don't have the above
338  * functions. */
339 extern long dos_errno (void);
340 #endif
341 
342 #ifdef DONT_HAVE_STDIO
343 extern FILE *stdioemu_fopen (const char *, const char *);
344 #define fopen(a,b) stdioemu_fopen(a, b)
345 extern int stdioemu_fseek (FILE *, int, int);
346 #define fseek(a,b,c) stdioemu_fseek(a, b, c)
347 extern int stdioemu_fread (char *, int, int, FILE *);
348 #define fread(a,b,c,d) stdioemu_fread(a, b, c, d)
349 extern int stdioemu_fwrite (const char *, int, int, FILE *);
350 #define fwrite(a,b,c,d) stdioemu_fwrite(a, b, c, d)
351 extern int stdioemu_ftell (FILE *);
352 #define ftell(a) stdioemu_ftell(a)
353 extern int stdioemu_fclose (FILE *);
354 #define fclose(a) stdioemu_fclose(a)
355 #endif
356 
357 #ifdef DONT_HAVE_MALLOC
358 #define malloc(a) mallocemu_malloc(a)
359 extern void *mallocemu_malloc (int size);
360 #define free(a) mallocemu_free(a)
361 extern void mallocemu_free (void *ptr);
362 #endif
363 
364 #ifdef X86_ASSEMBLY
365 #define ASM_SYM_FOR_FUNC(a) __asm__(a)
366 #else
367 #define ASM_SYM_FOR_FUNC(a)
368 #endif
369 
370 #ifdef __CELLOS_LV2__
371 #define timezone 0
372 #define daylight 0
373 #endif
374 
375 #ifdef ANDROID
376 #include "targets/t-android.h"
377 #else
378 #include "target.h"
379 #endif
380 
381 
382 //#include "target.h"
383 #if !defined(RECUR) && !defined(NO_MACHDEP)
384 #include "machdep/machdep.h"
385 #include "gfxdep/gfx.h"
386 #endif
387 
388 extern void console_out (const char *, ...);
389 extern void console_flush (void);
390 extern int  console_get (char *, int);
391 extern void f_out (void *, const char *, ...);
392 extern void gui_message (const char *,...);
393 extern int gui_message_multibutton (int flags, const char *format,...);
394 #define write_log_err write_log
395 
396 #if defined(__cplusplus)
397 }
398 #endif
399 
400 #ifndef O_BINARY
401 #define O_BINARY 0
402 #endif
403 
404 #ifndef STATIC_INLINE
405 #if __GNUC__ - 1 > 1 && __GNUC_MINOR__ - 1 >= 0
406 #define STATIC_INLINE static __inline__ __attribute__ ((always_inline))
407 #define NOINLINE __attribute__ ((noinline))
408 #define NORETURN __attribute__ ((noreturn))
409 #else
410 #define STATIC_INLINE static __inline__
411 #define NOINLINE
412 #define NORETURN
413 #endif
414 #endif
415 
416 #ifndef MAX_PATH
417 #define MAX_PATH	512
418 #endif
419 #ifndef MAX_DPATH
420 #define MAX_DPATH	512
421 #endif
422 
423 #ifndef __cplusplus
424 #define xmalloc(type, num) ((type*)malloc(sizeof (type) * (num)))
425 #define xcalloc(type, num) ((type*)calloc(sizeof (type), num))
426 #define xrealloc(type, buffer, num) ((type*)realloc(buffer, sizeof (type) * (num)))
427 #else
428 #define xmalloc(type, num) static_cast<type*>(malloc (sizeof (type) * (num)))
429 #define xcalloc(type, num) static_cast<type*>(calloc (sizeof (type), num))
430 #define xrealloc(type, buffer, num) static_cast<type*>(realloc (buffer, sizeof (type) * (num)))
431 #endif /* ! __cplusplus */
432 
433 #define XFREE_DEBUG 0
434 
435 // Please do NOT use identifiers with two leading underscores.
436 // See C++99 Standard chapter 7.1.3 "Reserved identifiers"
437 // #define __xfree(buffer) { free(buffer); buffer = NULL; }
438 #define xfree_d(buffer) { free(buffer); buffer = NULL; }
439 
440 #if XFREE_DEBUG > 0
441 # define xfree(buffer) { \
442 	if (buffer) { \
443 		xfree_d (buffer) \
444 	} else { \
445 		fprintf (stderr, "NULL pointer freed at %s:%d %s\n", \
446 				__FILE__, __LINE__, __FUNCTION__); \
447 	} }
448 #else
449 # define xfree(buffer) { if (buffer) { xfree_d (buffer) } }
450 #endif //xfree_debug
451 
452 #define DBLEQU(f, i) (abs ((f) - (i)) < 0.000001)
453 
454 #ifndef _WIN32
455 #define TCHAR char
456 #endif
457 #define REGPARAM3
458 #define uae_char char
459 #define _tcslen strlen
460 #define _tcscpy strcpy
461 #define _tcscmp strcmp
462 #define _tcsncat strncat
463 #define _tcsncmp strncmp
464 #define _tstol atol
465 #define _totupper toupper
466 #define _stprintf sprintf
467 #define _tcscat strcat
468 #define _tcsicmp strcasecmp
469 #define _tcsnicmp strncasecmp
470 #define _tcsstr strstr
471 #define _tcsrchr strrchr
472 #define _tcsncpy strncpy
473 #define _tcschr strchr
474 #define _tstof atof
475 #define _istdigit isdigit
476 #define _istspace isspace
477 #define _tstoi atoi
478 #define _tcstol strtol
479 #define _wunlink unlink
480 #define _tcsftime strftime
481 #define vsntprintf vsnprint
482 #define _tcsspn strspn
483 #define _istupper isupper
484 #define _totlower tolower
485 #define _tcstok strtok
486 #define _wunlink unlink
487 #define _tfopen fopen
488 #define _vsntprintf vsnprintf
489 #ifndef max
490 #define max(a,b) ((a) > (b) ? (a) : (b))
491 #endif
492 #define _tcstod strtod
493 #define _T
494 #define sleep_millis uae_msleep
495 
496 #define _istalnum iswalnum
497 #ifndef _WIN32
498 #define ULONG unsigned long
499 #endif
500 #define _strtoui64 strtoul
501 #define _tcscspn(wcs, reject) wcscspn((const wchar_t*)(wcs), (const wchar_t*)(reject))
502 
503 #ifndef _stat64
504 #define _stat64 stat64
505 #endif /* _stat64 */
506 
507 #ifndef offsetof
508 #  define offsetof(type, member)  __builtin_offsetof (type, member)
509 #endif /* offsetof */
510 #ifdef _WIN32
511 #define WIN32_LEAN_AND_MEAN
512 #include <windows.h>
513 #else
514 typedef int HANDLE;
515 typedef unsigned long DWORD;
516 typedef unsigned short WORD;
517 typedef long long LONGLONG;
518 DWORD GetLastError(void);
519 #endif
520 #ifndef ANDROID
521 typedef int64_t off64_t;
522 #endif
523 
524 
525 #endif /* UAE_SYSDEPS_H */
526