1/*
2Copyright (c) 1990 The Regents of the University of California.
3All rights reserved.
4
5Redistribution and use in source and binary forms are permitted
6provided that the above copyright notice and this paragraph are
7duplicated in all such forms and that any documentation,
8and/or other materials related to such
9distribution and use acknowledge that the software was developed
10by the University of California, Berkeley.  The name of the
11University may not be used to endorse or promote products derived
12from this software without specific prior written permission.
13THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17#include "setarch.h"
18
19#include "defines.h"
20
21#if defined (__H8300SX__)
22
23	.global	_memset
24_memset:
25	; Use er3 is a temporary since er0 must remain unchanged on exit.
26	mov.l	er0,er3
27
28	; Fill er1 with the byte to copy.
29	mov.b	r1l,r1h
30	mov.w	r1,e1
31
32	; Account for any excess bytes and words that will be copied after
33	; the main loop.  r2 >= 0 if there is a longword to copy.
34	sub	#4,LEN(r2)
35	blo	longs_done
36
37	; Copy one byte if doing so will make er3 word-aligned.
38	; This isn't needed for correctness but it makes the main loop
39	; slightly faster.
40	bld	#0,r3l
41	bcc	word_aligned
42	mov.b	r1l,@er3+
43	sub	#1,LEN(r2)
44	blo	longs_done
45
46word_aligned:
47	; Likewise one word for longword alignment.
48	bld	#1,r3l
49	bcc	long_copy
50	mov.w	r1,@er3+
51	sub	#2,LEN(r2)
52	blo	longs_done
53
54long_copy:
55	; Copy longwords.
56	mov.l	er1,@er3+
57	sub	#4,LEN(r2)
58	bhs	long_copy
59
60longs_done:
61	; At this point, we need to copy r2 & 3 bytes.  Copy a word
62	; if necessary.
63	bld	#1,r2l
64	bcc	words_done
65	mov.w	r1,@er3+
66
67words_done:
68	; Copy a byte.
69	bld	#0,r2l
70	bcc	bytes_done
71	mov.b	r1l,@er3+
72
73bytes_done:
74	rts
75
76#else
77
78; A0P pointer to cursor
79; A1P thing to copy
80	.global	_memset
81
82_memset:
83
84;	MOVP	@(2/4,r7),A2P	; dst
85;	MOVP	@(4/8,r7),A1	; src thing
86;	MOVP	@(6/12,r7),A3P	; len
87
88	MOVP	A2P,A2P
89	beq	quit
90
91	; A3 points to the end of the area
92	MOVP	A0P,A3P
93	ADDP	A2P,A3P
94
95	; see if we can do it in words
96	; by oring in the start of the buffer to the end address
97
98	or	A0L,A2L
99	btst	#0,A2L
100	bne	byteloop
101
102	; we can do it a word at a time
103
104	mov.b	A1L,A1H
105
106wordloop:
107	mov.w	A1,@-A3P
108	CMPP	A3P,A0P
109	bne	wordloop
110quit:	rts
111
112byteloop:
113	mov.b	A1L,@-A3P
114	CMPP	A3P,A0P
115	bne	byteloop
116	rts
117
118#endif
119