Skip to content

Commit 3e2121b

Browse files
authored
Support < and > prefixes for high and low bytes in VC patch templates (#1172)
1 parent 6d6d593 commit 3e2121b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

docs/vc_patch.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ Some commands may output a **value series**, which is a series of two-digit hexa
8383

8484
- Literal numbers in decimal (base 10, e.g. "`42`"), hexadecimal (base 16, e.g. "`0x2a`"), or octal (base 8, e.g. "`052`"). They may start with a plus sign "`+`". Numbers should not be negative.
8585
- Comparison operators: "`==`" is 0, "`>`" is 1, "`<`" is 2, "`>=`" is 3, "`<=`" is 4, "`!=`" is 5, and "`||`" is 0x11.
86-
- Symbol names from the two `.sym` files provided to `make_patch` may evaluate as their relative address or their absolute offset, depending on the command. (Addresses are relative to the symbol's bank for ROM addresses, or to 0x8000, the start of all RAM, for RAM addresses.) They may also be followed by a plus sign and a literal number that gets added to the value.
87-
- "`@`" evaluates as the address or absolute offset of the current patch/hook label, depending on the command.
86+
- Symbol names from the `.sym` file provided to `make_patch` may evaluate as their relative address or their absolute offset, depending on the command. (Addresses are relative to the symbol's bank for ROM addresses, or to 0x8000, the start of all RAM, for RAM addresses.)
87+
- Symbol names may be preceded by a less-than sign "`<`" to take only the low byte, or a greater-than sign "`>`" to take only the high byte.
88+
- Symbol names may be followed by a plus sign "`+`" and a literal number that gets added to the value.
89+
- "`@`" evaluates as the relative address or absolute offset of the current patch/hook label, depending on the command. It maybe be preceded by a "`<`" or "`>`", or followed by a "`+`" literal, like symbol names.
8890

8991
Any other characters are output as-is.
9092

tools/make_patch.c

Lines changed: 15 additions & 1 deletion
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) {

0 commit comments

Comments
 (0)