-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
77 lines (67 loc) · 1.78 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
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include "SDL.h"
#include "SDL_video.h"
#include "cpu.h"
#include "lcd.h"
#include "logging.h"
static int should_exit = 0;
static void catch_sig_int(int signum) {
should_exit = signum == SIGINT;
}
static int initialize_system (const char* const restrict bios,
const char* const restrict rom, struct cpu* const restrict cpu,
struct lcd* const restrict lcd) {
assert(rom != NULL);
assert(cpu != NULL);
struct mmu* const mmu = init_memory(bios, rom);
if (!mmu) return -1;
// TODO: registers get initialized differently based on model
init_cpu(cpu, mmu);
// TODO: init function
lcd->mmu = mmu;
lcd->mode = 2;
return 0;
}
int main (int argc, char** argv) {
if (argc < 2 || argc > 3) {
fprintf(stderr, "USAGE: ./pocketgb [bios.gb] <rom.gb>\n");
return -1;
}
struct cpu cpu = { 0 };
struct lcd lcd = { 0 };
int rc = 0;
if (argc == 2) {
// If just the bios is passed, init_cpu will look at rom size and not jump
// the pc forward.
rc = initialize_system(NULL, argv[1], &cpu, &lcd);
} else {
rc = initialize_system(argv[1], argv[2], &cpu, &lcd);
}
if (rc) {
fprintf(stderr, "Failed to initialize system.\n");
return -1;
}
if(signal(SIGINT, catch_sig_int) == SIG_ERR) {
perror("Unable to set SIGINT handler.\n");
}
assert(SDL_Init(SDL_INIT_VIDEO) == 0);
struct windows windows;
create_debug_windows(&windows);
SDL_Event e;
// TODO: while cpu not halted
while (1) {
SDL_PollEvent(&e);
if (should_exit || e.type == SDL_QUIT) {
break;
}
tick_once(&cpu);
update_lcd(&lcd, cpu.tick_cycles);
update_debug_windows(&windows, &lcd);
}
destroy_windows(&windows);
SDL_Quit();
deinit_memory(cpu.mmu);
printf("\nexiting cleanly\n");
}