summaryrefslogtreecommitdiff
path: root/romdb.c
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2015-07-02 19:19:06 -0700
committerMichael Pavone <pavone@retrodev.com>2015-07-02 19:19:06 -0700
commit8f0979a2c4db824e91ead3290d8088d97b20e189 (patch)
treef8f5045c229e52b7ac05ca06909be8559b4616bc /romdb.c
parent983959f838575b6af3e64be442a63647312fcc61 (diff)
Initial work on ROM database
Diffstat (limited to 'romdb.c')
-rw-r--r--romdb.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/romdb.c b/romdb.c
new file mode 100644
index 0000000..0de4af5
--- /dev/null
+++ b/romdb.c
@@ -0,0 +1,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;
+} \ No newline at end of file