memset.S revision 1.1 1 /* $NetBSD: memset.S,v 1.1 2020/08/16 06:43:43 isaki Exp $ */
2
3 /*
4 * Copyright (C) 2020 Tetsuya Isaki. All rights reserved.
5 * Copyright (C) 2020 Y.Sugahara (moveccr). All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Size optimized (but slow) version for primary bootloader.
31 */
32
33 #include <machine/asm.h>
34
35 |
36 | void bzero(void *dst, size_t len)
37 |
38 ASENTRY_NOPROFILE(bzero)
39 moveml %sp@,%d0-%d1/%a1 | %d0: (return address)
40 | %d1: dst
41 | %a1: len
42
43 subal %a0,%a0 | %a0: c = 0
44 jbra memset_common
45
46 |
47 | void *memset(void *dst, int c, size_t len);
48 |
49 ASENTRY_NOPROFILE(memset)
50 moveml %sp@,%d0-%d1/%a0-%a1 | %d0: (return address)
51 | %d1: dst
52 | %a0: c
53 | %a1: len
54 memset_common:
55 addal %d1,%a1 | %a1: loop start+1 address
56
57 exg %d1,%a0 | %d1: c
58 | %a0: initial dst
59 | for return value and
60 | loop-end condition address
61 jbra 2f
62 1:
63 moveb %d1,%a1@- | memset in reverse direction
64 2:
65 cmpl %a1,%a0
66 jne 1b | if condition met, %a0 = dst
67 rts
68
69
70 #if defined(SELFTEST)
71 #include "iocscall.h"
72 .macro PRINT msg
73 leal \msg,%a1
74 IOCS(__B_PRINT)
75 .endm
76
77 .macro TEST name
78 leal \name,%a2
79 jbsr test
80 .endm
81
82 ASENTRY_NOPROFILE(selftest_memset)
83 moveml %d2-%d7/%a2-%a6,%sp@-
84 PRINT %pc@(msg_testname)
85
86 TEST test1
87 TEST test2
88
89 PRINT %pc@(msg_crlf)
90 moveml %sp@+,%d2-%d7/%a2-%a6
91 rts
92
93 test:
94 movel %a2@+,buf:W | initial contents of buffer
95 movel %a2@+,%sp@- | push len
96 movel %a2@+,%sp@- | push c
97 movel %a2@+,%a3 | keep dst and
98 movel %a3,%sp@- | push dst
99 jbsr memset
100 leal %sp@(12),%sp
101
102 cmpal %a3,%a0 | compare return value
103 jne fail
104 movel %a2@+,%d0 | compare buf[0..4]
105 cmpl buf:W,%d0
106 jne fail
107 PRINT %pc@(msg_ok)
108 rts
109 fail:
110 PRINT %pc@(msg_fail)
111 rts
112
113 test1:
114 .long 0x11223344 | initial buf
115 .long 2 | len
116 .long 0xaa | c
117 .long buf+1 | dst
118 .long 0x11aaaa44 | expected buf
119
120 test2:
121 | len == 0
122 .long 0x11223344 | initial buf
123 .long 0 | len
124 .long 0x55 | c
125 .long buf+1 | dst
126 .long 0x11223344 | expected buf
127
128 msg_testname:
129 .asciz "memset"
130 msg_ok:
131 .asciz " ok"
132 msg_fail:
133 .asciz " fail"
134 msg_crlf:
135 .asciz "\r\n"
136
137 BSS(buf, 8)
138 #endif
139