From 22ef5edcd4f9395eb3d4e7ba8f3445d79147a4e9 Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Fri, 21 May 2021 08:15:02 +0100 Subject: [PATCH] Identify and fix byte-swapped ROMs Logs and byte swaps ROMs back if byte swapped versions are found. --- config_file/config_file.c | 2 +- config_file/rominfo.c | 28 ++++++++++++++++++++++++---- config_file/rominfo.h | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/config_file/config_file.c b/config_file/config_file.c index 0d736c5..0b5d86a 100644 --- a/config_file/config_file.c +++ b/config_file/config_file.c @@ -251,7 +251,7 @@ void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int ad memset(cfg->map_data[index], 0x00, cfg->map_size[index]); fread(cfg->map_data[index], cfg->rom_size[index], 1, in); fclose(in); - displayRomInfo(cfg->map_data[index]); + displayRomInfo(cfg->map_data[index], cfg->rom_size[index]); if (cfg->map_size[index] == cfg->rom_size[index]) m68k_add_rom_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]); break; diff --git a/config_file/rominfo.c b/config_file/rominfo.c index 46cc8a8..75b4c6c 100644 --- a/config_file/rominfo.c +++ b/config_file/rominfo.c @@ -46,12 +46,32 @@ static void getDiagRom(uint8_t *address, struct romInfo *info) info->extra = strtoul(endptr, NULL, 10); } -static void getRomInfo(uint8_t *address, struct romInfo *info) +static void swapRom(uint8_t *address, size_t length) +{ + uint8_t *ptr = address; + for (size_t pos = 0; pos < length; pos = pos + 2) + { + uint8_t low = ptr[pos]; + uint8_t high = ptr[pos+1]; + ptr[pos] = high; + ptr[pos+1] = low; + } +} + +static void getRomInfo(uint8_t *address, size_t length, struct romInfo *info) { uint8_t *ptr = address; uint8_t data = *ptr; info->isDiagRom = false; + if ((ptr[2] == 0xF9) && (ptr[3] == 0x4E)) + { + // ROM byte swapped! + printf("[CFG] Byte swapped ROM found, swapping back\n"); + swapRom(address, length); + data = *ptr; + } + if (data != 0x11) { info->id = ROM_TYPE_UNKNOWN; @@ -94,13 +114,13 @@ static void getRomInfo(uint8_t *address, struct romInfo *info) return; } -void displayRomInfo(uint8_t *address) +void displayRomInfo(uint8_t *address, size_t length) { struct romInfo info = {0}; const char *kversion; const char *size; - getRomInfo(address, &info); + getRomInfo(address, length, &info); if ((!info.isDiagRom) && (info.id != ROM_TYPE_UNKNOWN)) { @@ -199,7 +219,7 @@ void displayRomInfo(uint8_t *address) } else if (info.id == ROM_TYPE_UNKNOWN) { - printf("[CFG] ROM cannot be identified, if it is a byte swapped Kickstart this will fail\n"); + printf("[CFG] ROM cannot be identified\n"); } else { diff --git a/config_file/rominfo.h b/config_file/rominfo.h index 0c43f2f..e7b304c 100644 --- a/config_file/rominfo.h +++ b/config_file/rominfo.h @@ -26,5 +26,5 @@ enum romErrCode { ERR_ROM_UNKNOWN }; -void displayRomInfo(uint8_t *address); +void displayRomInfo(uint8_t *address, size_t length); #endif /* __ROMINFO */