Skip to content

Add brl long branch ($82) to the assembler - #90

Merged
manz merged 1 commit into
masterfrom
feat/brl-long-branch
May 31, 2026
Merged

manz merged 1 commit into
masterfrom
feat/brl-long-branch

Conversation

@manz

@manz manz commented May 31, 2026

Copy link
Copy Markdown
Owner

Why

brl ($82) — the 65816's unconditional 16-bit PC-relative branch (±32 KB) — was assemble-unsupported. The disassembler already decoded it (0x82: ("brl", RELATIVE_LONG, 2)), but the opcode table had no entry, so you could read brl but not write it.

The consequence: a long intra-routine back-edge (loop body > 127 bytes, past bra range) had to use jmp.w <local> — absolute, deferred to the linker, touching the symbol table. That's the exact path behind the recently-fixed local-label collisions (#88, #89). brl is the correct primitive: PC-relative, resolved in-section at assemble time, layout-invariant, never consults a symbol — immune to that whole class.

Closes the "natural fix" the jmp.w-local report flagged.

What

  • RelativeJumpOpcode parametrized by offset width (OFFSET_BYTES); displacement = dest - pc - (1 + OFFSET_BYTES). Existing 8-bit branches unchanged.
  • RelativeLongJumpOpcode (2-byte signed offset, range ±32767) → brl = $82.

Test plan

  • brl <backward label> / <forward label> encode 82 lo hi with the correct signed-16 displacement, both directions.
  • Target past ±32767 errors with "signed 16-bit range".
  • 8-bit branches (bra/bcc/…) still encode identically (shared, re-parametrized path).

The disassembler already knew $82; the opcode table lacked it, forcing
jmp.w for long intra-routine jumps. brl stays PC-relative (16-bit signed,
+/-32K), so it never touches the symbol table.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying a816 with  Cloudflare Pages  Cloudflare Pages

Latest commit: e9f2d53
Status: ✅  Deploy successful!
Preview URL: https://8772b65a.a816.pages.dev
Branch Preview URL: https://feat-brl-long-branch.a816.pages.dev

View logs

@sonarqubecloud

Copy link
Copy Markdown

@manz
manz merged commit f8a9bbf into master May 31, 2026
4 checks passed
@manz
manz deleted the feat/brl-long-branch branch May 31, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant