-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
79 lines (66 loc) · 2.6 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "common.h"
#include <errno.h>
#include "SDL.h"
#include "cpu.h"
int load_ROM(char* path);
int main(int argc, char* argv[]) {
uint8_t extraFlag;
bool quirks = false;
srand((unsigned) time(NULL));
if (argc == 1) { printf("chip8emu - a basic CHIP-8 emulator.\nUsage: chip8emu <romfile> <workaround flag>\n\n"
" If the program doesn't work properly, try inputting 1 after ROM file.\n This will enable workarounds for instructions 8XY6, 8XYE, 8X55 and 8X65.\n\n"
" Key mapping:\n 1 2 3 C -> 1 2 3 4\n 4 5 6 D -> Q W E R\n 7 8 9 E -> A S D F\n A 0 B F -> Z X C V\n"
" Press '[' key to decrease CPU speed, ']' to increase. Press 'P' to reset the system.\n\n"); return 1; }
if (sdl_init()) return 1;
if (argc == 3 && *argv[2] == '1') quirks = true;
chip8_init(quirks);
if (load_ROM(argv[1])) return 1;
play_beep();
printf("\nEntering main loop...\n");
while(!cpuHalted) {
if (chip8_cycle()) play_beep();
if (drawFlag) {
generate_state();
render_screen((uint8_t*)&screen, sizeof(screen), statusString);
drawFlag = false;
}
if (get_input((uint8_t*)&input, &extraFlag)) break;
if (waitForKey) {
for (uint8_t i = 0; i < sizeof(registers); i++) {
if (input[i] == 0xFF) { registers[waitForRegister] = i; waitForKey = false; }
}
}
switch (extraFlag) {
case 0xFF:
printf("Resetting...\n");
chip8_init(quirks);
load_ROM(argv[1]);
render_screen((uint8_t*)&screen, sizeof(screen), statusString);
extraFlag = 0x0;
break;
case 0xF0:
cpuClock -= 10;
cpuRate = (1.0 / (double)cpuClock) * 1000.0;
extraFlag = 0x0;
break;
case 0x0F:
cpuClock += 10;
cpuRate = (1.0 / (double)cpuClock) * 1000.0;
extraFlag = 0x0;
break;
}
}
sdl_quit();
return 0;
}
int load_ROM(char* path) {
FILE* romfile = fopen(path, "rb");
if (romfile == NULL) { printf("\nFailed to load ROM file: error %i\n",errno); return 1; }
fseek(romfile, 0, SEEK_END);
int file_length = ftell(romfile);
fseek(romfile, 0, SEEK_SET);
printf("\nLoading ROM file \"%s\" (%i bytes) to memory at offset 0x%03X...\n", path, file_length, PROGRAM_ADDRESS);
fread(memory + PROGRAM_ADDRESS, file_length, 1, romfile);
fclose(romfile);
return 0;
}