blob: 0de4af5631969cd7fe64d673fed1538a5aaecc1d (
plain)
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
80
81
82
83
|
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "romdb.h"
#include "util.h"
#define GAME_ID_OFF 0x183
#define GAME_ID_LEN 8
#define TITLE_START 0x150
#define TITLE_END (TITLE_START+48)
tern_node *load_rom_db()
{
char *exe_dir = get_exe_dir();
if (!exe_dir) {
fputs("Failed to find executable path\n", stderr);
exit(1);
}
char *path = alloc_concat(exe_dir, "/rom.db");
tern_node *db = parse_config_file(path);
free(path);
if (!db) {
fputs("Failed to load ROM DB\n", stderr);
}
return db;
}
char *get_header_name(uint8_t *rom)
{
uint8_t *last = rom + TITLE_END - 1;
uint8_t *src = rom + TITLE_START;
while (last > src && (*last <= 0x20 || *last >= 0x80))
{
last--;
}
if (last == src) {
//TODO: Use other name field
return strdup("UNKNOWN");
} else {
last++;
char *ret = malloc(last - (rom + TITLE_START) + 1);
uint8_t *dst;
for (dst = ret; src < last; src++)
{
if (*src >= 0x20 && *src < 0x80) {
*(dst++) = *src;
}
}
*dst = 0;
return ret;
}
}
rom_info configure_rom_heuristics(uint8_t *rom)
{
rom_info info;
info.name = get_header_name(rom);
}
rom_info configure_rom(tern_node *rom_db, void *vrom)
{
uint8_t product_id[GAME_ID_LEN+1];
uint8_t *rom = vrom;
product_id[GAME_ID_LEN] = 0;
for (int i = 0; i < GAME_ID_LEN; i++)
{
if (rom[GAME_ID_OFF + i] <= ' ') {
product_id[i] = 0;
break;
}
product_id[i] = rom[GAME_ID_OFF + i];
}
tern_node * entry = tern_find_prefix(rom_db, product_id);
if (!entry) {
return configure_rom_heuristics(rom);
}
rom_info info;
info.name = strdup(tern_find_ptr_default(entry, "name", "UNKNOWN"));
return info;
}
|