~ubuntu-branches/ubuntu/feisty/basilisk2/feisty

« back to all changes in this revision

Viewing changes to src/MacOSX/main_macosx.mm

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2006-06-01 01:11:16 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060601011116-xjhegbgyfsxag5fl
Tags: 0.9.20060529-1
* New upstream CVS snapshot.
* Update local cdbs snippet copyright-check.mk:
  + Broaden scan to also look for "(c)" by default.
  + Make egrep options configurable.
  + Ignore auto-tools files.
* Bump up standards-version to 3.7.2 (no changes needed).
* Let dh_strip do the stripping (not the make install target).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  $Id: main_macosx.mm,v 1.13 2005/01/30 21:42:13 gbeauche Exp $
 
2
 *  $Id: main_macosx.mm,v 1.16 2006/05/08 16:56:07 gbeauche Exp $
3
3
 *
4
4
 *  main_macosx.mm -    Startup code for MacOS X
5
5
 *                                              Based (in a small way) on the default main.m,
21
21
 *  along with this program; if not, write to the Free Software
22
22
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
23
 */
 
24
 
 
25
#import <AppKit/AppKit.h>
 
26
#undef check
 
27
 
24
28
#define PTHREADS        // Why is this here?
25
29
#include "sysdeps.h"
26
30
 
50
54
#include "xpram.h"
51
55
 
52
56
#if USE_JIT
53
 
extern void flush_icache_range(uint32 start, uint32 size);  // from compemu_support.cpp
 
57
extern void flush_icache_range(uint8 *start, uint32 size);  // from compemu_support.cpp
54
58
#endif
55
59
 
56
60
#ifdef ENABLE_MON
61
65
#include "debug.h"
62
66
 
63
67
 
64
 
#import <AppKit/AppKit.h>
65
 
 
66
68
#include "main_macosx.h"                // To bridge between main() and misc. classes
67
69
 
68
70
 
76
78
bool CPUIs68060;
77
79
int FPUType;
78
80
bool TwentyFourBitAddressing;
 
81
bool ThirtyThreeBitAddressing = false;
79
82
 
80
83
 
81
84
// Global variables
108
111
 
109
112
 
110
113
/*
 
114
 *  Helpers to map memory that can be accessed from the Mac side
 
115
 */
 
116
 
 
117
// NOTE: VM_MAP_33BIT is only used when compiling a 64-bit JIT on specific platforms
 
118
void *vm_acquire_mac(size_t size)
 
119
{
 
120
        void *m = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_33BIT);
 
121
#ifdef USE_33BIT_ADDRESSING
 
122
        if (m == VM_MAP_FAILED) {
 
123
                printf("WARNING: Cannot acquire memory in 33-bit address space (%s)\n", strerror(errno));
 
124
                ThirtyThreeBitAddressing = false;
 
125
                m = vm_acquire(size);
 
126
        }
 
127
#endif
 
128
        return m;
 
129
}
 
130
 
 
131
static int vm_acquire_mac_fixed(void *addr, size_t size)
 
132
{
 
133
        int ret = vm_acquire_fixed(addr, size, VM_MAP_DEFAULT | VM_MAP_33BIT);
 
134
#ifdef USE_33BIT_ADDRESSING
 
135
        if (ret < 0) {
 
136
                printf("WARNING: Cannot acquire fixed memory in 33-bit address space (%s)\n", strerror(errno));
 
137
                ThirtyThreeBitAddressing = false;
 
138
                ret = vm_acquire_fixed(addr, size);
 
139
        }
 
140
#endif
 
141
        return ret;
 
142
}
 
143
 
 
144
 
 
145
/*
111
146
 *  SIGSEGV handler
112
147
 */
113
148
 
156
191
#ifdef ENABLE_MON
157
192
        char *arg[4] = {"mon", "-m", "-r", NULL};
158
193
        mon(3, arg);
 
194
#endif
159
195
        QuitEmulator();
160
 
#endif
161
196
}
162
197
 
163
198
 
258
293
                WarningAlert(GetString(STR_SMALL_RAM_WARN));
259
294
                RAMSize = 1024*1024;
260
295
        }
 
296
        if (RAMSize > 1023*1024*1024)                                           // Cap to 1023MB (APD crashes at 1GB)
 
297
                RAMSize = 1023*1024*1024;
261
298
 
262
299
#if REAL_ADDRESSING || DIRECT_ADDRESSING
263
300
        RAMSize = RAMSize & -getpagesize();                                     // Round down to page boundary
266
303
        // Initialize VM system
267
304
        vm_init();
268
305
 
 
306
#ifdef USE_33BIT_ADDRESSING
 
307
        // Speculatively enables 33-bit addressing
 
308
        ThirtyThreeBitAddressing = true;
 
309
#endif
 
310
 
269
311
#if REAL_ADDRESSING
270
312
        // Flag: RAM and ROM are contigously allocated from address 0
271
313
        bool memory_mapped_from_zero = false;
272
 
        
273
 
        // Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash
274
 
        // when trying to map a too big chunk of memory starting at address 0
275
 
#if defined(OS_solaris) || defined(OS_netbsd) || defined(PAGEZERO_HACK)
 
314
 
 
315
        // Make sure to map RAM & ROM at address 0 only on platforms that
 
316
        // supports linker scripts to relocate the Basilisk II executable
 
