;;; -*- Mode: asm; indent-tabs-mode: t; tab-width: 8 -*-

;;Move memory from B_start B_end to A_start , (implied address)
;;Modifies A,X and B_start gets modified
.proc memcpy
        .include "mem.inc"

        ;; Let Y + A_start lower nibble represent A_start
        ;; therefor: A_start = Y - A_start
        ;; With Y we mean what Y will represent later aka >B_start
        Sub_16 A_start, A_start + 1, B_start, #$00

        ;;Lets move B_start lover-nibble to Y
        LDY B_start
        LDA #$00
        STA B_start
loop:
        Lag_16 B_end, B_end + 1, A, B_start, end_loop
        LDA (B_start), Y
        STA (A_start), Y
        ;Tip save time by counting downward, fast to check if Y ==0 // hugo
        INY
        TYA
        CMP #$ff
        BNE loop
        ;; Fix overflow
        LDY #$00
        INC A_start + 1
        INC B_start + 1
        JMP loop
end_loop:
RTS
.endproc