2
* timer_windows.cpp - Time Manager emulation, Windows specific stuff
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
23
#define WIN32_LEAN_AND_MEAN
27
#include "macos_util.h"
34
// Helper time functions
35
#define MSECS2TICKS(MSECS) (((uint64)(MSECS) * frequency) / 1000)
36
#define USECS2TICKS(USECS) (((uint64)(USECS) * frequency) / 1000000)
37
#define TICKS2USECS(TICKS) (((uint64)(TICKS) * 1000000) / frequency)
40
static uint32 frequency; // CPU frequency in Hz (< 4 GHz)
41
static tm_time_t mac_boot_ticks;
42
static tm_time_t mac_1904_ticks;
43
static tm_time_t mac_now_diff;
47
* Initialize native Windows timers
52
D(bug("SysTimerInit\n"));
55
if (!QueryPerformanceFrequency(&tt)) {
56
ErrorAlert("No high resolution timers available\n");
59
frequency = tt.LowPart;
60
D(bug(" frequency %d\n", frequency));
62
// mac_boot_ticks is 1.18 us since Basilisk II was started
63
QueryPerformanceCounter(&tt);
64
mac_boot_ticks = tt.QuadPart;
66
// mac_1904_ticks is 1.18 us since Mac time started 1904
67
mac_1904_ticks = time(NULL) * frequency;
68
mac_now_diff = mac_1904_ticks - mac_boot_ticks;
73
* Return microseconds since boot (64 bit)
76
void Microseconds(uint32 &hi, uint32 &lo)
78
D(bug("Microseconds\n"));
80
QueryPerformanceCounter(&tt);
81
tt.QuadPart = TICKS2USECS(tt.QuadPart - mac_boot_ticks);
88
* Return local date/time in Mac format (seconds since 1.1.1904)
91
uint32 TimerDateTime(void)
93
return TimeToMacTime(time(NULL));
101
void timer_current_time(tm_time_t &t)
104
QueryPerformanceCounter(&tt);
105
t = tt.QuadPart + mac_now_diff;
113
void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b)
123
void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b)
130
* Compare times (<0: a < b, =0: a = b, >0: a > b)
133
int timer_cmp_time(tm_time_t a, tm_time_t b)
136
return r < 0 ? -1 : (r > 0 ? 1 : 0);
141
* Convert Mac time value (>0: microseconds, <0: microseconds) to tm_time_t
144
void timer_mac2host_time(tm_time_t &res, int32 mactime)
147
// Time in milliseconds
148
res = MSECS2TICKS(mactime);
150
// Time in negative microseconds
151
res = USECS2TICKS(-mactime);
157
* Convert positive tm_time_t to Mac time value (>0: microseconds, <0: microseconds)
158
* A negative input value for hosttime results in a zero return value
159
* As long as the microseconds value fits in 32 bit, it must not be converted to milliseconds!
162
int32 timer_host2mac_time(tm_time_t hosttime)
167
uint64 t = TICKS2USECS(hosttime);
169
return t / 1000; // Time in milliseconds
171
return -t; // Time in negative microseconds
177
* Get current value of microsecond timer
180
uint64 GetTicks_usec(void)
183
QueryPerformanceCounter(&tt);
184
return TICKS2USECS(tt.QuadPart - mac_boot_ticks);
189
* Delay by specified number of microseconds (<1 second)
192
void Delay_usec(uint32 usec)
194
// FIXME: fortunately, Delay_usec() is generally used with
195
// millisecond resolution anyway
201
* Suspend emulator thread, virtual CPU in idle mode
211
* Resume execution of emulator thread, events just arrived
214
void idle_resume(void)