Ensure data doesn't cross page boundary

153 views Asked by At

I'm trying to create a switch statement as below, which works well until something crosses a page. The switch destination is auto generated, which is why its in another file. 'structure, x' holds the offset (the case switch). In the case below, it will be either $00, $02, $04 or $06.

Is there anyway to ensure that the returnAddr isn't at $xx00? (Does that actually matter here?) And that the switchlist doesn't cross a boundary?

    lda #>returnAddr
    pha
    lda #<returnAddr-1
    pha

    ; store where we want to go
    lda switchlist+1
    pha
    lda switchlist
    clc
    adc structure, x
    pha

    rts ; make call to the proc in the switchlist

returnAddr:
    ; ...
    rts

and in another file I have (where case_x are function labels)

switchlist:
    .word case_1
    .word case_2
    .word case_3
    .word case_4
1

There are 1 answers

0
puppydrum64 On

I was always taught to structure it like this:

LDA #2   ;say we want case_3
ASL      
TAX
JSR handleSwitch  ;pushes the return address for us.
;return here after we goto the desired case.
handleSwitch:
LDA switchlist+1,x
pha
LDA switchlist
pha
rts  ;"return" to desired case

case_1:
rts  ;each of these returns to after "jsr handleSwitch"
case_2:
rts
case_3:
rts
case_4:
rts

The key here is that you JSR to your trampoline, inlining it won't have the desired effect. If you build it this way it doesn't matter if your return address crosses a page boundary.