I'm having way more fun doing 6502 asm again rather than actually making a game. :p
This seemed useful enough to share, a fast divide by 240 with modulo. Allows you to store scroll offsets in > 8 bits, and quickly get the scroll value and starting page. 12 bits gives you lots of room to work with.
This seemed useful enough to share, a fast divide by 240 with modulo. Allows you to store scroll offsets in > 8 bits, and quickly get the scroll value and starting page. 12 bits gives you lots of room to work with.
Code:
; Divides sreg (up to 12 bits) by 240.
; Returns the quotient in x, remainder in a.
.proc div240_quick
; Start with q = n/256
ldx sreg + 1
; r += 16*q
txa
asl
asl
asl
asl
; Overflows for > 12 bits!
; Assume carry cleared by asl.
adc sreg
; Check for overflow, and adjust q and r again.
bcc :+
; q += 1
inx
; r += 16
adc #15 ; +1 for carry flag
:
; Check if r > 240 and adjust once more.
cmp #240
bcc :+
inx
sbc #240
:
rts
.endproc
; Returns the quotient in x, remainder in a.
.proc div240_quick
; Start with q = n/256
ldx sreg + 1
; r += 16*q
txa
asl
asl
asl
asl
; Overflows for > 12 bits!
; Assume carry cleared by asl.
adc sreg
; Check for overflow, and adjust q and r again.
bcc :+
; q += 1
inx
; r += 16
adc #15 ; +1 for carry flag
:
; Check if r > 240 and adjust once more.
cmp #240
bcc :+
inx
sbc #240
:
rts
.endproc