2 ; This file, instead of "common/memset.s", will be assembled for the pce
3 ; target. This version is smaller and faster because it uses a HuC6280
4 ; block-copy instruction.
6 ; 1998-05-29, Ullrich von Bassewitz
7 ; 2015-11-06, Greg King
9 ; void* __fastcall__ _bzero (void* ptr, size_t n);
10 ; void __fastcall__ bzero (void* ptr, size_t n);
11 ; void* __fastcall__ memset (void* ptr, int c, size_t n);
13 ; NOTE: bzero() will return its first argument, as memset() does. It is no
14 ; problem to declare the return value as void, because it can be ignored.
15 ; _bzero() (note the leading underscore) is declared with the proper
16 ; return type because the compiler will replace memset() by _bzero() if
17 ; the fill value is zero; and, the optimizer looks at the return type
18 ; to see if the value in .XA is of any use.
20 ; NOTE: This function uses entry points from "pce/memcpy.s"!
23 .export __bzero, _bzero, _memset
25 .import memcpy_getparams, memcpy_increment
27 .importzp ptr1, ptr2, ptr3
32 ; ----------------------------------------------------------------------
36 jsr pushax ; (high byte isn't important)
42 ; The fill byte is put at the beginning of the buffer; then, the buffer is
43 ; copied to a second buffer that starts one byte above the start of the first
44 ; buffer. Normally, we would use memmove() to avoid trouble; but here, we
45 ; exploit that overlap, by using memcpy(). Therefore, the fill value is copied
46 ; from each byte to the next byte, all the way to the end of the buffer.
48 lda ptr1 ; get fill value
51 lda ptr3 ; count first byte
57 jeq popax ; return ptr. if no more bytes
59 lda ptr2 ; point to first buffer
63 inc ptr2 ; point to second buffer
67 @L2: jmp memcpy_increment