ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.4
Committed: 1999-10-26T16:28:37Z (25 years ago) by cebix
Branch: MAIN
Changes since 1.3: +33 -0 lines
Log Message:
- mon is called for illegal EMUL_OP selectors and when pressing Ctrl-C (Unix)
- moved MemoryDispatch() patch routine from PatchAfterStartup() to
  InstallDrivers()
- fixed one place where ROM replaces MemoryDispatch() by unimplemented trap
  when no MMU is present
- Unix: ROM breakpoint can now be set with "-break" command line argument
- some changes to configure script, mon is now compiled with readline support

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_unix.cpp - Startup code for Unix
3     *
4     * Basilisk II (C) 1997-1999 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
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     #include "sysdeps.h"
22    
23     #include <stdio.h>
24     #include <stdlib.h>
25     #include <pthread.h>
26     #include <signal.h>
27    
28     #include "cpu_emulation.h"
29     #include "sys.h"
30 cebix 1.3 #include "rom_patches.h"
31 cebix 1.1 #include "xpram.h"
32     #include "timer.h"
33     #include "video.h"
34     #include "prefs.h"
35     #include "prefs_editor.h"
36     #include "macos_util.h"
37     #include "user_strings.h"
38     #include "version.h"
39     #include "main.h"
40    
41 cebix 1.3 #define DEBUG 0
42 cebix 1.1 #include "debug.h"
43    
44    
45     #include <X11/Xlib.h>
46    
47     #if ENABLE_GTK
48     #include <gtk/gtk.h>
49     #endif
50    
51     #if ENABLE_DGA
52     #include <X11/Xlib.h>
53     #include <X11/Xutil.h>
54     #include <X11/extensions/xf86dga.h>
55     #endif
56    
57 cebix 1.4 #if ENABLE_MON
58     #include "mon.h"
59     #endif
60    
61 cebix 1.1
62     // Constants
63     const char ROM_FILE_NAME[] = "ROM";
64    
65    
66     // CPU and FPU type, addressing mode
67     int CPUType;
68     bool CPUIs68060;
69     int FPUType;
70     bool TwentyFourBitAddressing;
71    
72    
73     // Global variables
74     static char *x_display_name = NULL; // X11 display name
75     Display *x_display = NULL; // X11 display handle
76    
77     static bool xpram_thread_active = false; // Flag: XPRAM watchdog installed
78     static volatile bool xpram_thread_cancel = false; // Flag: Cancel XPRAM thread
79     static pthread_t xpram_thread; // XPRAM watchdog
80    
81     static bool tick_thread_active = false; // Flag: 60Hz thread installed
82     static volatile bool tick_thread_cancel = false; // Flag: Cancel 60Hz thread
83     static pthread_t tick_thread; // 60Hz thread
84     static pthread_attr_t tick_thread_attr; // 60Hz thread attributes
85    
86     static pthread_mutex_t intflag_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect InterruptFlags
87    
88     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
89     #define SIG_TIMER SIGRTMIN
90     static struct sigaction timer_sa; // sigaction used for timer
91     static timer_t timer; // 60Hz timer
92     #endif
93    
94 cebix 1.4 #if ENABLE_MON
95     static struct sigaction sigint_sa; // sigaction for SIGINT handler
96     static void sigint_handler(...);
97     #endif
98    
99 cebix 1.1
100     // Prototypes
101     static void *xpram_func(void *arg);
102     static void *tick_func(void *arg);
103     static void one_tick(...);
104    
105    
106     /*
107     * Ersatz functions
108     */
109    
110     extern "C" {
111    
112     #ifndef HAVE_STRDUP
113     char *strdup(const char *s)
114     {
115     char *n = (char *)malloc(strlen(s) + 1);
116     strcpy(n, s);
117     return n;
118     }
119     #endif
120    
121     }
122    
123    
124     /*
125     * Main program
126     */
127    
128     int main(int argc, char **argv)
129     {
130     // Initialize variables
131     RAMBaseHost = NULL;
132     ROMBaseHost = NULL;
133     srand(time(NULL));
134     tzset();
135    
136     // Print some info
137     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
138     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
139    
140     // Parse arguments
141     for (int i=1; i<argc; i++) {
142     if (strcmp(argv[i], "-display") == 0 && ++i < argc)
143     x_display_name = argv[i];
144 cebix 1.4 else if (strcmp(argv[i], "-break") == 0 && ++i < argc)
145     ROMBreakpoint = strtol(argv[i], NULL, 0);
146 cebix 1.1 }
147    
148     // Open display
149     x_display = XOpenDisplay(x_display_name);
150     if (x_display == NULL) {
151     char str[256];
152     sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
153     ErrorAlert(str);
154     QuitEmulator();
155     }
156    
157     #if ENABLE_DGA
158     // Fork out, so we can return from fullscreen mode when things get ugly
159 cebix 1.2 XF86DGAForkApp(DefaultScreen(x_display));
160 cebix 1.1 #endif
161    
162     #if ENABLE_GTK
163     // Init GTK
164     gtk_set_locale();
165     gtk_init(&argc, &argv);
166     #endif
167    
168     // Read preferences
169     PrefsInit();
170    
171     // Init system routines
172     SysInit();
173    
174     // Show preferences editor
175     if (!PrefsFindBool("nogui"))
176     if (!PrefsEditor())
177     QuitEmulator();
178    
179     // Create area for Mac RAM
180     RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
181     if (RAMSize < 1024*1024) {
182     WarningAlert(GetString(STR_SMALL_RAM_WARN));
183     RAMSize = 1024*1024;
184     }
185     RAMBaseHost = new uint8[RAMSize];
186    
187     // Create area for Mac ROM
188     ROMBaseHost = new uint8[0x100000];
189    
190     // Get rom file path from preferences
191     const char *rom_path = PrefsFindString("rom");
192    
193     // Load Mac ROM
194     int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
195     if (rom_fd < 0) {
196     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
197     QuitEmulator();
198     }
199     printf(GetString(STR_READING_ROM_FILE));
200     ROMSize = lseek(rom_fd, 0, SEEK_END);
201     if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
202     ErrorAlert(GetString(STR_ROM_SIZE_ERR));
203     close(rom_fd);
204     QuitEmulator();
205     }
206     lseek(rom_fd, 0, SEEK_SET);
207     if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
208     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
209     close(rom_fd);
210     QuitEmulator();
211     }
212    
213 cebix 1.3 // Initialize everything
214     if (!InitAll())
215 cebix 1.1 QuitEmulator();
216    
217     // Start XPRAM watchdog thread
218     xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
219    
220     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
221     // Start 60Hz timer
222     sigemptyset(&timer_sa.sa_mask);
223     timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
224     timer_sa.sa_sigaction = one_tick;
225     if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
226     printf("FATAL: cannot set up timer signal handler\n");
227     QuitEmulator();
228     }
229     struct sigevent timer_event;
230     timer_event.sigev_notify = SIGEV_SIGNAL;
231     timer_event.sigev_signo = SIG_TIMER;
232     if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
233     printf("FATAL: cannot create timer\n");
234     QuitEmulator();
235     }
236     struct itimerspec req;
237     req.it_value.tv_sec = 0;
238     req.it_value.tv_nsec = 16625000;
239     req.it_interval.tv_sec = 0;
240     req.it_interval.tv_nsec = 16625000;
241     if (timer_settime(timer, TIMER_RELTIME, &req, NULL) < 0) {
242     printf("FATAL: cannot start timer\n");
243     QuitEmulator();
244     }
245    
246     #else
247    
248     // Start 60Hz thread
249     pthread_attr_init(&tick_thread_attr);
250     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
251     if (geteuid() == 0) {
252     pthread_attr_setinheritsched(&tick_thread_attr, PTHREAD_EXPLICIT_SCHED);
253     pthread_attr_setschedpolicy(&tick_thread_attr, SCHED_FIFO);
254     struct sched_param fifo_param;
255     fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2;
256     pthread_attr_setschedparam(&tick_thread_attr, &fifo_param);
257     }
258     #endif
259     tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
260     if (!tick_thread_active) {
261     printf("FATAL: cannot create tick thread\n");
262     QuitEmulator();
263     }
264     #endif
265    
266 cebix 1.4 #if ENABLE_MON
267     // Setup SIGINT handler to enter mon
268     sigemptyset(&sigint_sa.sa_mask);
269     sigint_sa.sa_flags = 0;
270     sigint_sa.sa_handler = sigint_handler;
271     sigaction(SIGINT, &sigint_sa, NULL);
272     #endif
273    
274 cebix 1.1 // Start 68k and jump to ROM boot routine
275     Start680x0();
276    
277     QuitEmulator();
278     return 0;
279     }
280    
281    
282     /*
283     * Quit emulator
284     */
285    
286     void QuitEmulator(void)
287     {
288     // Exit 680x0 emulation
289     Exit680x0();
290    
291     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
292     // Stop 60Hz timer
293     timer_delete(timer);
294     #else
295     // Stop 60Hz thread
296     if (tick_thread_active) {
297     tick_thread_cancel = true;
298     #ifdef HAVE_PTHREAD_CANCEL
299     pthread_cancel(tick_thread);
300     #endif
301     pthread_join(tick_thread, NULL);
302     }
303     #endif
304    
305     // Stop XPRAM watchdog thread
306     if (xpram_thread_active) {
307     xpram_thread_cancel = true;
308     #ifdef HAVE_PTHREAD_CANCEL
309     pthread_cancel(xpram_thread);
310     #endif
311     pthread_join(xpram_thread, NULL);
312     }
313    
314 cebix 1.3 // Deinitialize everything
315     ExitAll();
316 cebix 1.1
317     // Delete ROM area
318     delete[] ROMBaseHost;
319    
320     // Delete RAM area
321     delete[] RAMBaseHost;
322    
323     // Exit system routines
324     SysExit();
325    
326     // Exit preferences
327     PrefsExit();
328    
329     // Close X11 server connection
330     if (x_display)
331     XCloseDisplay(x_display);
332    
333     exit(0);
334     }
335    
336    
337     /*
338     * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
339     * or a dynamically recompiling emulator)
340     */
341    
342     #if EMULATED_68K
343     void FlushCodeCache(void *start, uint32 size)
344     {
345 cebix 1.4 }
346     #endif
347    
348    
349     /*
350     * SIGINT handler, enters mon
351     */
352    
353     #if ENABLE_MON
354     static void sigint_handler(...)
355     {
356     char *arg[2] = {"rmon", NULL};
357     mon(1, arg);
358     QuitEmulator();
359 cebix 1.1 }
360     #endif
361    
362    
363     /*
364     * Interrupt flags (must be handled atomically!)
365     */
366    
367     uint32 InterruptFlags = 0;
368    
369     void SetInterruptFlag(uint32 flag)
370     {
371     pthread_mutex_lock(&intflag_lock);
372     InterruptFlags |= flag;
373     pthread_mutex_unlock(&intflag_lock);
374     }
375    
376     void ClearInterruptFlag(uint32 flag)
377     {
378     pthread_mutex_lock(&intflag_lock);
379     InterruptFlags &= ~flag;
380     pthread_mutex_unlock(&intflag_lock);
381     }
382    
383    
384     /*
385     * 60Hz thread (really 60.15Hz)
386     */
387    
388     static void one_tick(...)
389     {
390     static int tick_counter = 0;
391    
392     // Pseudo Mac 1Hz interrupt, update local time
393     if (++tick_counter > 60) {
394     tick_counter = 0;
395     WriteMacInt32(0x20c, TimerDateTime());
396     }
397    
398     // Trigger 60Hz interrupt
399     if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
400     SetInterruptFlag(INTFLAG_60HZ);
401     TriggerInterrupt();
402     }
403     }
404    
405     static void *tick_func(void *arg)
406     {
407     while (!tick_thread_cancel) {
408    
409     // Wait
410     #ifdef HAVE_NANOSLEEP
411     struct timespec req = {0, 16625000};
412     nanosleep(&req, NULL);
413     #else
414     usleep(16625);
415     #endif
416    
417     // Action
418     one_tick();
419     }
420     return NULL;
421     }
422    
423    
424     /*
425     * XPRAM watchdog thread (saves XPRAM every minute)
426     */
427    
428     void *xpram_func(void *arg)
429     {
430     uint8 last_xpram[256];
431     memcpy(last_xpram, XPRAM, 256);
432    
433     while (!xpram_thread_cancel) {
434     for (int i=0; i<60 && !xpram_thread_cancel; i++) {
435     #ifdef HAVE_NANOSLEEP
436     struct timespec req = {1, 0};
437     nanosleep(&req, NULL);
438     #else
439     usleep(1000000);
440     #endif
441     }
442     if (memcmp(last_xpram, XPRAM, 256)) {
443     memcpy(last_xpram, XPRAM, 256);
444     SaveXPRAM();
445     }
446     }
447     return NULL;
448     }
449    
450    
451     /*
452     * Display alert
453     */
454    
455     #if ENABLE_GTK
456     static void dl_destroyed(void)
457     {
458     gtk_main_quit();
459     }
460    
461     static void dl_quit(GtkWidget *dialog)
462     {
463     gtk_widget_destroy(dialog);
464     }
465    
466     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
467     {
468     char str[256];
469     sprintf(str, GetString(prefix_id), text);
470    
471     GtkWidget *dialog = gtk_dialog_new();
472     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
473     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
474     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
475     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
476    
477     GtkWidget *label = gtk_label_new(str);
478     gtk_widget_show(label);
479     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
480    
481     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
482     gtk_widget_show(button);
483     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
484     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
485     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
486     gtk_widget_grab_default(button);
487     gtk_widget_show(dialog);
488    
489     gtk_main();
490     }
491     #endif
492    
493    
494     /*
495     * Display error alert
496     */
497    
498     void ErrorAlert(const char *text)
499     {
500     #if ENABLE_GTK
501     if (PrefsFindBool("nogui") || x_display == NULL) {
502     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
503     return;
504     }
505     VideoQuitFullScreen();
506     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
507     #else
508     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
509     #endif
510     }
511    
512    
513     /*
514     * Display warning alert
515     */
516    
517     void WarningAlert(const char *text)
518     {
519     #if ENABLE_GTK
520     if (PrefsFindBool("nogui") || x_display == NULL) {
521     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
522     return;
523     }
524     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
525     #else
526     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
527     #endif
528     }
529    
530    
531     /*
532     * Display choice alert
533     */
534    
535     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
536     {
537     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
538     return false; //!!
539     }