~ubuntu-branches/ubuntu/lucid/sdlmame/lucid

« back to all changes in this revision

Viewing changes to src/osd/sdl/sdlos_macosx.c

  • Committer: Bazaar Package Importer
  • Author(s): Cesare Falco
  • Date: 2009-11-03 17:10:15 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20091103171015-6hop4ory5lxnumpn
Tags: 0.135-0ubuntu1
* New upstream release - Closes (LP: #403212)
* debian/watch: unstable releases are no longer detected
* mame.ini: added the cheat subdirectories to cheatpath so zipped
  cheatfiles will be searched too
* renamed crsshair subdirectory to crosshair to reflect upstream change
* mame.ini: renamed references to crosshair subdirectory (see above)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//============================================================
 
2
//
 
3
//  sdlos_*.c - OS specific low level code
 
4
//
 
5
//  Copyright (c) 1996-2009, Nicola Salmoria and the MAME Team.
 
6
//  Visit http://mamedev.org for licensing and usage restrictions.
 
7
//
 
8
//  SDLMAME by Olivier Galibert and R. Belmont
 
9
//
 
10
//============================================================
 
11
 
 
12
// standard sdl header
 
13
#include <SDL/SDL.h>
 
14
#include <unistd.h>
 
15
#include <stdio.h>
 
16
#include <stdlib.h>
 
17
#include <string.h>
 
18
#include <unistd.h>
 
19
#include <ctype.h>
 
20
#include <sys/types.h>
 
21
#include <sys/mman.h>
 
22
#include <signal.h>
 
23
#include <unistd.h>
 
24
 
 
25
#include <mach/mach.h>
 
26
#include <mach/mach_time.h>
 
27
 
 
28
// MAME headers
 
29
#include "osdepend.h"
 
30
#include "osdcore.h"
 
31
 
 
32
//============================================================
 
33
//  PROTOTYPES
 
34
//============================================================
 
35
 
 
36
static osd_ticks_t init_cycle_counter(void);
 
37
static osd_ticks_t mach_cycle_counter(void);
 
38
 
 
39
//============================================================
 
40
//  STATIC VARIABLES
 
41
//============================================================
 
42
 
 
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;
 
46
 
 
47
//============================================================
 
48
//  init_cycle_counter
 
49
//
 
50
//  to avoid total grossness, this function is split by subarch
 
51
//============================================================
 
52
 
 
53
static osd_ticks_t init_cycle_counter(void)
 
54
{
 
55
        osd_ticks_t start, end;
 
56
        osd_ticks_t a, b;
 
57
 
 
58
        cycle_counter = mach_cycle_counter;
 
59
        ticks_counter = mach_cycle_counter;
 
60
 
 
61
        // wait for an edge on the timeGetTime call
 
62
        a = SDL_GetTicks();
 
63
        do
 
64
        {
 
65
                b = SDL_GetTicks();
 
66
        } while (a == b);
 
67
 
 
68
        // get the starting cycle count
 
69
        start = (*cycle_counter)();
 
70
 
 
71
        // now wait for 1/4 second total
 
72
        do
 
73
        {
 
74
                a = SDL_GetTicks();
 
75
        } while (a - b < 250);
 
76
 
 
77
        // get the ending cycle count
 
78
        end = (*cycle_counter)();
 
79
 
 
80
        // compute ticks_per_sec
 
81
        ticks_per_second = (end - start) * 4;
 
82
 
 
83
        // return the current cycle count
 
84
        return (*cycle_counter)();
 
85
}
 
86
 
 
87
//============================================================
 
88
//  performance_cycle_counter
 
89
//============================================================
 
90
 
 
91
//============================================================
 
92
//  mach_cycle_counter
 
93
//============================================================
 
94
static osd_ticks_t mach_cycle_counter(void)
 
95
{
 
96
        return mach_absolute_time();
 
97
}
 
98
 
 
99
//============================================================
 
100
//   osd_cycles
 
101
//============================================================
 
102
 
 
103
osd_ticks_t osd_ticks(void)
 
104
{
 
105
        return (*cycle_counter)();
 
106
}
 
107
 
 
108
 
 
109
//============================================================
 
110
//  osd_ticks_per_second
 
111
//============================================================
 
112
 
 
113
osd_ticks_t osd_ticks_per_second(void)
 
114
{
 
115
        if (ticks_per_second == 0)
 
116
        {
 
117
                return 1;       // this isn't correct, but it prevents the crash
 
118
        }
 
119
        return ticks_per_second;
 
120
}
 
121
 
 
122
 
 
123
 
 
124
//============================================================
 
125
//  osd_sleep
 
126
//============================================================
 
127
 
 
128
void osd_sleep(osd_ticks_t duration)
 
129
{
 
130
        UINT32 msec;
 
131
 
 
132
        // make sure we've computed ticks_per_second
 
133
        if (ticks_per_second == 0)
 
134
                (void)osd_ticks();
 
135
 
 
136
        // convert to milliseconds, rounding down
 
137
        msec = (UINT32)(duration * 1000 / ticks_per_second);
 
138
 
 
139
        // only sleep if at least 2 full milliseconds
 
140
        if (msec >= 2)
 
141
        {
 
142
                // take a couple of msecs off the top for good measure
 
143
                msec -= 2;
 
144
                usleep(msec*1000);
 
145
        }
 
146
}
 
147
 
 
148
//============================================================
 
149
//  osd_num_processors
 
150
//============================================================
 
151
 
 
152
int osd_num_processors(void)
 
153
{
 
154
        int processors = 1;
 
155
 
 
156
        struct host_basic_info host_basic_info;
 
157
        unsigned int count;
 
158
        kern_return_t r;
 
159
        mach_port_t my_mach_host_self;
 
160
        
 
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 )
 
164
        {
 
165
                processors = host_basic_info.avail_cpus;
 
166
        }
 
167
        mach_port_deallocate(mach_task_self(), my_mach_host_self);
 
168
        
 
169
        return processors;
 
170
}
 
171
 
 
172
//============================================================
 
173
//  osd_alloc_executable
 
174
//
 
175
//  allocates "size" bytes of executable memory.  this must take
 
176
//  things like NX support into account.
 
177
//============================================================
 
178
 
 
179
void *osd_alloc_executable(size_t size)
 
180
{
 
181
        return (void *)mmap(0, size, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
 
182
}
 
183
 
 
184
//============================================================
 
185
//  osd_free_executable
 
186
//
 
187
//  frees memory allocated with osd_alloc_executable
 
188
//============================================================
 
189
 
 
190
void osd_free_executable(void *ptr, size_t size)
 
191
{
 
192
        munmap(ptr, size);
 
193
}
 
194
 
 
195
//============================================================
 
196
//  osd_break_into_debugger
 
197
//============================================================
 
198
 
 
199
void osd_break_into_debugger(const char *message)
 
200
{
 
201
        #ifdef MAME_DEBUG
 
202
        printf("MAME exception: %s\n", message);
 
203
        printf("Attempting to fall into debugger\n");
 
204
        kill(getpid(), SIGTRAP); 
 
205
        #else
 
206
        printf("Ignoring MAME exception: %s\n", message);
 
207
        #endif
 
208
}
 
209