From 4e5462744a283daeeb0f5a0f287a05b81244a1cc Mon Sep 17 00:00:00 2001 From: Jamie Bainbridge Date: Fri, 1 Mar 2024 11:49:34 +1100 Subject: [PATCH 1/2] Add .gitignore Add a git ingore file so that git doesn't track objects or binary. Signed-off-by: Jamie Bainbridge --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c78f25f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +ems-flasher From 2ff0b1e50e569854285dbd8489517b7921920978 Mon Sep 17 00:00:00 2001 From: Jamie Bainbridge Date: Fri, 1 Mar 2024 11:50:43 +1100 Subject: [PATCH 2/2] Calculate header checksum correctly The old header checksum code never worked. First it calculated checksum incorrectly due to wrong algorithm, then we applied an incorrect fix in 2014 based on an incorrect GitHub comment. Calculate the header checksum correctly as per community documentation: * https://gbdev.io/pandocs/The_Cartridge_Header.html * https://gbdev.gg8.se/wiki/articles/The_Cartridge_Header The code is used multiple times so move it to a separate function to remove duplication of code. Tested by flashing good Redump ROMs to the cart and also verifying the ROMs are correct with RGBDS's rgbfix. Fixes: 8a1141b7e078 ("Correct header checksum, add debug when checksum OK") Signed-off-by: Jamie Bainbridge --- main.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/main.c b/main.c index 559b599..c932010 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,7 @@ #include "ems.h" // don't forget to bump this :P -#define VERSION "0.04" +#define VERSION "0.05" // one bank is 32 megabits #define BANK_SIZE 0x400000 @@ -212,6 +212,15 @@ void get_options(int argc, char **argv) { usage(argv[0]); } +uint8_t rom_checksum_header(unsigned char *rom) +{ + uint8_t checksum = 0; + for (uint16_t address = HEADER_TITLE; address < HEADER_CHKSUM; address++) { + checksum = checksum - rom[address] - 1; + } + return checksum; +} + /** * Main */ @@ -350,10 +359,7 @@ int main(int argc, char **argv) { } //Verify cartridge header checksum while we're at it - uint8_t calculated_chk = 0; - for (i = HEADER_TITLE; i < HEADER_CHKSUM; i++) { - calculated_chk -= buf[i] + 1; - } + uint8_t calculated_chk = rom_checksum_header(buf); if (calculated_chk != buf[HEADER_CHKSUM]) { printf("Cartridge header checksum invalid. This game will NOT boot on real hardware.\n"); @@ -418,10 +424,7 @@ int main(int argc, char **argv) { } //Verify cartridge header checksum while we're at it - calculated_chk = 0; - for (i = HEADER_TITLE; i < HEADER_CHKSUM; i++) { - calculated_chk -= buf[i] + 1; - } + calculated_chk = rom_checksum_header(buf); if (calculated_chk != buf[HEADER_CHKSUM]) { printf("Cartridge header checksum invalid. This game will NOT boot on real hardware.\n");