ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Windows/main_windows.cpp
Revision: 1.9
Committed: 2005-03-19T19:09:44Z (19 years, 8 months ago) by gbeauche
Branch: MAIN
Changes since 1.8: +21 -0 lines
Log Message:
Check that we are running a Windows NT kernel >= 4.0 and drivers are
installed correctly (namely cdenable.sys)

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.1 // Initialize SDL system
209     int sdl_flags = 0;
210     #ifdef USE_SDL_VIDEO
211     sdl_flags |= SDL_INIT_VIDEO;
212     #endif
213     #ifdef USE_SDL_AUDIO
214     sdl_flags |= SDL_INIT_AUDIO;
215     #endif
216     assert(sdl_flags != 0);
217     if (SDL_Init(sdl_flags) == -1) {
218     char str[256];
219     sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError());
220     ErrorAlert(str);
221     goto quit;
222     }
223     atexit(SDL_Quit);
224    
225     #ifdef ENABLE_MON
226     // Initialize mon
227     mon_init();
228     #endif
229    
230     // Install SIGSEGV handler for CPU emulator
231     if (!sigsegv_install_handler(sigsegv_handler)) {
232     sprintf(str, GetString(STR_SIGSEGV_INSTALL_ERR), strerror(errno));
233     ErrorAlert(str);
234     goto quit;
235     }
236    
237     // Initialize VM system
238     vm_init();
239    
240     // Get system info
241     PVR = 0x00040000; // Default: 604
242     CPUClockSpeed = 100000000; // Default: 100MHz
243     BusClockSpeed = 100000000; // Default: 100MHz
244     TimebaseSpeed = 25000000; // Default: 25MHz
245     PVR = 0x000c0000; // Default: 7400 (with AltiVec)
246     D(bug("PVR: %08x (assumed)\n", PVR));
247    
248     // Init system routines
249     SysInit();
250    
251     // Show preferences editor
252     if (!PrefsFindBool("nogui"))
253     if (!PrefsEditor())
254     goto quit;
255    
256     // Create Low Memory area (0x0000..0x3000)
257     if (vm_mac_acquire(0, 0x3000) < 0) {
258     sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
259     ErrorAlert(str);
260     goto quit;
261     }
262     lm_area_mapped = true;
263    
264     // Create areas for Kernel Data
265     if (!kernel_data_init())
266     goto quit;
267     kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
268     emulator_data = &kernel_data->ed;
269     KernelDataAddr = KERNEL_DATA_BASE;
270     D(bug("Kernel Data at %p (%08x)\n", kernel_data, KERNEL_DATA_BASE));
271     D(bug("Emulator Data at %p (%08x)\n", emulator_data, KERNEL_DATA_BASE + offsetof(KernelData, ed)));
272    
273     // Create area for DR Cache
274     if (vm_mac_acquire(DR_EMULATOR_BASE, DR_EMULATOR_SIZE) < 0) {
275     sprintf(str, GetString(STR_DR_EMULATOR_MMAP_ERR), strerror(errno));
276     ErrorAlert(str);
277     goto quit;
278     }
279     dr_emulator_area_mapped = true;
280     if (vm_mac_acquire(DR_CACHE_BASE, DR_CACHE_SIZE) < 0) {
281     sprintf(str, GetString(STR_DR_CACHE_MMAP_ERR), strerror(errno));
282     ErrorAlert(str);
283     goto quit;
284     }
285     dr_cache_area_mapped = true;
286     DRCacheAddr = (uint32)Mac2HostAddr(DR_CACHE_BASE);
287     D(bug("DR Cache at %p (%08x)\n", DRCacheAddr, DR_CACHE_BASE));
288    
289     // Create area for SheepShaver data
290     if (!SheepMem::Init()) {
291     sprintf(str, GetString(STR_SHEEP_MEM_MMAP_ERR), strerror(errno));
292     ErrorAlert(str);
293     goto quit;
294     }
295    
296     // Create area for Mac ROM
297     if (vm_mac_acquire(ROM_BASE, ROM_AREA_SIZE) < 0) {
298     sprintf(str, GetString(STR_ROM_MMAP_ERR), strerror(errno));
299     ErrorAlert(str);
300     goto quit;
301     }
302     ROMBaseHost = Mac2HostAddr(ROM_BASE);
303     rom_area_mapped = true;
304     D(bug("ROM area at %p (%08x)\n", ROMBaseHost, ROM_BASE));
305    
306     // Create area for Mac RAM
307     RAMSize = PrefsFindInt32("ramsize");
308     if (RAMSize < 8*1024*1024) {
309     WarningAlert(GetString(STR_SMALL_RAM_WARN));
310     RAMSize = 8*1024*1024;
311     }
312    
313     if (vm_mac_acquire(RAM_BASE, RAMSize) < 0) {
314     sprintf(str, GetString(STR_RAM_MMAP_ERR), strerror(errno));
315     ErrorAlert(str);
316     goto quit;
317     }
318     RAMBaseHost = Mac2HostAddr(RAM_BASE);
319     RAMBase = RAM_BASE;
320     ram_area_mapped = true;
321     D(bug("RAM area at %p (%08x)\n", RAMBaseHost, RAMBase));
322    
323     if (RAMBase > ROM_BASE) {
324     ErrorAlert(GetString(STR_RAM_HIGHER_THAN_ROM_ERR));
325     goto quit;
326     }
327    
328     // Load Mac ROM
329     rom_path = PrefsFindString("rom");
330     rom_fh = CreateFile(rom_path ? rom_path : ROM_FILE_NAME,
331     GENERIC_READ, 0, NULL, OPEN_EXISTING,
332     FILE_ATTRIBUTE_NORMAL, NULL);
333    
334     if (rom_fh == INVALID_HANDLE_VALUE) {
335     rom_fh = CreateFile(rom_path ? rom_path : ROM_FILE_NAME2,
336     GENERIC_READ, 0, NULL, OPEN_EXISTING,
337     FILE_ATTRIBUTE_NORMAL, NULL);
338    
339     if (rom_fh == INVALID_HANDLE_VALUE) {
340     ErrorAlert(GetString(STR_NO_ROM_FILE_ERR));
341     goto quit;
342     }
343     }
344     printf(GetString(STR_READING_ROM_FILE));
345     rom_size = GetFileSize(rom_fh, NULL);
346     rom_tmp = new uint8[ROM_SIZE];
347     ReadFile(rom_fh, (void *)rom_tmp, ROM_SIZE, &actual, NULL);
348     CloseHandle(rom_fh);
349    
350     // Decode Mac ROM
351     if (!DecodeROM(rom_tmp, actual)) {
352     if (rom_size != 4*1024*1024) {
353     ErrorAlert(GetString(STR_ROM_SIZE_ERR));
354     goto quit;
355     } else {
356     ErrorAlert(GetString(STR_ROM_FILE_READ_ERR));
357     goto quit;
358     }
359     }
360     delete[] rom_tmp;
361 gbeauche 1.8
362     // Initialize native timers
363     timer_init();
364 gbeauche 1.1
365 gbeauche 1.6 // Initialize everything
366     if (!InitAll())
367 gbeauche 1.1 goto quit;
368 gbeauche 1.6 D(bug("Initialization complete\n"));
369 gbeauche 1.1
370     // Write protect ROM
371 gbeauche 1.6 vm_protect(ROMBaseHost, ROM_AREA_SIZE, VM_PAGE_READ);
372 gbeauche 1.1
373     // Start 60Hz thread
374     tick_thread_cancel = false;
375 gbeauche 1.3 tick_thread_active = ((tick_thread = create_thread(tick_func)) != NULL);
376     SetThreadPriority(tick_thread, THREAD_PRIORITY_ABOVE_NORMAL);
377 gbeauche 1.1 D(bug("Tick thread installed (%ld)\n", tick_thread));
378    
379     // Start NVRAM watchdog thread
380     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
381     nvram_thread_cancel = false;
382 gbeauche 1.3 nvram_thread_active = ((nvram_thread = create_thread(nvram_func, NULL)) != NULL);
383     SetThreadPriority(nvram_thread, THREAD_PRIORITY_BELOW_NORMAL);
384 gbeauche 1.1 D(bug("NVRAM thread installed (%ld)\n", nvram_thread));
385    
386 gbeauche 1.3 // Get my thread ID and jump to ROM boot routine
387     emul_thread = GetCurrentThread();
388 gbeauche 1.1 D(bug("Jumping to ROM\n"));
389     jump_to_rom(ROM_BASE + 0x310000);
390     D(bug("Returned from ROM\n"));
391    
392     quit:
393     Quit();
394     return 0;
395     }
396    
397    
398     /*
399     * Cleanup and quit
400     */
401    
402     static void Quit(void)
403     {
404     // Exit PowerPC emulation
405     exit_emul_ppc();
406    
407     // Stop 60Hz thread
408     if (tick_thread_active) {
409     tick_thread_cancel = true;
410 gbeauche 1.3 wait_thread(tick_thread);
411 gbeauche 1.1 }
412    
413     // Stop NVRAM watchdog thread
414     if (nvram_thread_active) {
415     nvram_thread_cancel = true;
416 gbeauche 1.3 wait_thread(nvram_thread);
417 gbeauche 1.1 }
418    
419 gbeauche 1.6 // Deinitialize everything
420     ExitAll();
421 gbeauche 1.1
422     // Delete SheepShaver globals
423     SheepMem::Exit();
424    
425     // Delete RAM area
426     if (ram_area_mapped)
427     vm_mac_release(RAM_BASE, RAMSize);
428    
429     // Delete ROM area
430     if (rom_area_mapped)
431     vm_mac_release(ROM_BASE, ROM_AREA_SIZE);
432    
433     // Delete DR cache areas
434     if (dr_emulator_area_mapped)
435     vm_mac_release(DR_EMULATOR_BASE, DR_EMULATOR_SIZE);
436     if (dr_cache_area_mapped)
437     vm_mac_release(DR_CACHE_BASE, DR_CACHE_SIZE);
438    
439     // Delete Kernel Data area
440     kernel_data_exit();
441    
442     // Delete Low Memory area
443     if (lm_area_mapped)
444     vm_mac_release(0, 0x3000);
445    
446     // Exit system routines
447     SysExit();
448    
449     // Exit preferences
450     PrefsExit();
451    
452     #ifdef ENABLE_MON
453     // Exit mon
454     mon_exit();
455     #endif
456    
457     exit(0);
458     }
459    
460    
461     /*
462     * Initialize Kernel Data segments
463     */
464    
465     static HANDLE kernel_handle; // Shared memory handle for Kernel Data
466     static DWORD allocation_granule; // Minimum size of allocateable are (64K)
467     static DWORD kernel_area_size; // Size of Kernel Data area
468    
469     static bool kernel_data_init(void)
470     {
471     char str[256];
472     SYSTEM_INFO si;
473     GetSystemInfo(&si);
474     allocation_granule = si.dwAllocationGranularity;
475     kernel_area_size = (KERNEL_AREA_SIZE + allocation_granule - 1) & -allocation_granule;
476    
477     char rcs[10];
478     LPVOID kernel_addr;
479     kernel_handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, kernel_area_size, NULL);
480     if (kernel_handle == NULL) {
481     sprintf(rcs, "%d", GetLastError());
482     sprintf(str, GetString(STR_KD_SHMGET_ERR), rcs);
483     ErrorAlert(str);
484     return false;
485     }
486     kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule);
487     if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) {
488     sprintf(rcs, "%d", GetLastError());
489     sprintf(str, GetString(STR_KD_SHMAT_ERR), rcs);
490     ErrorAlert(str);
491     return false;
492     }
493     kernel_addr = (LPVOID)Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule);
494     if (MapViewOfFileEx(kernel_handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, kernel_area_size, kernel_addr) != kernel_addr) {
495     sprintf(rcs, "%d", GetLastError());
496     sprintf(str, GetString(STR_KD2_SHMAT_ERR), rcs);
497     ErrorAlert(str);
498     return false;
499     }
500     return true;
501     }
502    
503    
504     /*
505     * Deallocate Kernel Data segments
506     */
507    
508     static void kernel_data_exit(void)
509     {
510     if (kernel_handle) {
511     UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA_BASE & -allocation_granule));
512     UnmapViewOfFile(Mac2HostAddr(KERNEL_DATA2_BASE & -allocation_granule));
513     CloseHandle(kernel_handle);
514     }
515     }
516    
517    
518     /*
519     * Jump into Mac ROM, start 680x0 emulator
520     */
521    
522     void jump_to_rom(uint32 entry)
523     {
524     init_emul_ppc();
525     emul_ppc(entry);
526     }
527    
528    
529     /*
530     * Quit emulator (cause return from jump_to_rom)
531     */
532    
533     void QuitEmulator(void)
534     {
535     Quit();
536     }
537    
538    
539     /*
540     * Pause/resume emulator
541     */
542    
543     void PauseEmulator(void)
544     {
545 gbeauche 1.3 SuspendThread(emul_thread);
546 gbeauche 1.1 }
547    
548     void ResumeEmulator(void)
549     {
550 gbeauche 1.3 ResumeThread(emul_thread);
551 gbeauche 1.1 }
552    
553    
554     /*
555     * Dump 68k registers
556     */
557    
558     void Dump68kRegs(M68kRegisters *r)
559     {
560     // Display 68k registers
561     for (int i=0; i<8; i++) {
562     printf("d%d: %08x", i, r->d[i]);
563     if (i == 3 || i == 7)
564     printf("\n");
565     else
566     printf(", ");
567     }
568     for (int i=0; i<8; i++) {
569     printf("a%d: %08x", i, r->a[i]);
570     if (i == 3 || i == 7)
571     printf("\n");
572     else
573     printf(", ");
574     }
575     }
576    
577    
578     /*
579     * Make code executable
580     */
581    
582     void MakeExecutable(int dummy, uint32 start, uint32 length)
583     {
584     if ((start >= ROM_BASE) && (start < (ROM_BASE + ROM_SIZE)))
585     return;
586     FlushCodeCache(start, start + length);
587     }
588    
589    
590     /*
591     * NVRAM watchdog thread (saves NVRAM every minute)
592     */
593    
594     static void nvram_watchdog(void)
595     {
596     if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
597     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
598     SaveXPRAM();
599     }
600     }
601    
602 gbeauche 1.3 static DWORD nvram_func(void *arg)
603 gbeauche 1.1 {
604     while (!nvram_thread_cancel) {
605     for (int i=0; i<60 && !nvram_thread_cancel; i++)
606     Delay_usec(999999); // Only wait 1 second so we quit promptly when nvram_thread_cancel becomes true
607     nvram_watchdog();
608     }
609     return 0;
610     }
611    
612    
613     /*
614     * 60Hz thread (really 60.15Hz)
615     */
616    
617 gbeauche 1.3 static DWORD tick_func(void *arg)
618 gbeauche 1.1 {
619     int tick_counter = 0;
620     uint64 start = GetTicks_usec();
621     int64 ticks = 0;
622     uint64 next = GetTicks_usec();
623    
624     while (!tick_thread_cancel) {
625    
626     // Wait
627     next += 16625;
628     int64 delay = next - GetTicks_usec();
629     if (delay > 0)
630     Delay_usec(delay);
631     else if (delay < -16625)
632     next = GetTicks_usec();
633     ticks++;
634    
635     // Pseudo Mac 1Hz interrupt, update local time
636     if (++tick_counter > 60) {
637     tick_counter = 0;
638     WriteMacInt32(0x20c, TimerDateTime());
639     }
640    
641     // Trigger 60Hz interrupt
642     if (ReadMacInt32(XLM_IRQ_NEST) == 0) {
643     SetInterruptFlag(INTFLAG_VIA);
644     TriggerInterrupt();
645     }
646     }
647    
648     uint64 end = GetTicks_usec();
649     D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
650     return 0;
651     }
652    
653    
654     /*
655     * Mutexes
656     */
657    
658     struct B2_mutex {
659 gbeauche 1.4 mutex_t m;
660 gbeauche 1.1 };
661    
662     B2_mutex *B2_create_mutex(void)
663     {
664     return new B2_mutex;
665     }
666    
667     void B2_lock_mutex(B2_mutex *mutex)
668     {
669 gbeauche 1.4 mutex->m.lock();
670 gbeauche 1.1 }
671    
672     void B2_unlock_mutex(B2_mutex *mutex)
673     {
674 gbeauche 1.4 mutex->m.unlock();
675 gbeauche 1.1 }
676    
677     void B2_delete_mutex(B2_mutex *mutex)
678     {
679     delete mutex;
680     }
681    
682    
683     /*
684     * Interrupt flags (must be handled atomically!)
685     */
686    
687     volatile uint32 InterruptFlags = 0;
688 gbeauche 1.4 static mutex_t intflags_mutex;
689 gbeauche 1.1
690     void SetInterruptFlag(uint32 flag)
691     {
692 gbeauche 1.4 intflags_mutex.lock();
693     InterruptFlags |= flag;
694     intflags_mutex.unlock();
695 gbeauche 1.1 }
696    
697     void ClearInterruptFlag(uint32 flag)
698     {
699 gbeauche 1.4 intflags_mutex.lock();
700     InterruptFlags &= ~flag;
701     intflags_mutex.unlock();
702 gbeauche 1.1 }
703    
704    
705     /*
706     * Disable interrupts
707     */
708    
709     void DisableInterrupt(void)
710     {
711     WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) + 1);
712     }
713    
714    
715     /*
716     * Enable interrupts
717     */
718    
719     void EnableInterrupt(void)
720     {
721     WriteMacInt32(XLM_IRQ_NEST, int32(ReadMacInt32(XLM_IRQ_NEST)) - 1);
722     }
723    
724    
725     /*
726     * Helpers to share 32-bit addressable data with MacOS
727     */
728    
729     bool SheepMem::Init(void)
730     {
731     // Size of a native page
732 gbeauche 1.5 page_size = vm_get_page_size();
733 gbeauche 1.1
734     // Allocate SheepShaver globals
735     proc = base;
736     if (vm_mac_acquire(base, size) < 0)
737     return false;
738    
739     // Allocate page with all bits set to 0, right in the middle
740     // This is also used to catch undesired overlaps between proc and data areas
741     zero_page = proc + (size / 2);
742     Mac_memset(zero_page, 0, page_size);
743     if (vm_protect(Mac2HostAddr(zero_page), page_size, VM_PAGE_READ) < 0)
744     return false;
745    
746     // Allocate alternate stack for PowerPC interrupt routine
747     sig_stack = base + size;
748     if (vm_mac_acquire(sig_stack, SIG_STACK_SIZE) < 0)
749     return false;
750    
751     data = base + size;
752     return true;
753     }
754    
755     void SheepMem::Exit(void)
756     {
757     if (data) {
758     // Delete SheepShaver globals
759     vm_mac_release(base, size);
760    
761     // Delete alternate stack for PowerPC interrupt routine
762     vm_mac_release(sig_stack, SIG_STACK_SIZE);
763     }
764     }
765    
766    
767     /*
768     * Get the main window handle
769     */
770    
771     #ifdef USE_SDL_VIDEO
772     #include <SDL_syswm.h>
773     static HWND GetMainWindowHandle(void)
774     {
775     SDL_SysWMinfo wmInfo;
776     wmInfo.version.major = SDL_MAJOR_VERSION;
777     wmInfo.version.minor = SDL_MINOR_VERSION;
778     wmInfo.version.patch = SDL_PATCHLEVEL;
779     return SDL_GetWMInfo(&wmInfo) ? wmInfo.window : NULL;
780     }
781     #endif
782    
783    
784     /*
785     * Display alert
786     */
787    
788     static void display_alert(int title_id, const char *text, int flags)
789     {
790     HWND hMainWnd = GetMainWindowHandle();
791     MessageBox(hMainWnd, text, GetString(title_id), MB_OK | flags);
792     }
793    
794    
795     /*
796     * Display error alert
797     */
798    
799     void ErrorAlert(const char *text)
800     {
801     if (PrefsFindBool("nogui"))
802     return;
803    
804     VideoQuitFullScreen();
805     display_alert(STR_ERROR_ALERT_TITLE, text, MB_ICONSTOP);
806     }
807    
808    
809     /*
810     * Display warning alert
811     */
812    
813     void WarningAlert(const char *text)
814     {
815     if (PrefsFindBool("nogui"))
816     return;
817    
818     display_alert(STR_WARNING_ALERT_TITLE, text, MB_ICONINFORMATION);
819     }
820    
821    
822     /*
823     * Display choice alert
824     */
825    
826     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
827     {
828     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
829     return false; //!!
830     }