Skip to content

Commit cfbd67a

Browse files
authored
Support < and > prefixes for high and low bytes in VC patch templates (#502)
1 parent ca7b62f commit cfbd67a

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

tools/make_patch.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,30 @@ int parse_arg_value(const char *arg, bool absolute, const struct Symbol *symbols
183183
return parse_number(arg, 0);
184184
}
185185

186+
// Symbols may take the low or high part
187+
enum { SYM_WHOLE, SYM_LOW, SYM_HIGH } part = SYM_WHOLE;
188+
if (arg[0] == '<') {
189+
part = SYM_LOW;
190+
arg++;
191+
} else if (arg[0] == '>') {
192+
part = SYM_HIGH;
193+
arg++;
194+
}
195+
186196
// Symbols evaluate to their offset or address, plus an optional offset mod
187197
int offset_mod = 0;
188198
char *plus = strchr(arg, '+');
189199
if (plus) {
190200
offset_mod = parse_number(plus, 0);
191201
*plus = '\0';
192202
}
203+
204+
// Symbols evaluate to their offset or address
193205
const char *sym_name = !strcmp(arg, "@") ? patch_name : arg; // "@" is the current patch label
194206
const struct Symbol *symbol = symbol_find(symbols, sym_name);
195-
return (absolute ? symbol->offset : symbol->address) + offset_mod;
207+
208+
int value = (absolute ? symbol->offset : symbol->address) + offset_mod;
209+
return part == SYM_LOW ? value & 0xff : part == SYM_HIGH ? value >> 8 : value;
196210
}
197211

198212
void interpret_command(char *command, const struct Symbol *current_hook, const struct Symbol *symbols, struct Buffer *patches, FILE *restrict new_rom, FILE *restrict orig_rom, FILE *restrict output) {
@@ -344,6 +358,12 @@ struct Buffer *process_template(const char *template_filename, const char *patch
344358

345359
// The ROM checksum will always differ
346360
buffer_append(patches, &(struct Patch){0x14e, 2});
361+
// The Stadium data (see stadium.c) will always differ
362+
unsigned int rom_size = (unsigned int)xfsize("", orig_rom);
363+
if (rom_size == 128 * 0x4000) {
364+
unsigned int stadium_size = 24 + 6 + 2 + 128 * 2 * 2;
365+
buffer_append(patches, &(struct Patch){rom_size - stadium_size, stadium_size});
366+
}
347367

348368
// Fill in the template
349369
const struct Symbol *current_hook = NULL;
@@ -413,7 +433,7 @@ struct Buffer *process_template(const char *template_filename, const char *patch
413433
int compare_patch(const void *patch1, const void *patch2) {
414434
unsigned int offset1 = ((const struct Patch *)patch1)->offset;
415435
unsigned int offset2 = ((const struct Patch *)patch2)->offset;
416-
return offset1 > offset2 ? 1 : offset1 < offset2 ? -1 : 0;
436+
return (offset1 > offset2) - (offset1 < offset2);
417437
}
418438

419439
bool verify_completeness(FILE *restrict orig_rom, FILE *restrict new_rom, struct Buffer *patches) {

0 commit comments

Comments
 (0)