2 ; void* __fastcall__ memset (void* ptr, int c, size_t n);
3 ; void* __fastcall__ _bzero (void* ptr, size_t n);
4 ; void __fastcall__ bzero (void* ptr, size_t n);
6 ; Ullrich von Bassewitz, 29.05.1998
7 ; Performance increase (about 20%) by
8 ; Christian Krueger, 12.09.2009, slightly improved 12.01.2011
10 ; NOTE: bzero will return it's first argument as memset does. It is no problem
11 ; to declare the return value as void, since it may be ignored. _bzero
12 ; (note the leading underscore) is declared with the proper return type,
13 ; because the compiler will replace memset by _bzero if the fill value
14 ; is zero, and the optimizer looks at the return type to see if the value
15 ; in a/x is of any use.
18 .export _memset, _bzero, __bzero
20 .importzp sp, ptr1, ptr2, ptr3
26 ldx #0 ; Fill with zeros
35 ; Common stuff for memset and bzero from here
37 common: ; Fill value is in X!
40 sta ptr1+1 ; save high byte of ptr
45 lsr ptr3+1 ; divide number of
46 ror ptr3 ; bytes by two to increase
47 bcc evenCount ; speed (ptr3 = ptr3/2)
50 txa ; restore fill value
51 sta (ptr1),y ; save value and increase
52 inc ptr1 ; dest. pointer
56 lda ptr1 ; build second pointer section
58 adc ptr3 ; ptr2 = ptr1 + (length/2) <- ptr3
64 txa ; restore fill value
65 ldx ptr3+1 ; Get high byte of n
68 ; Set 256/512 byte blocks
70 L1: .repeat 2 ; Unroll this a bit to make it faster
71 sta (ptr1),y ; Set byte in lower section
72 sta (ptr2),y ; Set byte in upper section
78 dex ; Next 256 byte block
79 bne L1 ; Repeat if any
81 ; Set the remaining bytes if any
83 L2: ldy ptr3 ; Get the low byte of n
84 beq leave ; something to set? No -> leave
87 sta (ptr1),y ; set bytes in low
88 sta (ptr2),y ; and high section
89 bne L3 ; flags still up to date from dey!
91 jmp popax ; Pop ptr and return as result