| 1 |
#ifndef __APPLE__ |
| 2 |
#ifndef _MSC_VER |
| 3 |
#define _XOPEN_SOURCE 600 |
| 4 |
#define _POSIX_C_SOURCE 1 |
| 5 |
#endif |
| 6 |
#endif |
| 7 |
#include "glue.h" |
| 8 |
#include "video.h" |
| 9 |
#include "timing.h" |
| 10 |
#include "cpu/fake6502.h" |
| 11 |
#include <SDL.h> |
| 12 |
#include <stdio.h> |
| 13 |
#include "compat.h" |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define MAX_CATCHUP_US (250 * 1000) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#define THROTTLE_SLICE_THRESHOLD_US (25 * 1000) |
| 27 |
#define THROTTLE_SLICE_US (12 * 1000) |
| 28 |
|
| 29 |
static void timing_update_ex(bool may_sleep); |
| 30 |
|
| 31 |
uint32_t frames; |
| 32 |
uint32_t sdlTicks_base; |
| 33 |
uint32_t last_perf_update; |
| 34 |
uint32_t clockticks6502_old; |
| 35 |
int64_t cpu_ticks; |
| 36 |
int64_t last_perf_cpu_ticks; |
| 37 |
char window_title[255]; |
| 38 |
|
| 39 |
|
| 40 |
static int emu_speed_khz = 0; |
| 41 |
|
| 42 |
int |
| 43 |
timing_native_khz(void) |
| 44 |
{ |
| 45 |
return (int)MHZ * 1000; |
| 46 |
} |
| 47 |
|
| 48 |
int |
| 49 |
timing_get_speed_khz(void) |
| 50 |
{ |
| 51 |
return emu_speed_khz > 0 ? emu_speed_khz : timing_native_khz(); |
| 52 |
} |
| 53 |
|
| 54 |
int |
| 55 |
timing_get_speed_percent(void) |
| 56 |
{ |
| 57 |
const int native = timing_native_khz(); |
| 58 |
return native > 0 |
| 59 |
? (int)(((int64_t)timing_get_speed_khz() * 100 + native / 2) / native) |
| 60 |
: 100; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static int64_t |
| 68 |
scaled_us(int64_t ticks) |
| 69 |
{ |
| 70 |
const int khz = timing_get_speed_khz(); |
| 71 |
return khz > 0 ? ticks * 1000 / (int64_t)khz : ticks / (MHZ ? MHZ : 1); |
| 72 |
} |
| 73 |
|
| 74 |
static int64_t |
| 75 |
scaled_emulated_us(void) |
| 76 |
{ |
| 77 |
return scaled_us(cpu_ticks); |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
timing_set_speed_khz(int khz) |
| 82 |
{ |
| 83 |
if (khz < TIMING_SPEED_MIN_KHZ) |
| 84 |
khz = TIMING_SPEED_MIN_KHZ; |
| 85 |
if (khz > TIMING_SPEED_MAX_KHZ) |
| 86 |
khz = TIMING_SPEED_MAX_KHZ; |
| 87 |
if (khz == emu_speed_khz) |
| 88 |
return; |
| 89 |
emu_speed_khz = khz; |
| 90 |
|
| 91 |
|
| 92 |
timing_init(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static const int speed_ladder_khz[] = { |
| 99 |
25, 50, 100, 250, 500, 750, |
| 100 |
1000, 1500, 2000, 3000, 4000, 6000, |
| 101 |
8000, 10000, 12000, 14000, 16000, 20000, 24000, |
| 102 |
|
| 103 |
|
| 104 |
32000, 40000, 48000, 64000, 80000, 100000, |
| 105 |
}; |
| 106 |
#define SPEED_LADDER_COUNT ((int)(sizeof(speed_ladder_khz) / sizeof(speed_ladder_khz[0]))) |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static int |
| 111 |
build_ladder(int *out, int max) |
| 112 |
{ |
| 113 |
const int native = timing_native_khz(); |
| 114 |
int n = 0; |
| 115 |
bool inserted = false; |
| 116 |
|
| 117 |
for (int i = 0; i < SPEED_LADDER_COUNT && n < max; i++) { |
| 118 |
const int v = speed_ladder_khz[i]; |
| 119 |
if (!inserted && v >= native) { |
| 120 |
out[n++] = native; |
| 121 |
inserted = true; |
| 122 |
if (v == native) |
| 123 |
continue; |
| 124 |
if (n >= max) |
| 125 |
break; |
| 126 |
} |
| 127 |
out[n++] = v; |
| 128 |
} |
| 129 |
if (!inserted && n < max) |
| 130 |
out[n++] = native; |
| 131 |
return n; |
| 132 |
} |
| 133 |
|
| 134 |
void |
| 135 |
timing_step_speed(int direction) |
| 136 |
{ |
| 137 |
int ladder[SPEED_LADDER_COUNT + 1]; |
| 138 |
const int n = build_ladder(ladder, (int)(sizeof(ladder) / sizeof(ladder[0]))); |
| 139 |
if (n <= 0) |
| 140 |
return; |
| 141 |
|
| 142 |
|
| 143 |
const int cur = timing_get_speed_khz(); |
| 144 |
int best = 0; |
| 145 |
int bestd = 1 << 30; |
| 146 |
for (int i = 0; i < n; i++) { |
| 147 |
const int d = ladder[i] > cur ? ladder[i] - cur : cur - ladder[i]; |
| 148 |
if (d < bestd) { |
| 149 |
bestd = d; |
| 150 |
best = i; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
int idx = best; |
| 155 |
if (ladder[best] == cur) { |
| 156 |
idx = best + (direction > 0 ? 1 : (direction < 0 ? -1 : 0)); |
| 157 |
} else if (direction > 0 && ladder[best] < cur) { |
| 158 |
idx = best + 1; |
| 159 |
} else if (direction < 0 && ladder[best] > cur) { |
| 160 |
idx = best - 1; |
| 161 |
} |
| 162 |
if (idx < 0) |
| 163 |
idx = 0; |
| 164 |
if (idx >= n) |
| 165 |
idx = n - 1; |
| 166 |
timing_set_speed_khz(ladder[idx]); |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
timing_reset_speed(void) |
| 171 |
{ |
| 172 |
timing_set_speed_khz(timing_native_khz()); |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
timing_init() { |
| 177 |
frames = 0; |
| 178 |
sdlTicks_base = SDL_GetTicks(); |
| 179 |
last_perf_update = 0; |
| 180 |
last_perf_cpu_ticks = 0; |
| 181 |
clockticks6502_old = clockticks6502; |
| 182 |
cpu_ticks = 0; |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
timing_update() |
| 187 |
{ |
| 188 |
timing_update_ex(true); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
void |
| 196 |
timing_update_no_sleep() |
| 197 |
{ |
| 198 |
timing_update_ex(false); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
int64_t |
| 204 |
timing_lead_us() |
| 205 |
{ |
| 206 |
int64_t ticks = cpu_ticks + (int64_t)(clockticks6502 - clockticks6502_old); |
| 207 |
uint32_t sdlTicks = SDL_GetTicks() - sdlTicks_base; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
return scaled_us(ticks) - sdlTicks * 1000LL; |
| 213 |
} |
| 214 |
|
| 215 |
static void |
| 216 |
timing_update_ex(bool may_sleep) |
| 217 |
{ |
| 218 |
frames++; |
| 219 |
cpu_ticks += clockticks6502 - clockticks6502_old; |
| 220 |
clockticks6502_old = clockticks6502; |
| 221 |
uint32_t sdlTicks = SDL_GetTicks() - sdlTicks_base; |
| 222 |
int64_t diff_time = scaled_emulated_us() - sdlTicks * 1000LL; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if (!warp_mode && diff_time < -(int64_t)MAX_CATCHUP_US) { |
| 230 |
uint32_t now = SDL_GetTicks(); |
| 231 |
uint32_t new_base = now - (uint32_t)(scaled_emulated_us() / 1000); |
| 232 |
last_perf_update -= new_base - sdlTicks_base; |
| 233 |
sdlTicks_base = new_base; |
| 234 |
sdlTicks = now - sdlTicks_base; |
| 235 |
diff_time = scaled_emulated_us() - sdlTicks * 1000LL; |
| 236 |
} |
| 237 |
|
| 238 |
if (may_sleep && !warp_mode && diff_time > 0) { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
if (diff_time > THROTTLE_SLICE_THRESHOLD_US) { |
| 263 |
for (;;) { |
| 264 |
const int64_t left = scaled_emulated_us() |
| 265 |
- (int64_t)(SDL_GetTicks() - sdlTicks_base) * 1000; |
| 266 |
if (left <= 0) |
| 267 |
break; |
| 268 |
|
| 269 |
usleep(left > THROTTLE_SLICE_US ? THROTTLE_SLICE_US : left); |
| 270 |
|
| 271 |
SDL_PumpEvents(); |
| 272 |
video_repaint_only(); |
| 273 |
} |
| 274 |
diff_time = 0; |
| 275 |
} else { |
| 276 |
if (diff_time >= 1000000) { |
| 277 |
sleep(diff_time / 1000000); |
| 278 |
diff_time %= 1000000; |
| 279 |
} |
| 280 |
usleep(diff_time); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
if (sdlTicks - last_perf_update > 5000) { |
| 285 |
uint32_t perf = (cpu_ticks - last_perf_cpu_ticks) / (MHZ * 50000); |
| 286 |
|
| 287 |
if (perf < 100 || warp_mode) { |
| 288 |
sprintf(window_title, WINDOW_TITLE " (%d%%)%s", perf, mouse_grabbed ? MOUSE_GRAB_MSG : ""); |
| 289 |
} else { |
| 290 |
sprintf(window_title, WINDOW_TITLE "%s", mouse_grabbed ? MOUSE_GRAB_MSG : ""); |
| 291 |
} |
| 292 |
|
| 293 |
video_update_title(window_title); |
| 294 |
|
| 295 |
last_perf_cpu_ticks = cpu_ticks; |
| 296 |
last_perf_update = sdlTicks; |
| 297 |
} |
| 298 |
|
| 299 |
if (log_speed) { |
| 300 |
float frames_behind = -((float)diff_time * 6e-5); |
| 301 |
int load = (int)((1 + frames_behind) * 100); |
| 302 |
printf("Load: %d%%\n", load > 100 ? 100 : load); |
| 303 |
|
| 304 |
if ((int)frames_behind > 0) { |
| 305 |
printf("Rendering is behind %d frames.\n", -(int)frames_behind); |
| 306 |
} else { |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|