It's time for me to be a man, quit hiding behind my desk chair, and face the horrors of bank and page wrapping. With the help of Bruce's great 65816 opcode tutorial (
http://6502.org/tutorials/65c816opcodes.html) and
Programming the 65816, I think I have condensed the logic of it all down to these pseudocode rules, organized by the addressing modes, since that is how I will be coding them.
First,
Direct Page Addressing:
Code:
IF (emulation mode) AND
(DL == 0) AND
(instruction from 6502 or 65C02)
THEN wrap on page boundry
ALWAYS: wrap bank 0
Then,
Stack Addressing:
Code:
IF (emulation mode) AND
(instruction from 6502 or 65C02)
THEN wrap page 01
ALWAYS: wrap bank 0
That seems straightforward, thought it took me hours to understand it. We have two more cases of
wrapping at bank 0:
Code:
IF (absolute mode) \ eg. JMP ($aaaa) / jmp.i aaaa
THEN wrap on bank 0 \ NOT rpt not PBR
IF (absolute indirect long) \ eg. JMP [$aaaa] / jmp.il aaaa
THEN wrap on bank 0 \ NOT rpt not PBR
The important part about Absolute Indrect Long is that the resulting effective address is 24 bit again, that is, we pull the Bank Byte, too.
Next, three cases of
wrapping at the Program Bank Register (PBR):
Code:
IF (absolute X indirect) \ eg. JSR ($aaaa,X) / jsr.xi aaaa
THEN wrap on PBR
IF (branch instuction) THEN wrap on PBR
Program Counter (PC) wraps at PBR \ duh
Last, we have instructions that
use the Data Bank Register (DBR) and are not Direct Page or Stack addressing. This is pretty much only:
Code:
IF (move addressing) THEN wrap on banks
which means MVN and MVP.
The last case hides an important rule: All X and X indexed absolute and long addressing modes that deal with the DBR extend into the next bank. Even more supremely important: This is true for Emulation Mode as well, an real, hard-core difference to the real 6502/65C02. The horror, the horror.
Or at least this is how I understand it. I'd be grateful if anybody sees something I might have missed?
EDIT: Corrected error in Direct Mode, must be DL instead of DH.
EDIT: DP and S mode always wrap on bank boundry, not only if we're not wrapping on page