Ultimate 16-bit block clear and copy
Posted: Wed Oct 11, 2023 1:25 pm
Banged my head on these for ages so I'm posting them to get them out of my system. Great compromise between size and speed and ROM-compatible.
First the zero page pointer 'registers'.
Zeroing out blocks:
Moves (A=0 forwards):
Wouldn't be surprised if I missed some problems with this code. There cannot be blocks larger than $ff00 bytes but I'm sure you won't need any.
EDIT: X also had to be incremented at init.
First the zero page pointer 'registers'.
Code: Select all
; Source
sl:
s: .byte 0
sh: .byte 0
; Destination
dl:
d: .byte 0
dh: .byte 0
; Count
cl:
c: .byte 0
ch: .byte 0
Code: Select all
clrram:
ldx cl
inx
inc ch
ldy dl
lda #0
sta dl
beq +m ; (jmp)
l: sta (d),y
iny
beq +n
m: dex
bne -l
dec ch
bne -l
rts
n: inc dh
bne -m ; (jmp)
Code: Select all
moveram:
ldy #0
ldx cl
inx
inc ch
cmp #0
beq copy_forwards
bne copy_backwards
l: lda (s),y
sta (d),y
iny
beq +m
copy_forwards:
n: dex
bne -l
dec ch
bne -l
rts
m: inc sh
inc dh
bne -n ; (jmp)
l: lda (s),y
sta (d),y
dey
cpy #$ff
beq +m
copy_backwards:
n: dex
bne -l
dec ch
bne -l
rts
m: dec sh
dec dh
jmp -n
EDIT: X also had to be incremented at init.