1 |
|
/* |
2 |
|
* main_unix.cpp - Startup code for Unix |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2000 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2001 Christian Bauer |
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 |
30 |
|
# include <pthread.h> |
31 |
|
#endif |
32 |
|
|
33 |
< |
#if defined(USE_MAPPED_MEMORY) || REAL_ADDRESSING || DIRECT_ADDRESSING |
33 |
> |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
34 |
|
# include <sys/mman.h> |
35 |
|
#endif |
36 |
|
|
50 |
|
|
51 |
|
#ifdef ENABLE_GTK |
52 |
|
# include <gtk/gtk.h> |
53 |
+ |
# include <gdk/gdk.h> |
54 |
|
#endif |
55 |
|
|
56 |
|
#ifdef ENABLE_XF86_DGA |
161 |
|
static uint32 mapped_ram_rom_size; // Total size of mmap()ed RAM/ROM area |
162 |
|
#endif |
163 |
|
|
163 |
– |
#ifdef USE_MAPPED_MEMORY |
164 |
– |
extern char *address_space, *good_address_map; |
165 |
– |
#endif |
166 |
– |
|
164 |
|
|
165 |
|
// Prototypes |
166 |
|
static void *xpram_func(void *arg); |
209 |
|
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR); |
210 |
|
printf(" %s\n", GetString(STR_ABOUT_TEXT2)); |
211 |
|
|
212 |
< |
// Parse arguments |
212 |
> |
#ifdef ENABLE_GTK |
213 |
> |
// Init GTK |
214 |
> |
gtk_set_locale(); |
215 |
> |
gtk_init(&argc, &argv); |
216 |
> |
x_display_name = gdk_get_display(); // gtk_init() handles and removes the "--display" argument |
217 |
> |
#endif |
218 |
> |
|
219 |
> |
// Parse and remove arguments |
220 |
|
for (int i=1; i<argc; i++) { |
221 |
< |
if (strcmp(argv[i], "-display") == 0 && ++i < argc) |
222 |
< |
x_display_name = argv[i]; |
223 |
< |
else if (strcmp(argv[i], "-break") == 0 && ++i < argc) |
224 |
< |
ROMBreakpoint = strtol(argv[i], NULL, 0); |
225 |
< |
else if (strcmp(argv[i], "-rominfo") == 0) |
221 |
> |
if (strcmp(argv[i], "--display") == 0) { |
222 |
> |
argv[i] = NULL; |
223 |
> |
if ((i + 1) < argc && argv[i + 1]) { |
224 |
> |
argv[i++] = NULL; |
225 |
> |
x_display_name = strdup(argv[i]); |
226 |
> |
} |
227 |
> |
} else if (strcmp(argv[i], "--break") == 0) { |
228 |
> |
argv[i] = NULL; |
229 |
> |
if ((i + 1) < argc && argv[i + 1]) { |
230 |
> |
argv[i++] = NULL; |
231 |
> |
ROMBreakpoint = strtol(argv[i], NULL, 0); |
232 |
> |
} |
233 |
> |
} else if (strcmp(argv[i], "--rominfo") == 0) { |
234 |
> |
argv[i] = NULL; |
235 |
|
PrintROMInfo = true; |
236 |
+ |
} |
237 |
+ |
} |
238 |
+ |
for (int i=1; i<argc; i++) { |
239 |
+ |
int k; |
240 |
+ |
for (k=i; k<argc; k++) |
241 |
+ |
if (argv[k] != NULL) |
242 |
+ |
break; |
243 |
+ |
if (k > i) { |
244 |
+ |
k -= i; |
245 |
+ |
for (int j=i+k; j<argc; j++) |
246 |
+ |
argv[j-k] = argv[j]; |
247 |
+ |
argc -= k; |
248 |
+ |
} |
249 |
|
} |
250 |
|
|
251 |
|
// Open display |
262 |
|
XF86DGAForkApp(DefaultScreen(x_display)); |
263 |
|
#endif |
264 |
|
|
239 |
– |
#ifdef ENABLE_GTK |
240 |
– |
// Init GTK |
241 |
– |
gtk_set_locale(); |
242 |
– |
gtk_init(&argc, &argv); |
243 |
– |
#endif |
244 |
– |
|
265 |
|
// Read preferences |
266 |
< |
PrefsInit(); |
266 |
> |
PrefsInit(argc, argv); |
267 |
|
|
268 |
|
// Init system routines |
269 |
|
SysInit(); |
333 |
|
#endif |
334 |
|
|
335 |
|
// Create areas for Mac RAM and ROM |
336 |
< |
#if defined(USE_MAPPED_MEMORY) |
317 |
< |
good_address_map = (char *)mmap(NULL, 1<<24, PROT_READ, MAP_PRIVATE, zero_fd, 0); |
318 |
< |
address_space = (char *)mmap(NULL, 1<<24, PROT_READ | PROT_WRITE, MAP_PRIVATE, zero_fd, 0); |
319 |
< |
if ((int)address_space < 0 || (int)good_address_map < 0) { |
320 |
< |
ErrorAlert(GetString(STR_NOT_ENOUGH_MEMORY_ERR)); |
321 |
< |
QuitEmulator(); |
322 |
< |
} |
323 |
< |
RAMBaseHost = (uint8 *)mmap(address_space, RAMSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0); |
324 |
< |
ROMBaseHost = (uint8 *)mmap(address_space + 0x00400000, 0x80000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED, zero_fd, 0); |
325 |
< |
char *nam = tmpnam(NULL); |
326 |
< |
int good_address_fd = open(nam, O_CREAT | O_RDWR, 0600); |
327 |
< |
char buffer[4096]; |
328 |
< |
memset(buffer, 1, sizeof(buffer)); |
329 |
< |
write(good_address_fd, buffer, sizeof(buffer)); |
330 |
< |
unlink(nam); |
331 |
< |
for (int i=0; i<RAMSize; i+=4096) |
332 |
< |
mmap(good_address_map + i, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); |
333 |
< |
for (int i=0; i<0x80000; i+=4096) |
334 |
< |
mmap(good_address_map + i + 0x00400000, 4096, PROT_READ, MAP_FIXED | MAP_PRIVATE, good_address_fd, 0); |
335 |
< |
#elif REAL_ADDRESSING || DIRECT_ADDRESSING |
336 |
> |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
337 |
|
// gb-- Overkill, needs to be cleaned up. Probably explode it for either |
338 |
|
// real or direct addressing mode. |
339 |
|
#if REAL_ADDRESSING |
774 |
|
{ |
775 |
|
while (!xpram_thread_cancel) { |
776 |
|
for (int i=0; i<60 && !xpram_thread_cancel; i++) |
777 |
< |
Delay_usec(1000000); |
777 |
> |
Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true |
778 |
|
xpram_watchdog(); |
779 |
|
} |
780 |
|
return NULL; |
861 |
|
|
862 |
|
/* |
863 |
|
* Delay by specified number of microseconds (<1 second) |
864 |
< |
* (adapted from SDL_Delay() source) |
864 |
> |
* (adapted from SDL_Delay() source; this function is designed to provide |
865 |
> |
* the highest accuracy possible) |
866 |
|
*/ |
867 |
|
|
868 |
+ |
#if defined(linux) |
869 |
+ |
// Linux select() changes its timeout parameter upon return to contain |
870 |
+ |
// the remaining time. Most other unixen leave it unchanged or undefined. |
871 |
+ |
#define SELECT_SETS_REMAINING |
872 |
+ |
#elif defined(__FreeBSD__) || defined(__sun__) |
873 |
+ |
#define USE_NANOSLEEP |
874 |
+ |
#elif defined(HAVE_PTHREADS) && defined(sgi) |
875 |
+ |
// SGI pthreads has a bug when using pthreads+signals+nanosleep, |
876 |
+ |
// so instead of using nanosleep, wait on a CV which is never signalled. |
877 |
+ |
#define USE_COND_TIMEDWAIT |
878 |
+ |
#endif |
879 |
+ |
|
880 |
|
void Delay_usec(uint32 usec) |
881 |
|
{ |
882 |
|
int was_error; |
883 |
< |
#ifndef __linux__ // Non-Linux implementations need to calculate time left |
883 |
> |
|
884 |
> |
#if defined(USE_NANOSLEEP) |
885 |
> |
struct timespec elapsed, tv; |
886 |
> |
#elif defined(USE_COND_TIMEDWAIT) |
887 |
> |
// Use a local mutex and cv, so threads remain independent |
888 |
> |
pthread_cond_t delay_cond = PTHREAD_COND_INITIALIZER; |
889 |
> |
pthread_mutex_t delay_mutex = PTHREAD_MUTEX_INITIALIZER; |
890 |
> |
struct timespec elapsed; |
891 |
> |
uint64 future; |
892 |
> |
#else |
893 |
> |
struct timeval tv; |
894 |
> |
#ifndef SELECT_SETS_REMAINING |
895 |
|
uint64 then, now, elapsed; |
896 |
|
#endif |
897 |
< |
struct timeval tv; |
897 |
> |
#endif |
898 |
|
|
899 |
|
// Set the timeout interval - Linux only needs to do this once |
900 |
< |
#ifdef __linux__ |
901 |
< |
tv.tv_sec = 0; |
902 |
< |
tv.tv_usec = usec; |
900 |
> |
#if defined(SELECT_SETS_REMAINING) |
901 |
> |
tv.tv_sec = 0; |
902 |
> |
tv.tv_usec = usec; |
903 |
> |
#elif defined(USE_NANOSLEEP) |
904 |
> |
elapsed.tv_sec = 0; |
905 |
> |
elapsed.tv_nsec = usec * 1000; |
906 |
> |
#elif defined(USE_COND_TIMEDWAIT) |
907 |
> |
future = GetTicks_usec() + usec; |
908 |
> |
elapsed.tv_sec = future / 1000000; |
909 |
> |
elapsed.tv_nsec = (future % 1000000) * 1000; |
910 |
|
#else |
911 |
< |
then = GetTicks_usec(); |
911 |
> |
then = GetTicks_usec(); |
912 |
|
#endif |
913 |
+ |
|
914 |
|
do { |
915 |
|
errno = 0; |
916 |
< |
#ifndef __linux__ |
917 |
< |
/* Calculate the time interval left (in case of interrupt) */ |
916 |
> |
#if defined(USE_NANOSLEEP) |
917 |
> |
tv.tv_sec = elapsed.tv_sec; |
918 |
> |
tv.tv_nsec = elapsed.tv_nsec; |
919 |
> |
was_error = nanosleep(&tv, &elapsed); |
920 |
> |
#elif defined(USE_COND_TIMEDWAIT) |
921 |
> |
was_error = pthread_mutex_lock(&delay_mutex); |
922 |
> |
was_error = pthread_cond_timedwait(&delay_cond, &delay_mutex, &elapsed); |
923 |
> |
was_error = pthread_mutex_unlock(&delay_mutex); |
924 |
> |
#else |
925 |
> |
#ifndef SELECT_SETS_REMAINING |
926 |
> |
// Calculate the time interval left (in case of interrupt) |
927 |
|
now = GetTicks_usec(); |
928 |
|
elapsed = now - then; |
929 |
|
then = now; |
934 |
|
tv.tv_usec = usec; |
935 |
|
#endif |
936 |
|
was_error = select(0, NULL, NULL, NULL, &tv); |
937 |
+ |
#endif |
938 |
|
} while (was_error && (errno == EINTR)); |
939 |
|
} |
940 |
|
|