Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

6502 Relative Address mode wrapping

Tags:

assembly

6502

Reading the documentation on the 6502, and I havent been able to find this answered anywhere. It's possible to wrap when using relative addressing. But is it possible to wrap around both ends? I am guessing that is possible however unlikely though.

And when I mean both ends, I mean I know you can wrap from a high PC to a low PC. But could you also wrap from a low PC to a high PC?

like image 349
Aaron M Avatar asked Dec 27 '22 06:12

Aaron M


2 Answers

Crossing a page boundary with a relative branch will incur an extra cycle, but it doesn't make any difference whether the page-crossing branch is a forward branch or a backwards branch.

You can try this out in Visual6502. Enter the program A9 00 F0 EC (LDA #00 / BEQ $FFF0) at address 0000, single-step through the code and see where it ends up after the BEQ. If you only trust real hardware you could easily verify this on something like a Nintendo Entertainment System.

Obviously to be able to reach page FF with a relative branch you'd have to be executing out of zeropage RAM, and you probably don't want to waste ZP RAM on code since it needs to fit your most frequently accessed data. So that would make this particular scenario unlikely.

like image 57
Michael Avatar answered Jan 14 '23 20:01

Michael


Yes, it works backwards, too. But many assemblers doesn't support that. When you try to put a branch operator at the beginning of zeropage addressing >$ff80 area, assemblers usually throws errors like "offset too large". But when you try placing the opcode with byte values like > 0002 f0 e0 you get the result beq $ffe4. I used 0002 as the first memory address available since I used a Commodore 64 to try it and $0001 is not usable for that machine. For other 6502 based machines, $0000 should be ok, too.

like image 38
Emir Akaydın Avatar answered Jan 14 '23 18:01

Emir Akaydın