ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Windows/timer_windows.cpp
Revision: 1.1
Committed: 2004-11-28T17:54:05Z (20 years ago) by gbeauche
Branch: MAIN
Log Message:
add native windows timers

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * timer_windows.cpp - Time Manager emulation, Windows specific stuff
3     *
4     * Basilisk II (C) 1997-2004 Christian Bauer
5     *
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.
10     *
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.
15     *
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
19     */
20    
21     #include "sysdeps.h"
22    
23     #define WIN32_LEAN_AND_MEAN
24     #include <windows.h>
25    
26     #include "main.h"
27     #include "macos_util.h"
28     #include "timer.h"
29    
30     #define DEBUG 0
31     #include "debug.h"
32    
33    
34     // Helper time functions
35     #define MSECS2TICKS(MSECS) (((uint64)(MSECS) * frequency) / 1000)
36     #define USECS2TICKS(USECS) (((uint64)(USECS) * frequency) / 1000000)
37     #define TICKS2MSECS(TICKS) (((uint64)(TICKS) * 1000) / frequency)
38     #define TICKS2USECS(TICKS) (((uint64)(TICKS) * 1000000) / frequency)
39    
40     // Global variables
41     static uint32 frequency; // CPU frequency in Hz (< 4 GHz)
42     static tm_time_t mac_boot_ticks;
43     static tm_time_t mac_1904_ticks;
44     static tm_time_t mac_now_diff;
45    
46    
47     /*
48     * Initialize native Windows timers
49     */
50    
51     void SysTimerInit(void)
52     {
53     D(bug("SysTimerInit\n"));
54    
55     LARGE_INTEGER tt;
56     if (!QueryPerformanceFrequency(&tt)) {
57     ErrorAlert("No high resolution timers available\n");
58     QuitEmulator();
59     }
60     frequency = tt.LowPart;
61     D(bug(" frequency %d\n", frequency));
62    
63     // mac_boot_ticks is 1.18 us since Basilisk II was started
64     QueryPerformanceCounter(&tt);
65     mac_boot_ticks = tt.QuadPart;
66    
67     // mac_1904_ticks is 1.18 us since Mac time started 1904
68     mac_1904_ticks = time(NULL) * frequency;
69     mac_now_diff = mac_1904_ticks - mac_boot_ticks;
70     }
71    
72    
73     /*
74     * Return microseconds since boot (64 bit)
75     */
76    
77     void Microseconds(uint32 &hi, uint32 &lo)
78     {
79     D(bug("Microseconds\n"));
80     LARGE_INTEGER tt;
81     QueryPerformanceCounter(&tt);
82     tt.QuadPart = TICKS2USECS(tt.QuadPart - mac_boot_ticks);
83     hi = tt.HighPart;
84     lo = tt.LowPart;
85     }
86    
87    
88     /*
89     * Return local date/time in Mac format (seconds since 1.1.1904)
90     */
91    
92     uint32 TimerDateTime(void)
93     {
94     return TimeToMacTime(time(NULL));
95     }
96    
97    
98     /*
99     * Get current time
100     */
101    
102     void timer_current_time(tm_time_t &t)
103     {
104     LARGE_INTEGER tt;
105     QueryPerformanceCounter(&tt);
106     t = tt.QuadPart + mac_now_diff;
107     }
108    
109    
110     /*
111     * Add times
112     */
113    
114     void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b)
115     {
116     res = a + b;
117     }
118    
119    
120     /*
121     * Subtract times
122     */
123    
124     void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b)
125     {
126     res = a - b;
127     }
128    
129    
130     /*
131     * Compare times (<0: a < b, =0: a = b, >0: a > b)
132     */
133    
134     int timer_cmp_time(tm_time_t a, tm_time_t b)
135     {
136     tm_time_t r = a - b;
137     return r < 0 ? -1 : (r > 0 ? 1 : 0);
138     }
139    
140    
141     /*
142     * Convert Mac time value (>0: microseconds, <0: microseconds) to tm_time_t
143     */
144    
145     void timer_mac2host_time(tm_time_t &res, int32 mactime)
146     {
147     if (mactime > 0) {
148     // Time in milliseconds
149     res = MSECS2TICKS(mactime);
150     } else {
151     // Time in negative microseconds
152     res = USECS2TICKS(-mactime);
153     }
154     }
155    
156    
157     /*
158     * Convert positive tm_time_t to Mac time value (>0: microseconds, <0: microseconds)
159     * A negative input value for hosttime results in a zero return value
160     * As long as the microseconds value fits in 32 bit, it must not be converted to milliseconds!
161     */
162    
163     int32 timer_host2mac_time(tm_time_t hosttime)
164     {
165     if (hosttime < 0)
166     return 0;
167     else {
168     uint64 t = TICKS2MSECS(hosttime);
169     if (t > 0x7fffffff)
170     return t / 1000; // Time in milliseconds
171     else
172     return -t; // Time in negative microseconds
173     }
174     }
175    
176    
177     /*
178     * Get current value of microsecond timer
179     */
180    
181     uint64 GetTicks_usec(void)
182     {
183     LARGE_INTEGER tt;
184     QueryPerformanceCounter(&tt);
185     return TICKS2USECS(tt.QuadPart - mac_boot_ticks);
186     }
187    
188    
189     /*
190     * Delay by specified number of microseconds (<1 second)
191     */
192    
193     void Delay_usec(uint32 usec)
194     {
195     // FIXME: fortunately, Delay_usec() is generally used with
196     // millisecond resolution anyway
197     Sleep(usec / 1000);
198     }