-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmem.h
139 lines (121 loc) · 2.05 KB
/
mem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @file mem.h
* @brief Memory functions.
* @author Miguel I. Garcia Lopez / FloppySoftware
*
* Memory functions, for MESCC (Mike's Enhanced
* Small C Compiler for Z80 & CP/M).
*
* Revisions:
* - 25 Oct 2000 : Last revision.
* - 16 Apr 2007 : GPL'd.
* - 25 Aug 2016 : Documented. GPL v3.
* - 25 Dec 2018 : Optimize memset() for speed.
*
* Copyright (c) 1999-2018 Miguel I. Garcia Lopez / FloppySoftware.
*
* Licensed under the GNU General Public License v3.
*
* http://www.floppysoftware.es
* floppysoftware@gmail.com
*/
#ifndef MEM_H
#define MEM_H
/**
* @fn char *memset(char *dst, char data, int count)
* @brief Fill 'count' bytes with 'data' into 'dst'.
* @param dst - destination
* @param data - fill byte
* @param count - how many
* @return pointer to 'dst'
*/
#asm
memset:
POP AF
POP BC
POP DE
POP HL
PUSH HL
PUSH DE
PUSH BC
PUSH AF
LD A,B
OR C
RET Z
PUSH HL
LD (HL),E
LD D,H
LD E,L
INC DE
DEC BC
LDIR
POP HL
RET
#endasm
/**
* @fn char *memcpy(char *dst, char *src, int count)
* @brief Copy 'count' bytes from 'src' to 'dst'.
* @param dst - destination
* @param src - source
* @param count - how many
* @return pointer to 'dst'
*/
#asm
memcpy:
POP AF
POP BC
POP HL
POP DE
PUSH DE
PUSH HL
PUSH BC
PUSH AF
PUSH DE
LD A,B
OR C
JR Z,memcpy2
LDIR
memcpy2
POP HL
RET
#endasm
/**
* @fn int memcmp(char *mem1, char *mem2, int count)
* @brief Compare 'count' bytes.
* @param mem1 - pointer
* @param mem2 - pointer
* @param count - how many
* @return <0 on mem1 < mem2; =0 on mem1 == mem2; >0 on mem1 > mem2
*/
#asm
memcmp
POP AF
POP BC
POP HL
POP DE
PUSH DE
PUSH HL
PUSH BC
PUSH AF
memcmp1
LD A,C
OR B
JR Z,memcmp2
DEC BC
LD A,(DE)
CP (HL)
INC DE
INC HL
JR Z,memcmp1
memcmp2
LD HL,0
RET Z
JR NC,memcmp3
DEC HL
RET
memcmp3
INC L
RET
#endasm
#endif