I have two bytes for both the regular 16 bit scroll (scrolly), and for the y scroll whose low byte wraps at > 239. (scrollyscreen)
I add the same value to both whenever the game scrolls on the y axis. For scrollyscreen, I just check if the low byte is higher than 239 and add an extra 16 in that case, then update the high byte accordingly.
For subtraction, when the carry is clear after passing zero, subtract an additional 16.
All that said, I also have subroutine that can pull the correct scrollyscreen value from the regular scrolly. But I use it only when the regular scroll is forced in bounds by the bottom of the level map. (And only for safety, I'm pretty sure even that's not needed with how I do things.) You can just subtract from scrollyscreen how far scrolly was ejected and never need to call this.
Here it is anyway:
Code:
pullyscreenfromyscroll:;{
ldx #$FF;FF because an extra 1 is always added
lda <scrollyhigh
sec
yscreenfromyscrollbigadd:
inx;We add 1 to X for every time $0F can be subtracted from scrollyhigh
sbc #$0F
bcs yscreenfromyscrollbigadd
ldy highzerotosixteen,x;We then "add" $10 for every $0F in scrollyscreenhigh
sty <scrollyscreenhigh
ldx <scrollylow
adc #$0F;Fixing the remainder, since an extra $0F is always subtracted
beq yscreenfromyscrollfinalcheck
tay;Store the remainder in Y
txa
yscreenfromyscrollhighcheck.loopcheck:
sec
yscreenfromyscrollhighcheck.2loopcheck:
sbc #$F0
inc <scrollyscreenhigh
bcs yscreenfromyscrollhighcheck.2loopcheck
dey
bne yscreenfromyscrollhighcheck.loopcheck
tax
yscreenfromyscrollfinalcheck:
txa
cmp #$F0
bcc yscreenfromyscrollfinaladd
;sec
sbc #$F0
inc <scrollyscreenhigh
yscreenfromyscrollfinaladd:
sta <scrollyscreenlow
rts;}
highzerotosixteen:
.db $00, $10, $20, $30, $40, $50, $60, $70
.db $80, $90, $A0, $B0, $C0, $D0, $E0, $F0
It could probably be much faster, but what I've got is WAY faster than how it used to be, heh. And that was when it was called every frame.
It depends on how big your level is (and also probably fails if your level is taller than 239 screens), but in the bottom of my tallest vertical level (2304 pixels tall) at the moment it runs at 164 cycles. Certainly fast enough to call every frame, I'd say.
Edit: Hah, this can probably be made faster by messing with the txa/tax instructions. But honestly? I'm too scared of breaking it right now.
This seems like it'd work:
Edit 2: Nope, it wouldn't so removed it. There's gotta be a way, but the savings would be miniscule for that small change anyway. New, faster methods are welcome, though. I'm not exactly a 6502 magician.