| 1 |
#ifndef __APPLE__ |
| 2 |
#define _XOPEN_SOURCE 600 |
| 3 |
#define _POSIX_C_SOURCE 1 |
| 4 |
#endif |
| 5 |
#include "glue.h" |
| 6 |
#include "video.h" |
| 7 |
#include "cpu/fake6502.h" |
| 8 |
#include <SDL.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <unistd.h> |
| 11 |
|
| 12 |
uint32_t frames; |
| 13 |
uint32_t sdlTicks_base; |
| 14 |
uint32_t last_perf_update; |
| 15 |
uint32_t clockticks6502_old; |
| 16 |
int64_t cpu_ticks; |
| 17 |
int64_t last_perf_cpu_ticks; |
| 18 |
char window_title[255]; |
| 19 |
|
| 20 |
void |
| 21 |
timing_init() { |
| 22 |
frames = 0; |
| 23 |
sdlTicks_base = SDL_GetTicks(); |
| 24 |
last_perf_update = 0; |
| 25 |
last_perf_cpu_ticks = 0; |
| 26 |
clockticks6502_old = clockticks6502; |
| 27 |
cpu_ticks = 0; |
| 28 |
} |
| 29 |
|
| 30 |
void |
| 31 |
timing_update() |
| 32 |
{ |
| 33 |
frames++; |
| 34 |
cpu_ticks += clockticks6502 - clockticks6502_old; |
| 35 |
clockticks6502_old = clockticks6502; |
| 36 |
uint32_t sdlTicks = SDL_GetTicks() - sdlTicks_base; |
| 37 |
int64_t diff_time = cpu_ticks / MHZ - sdlTicks * 1000LL; |
| 38 |
if (!warp_mode && diff_time > 0) { |
| 39 |
if (diff_time >= 1000000) { |
| 40 |
sleep(diff_time / 1000000); |
| 41 |
diff_time %= 1000000; |
| 42 |
} |
| 43 |
usleep(diff_time); |
| 44 |
} |
| 45 |
|
| 46 |
if (sdlTicks - last_perf_update > 5000) { |
| 47 |
uint32_t perf = (cpu_ticks - last_perf_cpu_ticks) / (MHZ * 50000); |
| 48 |
|
| 49 |
if (perf < 100 || warp_mode) { |
| 50 |
sprintf(window_title, WINDOW_TITLE " (%d%%)%s", perf, mouse_grabbed ? MOUSE_GRAB_MSG : ""); |
| 51 |
} else { |
| 52 |
sprintf(window_title, WINDOW_TITLE "%s", mouse_grabbed ? MOUSE_GRAB_MSG : ""); |
| 53 |
} |
| 54 |
|
| 55 |
video_update_title(window_title); |
| 56 |
|
| 57 |
last_perf_cpu_ticks = cpu_ticks; |
| 58 |
last_perf_update = sdlTicks; |
| 59 |
} |
| 60 |
|
| 61 |
if (log_speed) { |
| 62 |
float frames_behind = -((float)diff_time * 6e-5); |
| 63 |
int load = (int)((1 + frames_behind) * 100); |
| 64 |
printf("Load: %d%%\n", load > 100 ? 100 : load); |
| 65 |
|
| 66 |
if ((int)frames_behind > 0) { |
| 67 |
printf("Rendering is behind %d frames.\n", -(int)frames_behind); |
| 68 |
} else { |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|