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

« back to all changes in this revision

Viewing changes to src/osd/sdl/sdlos_win32.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
 
 
17
 
 
18
#define WIN32_LEAN_AND_MEAN
 
19
#include <windows.h>
 
20
 
 
21
// MAME headers
 
22
#include "osdcore.h"
 
23
 
 
24
//============================================================
 
25
//  PROTOTYPES
 
26
//============================================================
 
27
 
 
28
static osd_ticks_t init_cycle_counter(void);
 
29
static osd_ticks_t performance_cycle_counter(void);
 
30
 
 
31
//============================================================
 
32
//  STATIC VARIABLES
 
33
//============================================================
 
34
 
 
35
// global cycle_counter function and divider
 
36
static osd_ticks_t              (*cycle_counter)(void) = init_cycle_counter;
 
37
static osd_ticks_t              (*ticks_counter)(void) = init_cycle_counter;
 
38
static osd_ticks_t              ticks_per_second;
 
39
 
 
40
//============================================================
 
41
//  init_cycle_counter
 
42
//
 
43
//  to avoid total grossness, this function is split by subarch
 
44
//============================================================
 
45
 
 
46
static osd_ticks_t init_cycle_counter(void)
 
47
{
 
48
        osd_ticks_t start, end;
 
49
        osd_ticks_t a, b;
 
50
        int priority = GetThreadPriority(GetCurrentThread());
 
51
        LARGE_INTEGER frequency;
 
52
 
 
53
        if (QueryPerformanceFrequency( &frequency ))
 
54
        {
 
55
                // use performance counter if available as it is constant
 
56
                cycle_counter = performance_cycle_counter;
 
57
                ticks_counter = performance_cycle_counter;
 
58
 
 
59
                ticks_per_second = frequency.QuadPart;
 
60
 
 
61
                // return the current cycle count
 
62
                return (*cycle_counter)();
 
63
        }
 
64
        else
 
65
        {
 
66
                fprintf(stderr, "Error!  Unable to QueryPerformanceFrequency!\n");
 
67
                exit(-1);
 
68
        }
 
69
 
 
70
        // temporarily set our priority higher
 
71
        SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
 
72
 
 
73
        // wait for an edge on the timeGetTime call
 
74
        a = SDL_GetTicks();
 
75
        do
 
76
        {
 
77
                b = SDL_GetTicks();
 
78
        } while (a == b);
 
79
 
 
80
        // get the starting cycle count
 
81
        start = (*cycle_counter)();
 
82
 
 
83
        // now wait for 1/4 second total
 
84
        do
 
85
        {
 
86
                a = SDL_GetTicks();
 
87
        } while (a - b < 250);
 
88
 
 
89
        // get the ending cycle count
 
90
        end = (*cycle_counter)();
 
91
 
 
92
        // compute ticks_per_sec
 
93
        ticks_per_second = (end - start) * 4;
 
94
 
 
95
        // restore our priority
 
96
        SetThreadPriority(GetCurrentThread(), priority);
 
97
 
 
98
        // return the current cycle count
 
99
        return (*cycle_counter)();
 
100
}
 
101
 
 
102
//============================================================
 
103
//  performance_cycle_counter
 
104
//============================================================
 
105
 
 
106
static osd_ticks_t performance_cycle_counter(void)
 
107
{
 
108
        LARGE_INTEGER performance_count;
 
109
        QueryPerformanceCounter(&performance_count);
 
110
        return (osd_ticks_t)performance_count.QuadPart;
 
111
}
 
112
 
 
113
//============================================================
 
114
//   osd_cycles
 
115
//============================================================
 
116
 
 
117
osd_ticks_t osd_ticks(void)
 
118
{
 
119
        return (*cycle_counter)();
 
120
}
 
121
 
 
122
 
 
123
//============================================================
 
124
//  osd_ticks_per_second
 
125
//============================================================
 
126
 
 
127
osd_ticks_t osd_ticks_per_second(void)
 
128
{
 
129
        if (ticks_per_second == 0)
 
130
        {
 
131
                return 1;       // this isn't correct, but it prevents the crash
 
132
        }
 
133
        return ticks_per_second;
 
134
}
 
135
 
 
136
//============================================================
 
137
//  osd_sleep
 
138
//============================================================
 
139
 
 
140
void osd_sleep(osd_ticks_t duration)
 
141
{
 
142
        UINT32 msec;
 
143
 
 
144
        // make sure we've computed ticks_per_second
 
145
        if (ticks_per_second == 0)
 
146
                (void)osd_ticks();
 
147
 
 
148
        // convert to milliseconds, rounding down
 
149
        msec = (UINT32)(duration * 1000 / ticks_per_second);
 
150
 
 
151
        // only sleep if at least 2 full milliseconds
 
152
        if (msec >= 2)
 
153
        {
 
154
                // take a couple of msecs off the top for good measure
 
155
                msec -= 2;
 
156
                Sleep(msec);
 
157
        }
 
158
}
 
159
 
 
160
//============================================================
 
161
//  osd_num_processors
 
162
//============================================================
 
163
 
 
164
int osd_num_processors(void)
 
165
{
 
166
        SYSTEM_INFO info;
 
167
 
 
168
        // otherwise, fetch the info from the system
 
169
        GetSystemInfo(&info);
 
170
 
 
171
        // max out at 4 for now since scaling above that seems to do poorly
 
172
        return MIN(info.dwNumberOfProcessors, 4);
 
173
}
 
174
 
 
175
//============================================================
 
176
//  osd_alloc_executable
 
177
//
 
178
//  allocates "size" bytes of executable memory.  this must take
 
179
//  things like NX support into account.
 
180
//============================================================
 
181
 
 
182
void *osd_alloc_executable(size_t size)
 
183
{
 
184
        return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
 
185
}
 
186
 
 
187
//============================================================
 
188
//  osd_free_executable
 
189
//
 
190
//  frees memory allocated with osd_alloc_executable
 
191
//============================================================
 
192
 
 
193
void osd_free_executable(void *ptr, size_t size)
 
194
{
 
195
        VirtualFree(ptr, 0, MEM_RELEASE);
 
196
}
 
197
 
 
198
//============================================================
 
199
//  osd_break_into_debugger
 
200
//============================================================
 
201
 
 
202
void osd_break_into_debugger(const char *message)
 
203
{
 
204
        if (IsDebuggerPresent())
 
205
        {
 
206
                OutputDebugString(message);
 
207
                DebugBreak();
 
208
        }
 
209
}
 
210