ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Windows/main_windows.cpp
Revision: 1.10
Committed: 2005-11-27T20:40:52Z (19 years ago) by gbeauche
Branch: MAIN
Changes since 1.9: +4 -0 lines
Log Message:
Force DIB driver for SDL/Windows (HACK from Basilisk II tree), improves
responsiveness for me.

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * main_windows.cpp - Emulation core, Windows implementation
3     *
4 gbeauche 1.7 * SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig
5 gbeauche 1.1 *
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 <errno.h>
22     #include <stdio.h>
23     #include <stdlib.h>
24     #include <string.h>
25    
26     #include <SDL.h>
27    
28     #include "sysdeps.h"
29     #include "main.h"
30     #include "version.h"
31     #include "prefs.h"
32     #include "prefs_editor.h"
33     #include "cpu_emulation.h"
34     #include "emul_op.h"
35     #include "xlowmem.h"
36     #include "xpram.h"
37     #include "timer.h"
38     #include "adb.h"
39     #include "video.h"
40     #include "sys.h"
41     #include "macos_util.h"
42     #include "rom_patches.h"
43     #include "user_strings.h"
44     #include "vm_alloc.h"
45     #include "sigsegv.h"
46 gbeauche 1.3 #include "util_windows.h"
47 gbeauche 1.1
48     #define DEBUG 0
49     #include "debug.h"
50    
51     #ifdef ENABLE_MON
52     #include "mon.h"
53     #endif
54    
55    
56     // Constants
57     const char ROM_FILE_NAME[] = "ROM";
58     const char ROM_FILE_NAME2[] = "Mac OS ROM";
59    
60 gbeauche 1.8 const uintptr RAM_BASE = 0x20000000; // Base address of RAM
61 gbeauche 1.1 const uint32 SIG_STACK_SIZE = 0x10000; // Size of signal stack
62    
63    
64     // Global variables (exported)
65     uint32 RAMBase; // Base address of Mac RAM
66     uint32 RAMSize; // Size of Mac RAM
67     uint32 KernelDataAddr; // Address of Kernel Data
68     uint32 BootGlobsAddr; // Address of BootGlobs structure at top of Mac RAM
69     uint32 DRCacheAddr; // Address of DR Cache
70     uint32 PVR; // Theoretical PVR
71     int64 CPUClockSpeed; // Processor clock speed (Hz)
72     int64 BusClockSpeed; // Bus clock speed (Hz)
73     int64 TimebaseSpeed; // Timebase clock speed (Hz)
74     uint8 *RAMBaseHost; // Base address of Mac RAM (host address space)
75     uint8 *ROMBaseHost; // Base address of Mac ROM (host address space)
76 gbeauche 1.9 DWORD win_os; // Windows OS id
77     DWORD win_os_major; // Windows OS version major
78 gbeauche 1.1
79    
80     // Global variables
81     static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
82     static int kernel_area = -1; // SHM ID of Kernel Data area
83     static bool rom_area_mapped = false; // Flag: Mac ROM mmap()ped
84     static bool ram_area_mapped = false; // Flag: Mac RAM mmap()ped
85     static bool dr_cache_area_mapped = false; // Flag: Mac DR Cache mmap()ped
86     static bool dr_emulator_area_mapped = false;// Flag: Mac DR Emulator mmap()ped
87     static KernelData *kernel_data; // Pointer to Kernel Data
88     static EmulatorData *emulator_data;
89    
90     static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes
91     static bool nvram_thread_active = false; // Flag: NVRAM watchdog installed
92     static volatile bool nvram_thread_cancel; // Flag: Cancel NVRAM thread
93 gbeauche 1.3 static HANDLE nvram_thread = NULL; // NVRAM watchdog
94 gbeauche 1.1 static bool tick_thread_active = false; // Flag: MacOS thread installed
95     static volatile bool tick_thread_cancel; // Flag: Cancel 60Hz thread
96 gbeauche 1.3 static HANDLE tick_thread = NULL; // 60Hz thread
97     static HANDLE emul_thread = NULL; // MacOS thread
98 gbeauche 1.1 static uintptr sig_stack = 0; // Stack for PowerPC interrupt routine
99    
100     uint32 SheepMem::page_size; // Size of a native page
101     uintptr SheepMem::zero_page = 0; // Address of ro page filled in with zeros
102     uintptr SheepMem::base = 0x60000000; // Address of SheepShaver data
103     uintptr SheepMem::proc; // Bottom address of SheepShave procedures
104     uintptr SheepMem::data; // Top of SheepShaver data (stack like storage)
105    
106    
107     // Prototypes
108     static bool kernel_data_init(void);
109     static void kernel_data_exit(void);
110     static void Quit(void);
111 gbeauche 1.3 static DWORD WINAPI nvram_func(void *arg);
112     static DWORD WINAPI tick_func(void *arg);
113 gbeauche 1.1
114     static void jump_to_rom(uint32 entry);
115     extern void emul_ppc(uint32 start);
116     extern void init_emul_ppc(void);
117     extern void exit_emul_ppc(void);
118     sigsegv_return_t sigsegv_handler(sigsegv_address_t, sigsegv_address_t);
119    
120    
121     /*
122     * Return signal stack base
123     */
124    
125     uintptr SignalStackBase(void)
126     {
127     return sig_stack + SIG_STACK_SIZE;
128     }
129    
130    
131     /*
132     * Memory management helpers
133     */
134    
135     static inline int vm_mac_acquire(uint32 addr, uint32 size)
136     {
137     return vm_acquire_fixed(Mac2HostAddr(addr), size);
138     }
139    
140     static inline int vm_mac_release(uint32 addr, uint32 size)
141     {
142     return vm_release(Mac2HostAddr(addr), size);
143     }
144    
145    
146     /*
147     * Main program
148     */
149    
150     static void usage(const char *prg_name)
151     {
152     printf("Usage: %s [OPTION...]\n", prg_name);
153     printf("\nUnix options:\n");
154     printf(" --display STRING\n X display to use\n");
155     PrefsPrintUsage();
156     exit(0);
157     }
158    
159     int main(int argc, char **argv)
160     {
161     char str[256];
162     int16 i16;
163     HANDLE rom_fh;
164     const char *rom_path;
165     uint32 rom_size;
166     DWORD actual;
167     uint8 *rom_tmp;
168    
169     // Initialize variables
170     RAMBase = 0;
171    
172     // Print some info
173     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
174     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
175    
176     // Read preferences
177     PrefsInit(argc, argv);
178    
179     // Parse command line arguments
180     for (int i=1; i<argc; i++) {
181     if (strcmp(argv[i], "--help") == 0) {
182     usage(argv[0]);
183     } else if (argv[i][0] == '-') {
184     fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
185     usage(argv[0]);
186     }
187     }
188    
189 gbeauche 1.9 // Check we are using a Windows NT kernel >= 4.0
190     OSVERSIONINFO osvi;
191     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
192     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
193     if (!GetVersionEx(&osvi)) {
194     ErrorAlert("Could not determine OS type");
195     QuitEmulator();
196     }
197     win_os = osvi.dwPlatformId;
198     win_os_major = osvi.dwMajorVersion;
199     if (win_os != VER_PLATFORM_WIN32_NT || win_os_major < 4) {
200     ErrorAlert(GetString(STR_NO_WIN32_NT_4));
201     QuitEmulator();
202     }
203    
204     // Check that drivers are installed
205     if (!check_drivers())
206     QuitEmulator();
207    
208 gbeauche 1.10 // FIXME: default to DIB driver
209     if (getenv("SDL_VIDEODRIVER") == NULL)
210     putenv("SDL_VIDEODRIVER=windib");
211    
212 gbeauche 1.1 // Initialize SDL system
213     int sdl_flags = 0;
214     #ifdef USE_SDL_VIDEO
215     sdl_flags |= SDL_INIT_VIDEO;
216     #endif
217     #ifdef USE_SDL_AUDIO
218     sdl_flags |= SDL_INIT_AUDIO;
219     #endif
220     assert(sdl_flags != 0);
221     if (SDL_Init(sdl_flags) == -1) {
222     char str[256];
223     sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError());
224     ErrorAlert(str);
225     goto quit;
226     }
227     atexit(SDL_Quit);
228    
229     #ifdef ENABLE_MON
230     // Initialize mon
231     mon_init();
232     #endif
233    
234     // Install SIGSEGV handler for CPU emulator
235     if (!sigsegv_install_handler(sigsegv_handler)) {
236     sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
237     ErrorAlert(str);
238     goto quit;
239     }
240    
241     // Initialize VM system
242     vm_init();
243    
244     // Get system info
245     PVR = 0x00040000; // Default: 604
246     CPUClockSpeed = 100000000; // Default: 100MHz
247     BusClockSpeed = 100000000; // Default: 100MHz
248     TimebaseSpeed = 25000000; // Default: 25MHz
249     PVR = 0x000c0000; // Default: 7400 (with AltiVec)
250     D(bug("PVR: %08x (assumed)\n", PVR));
251    
252     // Init system routines
253     SysInit();
254    
255     // Show preferences editor
256     if (!PrefsFindBool("nogui"))
257     if (!PrefsEditor())
258     goto quit;
259    
260     // Create Low Memory area (0x0000..0x3000)
261     if (vm_mac_acquire(0, 0x3000) < 0) {
262     sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
263     ErrorAlert(str);
264     goto quit;
265     }
266     lm_area_mapped = true;
267    
268     // Create areas for Kernel Data
269     if (!kernel_data_init())
270     goto quit;
271     kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
272     emulator_data = &kernel_data->ed;
273     KernelDataAddr = KERNEL_DATA_BASE;
274     D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE));
275     D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed)));
276    
277     // Create area for DR Cache
278     if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
279     sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
280     ErrorAlert(str);
281     goto quit;
282     }
283     dr_emulator_area_mapped = true;
284     if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
285     sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
286     ErrorAlert(str);
287     goto quit;
288     }
289     dr_cache_area_mapped = true;
290     DRCacheAddr = (uint32)Mac2HostAddr(DR_CACHE_BASE);
291     D(bug("DR Cache at %p (%08x)\n", DRCacheAddr, DR_CACHE_BASE));
292    
293     // Create area for SheepShaver data
294     if (!SheepMem::Init()) {
295     sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
296     ErrorAlert(str);
297     goto quit;
298     }
299    
300     // Create area for Mac ROM
301     if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) {
302     sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
303     ErrorAlert(str);
304     goto quit;
305     }
306     ROMBaseHost = Mac2HostAddr(ROM_BASE);
307     rom_area_mapped = true;
308     D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE));
309    
310     // Create area for Mac RAM
311     RAMSize = PrefsFindInt32("ramsize");
312     if (RAMSize < 8*1024*1024) {
313     WarningAlert(GetString(STR_SMALL_RAM_WARN));
314     RAMSize = 8*1024*1024;
315     }
316    
317     if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) {
318     sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
319     ErrorAlert(str);
320     goto quit;
321     }
322     RAMBaseHost = Mac2HostAddr(RAM_BASE);
323     RAMBase = RAM_BASE;
324     ram_area_mapped = true;
325     D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase));
326    
327     if (RAMBase > ROM_BASE) {
328     ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
329     goto quit;
330     }
331    
332     // Load Mac ROM
333     rom_path = PrefsFindString("rom");
334     rom_fh = CreateFile(rom_path ? rom_path : ROM_FILE_NAME,
335     GENERIC_READ, 0, NULL, OPEN_EXISTING,
336     FILE_ATTRIBUTE_NORMAL, NULL);
337    
338     if (rom_fh == INVALID_HANDLE_VALUE) {
339     rom_fh = CreateFile(rom_path ? rom_path : ROM_FILE_NAME2,
340     GENERIC_READ, 0, NULL, OPEN_EXISTING,
341     FILE_ATTRIBUTE_NORMAL, NULL);
342    
343     if (rom_fh == INVALID_HANDLE_VALUE) {
344     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
345     goto quit;
346     }
347     }
348     printf(GetString(STR_READING_ROM_FILE));
349     rom_size = GetFileSize(rom_fh, NULL);
350     rom_tmp = new uint8[ROM_SIZE];
351     ReadFile(rom_fh, (void *)rom_tmp, ROM_SIZE, &actual, NULL);
352     CloseHandle(rom_fh);
353    
354     // Decode Mac ROM
355     if (!DecodeROM(rom_tmp, actual)) {
356     if (rom_size != 4*1024*1024) {
357     ErrorAlert(GetString(STR_ROM_SIZE_ERR));
358     goto quit;
359     } else {
360     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
361     goto quit;
362     }
363     }
364     delete[] rom_tmp;
365 gbeauche 1.8
366     // Initialize native timers
367     timer_init();
368 gbeauche 1.1
369 gbeauche 1.6 // Initialize everything
370     if (!InitAll())
371 gbeauche 1.1 goto quit;
372 gbeauche 1.6 D(bug("Initialization complete\n"));
373 gbeauche 1.1
374     // Write protect ROM
375 gbeauche 1.6 vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ);
376 gbeauche 1.1
377     // Start 60Hz thread
378     tick_thread_cancel = false;
379 gbeauche 1.3 tick_thread_active = ((tick_thread = create_thread(tick_func)) != NULL);
380     SetThreadPriority(tick_thread, THREAD_PRIORITY_ABOVE_NORMAL);
381 gbeauche 1.1 D(bug("Tick thread installed (%ld)\n", tick_thread));
382    
383     // Start NVRAM watchdog thread
384     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
385     nvram_thread_cancel = false;
386 gbeauche 1.3 nvram_thread_active = ((nvram_thread = create_thread(nvram_func, NULL)) != NULL);
387     SetThreadPriority(nvram_thread, THREAD_PRIORITY_BELOW_NORMAL);
388 gbeauche 1.1 D(bug("NVRAM thread installed (%ld)\n", nvram_thread));
389    
390 gbeauche 1.3 // Get my thread ID and jump to ROM boot routine
391     emul_thread = GetCurrentThread();
392 gbeauche 1.1 D(bug("Jumping to ROM\n"));
393     jump_to_rom(ROM_BASE + 0x310000);
394     D(bug("Returned from ROM\n"));
395    
396     quit:
397     Quit();
398     return 0;
399     }
400    
401    
402     /*
403     * Cleanup and quit
404     */
405    
406     static void Quit(void)
407     {
408     // Exit PowerPC emulation
409     exit_emul_ppc();
410    
411     // Stop 60Hz thread
412     if (tick_thread_active) {
413     tick_thread_cancel = true;
414 gbeauche 1.3 wait_thread(tick_thread);
415 gbeauche 1.1 }
416    
417     // Stop NVRAM watchdog thread
418     if (nvram_thread_active) {
419     nvram_thread_cancel = true;
420 gbeauche 1.3 wait_thread(nvram_thread);
421 gbeauche 1.1 }
422    
423 gbeauche 1.6 // Deinitialize everything
424     ExitAll();
425 gbeauche 1.1
426     // Delete SheepShaver globals
427     SheepMem::Exit();
428    
429     // Delete RAM area
430     if (ram_area_mapped)
431     vm_mac_release(RAM_BASE, RAMSize);
432    
433     // Delete ROM area
434     if (rom_area_mapped)
435     vm_mac_release(ROM_BASE, ROM_AREA_SIZE);
436    
437     // Delete DR cache areas
438     if (dr_emulator_area_mapped)
439     vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
440     if (dr_cache_area_mapped)
441     vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE);
442    
443     // Delete Kernel Data area
444     kernel_data_exit();
445    
446     // Delete Low Memory area
447     if (lm_area_mapped)
448     vm_mac_release(0, 0x3000);
449    
450     // Exit system routines
451     SysExit();
452    
453     // Exit preferences
454     PrefsExit();
455    
456     #ifdef ENABLE_MON
457     // Exit mon
458     mon_exit();
459     #endif
460    
461     exit(0);
462     }
463    
464    
465     /*
466     * Initialize Kernel Data segments
467     */
468    
469     static HANDLE kernel_handle; // Shared memory handle for Kernel Data
470     static DWORD allocation_granule; // Minimum size of allocateable are (64K)
471     static DWORD kernel_area_size; // Size of Kernel Data area
472    
473     static bool kernel_data_init(void)
474     {
475     char str[256];
476     SYSTEM_INFO si;
477     GetSystemInfo(&si);
478     allocation_granule = si.dwAllocationGranularity;
479     kernel_area_size = (KERNEL_AREA_SIZE + allocation_granule - 1) & -allocation_granule;
480    
481     char rcs[10];
482     LPVOID kernel_addr;
483     kernel_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, kernel_area_size, NULL);
484     if (kernel_handle == NULL) {
485     sprintf(rcs, "%d", GetLastError());
486     sprintf(str, GetString(STR_KD_SHMGET_ERR), rcs);
487     ErrorAlert(str);
488     return false;
489     }
490     kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule);
491     if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) {
492     sprintf(rcs, "%d", GetLastError());
493     sprintf(str, GetString(STR_KD_SHMAT_ERR), rcs);
494     ErrorAlert(str);
495     return false;
496     }
497     kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule);
498     if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) {
499     sprintf(rcs, "%d", GetLastError());
500     sprintf(str, GetString(STR_KD2_SHMAT_ERR), rcs);
501     ErrorAlert(str);
502     return false;
503     }
504     return true;
505     }
506    
507    
508     /*
509     * Deallocate Kernel Data segments
510     */
511    
512     static void kernel_data_exit(void)
513     {
514     if (kernel_handle) {
515     UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule));
516     UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule));
517     CloseHandle(kernel_handle);
518     }
519     }
520    
521    
522     /*
523     * Jump into Mac ROM, start 680x0 emulator
524     */
525    
526     void jump_to_rom(uint32 entry)
527     {
528     init_emul_ppc();
529     emul_ppc(entry);
530     }
531    
532    
533     /*
534     * Quit emulator (cause return from jump_to_rom)
535     */
536    
537     void QuitEmulator(void)
538     {
539     Quit();
540     }
541    
542    
543     /*
544     * Pause/resume emulator
545     */
546    
547     void PauseEmulator(void)
548     {
549 gbeauche 1.3 SuspendThread(emul_thread);
550 gbeauche 1.1 }
551    
552     void ResumeEmulator(void)
553     {
554 gbeauche 1.3 ResumeThread(emul_thread);
555 gbeauche 1.1 }
556    
557    
558     /*
559     * Dump 68k registers
560     */
561    
562     void Dump68kRegs(M68kRegisters *r)
563     {
564     // Display 68k registers
565     for (int i=0; i<8; i++) {
566     printf("d%d: %08x", i, r->d[i]);
567     if (i == 3 || i == 7)
568     printf("\n");
569     else
570     printf(", ");
571     }
572     for (int i=0; i<8; i++) {
573     printf("a%d: %08x", i, r->a[i]);
574     if (i == 3 || i == 7)
575     printf("\n");
576     else
577     printf(", ");
578     }
579     }
580    
581    
582     /*
583     * Make code executable
584     */
585    
586     void MakeExecutable(int dummy, uint32 start, uint32 length)
587     {
588     if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE)))
589     return;
590     FlushCodeCache(start, start + length);
591     }
592    
593    
594     /*
595     * NVRAM watchdog thread (saves NVRAM every minute)
596     */
597    
598     static void nvram_watchdog(void)
599     {
600     if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
601     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
602     SaveXPRAM();
603     }
604     }
605    
606 gbeauche 1.3 static DWORD nvram_func(void *arg)
607 gbeauche 1.1 {
608     while (!nvram_thread_cancel) {
609     for (int i=0; i<60 && !nvram_thread_cancel; i++)
610     Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true
611     nvram_watchdog();
612     }
613     return 0;
614     }
615    
616    
617     /*
618     * 60Hz thread (really 60.15Hz)
619     */
620    
621 gbeauche 1.3 static DWORD tick_func(void *arg)
622 gbeauche 1.1 {
623     int tick_counter = 0;
624     uint64 start = GetTicks_usec();
625     int64 ticks = 0;
626     uint64 next = GetTicks_usec();
627    
628     while (!tick_thread_cancel) {
629    
630     // Wait
631     next += 16625;
632     int64 delay = next - GetTicks_usec();
633     if (delay > 0)
634     Delay_usec(delay);
635     else if (delay < -16625)
636     next = GetTicks_usec();
637     ticks++;
638    
639     // Pseudo Mac 1Hz interrupt, update local time
640     if (++tick_counter > 60) {
641     tick_counter = 0;
642     WriteMacInt32(0x20c, TimerDateTime());
643     }
644    
645     // Trigger 60Hz interrupt
646     if (ReadMacInt32(XLM_IRQ_NEST) == 0) {
647     SetInterruptFlag(INTFLAG_VIA);
648     TriggerInterrupt();
649     }
650     }
651    
652     uint64 end = GetTicks_usec();
653     D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
654     return 0;
655     }
656    
657    
658     /*
659     * Mutexes
660     */
661    
662     struct B2_mutex {
663 gbeauche 1.4 mutex_t m;
664 gbeauche 1.1 };
665    
666     B2_mutex *B2_create_mutex(void)
667     {
668     return new B2_mutex;
669     }
670    
671     void B2_lock_mutex(B2_mutex *mutex)
672     {
673 gbeauche 1.4 mutex->m.lock();
674 gbeauche 1.1 }
675    
676     void B2_unlock_mutex(B2_mutex *mutex)
677     {
678 gbeauche 1.4 mutex->m.unlock();
679 gbeauche 1.1 }
680    
681     void B2_delete_mutex(B2_mutex *mutex)
682     {
683     delete mutex;
684     }
685    
686    
687     /*
688     * Interrupt flags (must be handled atomically!)
689     */
690    
691     volatile uint32 InterruptFlags = 0;
692 gbeauche 1.4 static mutex_t intflags_mutex;
693 gbeauche 1.1
694     void SetInterruptFlag(uint32 flag)
695     {
696 gbeauche 1.4 intflags_mutex.lock();
697     InterruptFlags |= flag;
698     intflags_mutex.unlock();
699 gbeauche 1.1 }
700    
701     void ClearInterruptFlag(uint32 flag)
702     {
703 gbeauche 1.4 intflags_mutex.lock();
704     InterruptFlags &= ~flag;
705     intflags_mutex.unlock();
706 gbeauche 1.1 }
707    
708    
709     /*
710     * Disable interrupts
711     */
712    
713     void DisableInterrupt(void)
714     {
715     WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1);
716     }
717    
718    
719     /*
720     * Enable interrupts
721     */
722    
723     void EnableInterrupt(void)
724     {
725     WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1);
726     }
727    
728    
729     /*
730     * Helpers to share 32-bit addressable data with MacOS
731     */
732    
733     bool SheepMem::Init(void)
734     {
735     // Size of a native page
736 gbeauche 1.5 page_size = vm_get_page_size();
737 gbeauche 1.1
738     // Allocate SheepShaver globals
739     proc = base;
740     if (vm_mac_acquire(base, size) < 0)
741     return false;
742    
743     // Allocate page with all bits set to 0, right in the middle
744     // This is also used to catch undesired overlaps between proc and data areas
745     zero_page = proc + (size / 2);
746     Mac_memset(zero_page, 0, page_size);
747     if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0)
748     return false;
749    
750     // Allocate alternate stack for PowerPC interrupt routine
751     sig_stack = base + size;
752     if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0)
753     return false;
754    
755     data = base + size;
756     return true;
757     }
758    
759     void SheepMem::Exit(void)
760     {
761     if (data) {
762     // Delete SheepShaver globals
763     vm_mac_release(base, size);
764    
765     // Delete alternate stack for PowerPC interrupt routine
766     vm_mac_release(sig_stack, SIG_STACK_SIZE);
767     }
768     }
769    
770    
771     /*
772     * Get the main window handle
773     */
774    
775     #ifdef USE_SDL_VIDEO
776     #include <SDL_syswm.h>
777     static HWND GetMainWindowHandle(void)
778     {
779     SDL_SysWMinfo wmInfo;
780     wmInfo.version.major = SDL_MAJOR_VERSION;
781     wmInfo.version.minor = SDL_MINOR_VERSION;
782     wmInfo.version.patch = SDL_PATCHLEVEL;
783     return SDL_GetWMInfo(&wmInfo) ? wmInfo.window : NULL;
784     }
785     #endif
786    
787    
788     /*
789     * Display alert
790     */
791    
792     static void display_alert(int title_id, const char *text, int flags)
793     {
794     HWND hMainWnd = GetMainWindowHandle();
795     MessageBox(hMainWnd, text, GetString(title_id), MB_OK | flags);
796     }
797    
798    
799     /*
800     * Display error alert
801     */
802    
803     void ErrorAlert(const char *text)
804     {
805     if (PrefsFindBool("nogui"))
806     return;
807    
808     VideoQuitFullScreen();
809     display_alert(STR_ERROR_ALERT_TITLE, text, MB_ICONSTOP);
810     }
811    
812    
813     /*
814     * Display warning alert
815     */
816    
817     void WarningAlert(const char *text)
818     {
819     if (PrefsFindBool("nogui"))
820     return;
821    
822     display_alert(STR_WARNING_ALERT_TITLE, text, MB_ICONINFORMATION);
823     }
824    
825    
826     /*
827     * Display choice alert
828     */
829    
830     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
831     {
832     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
833     return false; //!!
834     }