From 7942c5209b6c1f6c7e517af70a7347366846aef6 Mon Sep 17 00:00:00 2001 From: Robin Jones Date: Thu, 8 Jun 2023 16:39:02 +0100 Subject: [PATCH] Move rom header function to more appropriate location --- src/menu/menu_main.c | 43 ++++++++--------------------------------- src/menu/rom_database.c | 39 ++++++++++++++++++++++++++++++++----- src/menu/rom_database.h | 2 +- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/menu/menu_main.c b/src/menu/menu_main.c index edcff272..0a224c9f 100644 --- a/src/menu/menu_main.c +++ b/src/menu/menu_main.c @@ -22,41 +22,6 @@ static int items_in_dir = 1; static FILINFO current_fileinfo; -rom_header_t file_read_rom_header(char *path) { - FILE *fp = fopen(path, "rb"); - printf("loading path: %s\n", path); - if (!fp) { - printf("Error loading rom file header\n"); - } - - rom_header_t *rom_header = malloc(sizeof(rom_header_t)); - - fseek(fp, 0x10, SEEK_SET); - fread(&(rom_header->checksum), sizeof(uint64_t), 1, fp); - fseek(fp, 0x20, SEEK_SET); - fread(&(rom_header->title), sizeof(rom_header->title), 1, fp); - rom_header->title[20] = '\0'; - fseek(fp, 0x3b, SEEK_SET); - fread(&(rom_header->metadata.media_type), sizeof(rom_header->metadata.media_type), 1, fp); - //fseek(fp, 0x3c, SEEK_SET); // Consecutive read (no need to seek). - fread(&(rom_header->metadata.unique_identifier), sizeof(rom_header->metadata.unique_identifier), 1, fp); - //fseek(fp, 0x3e, SEEK_SET); // Consecutive read (no need to seek). - fread(&(rom_header->metadata.destination_market), sizeof(rom_header->metadata.destination_market), 1, fp); - //fseek(fp, 0x3f, SEEK_SET); // Consecutive read (no need to seek). - fread(&(rom_header->version), sizeof(rom_header->version), 1, fp); - - fclose(fp); - - printf("ROM checksum: %llu\n", rom_header->checksum); - printf("ROM title: %s\n", rom_header->title); - printf("ROM media type: %c\n", rom_header->metadata.media_type); - printf("ROM unique id: %.2s\n", (char*)&(rom_header->metadata.unique_identifier)); - printf("ROM dest market: %c\n", rom_header->metadata.destination_market); - printf("ROM version: %hhu\n", rom_header->version); - - return *rom_header; -} - // FIXME: use newlib rather than fatfs to do this! FRESULT scan_file_path (char *path) { @@ -196,6 +161,14 @@ void menu_main_init (settings_t *settings) { char tmp_buffer[280]; sprintf(tmp_buffer, "sd:/%s", current_fileinfo.fname); rom_header_t temp_header = file_read_rom_header(tmp_buffer); + + printf("ROM checksum: %llu\n", temp_header.checksum); + printf("ROM title: %s\n", temp_header.title); + printf("ROM media type: %c\n", temp_header.metadata.media_type); + printf("ROM unique id: %.2s\n", (char*)&(temp_header.metadata.unique_identifier)); + printf("ROM dest market: %c\n", temp_header.metadata.destination_market); + printf("ROM version: %hhu\n", temp_header.version); + printf("uid as int: %d\n", temp_header.metadata.unique_identifier); uint8_t save_type = rom_db_match_save_type(temp_header); diff --git a/src/menu/rom_database.c b/src/menu/rom_database.c index 1671eeea..760b8bb8 100644 --- a/src/menu/rom_database.c +++ b/src/menu/rom_database.c @@ -1,5 +1,7 @@ #include "rom_database.h" #include +#include +#include uint8_t extract_homebrew_setting(uint8_t setting, uint8_t bit_position) { return (setting & (1 << bit_position)) ? 1 : 0; @@ -26,6 +28,33 @@ uint8_t extract_homebrew_save_type(uint8_t save_type) { } } +rom_header_t file_read_rom_header(char *path) { + FILE *fp = fopen(path, "rb"); + printf("loading path: %s\n", path); + if (!fp) { + printf("Error loading rom file header\n"); + } + + rom_header_t *rom_header = malloc(sizeof(rom_header_t)); + + fseek(fp, 0x10, SEEK_SET); + fread(&(rom_header->checksum), sizeof(uint64_t), 1, fp); + fseek(fp, 0x20, SEEK_SET); + fread(&(rom_header->title), sizeof(rom_header->title), 1, fp); + rom_header->title[20] = '\0'; + fseek(fp, 0x3b, SEEK_SET); + fread(&(rom_header->metadata.media_type), sizeof(rom_header->metadata.media_type), 1, fp); + //fseek(fp, 0x3c, SEEK_SET); // Consecutive read (no need to seek). + fread(&(rom_header->metadata.unique_identifier), sizeof(rom_header->metadata.unique_identifier), 1, fp); + //fseek(fp, 0x3e, SEEK_SET); // Consecutive read (no need to seek). + fread(&(rom_header->metadata.destination_market), sizeof(rom_header->metadata.destination_market), 1, fp); + //fseek(fp, 0x3f, SEEK_SET); // Consecutive read (no need to seek). + fread(&(rom_header->version), sizeof(rom_header->version), 1, fp); + + fclose(fp); + + return *rom_header; +} // TODO: make sure this can also handle an external input file. @@ -37,13 +66,13 @@ uint8_t rom_db_match_save_type(rom_header_t rom_header) { // TODO: if appropriate this can be improved with other unused codes... e.g. | `AA` | `ZZ` if (rom_header.metadata.unique_identifier == *(uint16_t *)"ED") { - // uint8_t info = rom_header.version & 0x0F; - // uint8_t rtc_enabled = extract_homebrew_setting(info, 0); // Bit 0 - // uint8_t region_free_enabled = extract_homebrew_setting(info, 1); // Bit 1 + // uint8_t low_nibble = rom_header.version & 0x0F; + // uint8_t rtc_enabled = extract_homebrew_setting(low_nibble, 0); // Bit 0 + // uint8_t region_free_enabled = extract_homebrew_setting(low_nibble, 1); // Bit 1 - uint8_t save_type = (rom_header.version >> 4) & 0x0F; + uint8_t high_nibble = (rom_header.version >> 4) & 0x0F; - return extract_homebrew_save_type(save_type); + return extract_homebrew_save_type(high_nibble); } // Second: Match the default entries for crc_high. diff --git a/src/menu/rom_database.h b/src/menu/rom_database.h index 9fc70977..b5ce008c 100644 --- a/src/menu/rom_database.h +++ b/src/menu/rom_database.h @@ -73,5 +73,5 @@ typedef struct { uint8_t version; } rom_header_t; - +rom_header_t file_read_rom_header(char *path); uint8_t rom_db_match_save_type(rom_header_t rom_header);