1
//============================================================
3
// sdlos_*.c - OS specific low level code
5
// Copyright (c) 1996-2009, Nicola Salmoria and the MAME Team.
6
// Visit http://mamedev.org for licensing and usage restrictions.
8
// SDLMAME by Olivier Galibert and R. Belmont
10
//============================================================
12
// standard sdl header
20
#include <sys/types.h>
25
#include <mach/mach.h>
26
#include <mach/mach_time.h>
32
//============================================================
34
//============================================================
36
static osd_ticks_t init_cycle_counter(void);
37
static osd_ticks_t mach_cycle_counter(void);
39
//============================================================
41
//============================================================
43
static osd_ticks_t (*cycle_counter)(void) = init_cycle_counter;
44
static osd_ticks_t (*ticks_counter)(void) = init_cycle_counter;
45
static osd_ticks_t ticks_per_second;
47
//============================================================
50
// to avoid total grossness, this function is split by subarch
51
//============================================================
53
static osd_ticks_t init_cycle_counter(void)
55
osd_ticks_t start, end;
58
cycle_counter = mach_cycle_counter;
59
ticks_counter = mach_cycle_counter;
61
// wait for an edge on the timeGetTime call
68
// get the starting cycle count
69
start = (*cycle_counter)();
71
// now wait for 1/4 second total
75
} while (a - b < 250);
77
// get the ending cycle count
78
end = (*cycle_counter)();
80
// compute ticks_per_sec
81
ticks_per_second = (end - start) * 4;
83
// return the current cycle count
84
return (*cycle_counter)();
87
//============================================================
88
// performance_cycle_counter
89
//============================================================
91
//============================================================
93
//============================================================
94
static osd_ticks_t mach_cycle_counter(void)
96
return mach_absolute_time();
99
//============================================================
101
//============================================================
103
osd_ticks_t osd_ticks(void)
105
return (*cycle_counter)();
109
//============================================================
110
// osd_ticks_per_second
111
//============================================================
113
osd_ticks_t osd_ticks_per_second(void)
115
if (ticks_per_second == 0)
117
return 1; // this isn't correct, but it prevents the crash
119
return ticks_per_second;
124
//============================================================
126
//============================================================
128
void osd_sleep(osd_ticks_t duration)
132
// make sure we've computed ticks_per_second
133
if (ticks_per_second == 0)
136
// convert to milliseconds, rounding down
137
msec = (UINT32)(duration * 1000 / ticks_per_second);
139
// only sleep if at least 2 full milliseconds
142
// take a couple of msecs off the top for good measure
148
//============================================================
149
// osd_num_processors
150
//============================================================
152
int osd_num_processors(void)
156
struct host_basic_info host_basic_info;
159
mach_port_t my_mach_host_self;
161
count = HOST_BASIC_INFO_COUNT;
162
my_mach_host_self = mach_host_self();
163
if ( ( r = host_info(my_mach_host_self, HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count)) == KERN_SUCCESS )
165
processors = host_basic_info.avail_cpus;
167
mach_port_deallocate(mach_task_self(), my_mach_host_self);
172
//============================================================
173
// osd_alloc_executable
175
// allocates "size" bytes of executable memory. this must take
176
// things like NX support into account.
177
//============================================================
179
void *osd_alloc_executable(size_t size)
181
return (void *)mmap(0, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
184
//============================================================
185
// osd_free_executable
187
// frees memory allocated with osd_alloc_executable
188
//============================================================
190
void osd_free_executable(void *ptr, size_t size)
195
//============================================================
196
// osd_break_into_debugger
197
//============================================================
199
void osd_break_into_debugger(const char *message)
202
printf("MAME exception: %s\n", message);
203
printf("Attempting to fall into debugger\n");
204
kill(getpid(), SIGTRAP);
206
printf("Ignoring MAME exception: %s\n", message);