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