Skip to content

Commit d9c0d37

Browse files
committed
Explicitly allow JR offsets to wrap around in RGBLINK
This reverts commit f27a813.
1 parent 3b1606c commit d9c0d37

6 files changed

Lines changed: 26 additions & 2 deletions

File tree

src/link/patch.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,14 @@ static void applyFilePatches(Section &section, Section &dataSection) {
588588
// Offset is relative to the byte *after* the operand
589589
// PC as operand to `jr` is lower than reference PC by 2
590590
uint16_t address = patch.pcSection->org + patch.pcOffset + 2;
591-
int32_t jumpOffset = value - address;
591+
// The truncation of `value - address` is intentional, since
592+
// a low ROM0 address may `jr` to a high HRAM one.
593+
int16_t jumpOffset = value - address;
592594

593595
if (jumpOffset < -128 || jumpOffset > 127) {
594596
firstErrorAt(
595597
patch,
596-
"`JR` target must be between -128 and 127 bytes away, not %" PRId32
598+
"`JR` target must be between -128 and 127 bytes away, not %" PRId16
597599
"; use `JP` instead",
598600
jumpOffset
599601
);

test/link/jr-wraparound/a.asm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SECTION "low", ROM0[$0048]
2+
LCDInterrupt:
3+
jr hLCDInterruptHandler

test/link/jr-wraparound/b.asm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SECTION "high", HRAM[$FFE0]
2+
hLCDInterruptHandler::
3+
.jp: db ; should be $c3 (jp)
4+
.address: dw
16 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; File generated by rgblink
2+
00:0048 LCDInterrupt
3+
00:ffe0 hLCDInterruptHandler
4+
00:ffe0 hLCDInterruptHandler.jp
5+
00:ffe1 hLCDInterruptHandler.address

test/link/test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ tryDiff "$test"/ref.out.map "$outtemp"
191191
tryDiff "$test"/ref.out.sym "$outtemp2"
192192
evaluateTest
193193

194+
test="jr-wraparound"
195+
startTest
196+
"$RGBASM" -o "$otemp" "$test"/a.asm
197+
"$RGBASM" -o "$outtemp" "$test"/b.asm
198+
continueTest
199+
rgblinkQuiet -o "$gbtemp" -n "$outtemp2" "$otemp" "$outtemp"
200+
tryCmpRom "$test"/ref.out.bin
201+
tryDiff "$test"/ref.out.sym "$outtemp2"
202+
evaluateTest
203+
194204
test="high-low"
195205
startTest
196206
"$RGBASM" -o "$otemp" "$test"/a.asm

0 commit comments

Comments
 (0)