-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.c
65 lines (54 loc) · 1.49 KB
/
cart.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 "config.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "vg8m.h"
#include "internal.h"
static const char MAGIC[] = "VGDMP100";
typedef struct s_dump_header DumpHeader;
struct s_dump_header {
char magic[8];
uint16_t flags;
uint16_t prog_offset;
uint16_t prog_size;
uint16_t pat_2bpp_offset;
uint16_t pat_2bpp_size;
uint16_t pat_3bpp_offset;
uint16_t pat_3bpp_size;
};
typedef struct s_cart_header CartHeader;
struct s_cart_header {
char magic[8];
char title[0x20];
char developer[0x20];
char lang[2];
uint16_t flags;
uint8_t version;
};
bool vg8m_load_cart(VG8M *emu, const char *filename) {
DumpHeader header;
int fd = open(filename, O_RDONLY);
if (fd == -1) goto error;
if (read(fd, &header, sizeof(header)) == -1)
goto error;
if (header.prog_size > 0) {
if (lseek(fd, header.prog_offset, SEEK_SET) == -1
|| read(fd, emu->cart_prog_rom, header.prog_size) == -1)
goto error;
}
if (header.pat_2bpp_size > 0) {
if (lseek(fd, header.pat_2bpp_offset, SEEK_SET) == -1
|| read(fd, emu->cart_2bpp_rom, header.pat_2bpp_size) == -1)
goto error;
}
if (header.pat_3bpp_size > 0) {
if (lseek(fd, header.pat_3bpp_offset, SEEK_SET) == -1
|| read(fd, emu->cart_3bpp_rom, header.pat_3bpp_size) == -1)
goto error;
}
close(fd);
return true;
error:
if (fd != -1) close(fd);
return false;
}