-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.c
90 lines (72 loc) · 1.76 KB
/
chip8.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
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
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/time.h>
#include <signal.h>
#include <pigpio.h>
#include "keypad.c"
#include "buzzer.c"
#include "emulator.c"
#include "led-matrix.c"
#define TICK_INTERVAL 1000.0/60
#define FILE_NAME "chip.bin"
#define BUZZ_FRAMES 1
volatile bool quit = false;
void close_all(int _) {
cleanup_buzzer();
cleanup_keypad();
cleanup_led_matrix();
gpioTerminate();
exit(-1);
quit = true;
}
float timedifference_msec(struct timeval t0, struct timeval t1) {
return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f;
}
void mainloop() {
///tick down 60Hz
allowDraw = true;
struct timeval now, looped;
gettimeofday(&now, 0); gettimeofday(&looped, 0);
while (timedifference_msec(now, looped) <= TICK_INTERVAL-1) {
emulatecycle();
allowDraw = false;
gettimeofday(&looped, 0);
}
if (draw) {
draw = false;
drawScreen(screen, SCR_WIDTH, SCR_HEIGHT);
}
if (!paused && delay > 0)
delay--;
if (sound > 0)
sound--;
/* better system - this won't do 2 buzzes if done 1 frame after the other */
if (sound == -1) {
sound = -1 - BUZZ_FRAMES;
unbuzz();
}
if (sound == 0) {
sound = -1;
buzz();
}
/* end better system */
set_key(key);
}
int main(int argc, char **argv) {
if (gpioInitialise() < 0) {
fprintf(stderr, "pigpio initialisation failed\n");
return 1;
}
initialize();
init_buzzer();
init_keypad();
init_led_matrix(argc, argv);
loadGame(FILE_NAME);
signal(SIGINT, close_all);
signal(SIGTERM, close_all);
do {
mainloop();
} while (!quit);
return 0;
}