]> git.sur5r.net Git - cc65/blob - libsrc/common/memmove.s
Made Olivers devnum patch (r4588) work with the PET-II models. On these
[cc65] / libsrc / common / memmove.s
1 ;
2 ; Ullrich von Bassewitz, 2003-08-20
3 ; Performance increase (about 20%) by
4 ; Christian Krueger, 2009-09-13
5 ;
6 ; void* __fastcall__ memmove (void* dest, const void* src, size_t size);
7 ;
8 ; NOTE: This function uses entry points from memcpy!
9 ;
10
11         .export         _memmove
12         .import         memcpy_getparams, memcpy_upwards, popax
13         .importzp       ptr1, ptr2, ptr3, ptr4, tmp1
14
15         .macpack        generic
16         .macpack        longbranch
17
18 ; ----------------------------------------------------------------------
19 _memmove:
20         jsr     memcpy_getparams
21
22 ; Check for the copy direction. If dest < src, we must copy upwards (start at
23 ; low addresses and increase pointers), otherwise we must copy downwards
24 ; (start at high addresses and decrease pointers).
25
26         sec
27         sbc     ptr1
28         txa
29         sbc     ptr1+1
30         jcc     memcpy_upwards  ; Branch if dest < src (upwards copy)
31
32 ; Copy downwards. Adjust the pointers to the end of the memory regions.
33
34         lda     ptr1+1
35         add     ptr3+1
36         sta     ptr1+1
37
38         lda     ptr2+1
39         add     ptr3+1
40         sta     ptr2+1
41
42 ; handle fractions of a page size first
43
44         ldy     ptr3            ; count, low byte
45         bne     @entry          ; something to copy?
46         beq     PageSizeCopy    ; here like bra...
47
48 @copyByte:
49         lda     (ptr1),y
50         sta     (ptr2),y
51 @entry:
52         dey
53         bne     @copyByte
54         lda     (ptr1),y        ; copy remaining byte
55         sta     (ptr2),y
56
57 PageSizeCopy:                   ; assert Y = 0
58         ldx     ptr3+1          ; number of pages
59         beq     done            ; none? -> done
60
61 @initBase:
62         dec     ptr1+1          ; adjust base...
63         dec     ptr2+1
64         dey                     ; in entry case: 0 -> FF
65         lda     (ptr1),y        ; need to copy this 'intro byte'
66         sta     (ptr2),y        ; to 'land' later on Y=0! (as a result of the '.repeat'-block!)
67         dey                     ; FF ->FE
68 @copyBytes:
69         .repeat 2               ; Unroll this a bit to make it faster...
70         lda     (ptr1),y
71         sta     (ptr2),y
72         dey
73         .endrepeat
74 @copyEntry:                     ; in entry case: 0 -> FF
75         bne     @copyBytes
76         lda     (ptr1),y        ; Y = 0, copy last byte
77         sta     (ptr2),y
78         dex                     ; one page to copy less
79         bne     @initBase       ; still a page to copy?
80
81 ; Done, return dest
82
83 done:   jmp     popax           ; Pop ptr and return as result
84