Ultimate 16-bit block clear and copy

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
pixel
Vic 20 Scientist
Posts: 1358
Joined: Fri Feb 28, 2014 3:56 am
Website: http://hugbox.org/
Location: Berlin, Germany
Occupation: Pan–galactic shaman

Ultimate 16-bit block clear and copy

Post by pixel »

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'.

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
Zeroing out blocks:

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)
Moves (A=0 forwards):

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
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.
A man without talent or ambition is most easily pleased. Others set his path and he is content.
https://github.com/SvenMichaelKlose
Post Reply