1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* UAE - The Un*x Amiga Emulator
*
* Miscellaneous machine dependent support functions and definitions
*
* Copyright 1996 Bernd Schmidt
* Copyright 2003-2005 Richard Drummond
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "sleep.h"
#include "machdep/rpt.h"
#include "machdep/m68k.h"
#include "events.h"
#include "custom.h"
#ifndef USE_UNDERSCORE
#define LARGE_ALIGNMENT ".align 16\n"
#else
#define LARGE_ALIGNMENT ".align 4,0x90\n"
#endif
struct flag_struct regflags;
#include <signal.h>
#ifdef __linux__
/*
* Extract x86/AMD64 timestamp counter frequency
* from /proc/cpuinfo.
*
* TODO: Make this more robust.
*/
frame_time_t linux_get_tsc_freq (void)
{
int cpuinfo_fd;
char buffer[1024];
uae_s64 tsc_freq = 0;
cpuinfo_fd = open ("/proc/cpuinfo", O_RDONLY);
if (cpuinfo_fd >= 0) {
char *ptr = &buffer[0];
int size_read = read (cpuinfo_fd, ptr, 1024);
while (size_read > 0) {
if (strncmp (ptr, "bogomips\t: ", 11) != 0) {
while ((size_read-- > 0) && (*ptr != '\n'))
ptr++;
size_read--;
ptr++;
continue;
} else {
ptr += 11;
tsc_freq = atoll (ptr) * 1000000 / 2;
}
}
}
close (cpuinfo_fd);
return tsc_freq;
}
#endif
#ifdef __BEOS__
# include <be/kernel/OS.h>
/*
* Get timestamp counter frequency from the kernel
*/
static frame_time_t beos_get_tsc_freq (void)
{
system_info info;
get_system_info (&info);
return info.cpu_clock_speed;
}
#endif
#ifdef __APPLE__
frame_time_t apple_get_tsc_freq (void)
{
int sysctl_hw;
char buffer[1024];
uae_s64 tsc_freq = 0;
sysctl_hw = open ("sysctl -a hw", O_RDONLY);
if (sysctl_hw >= 0) {
char *ptr = &buffer[0];
int size_read = read (sysctl_hw, ptr, 1024);
while (size_read > 0) {
if (strncmp (ptr, "hw.cpufrequency: ", 17) != 0) {
while ((size_read-- > 0) && (*ptr != '\n'))
ptr++;
size_read--;
ptr++;
continue;
} else {
ptr += 17;
tsc_freq = atoll (ptr) * 1000000 / 2;
}
}
}
close (sysctl_hw);
return tsc_freq;
}
#endif
static volatile frame_time_t last_time, best_time;
static frame_time_t timebase;
static volatile int loops_to_go;
#if defined HAVE_SETITIMER || defined HAVE_ALARM
# define USE_ALARM
# ifndef HAVE_SETITIMER
# define TIME_UNIT 1000000
# else
# define TIME_UNIT 100000
# endif
#else
# define TIME_DELAY 200
# define TIME_UNIT (TIME_DELAY*1000)
#endif
#ifndef HAVE_SYNC
# define sync()
#endif
#ifdef USE_ALARM
static void set_the_alarm (void)
{
# ifndef HAVE_SETITIMER
alarm (1);
# else
struct itimerval t;
t.it_value.tv_sec = 0;
t.it_value.tv_usec = TIME_UNIT;
t.it_interval.tv_sec = 0;
t.it_interval.tv_usec = TIME_UNIT;
setitimer (ITIMER_REAL, &t, NULL);
# endif
}
static int first_loop = 1;
#ifdef __cplusplus
static RETSIGTYPE alarmhandler(...)
#else
static RETSIGTYPE alarmhandler(int foo)
#endif
{
frame_time_t bar;
bar = read_processor_time ();
if (! first_loop && bar - last_time < best_time)
best_time = bar - last_time;
first_loop = 0;
if (--loops_to_go > 0) {
signal (SIGALRM, alarmhandler);
last_time = read_processor_time ();
set_the_alarm ();
} else {
alarm (0);
signal (SIGALRM, SIG_IGN);
}
}
#endif /* USE_ALARM */
#include <setjmp.h>
static jmp_buf catch_test;
#ifdef __cplusplus
static RETSIGTYPE illhandler (...)
#else
static RETSIGTYPE illhandler (int foo)
#endif
{
rpt_available = 0;
longjmp (catch_test, 1);
}
int machdep_inithrtimer (void)
{
static int done = 0;
if (!done) {
rpt_available = 1;
write_log ("Testing the RDTSC instruction ... ");
signal (SIGILL, illhandler);
if (setjmp (catch_test) == 0)
read_processor_time ();
signal (SIGILL, SIG_DFL);
write_log ("done.\n");
if (! rpt_available) {
write_log ("Your processor does not support the RDTSC instruction.\n");
return 0;
}
timebase = 0;
#ifdef __linux__
timebase = linux_get_tsc_freq ();
#else
#ifdef __BEOS__
timebase = beos_get_tsc_freq ();
#endif
#ifdef __APPLE__
// timebase = apple_get_tsc_freq ();
#endif
#endif
if (timebase <= 0) {
write_log ("Calibrating TSC frequency...");
flush_log ();
best_time = MAX_FRAME_TIME;
loops_to_go = 5;
#ifdef USE_ALARM
signal (SIGALRM, alarmhandler);
#endif
/* We want exact values... */
sync (); sync (); sync ();
#ifdef USE_ALARM
last_time = read_processor_time ();
set_the_alarm ();
while (loops_to_go != 0)
uae_msleep (10);
#else
int i = loops_to_go;
frame_time_t bar;
while (i-- > 0) {
last_time = read_processor_time ();
uae_msleep (TIME_DELAY);
bar = read_processor_time ();
if (i != loops_to_go && bar - last_time < best_time)
best_time = bar - last_time;
}
#endif
timebase = best_time * (1000000.0 / TIME_UNIT);
}
write_log ("TSC frequency: %f MHz\n", timebase / 1000000.0);
done = 1;
}
return done;
}
frame_time_t machdep_gethrtimebase (void)
{
return timebase;
}
int machdep_init (void)
{
return 1;
}
/*
* Handle processor-specific cfgfile options
*/
void machdep_save_options (FILE *f, const struct uae_prefs *p)
{
cfgfile_write (f, MACHDEP_NAME ".use_tsc=%s\n", p->use_processor_clock ? "yes" : "no");
}
int machdep_parse_option (struct uae_prefs *p, const char *option, const char *value)
{
return cfgfile_yesno (option, value, "use_tsc", &p->use_processor_clock);
}
void machdep_default_options (struct uae_prefs *p)
{
p->use_processor_clock = 1;
}