2
* main_windows.cpp - Startup code for Windows
4
* Basilisk II (C) 1997-2005 Christian Bauer
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.
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.
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
29
#include <SDL_mutex.h>
30
#include <SDL_thread.h>
35
#include "cpu_emulation.h"
37
#include "rom_patches.h"
44
#include "prefs_editor.h"
45
#include "macos_util.h"
46
#include "user_strings.h"
51
#include "util_windows.h"
52
#include "kernel_windows.h"
55
extern void flush_icache_range(uint32 start, uint32 size); // from compemu_support.cpp
67
const char ROM_FILE_NAME[] = "ROM";
68
const int SCRATCH_MEM_SIZE = 0x10000; // Size of scratch memory area
71
// CPU and FPU type, addressing mode
75
bool TwentyFourBitAddressing;
76
bool ThirtyThreeBitAddressing = false;
80
static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes
82
static bool xpram_thread_active = false; // Flag: XPRAM watchdog installed
83
static volatile bool xpram_thread_cancel = false; // Flag: Cancel XPRAM thread
84
static SDL_Thread *xpram_thread = NULL; // XPRAM watchdog
86
static bool tick_thread_active = false; // Flag: 60Hz thread installed
87
static volatile bool tick_thread_cancel = false; // Flag: Cancel 60Hz thread
88
static SDL_Thread *tick_thread; // 60Hz thread
90
static SDL_mutex *intflag_lock = NULL; // Mutex to protect InterruptFlags
91
#define LOCK_INTFLAGS SDL_LockMutex(intflag_lock)
92
#define UNLOCK_INTFLAGS SDL_UnlockMutex(intflag_lock)
94
DWORD win_os; // Windows OS id
95
DWORD win_os_major; // Windows OS version major
97
#if USE_SCRATCHMEM_SUBTERFUGE
98
uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes
102
static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
107
static int xpram_func(void *arg);
108
static int tick_func(void *arg);
109
static void one_tick(...);
119
char *strdup(const char *s)
121
char *n = (char *)malloc(strlen(s) + 1);
131
* Map memory that can be accessed from the Mac side
134
void *vm_acquire_mac(size_t size)
136
void *m = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_33BIT);
138
ThirtyThreeBitAddressing = false;
139
m = vm_acquire(size);
149
static sigsegv_return_t sigsegv_handler(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
152
// Handle screen fault
153
extern bool Screen_fault_handler(sigsegv_address_t, sigsegv_address_t);
154
if (Screen_fault_handler(fault_address, fault_instruction))
155
return SIGSEGV_RETURN_SUCCESS;
158
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
159
// Ignore writes to ROM
160
if (((uintptr)fault_address - (uintptr)ROMBaseHost) < ROMSize)
161
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
163
// Ignore all other faults, if requested
164
if (PrefsFindBool("ignoresegv"))
165
return SIGSEGV_RETURN_SKIP_INSTRUCTION;
168
return SIGSEGV_RETURN_FAILURE;
172
* Dump state when everything went wrong after a SEGV
175
static void sigsegv_dump_state(sigsegv_address_t fault_address, sigsegv_address_t fault_instruction)
177
fprintf(stderr, "Caught SIGSEGV at address %p", fault_address);
178
if (fault_instruction != SIGSEGV_INVALID_PC)
179
fprintf(stderr, " [IP=%p]", fault_instruction);
180
fprintf(stderr, "\n");
182
extern void m68k_dumpstate(uaecptr *nextpc);
183
m68k_dumpstate(&nextpc);
184
#if USE_JIT && JIT_DEBUG
185
extern void compiler_dumpstate(void);
186
compiler_dumpstate();
188
VideoQuitFullScreen();
190
char *arg[4] = {"mon", "-m", "-r", NULL};
201
static void usage(const char *prg_name)
204
"Usage: %s [OPTION...]\n"
206
" --config FILE\n read/write configuration from/to FILE\n"
207
" --display STRING\n X display to use\n"
208
" --break ADDRESS\n set ROM breakpoint\n"
209
" --rominfo\n dump ROM information\n", prg_name
211
LoadPrefs(); // read the prefs file so PrefsPrintUsage() will print the correct default values
216
int main(int argc, char **argv)
219
bool cd_boot = false;
221
// Initialize variables
228
printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
229
printf(" %s\n", GetString(STR_ABOUT_TEXT2));
231
// Parse command line arguments
232
for (int i=1; i<argc; i++) {
233
if (strcmp(argv[i], "--help") == 0) {
235
} else if (strcmp(argv[i], "--break") == 0) {
238
ROMBreakpoint = strtol(argv[i], NULL, 0);
241
} else if (strcmp(argv[i], "--config") == 0) {
244
extern string UserPrefsPath; // from prefs_unix.cpp
245
UserPrefsPath = argv[i];
248
} else if (strcmp(argv[i], "--rominfo") == 0) {
251
} else if (strcmp(argv[i], "--cdboot") == 0) {
257
// Remove processed arguments
258
for (int i=1; i<argc; i++) {
260
for (k=i; k<argc; k++)
265
for (int j=i+k; j<argc; j++)
272
PrefsInit(argc, argv);
274
// Boot MacOS from CD-ROM?
276
PrefsReplaceInt32("bootdriver", CDROMRefNum);
278
// Any command line arguments left?
279
for (int i=1; i<argc; i++) {
280
if (argv[i][0] == '-') {
281
fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
286
// Check we are using a Windows NT kernel >= 4.0
288
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
289
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
290
if (!GetVersionEx(&osvi)) {
291
ErrorAlert("Could not determine OS type");
294
win_os = osvi.dwPlatformId;
295
win_os_major = osvi.dwMajorVersion;
296
if (win_os != VER_PLATFORM_WIN32_NT || win_os_major < 4) {
297
ErrorAlert(STR_NO_WIN32_NT_4);
301
// Check that drivers are installed
302
if (!check_drivers())
305
// Load win32 libraries
308
// Initialize SDL system
311
sdl_flags |= SDL_INIT_VIDEO;
314
sdl_flags |= SDL_INIT_AUDIO;
316
assert(sdl_flags != 0);
317
if (SDL_Init(sdl_flags) == -1) {
319
sprintf(str, "Could not initialize SDL: %s.\n", SDL_GetError());
325
// Init system routines
328
// Show preferences editor
329
if (!PrefsFindBool("nogui"))
333
// Install the handler for SIGSEGV
334
if (!sigsegv_install_handler(sigsegv_handler)) {
335
sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGSEGV", strerror(errno));
340
// Register dump state function when we got mad after a segfault
341
sigsegv_set_dump_state(sigsegv_dump_state);
344
RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
345
if (RAMSize < 1024*1024) {
346
WarningAlert(GetString(STR_SMALL_RAM_WARN));
350
// Initialize VM system
353
// Create areas for Mac RAM and ROM
354
#ifdef USE_33BIT_ADDRESSING
355
// Speculatively enables 33-bit addressing
356
ThirtyThreeBitAddressing = true;
358
RAMBaseHost = (uint8 *)vm_acquire_mac(RAMSize);
359
ROMBaseHost = (uint8 *)vm_acquire_mac(0x100000);
360
if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) {
361
ErrorAlert(STR_NO_MEM_ERR);
365
#if USE_SCRATCHMEM_SUBTERFUGE
366
// Allocate scratch memory
367
ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE);
368
if (ScratchMem == VM_MAP_FAILED) {
369
ErrorAlert(STR_NO_MEM_ERR);
372
ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
375
#if DIRECT_ADDRESSING
376
// RAMBaseMac shall always be zero
377
MEMBaseDiff = (uintptr)RAMBaseHost;
379
ROMBaseMac = Host2MacAddr(ROMBaseHost);
381
D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
382
D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
384
// Get rom file path from preferences
385
const char *rom_path = PrefsFindString("rom");
388
HANDLE rom_fh = CreateFile(rom_path ? rom_path : ROM_FILE_NAME,
392
FILE_ATTRIBUTE_NORMAL,
394
if (rom_fh == INVALID_HANDLE_VALUE) {
395
ErrorAlert(STR_NO_ROM_FILE_ERR);
398
printf(GetString(STR_READING_ROM_FILE));
399
ROMSize = GetFileSize(rom_fh, NULL);
400
if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
401
ErrorAlert(STR_ROM_SIZE_ERR);
406
if (ReadFile(rom_fh, ROMBaseHost, ROMSize, &bytes_read, NULL) == 0 || bytes_read != ROMSize) {
407
ErrorAlert(STR_ROM_FILE_READ_ERR);
412
// Initialize native timers
415
// Initialize everything
418
D(bug("Initialization complete\n"));
420
// SDL threads available, start 60Hz thread
421
tick_thread_active = ((tick_thread = SDL_CreateThread(tick_func, NULL)) != NULL);
422
if (!tick_thread_active) {
423
sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
427
D(bug("60Hz thread started\n"));
429
// Start XPRAM watchdog thread
430
memcpy(last_xpram, XPRAM, XPRAM_SIZE);
431
xpram_thread_active = ((xpram_thread = SDL_CreateThread(xpram_func, NULL)) != NULL);
432
D(bug("XPRAM thread started\n"));
434
// Start 68k and jump to ROM boot routine
435
D(bug("Starting emulation...\n"));
447
void QuitEmulator(void)
449
D(bug("QuitEmulator\n"));
451
// Exit 680x0 emulation
455
if (tick_thread_active) {
456
tick_thread_cancel = true;
457
SDL_WaitThread(tick_thread, NULL);
460
// Stop XPRAM watchdog thread
461
if (xpram_thread_active) {
462
xpram_thread_cancel = true;
463
SDL_WaitThread(xpram_thread, NULL);
466
// Deinitialize everything
469
// Free ROM/RAM areas
470
if (RAMBaseHost != VM_MAP_FAILED) {
471
vm_release(RAMBaseHost, RAMSize);
474
if (ROMBaseHost != VM_MAP_FAILED) {
475
vm_release(ROMBaseHost, 0x100000);
479
#if USE_SCRATCHMEM_SUBTERFUGE
480
// Delete scratch memory area
481
if (ScratchMem != (uint8 *)VM_MAP_FAILED) {
482
vm_release((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
490
// Exit system routines
496
// Release win32 libraries
504
* Code was patched, flush caches if neccessary (i.e. when using a real 680x0
505
* or a dynamically recompiling emulator)
508
void FlushCodeCache(void *start, uint32 size)
512
flush_icache_range((uintptr)start, size);
522
B2_mutex() { m = SDL_CreateMutex(); }
523
~B2_mutex() { if (m) SDL_DestroyMutex(m); }
527
B2_mutex *B2_create_mutex(void)
532
void B2_lock_mutex(B2_mutex *mutex)
535
SDL_LockMutex(mutex->m);
538
void B2_unlock_mutex(B2_mutex *mutex)
541
SDL_UnlockMutex(mutex->m);
544
void B2_delete_mutex(B2_mutex *mutex)
551
* Interrupt flags (must be handled atomically!)
554
uint32 InterruptFlags = 0;
556
void SetInterruptFlag(uint32 flag)
559
InterruptFlags |= flag;
563
void ClearInterruptFlag(uint32 flag)
566
InterruptFlags &= ~flag;
572
* XPRAM watchdog thread (saves XPRAM every minute)
575
static void xpram_watchdog(void)
577
if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
578
memcpy(last_xpram, XPRAM, XPRAM_SIZE);
583
static int xpram_func(void *arg)
585
while (!xpram_thread_cancel) {
586
for (int i=0; i<60 && !xpram_thread_cancel; i++)
587
Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true
595
* 60Hz thread (really 60.15Hz)
598
static void one_second(void)
600
// Pseudo Mac 1Hz interrupt, update local time
601
WriteMacInt32(0x20c, TimerDateTime());
603
SetInterruptFlag(INTFLAG_1HZ);
607
static void one_tick(...)
609
static int tick_counter = 0;
610
if (++tick_counter > 60) {
615
// Trigger 60Hz interrupt
616
if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
617
SetInterruptFlag(INTFLAG_60HZ);
622
static int tick_func(void *arg)
624
uint64 start = GetTicks_usec();
626
uint64 next = GetTicks_usec();
627
while (!tick_thread_cancel) {
630
int64 delay = next - GetTicks_usec();
633
else if (delay < -16625)
634
next = GetTicks_usec();
637
uint64 end = GetTicks_usec();
638
D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
644
* Get the main window handle
648
#include <SDL_syswm.h>
649
static HWND GetMainWindowHandle(void)
651
SDL_SysWMinfo wmInfo;
652
wmInfo.version.major = SDL_MAJOR_VERSION;
653
wmInfo.version.minor = SDL_MINOR_VERSION;
654
wmInfo.version.patch = SDL_PATCHLEVEL;
655
return SDL_GetWMInfo(&wmInfo) ? wmInfo.window : NULL;
664
static void display_alert(int title_id, const char *text, int flags)
666
HWND hMainWnd = GetMainWindowHandle();
667
MessageBox(hMainWnd, text, GetString(title_id), MB_OK | flags);
672
* Display error alert
675
void ErrorAlert(const char *text)
677
if (PrefsFindBool("nogui"))
680
VideoQuitFullScreen();
681
display_alert(STR_ERROR_ALERT_TITLE, text, MB_ICONSTOP);
686
* Display warning alert
689
void WarningAlert(const char *text)
691
if (PrefsFindBool("nogui"))
694
display_alert(STR_WARNING_ALERT_TITLE, text, MB_ICONINFORMATION);
699
* Display choice alert
702
bool ChoiceAlert(const char *text, const char *pos, const char *neg)
704
printf(GetString(STR_SHELL_WARNING_PREFIX), text);