-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
77 lines (62 loc) · 1.52 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 <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "xgbc.h"
extern int romfd, ramfd;
extern unsigned ram_size;
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "ROM-Imagename erwartet.\n");
return 1;
}
FILE *fp = fopen(argv[1], "rb");
if (fp == NULL)
{
perror("Konnte ROM-Image nicht öffnen");
return 1;
}
romfd = fileno(fp);
if (!check_cartridge(fp))
{
fprintf(stderr, "Diese Datei scheint kein ROM-Image zu sein.\n");
return 1;
}
if (ram_size)
{
char *ramfn;
if (argc >= 3)
ramfn = argv[2];
else
{
ramfn = malloc(strlen(argv[1]) + 5);
strcpy(ramfn, argv[1]);
char *sep = strrchr(ramfn, '.');
if ((uintptr_t)sep > (uintptr_t)strrchr(ramfn, '/'))
*sep = 0;
strcat(ramfn, ".sav");
}
FILE *ramfp = fopen(ramfn, "rb+");
if (ramfp == NULL)
{
ramfp = fopen(ramfn, "wb+");
if (ramfp == NULL)
{
fprintf(stderr, "Konnte RAM-Image \"%s\" nicht öffnen: %s\n", ramfn, strerror(errno));
return 1;
}
ftruncate(fileno(ramfp), ram_size * 0x2000);
}
ramfd = fileno(ramfp);
}
init_memory();
install_segv_handler();
init_video(1);
begin_execution();
return 0;
}