Skip to content

Commit

Permalink
Merge pull request #7 from superjamie/master
Browse files Browse the repository at this point in the history
Calculate header checksum correctly, add gitignore
  • Loading branch information
gheja authored Oct 4, 2024
2 parents 70de8ce + 2ff0b1e commit 6388758
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
ems-flasher
21 changes: 12 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 6388758

Please sign in to comment.