-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
176 lines (152 loc) · 3.93 KB
/
Copy pathmain.c
File metadata and controls
176 lines (152 loc) · 3.93 KB
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
#include "common.h"
#include "cpu.h"
#include "gpu.h"
#include "joypad.h"
#include "timer.h"
#include "sound.h"
#ifdef DEBUG_BUILD
// per-subsystem wall-clock timing, to find out which of cpu_step/gpu_step/
// timer_step/sound_step is actually eating a slow frame's time, rather than
// inferring it from correlation. Reset and logged once per frame in main().
static Uint64 profile_cpu_ticks = 0;
static Uint64 profile_gpu_ticks = 0;
static Uint64 profile_timer_ticks = 0;
static Uint64 profile_sound_ticks = 0;
#endif
void render_frame () {
int cycles = 0;
int frame_cycles = CPU_FREQ / 60.0;
while(frame_cycles > 0) {
#ifdef DEBUG_BUILD
Uint64 t0 = SDL_GetPerformanceCounter();
cycles = cpu_step();
Uint64 t1 = SDL_GetPerformanceCounter();
gpu_step(cycles);
Uint64 t2 = SDL_GetPerformanceCounter();
timer_step(cycles);
Uint64 t3 = SDL_GetPerformanceCounter();
sound_step(cycles);
Uint64 t4 = SDL_GetPerformanceCounter();
profile_cpu_ticks += (t1 - t0);
profile_gpu_ticks += (t2 - t1);
profile_timer_ticks += (t3 - t2);
profile_sound_ticks += (t4 - t3);
#else
cycles = cpu_step();
gpu_step(cycles);
timer_step(cycles);
sound_step(cycles);
#endif
frame_cycles -= cycles;
}
}
#ifdef __EMSCRIPTEN__
static EM_BOOL key_callback(int event_type, const EmscriptenKeyboardEvent *event, void *user_data) {
SDL_Event e = {0};
switch (event_type) {
case EMSCRIPTEN_EVENT_KEYDOWN:
e.type = SDL_KEYDOWN;
break;
case EMSCRIPTEN_EVENT_KEYUP:
e.type = SDL_KEYUP;
break;
default:
return EM_TRUE;
}
switch (event->keyCode) {
case 90:
e.key.keysym.sym = SDLK_z;
break;
case 65:
e.key.keysym.sym = SDLK_x;
break;
case 32:
e.key.keysym.sym = SDLK_SPACE;
break;
case 13:
e.key.keysym.sym = SDLK_RETURN;
break;
case 38:
e.key.keysym.sym = SDLK_UP;
break;
case 40:
e.key.keysym.sym = SDLK_DOWN;
break;
case 37:
e.key.keysym.sym = SDLK_LEFT;
break;
case 39:
e.key.keysym.sym = SDLK_RIGHT;
break;
default:
return EM_TRUE;
}
keyboard_handle_input(&e);
return EM_TRUE;
}
#endif
int main(int argc, char *argv[]) {
SDL_Event e = {0};
bool quit = false;
int32_t ticks = 0;
common_init();
println("EMULATOR INIT");
keyboard_set_handlers(joypad_key_down, joypad_key_up);
gpu_init();
cpu_init();
sound_init();
sound_set_callback(sound_callback);
#ifndef __EMSCRIPTEN__
if (argc != 2) {
println("USAGE: %s game.gb", argv[0]);
quit = true;
} else {
file_load_rom(argv[1]);
}
#ifdef DEBUG_BUILD
uint32_t frame_number = 0;
#endif
while (!quit || SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) {
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
if (e.type == SDL_KEYUP || e.type == SDL_KEYDOWN) {
keyboard_handle_input(&e);
}
}
ticks = SDL_GetTicks();
render_frame();
uint32_t frame_wall_ms = SDL_GetTicks() - ticks;
#ifdef DEBUG_BUILD
double perf_freq_ms = SDL_GetPerformanceFrequency() / 1000.0;
println(
"PROFILE frame=%u cpu_ms=%.2f gpu_ms=%.2f timer_ms=%.2f sound_ms=%.2f",
frame_number,
profile_cpu_ticks / perf_freq_ms,
profile_gpu_ticks / perf_freq_ms,
profile_timer_ticks / perf_freq_ms,
profile_sound_ticks / perf_freq_ms
);
profile_cpu_ticks = 0;
profile_gpu_ticks = 0;
profile_timer_ticks = 0;
profile_sound_ticks = 0;
uint32_t halted_cycles = 0, active_cycles = 0;
cpu_get_debug_stats(&halted_cycles, &active_cycles);
sound_log_frame_debug(frame_number++, (float) frame_wall_ms, halted_cycles, active_cycles);
#endif
int32_t time_to_delay = 1000/60.0f - frame_wall_ms;
if (time_to_delay > 0) {
SDL_Delay(time_to_delay);
}
}
#else
file_load_rom("game.gb");
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, true, key_callback);
emscripten_set_keyup_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, true, key_callback);
emscripten_set_main_loop(render_frame, 60, 1);
#endif /* __EMSCRIPTEN__ */
common_shutdown();
return 0;
}