; simple banked memcpy routines
; could be improved by buffering and more

	zpage	btmp0
	zpage	btmp1
	zpage	r0
	zpage	r1
	zpage	r28
	zpage	r29
	zpage	r30
	zpage	r31

	global	__fmemcpy
	global	___bankcopy
	global	___bankcopy_len

bufsize	equ	24

	global	__fmemcpy
	section	text
__fmemcpy:
	sta	retbank
	lda	r0
	sta	___bankcopy_len
	lda	r1
	sta	___bankcopy_len+1
	lda	btmp1
	sta	r28
	lda	btmp1+1
	sta	r29
	ldy	btmp1+2
	lda	btmp0
	sta	r30
	lda	btmp0+1
	sta	r31
	ldx	btmp0+2
	jmp	in

___bankcopy:
	sta	retbank
in:
	sty	sbank
	ldy	#0
loop:
	lda	sbank
	sta	$f4
	sta	$fe30
	lda	(r28),y
	stx	$f4
	stx	$fe30
	sta	(r30),y
	inc	r28
	bne	skip1
	inc	r29
skip1:
	inc	r30
	bne	skip2
	inc	r31
skip2:
	lda	___bankcopy_len
	bne	skip3
	dec	___bankcopy_len+1
skip3:
	dec	___bankcopy_len
	lda	___bankcopy_len
	ora	___bankcopy_len+1
	bne	loop

	lda	retbank
	sta	$f4
	sta	$fe30

	rts

	section	bss
retbank:
	reserve	1
sbank:
	reserve	1
___bankcopy_len:
	reserve	2
