]> git.sur5r.net Git - cc65/blob - libsrc/common/memcpy.s
Changed most "backticks" (grave accents) into apostrophes.
[cc65] / libsrc / common / memcpy.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__ memcpy (void* dest, const void* src, size_t n);
7 ;
8 ; NOTE: This function contains entry points for memmove, which will ressort
9 ; to memcpy for an upwards copy. Don't change this module without looking
10 ; at memmove!
11 ;
12
13         .export         _memcpy, memcpy_upwards, memcpy_getparams
14         .import         popax, popptr1
15         .importzp       sp, ptr1, ptr2, ptr3
16
17 ; ----------------------------------------------------------------------
18 _memcpy:
19         jsr     memcpy_getparams
20
21 memcpy_upwards:                 ; assert Y = 0
22         ldx     ptr3+1          ; Get high byte of n
23         beq     L2              ; Jump if zero
24
25 L1:     .repeat 2               ; Unroll this a bit to make it faster...
26         lda     (ptr1),Y        ; copy a byte
27         sta     (ptr2),Y
28         iny
29         .endrepeat
30         bne     L1
31         inc     ptr1+1
32         inc     ptr2+1
33         dex                     ; Next 256 byte block
34         bne     L1              ; Repeat if any
35
36         ; the following section could be 10% faster if we were able to copy
37         ; back to front - unfortunately we are forced to copy strict from
38         ; low to high since this function is also used for
39         ; memmove and blocks could be overlapping!
40         ; {
41 L2:                             ; assert Y = 0
42         ldx     ptr3            ; Get the low byte of n
43         beq     done            ; something to copy
44
45 L3:     lda     (ptr1),Y        ; copy a byte
46         sta     (ptr2),Y
47         iny
48         dex
49         bne     L3
50
51         ; }
52
53 done:   jmp     popax           ; Pop ptr and return as result
54
55 ; ----------------------------------------------------------------------
56 ; Get the parameters from stack as follows:
57 ;
58 ;       size            --> ptr3
59 ;       src             --> ptr1
60 ;       dest            --> ptr2
61 ;       First argument (dest) will remain on stack and is returned in a/x!
62
63 memcpy_getparams:               ; IMPORTANT! Function has to leave with Y=0!
64         sta     ptr3
65         stx     ptr3+1          ; save n to ptr3
66
67         jsr     popptr1         ; save src to ptr1
68
69                                 ; save dest to ptr2
70         iny                     ; Y=0 guaranteed by popptr1, we need '1' here...                        
71                                 ; (direct stack access is three cycles faster
72                                 ; (total cycle count with return))
73         lda     (sp),y
74         tax
75         stx     ptr2+1          ; save high byte of ptr2
76         dey                     ; Y = 0
77         lda     (sp),y          ; Get ptr2 low
78         sta     ptr2
79         rts