From d9c0d37fd52481304c345fa9537bec6a5302d4c5 Mon Sep 17 00:00:00 2001 From: Rangi Date: Mon, 20 Jul 2026 19:12:54 -0400 Subject: [PATCH] Explicitly allow `JR` offsets to wrap around in RGBLINK This reverts commit f27a8132d993fc48df27410c0660fde14c0aaccd. --- src/link/patch.cpp | 6 ++++-- test/link/jr-wraparound/a.asm | 3 +++ test/link/jr-wraparound/b.asm | 4 ++++ test/link/jr-wraparound/ref.out.bin | Bin 0 -> 16384 bytes test/link/jr-wraparound/ref.out.sym | 5 +++++ test/link/test.sh | 10 ++++++++++ 6 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/link/jr-wraparound/a.asm create mode 100644 test/link/jr-wraparound/b.asm create mode 100644 test/link/jr-wraparound/ref.out.bin create mode 100644 test/link/jr-wraparound/ref.out.sym diff --git a/src/link/patch.cpp b/src/link/patch.cpp index e16befd6..8a0e43f7 100644 --- a/src/link/patch.cpp +++ b/src/link/patch.cpp @@ -588,12 +588,14 @@ static void applyFilePatches(Section §ion, Section &dataSection) { // Offset is relative to the byte *after* the operand // PC as operand to `jr` is lower than reference PC by 2 uint16_t address = patch.pcSection->org + patch.pcOffset + 2; - int32_t jumpOffset = value - address; + // The truncation of `value - address` is intentional, since + // a low ROM0 address may `jr` to a high HRAM one. + int16_t jumpOffset = value - address; if (jumpOffset < -128 || jumpOffset > 127) { firstErrorAt( patch, - "`JR` target must be between -128 and 127 bytes away, not %" PRId32 + "`JR` target must be between -128 and 127 bytes away, not %" PRId16 "; use `JP` instead", jumpOffset ); diff --git a/test/link/jr-wraparound/a.asm b/test/link/jr-wraparound/a.asm new file mode 100644 index 00000000..796baf67 --- /dev/null +++ b/test/link/jr-wraparound/a.asm @@ -0,0 +1,3 @@ +SECTION "low", ROM0[$0048] +LCDInterrupt: + jr hLCDInterruptHandler diff --git a/test/link/jr-wraparound/b.asm b/test/link/jr-wraparound/b.asm new file mode 100644 index 00000000..df368750 --- /dev/null +++ b/test/link/jr-wraparound/b.asm @@ -0,0 +1,4 @@ +SECTION "high", HRAM[$FFE0] +hLCDInterruptHandler:: +.jp: db ; should be $c3 (jp) +.address: dw diff --git a/test/link/jr-wraparound/ref.out.bin b/test/link/jr-wraparound/ref.out.bin new file mode 100644 index 0000000000000000000000000000000000000000..801fd75fe6aa2783bd40eb11d8caefb25c4a72b4 GIT binary patch literal 16384 zcmeIuF#!Mo2*a?+KskCbv5!