9 // These are my (Joe's) functions added to the md class.
11 // This takes a comma or whitespace-separated list of Game Genie and/or hex
12 // codes to patch the ROM with.
13 void md::patch(const char *list
)
15 static const char delims
[] = " \t\n,";
19 // Copy the given list to a working list so we can strtok it
20 worklist
= (char*)malloc(strlen(list
)+1);
21 strcpy(worklist
, list
);
23 for(tok
= strtok(worklist
, delims
); tok
; tok
= strtok(NULL
, delims
))
25 // If it's empty, toss it
26 if(*tok
== '\0') continue;
29 // Discard it if it was bad code
30 if((signed)p
.addr
== -1) {
31 printf("Bad patch \"%s\"\n", tok
);
34 // Put it into the ROM (remember byteswapping)
35 printf("Patch \"%s\" -> %06X:%04X\n", tok
, p
.addr
, p
.data
);
36 rom
[p
.addr
] = (char)(p
.data
& 0xFF);
37 rom
[p
.addr
+1] = (char)((p
.data
& 0xFF00) >> 8);
44 // Get/put saveram from/to FILE*'s
45 void md::get_save_ram(FILE *from
)
47 // Pretty simple, just read the saveram raw
48 fread((void*)saveram
, 1, save_len
, from
);
51 void md::put_save_ram(FILE *into
)
53 // Just the opposite of the above :)
54 fwrite((void*)saveram
, 1, save_len
, into
);
57 // Dave: This is my code, but I thought it belonged here
58 // Joe: Thanks Dave! No problem ;)
59 static unsigned short calculate_checksum(unsigned char *rom
,int len
)
61 unsigned short checksum
=0;
63 for (i
=512;i
<=(len
-2);i
+=2)
65 checksum
+=(rom
[i
+1]<<8);
71 void md::fix_rom_checksum()
73 unsigned short cs
; cs
=calculate_checksum(rom
,romlen
);
74 if (romlen
>=0x190) { rom
[0x18f]=cs
>>8; rom
[0x18e]=cs
&255; }