-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
65 lines (48 loc) · 1.41 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include "ops.h"
#include "video.h"
#include "chip8.h"
#include <time.h>
int main(int argc, char** argv) {
const char* videoScaleStr = argv[1];
const char* cycleDelayStr = argv[2];
const char* fname = argv[3];
bool rawDisassemble = false;
if (argc > 4 && !strcmp(argv[4], "-d")) set_disassemble_exec();
else if (argc > 4 && !strcmp(argv[4], "-raw")) {
set_disassemble_exec();
set_disassemble_raw();
rawDisassemble = true;
}
char* _a;
int videoScale = (int)(strtol(videoScaleStr, &_a, 10));
int cycleDelay = (int)(strtol(cycleDelayStr, &_a, 10));
chip_8 emulator;
ch_initialize(&emulator);
ch_loadRom(&emulator, fname);
if (rawDisassemble) {
int i = 0;
while (i < ch_numBytes()) {
ch_cycle(&emulator);
i+=2;
}
return 0;
}
ch_video platform;
initialize_video(&platform, fname, VIDEO_WIDTH*videoScale, VIDEO_HEIGHT*videoScale, VIDEO_WIDTH, VIDEO_HEIGHT);
int videoPitch = sizeof(emulator.video[0]) * VIDEO_WIDTH;
struct timespec last_cycle;
clock_gettime(CLOCK_MONOTONIC, &last_cycle);
while (!proc_input(emulator.keypad)) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
double dt = (now.tv_sec - last_cycle.tv_sec) * 1000.0f + (now.tv_nsec - last_cycle.tv_nsec) / 1000000.0f;
if (dt > cycleDelay) {
last_cycle = now;
ch_cycle(&emulator);
update_video(&platform, emulator.video, videoPitch);
}
}
deinit_video(&platform);
return 0;
}