317
        // above 0x70000000
 
318
#if HAVE_LINKER_SCRIPT
 
319
        const bool can_map_all_memory = true;
 
320
#else
276
321
        const bool can_map_all_memory = false;
277
 
#else
278
 
        const bool can_map_all_memory = true;
279
322
#endif
280
323
        
281
324
        // Try to allocate all memory from 0x0000, if it is not known to crash
282
 
        if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) {
 
325
        if (can_map_all_memory && (vm_acquire_mac_fixed(0, RAMSize + 0x100000) == 0)) {
283
326
                D(bug("Could allocate RAM and ROM from 0x0000\n"));
284
327
                memory_mapped_from_zero = true;
285
328
        }
286
 
 
 
329
        
287
330
#ifndef PAGEZERO_HACK
288
331
        // Otherwise, just create the Low Memory area (0x0000..0x2000)
289
 
        else if (vm_acquire_fixed(0, 0x2000) == 0) {
 
332
        else if (vm_acquire_mac_fixed(0, 0x2000) == 0) {
290
333
                D(bug("Could allocate the Low Memory globals\n"));
291
334
                lm_area_mapped = true;
292
335
        }
311
354
        else
312
355
#endif
313
356
        {
314
 
                RAMBaseHost = (uint8 *)vm_acquire(RAMSize);
315
 
                ROMBaseHost = (uint8 *)vm_acquire(0x100000);
316
 
                if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) {
 
357
                uint8 *ram_rom_area = (uint8 *)vm_acquire_mac(RAMSize + 0x100000);
 
358
                if (ram_rom_area == VM_MAP_FAILED) { 
317
359
                        ErrorAlert(STR_NO_MEM_ERR);
318
360
                        QuitEmulator();
319
361
                }
 
362
                RAMBaseHost = ram_rom_area;
 
363
                ROMBaseHost = RAMBaseHost + RAMSize;
320
364
        }
321
365
 
322
366
#if USE_SCRATCHMEM_SUBTERFUGE
323
367
        // Allocate scratch memory
324
 
        ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE);
 
368
        ScratchMem = (uint8 *)vm_acquire_mac(SCRATCH_MEM_SIZE);
325
369
        if (ScratchMem == VM_MAP_FAILED) {
326
370
                ErrorAlert(STR_NO_MEM_ERR);
327
371
                QuitEmulator();
336
380
        ROMBaseMac = Host2MacAddr(ROMBaseHost);
337
381
#endif
338
382
#if REAL_ADDRESSING
339
 
        RAMBaseMac = (uint32)RAMBaseHost;
340
 
        ROMBaseMac = (uint32)ROMBaseHost;
 
383
        RAMBaseMac = Host2MacAddr(RAMBaseHost);
 
384
        ROMBaseMac = Host2MacAddr(ROMBaseHost);
341
385
#endif
342
386
        D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
343
387
        D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
405
449
 
406
450
        // Free ROM/RAM areas
407
451
        if (RAMBaseHost != VM_MAP_FAILED) {
408
 
                vm_release(RAMBaseHost, RAMSize);
 
452
                vm_release(RAMBaseHost, RAMSize + 0x100000);
409
453
                RAMBaseHost = NULL;
410
 
        }
411
 
        if (ROMBaseHost != VM_MAP_FAILED) {
412
 
                vm_release(ROMBaseHost, 0x100000);
413
454
                ROMBaseHost = NULL;
414
455
        }
415
456
 
439
480
 
440
481
void QuitEmulator(void)
441
482
{
442
 
        extern  NSApplication *NSApp;
443
 
 
444
 
 
445
483
        QuitEmuNoExit();
446
484
 
447
485
        // Stop run loop?
460
498
{
461
499
#if USE_JIT
462
500
    if (UseJIT)
463
 
                flush_icache_range((uintptr)start, size);
 
501
                flush_icache_range((uint8 *)start, size);
464
502
#endif
465
503
}
466
504
 
483
521
#endif
484
522
 
485
523
 
 
524
#ifdef HAVE_PTHREADS
 
525
/*
 
526
 *  Pthread configuration
 
527
 */
 
528
 
 
529
void Set_pthread_attr(pthread_attr_t *attr, int priority)
 
530
{
 
531
        pthread_attr_init(attr);
 
532
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
 
533
        // Some of these only work for superuser
 
534
        if (geteuid() == 0) {
 
535
                pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
 
536
                pthread_attr_setschedpolicy(attr, SCHED_FIFO);
 
537
                struct sched_param fifo_param;
 
538
                fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO)
 
539
                                                                         + sched_get_priority_max(SCHED_FIFO))
 
540
                                                                         / 2 + priority);
 
541
                pthread_attr_setschedparam(attr, &fifo_param);
 
542
        }
 
543
        if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) {
 
544
#ifdef PTHREAD_SCOPE_BOUND_NP
 
545
            // If system scope is not available (eg. we're not running
 
546
            // with CAP_SCHED_MGT capability on an SGI box), try bound
 
547
            // scope.  It exposes pthread scheduling to the kernel,
 
548
            // without setting realtime priority.
 
549
            pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP);
 
550
#endif
 
551
        }
 
552
#endif
 
553
}
 
554
#endif // HAVE_PTHREADS
 
555
 
 
556
 
486
557
/*
487
558
 *  Mutexes
488
559
 */