-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
49 lines (41 loc) · 966 Bytes
/
main.c
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
#include <stdio.h>
#include <time.h>
#include "chip8.h"
#include "display.h"
#define OP_HZ 700
#define DISPLAY_HZ 60
int main(int argc, char **argv) {
if (argc != 2) {
printf("./chip8 <binary>\n");
return 1;
}
if (init_chip8(argv[1]) != 0) {
printf("Failed to initialize chip8\n");
return 1;
}
// init display
if (init_display() != 0) {
printf("Failed to initialize display\n");
return 1;
}
// Run the main loop
clock_t now, op_prev, display_prev;
now = op_prev = display_prev = clock();
long op_period = CLOCKS_PER_SEC / OP_HZ;
long display_period = CLOCKS_PER_SEC / DISPLAY_HZ;
while (running) {
now = clock();
if (now - op_prev >= op_period) {
op_prev = now;
execute_instruction();
}
if (now - display_prev >= display_period) {
display_prev = now;
timer_tick();
process_events();
refresh_display(chip8.display);
}
}
quit_display();
return 0;
}