-
Notifications
You must be signed in to change notification settings - Fork 6
/
conio.h
252 lines (209 loc) · 3.93 KB
/
conio.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* @file conio.h
* @brief Console I/O.
* @author Miguel I. Garcia Lopez / FloppySoftware
*
* Console I/O functions, for MESCC (Mike's Enhanced
* Small C Compiler for Z80 & CP/M).
*
* Supports following #defines:
* - CC_STDIO Support for stdin, stdout & stderr.
* - CC_CONIO_BIOS Support for direct console I/O.
*
* Revisions:
* - 22 Jan 2001 : Last revision.
* - 16 Apr 2007 : GPL'd.
* - 21 Apr 2007 : Changed puts for ANSI compatibility.
* - 15 May 2007 : Bug solved - added LF output to puts.
* - 13 Jul 2014 : Added kbhit().
* - 08 Dec 2014 : Added support for stdin, stdout & stderr.
* - 31 Dec 2014 : Solved bug in putstr when characters are > 0x7F.
* - 20 Dec 2015 : Added macro CC_CONIO_BIOS to support direct console I/O using BIOS, instead of BDOS.
* - 08 Jan 2015 : Modified getch() when access BDOS (fn. 6 instead of 1).
* - 10 Dec 2016 : Documented. GPL v3.
*
* Copyright (c) 1999-2016 Miguel I. Garcia Lopez / FloppySoftware.
*
* Licensed under the GNU General Public License v3.
*
* http://www.floppysoftware.es
* floppysoftware@gmail.com
*/
#ifndef CONIO_H
#define CONIO_H
/**
* @fn int putch(int ch)
* @brief Send character to the console.
* @param ch - character
* @return ch
*/
#ifdef CC_CONIO_BIOS
#asm
putch
PUSH HL
LD C,L
LD E,9
CALL xbios
POP HL
RET
xbios
LD HL,(1)
LD D,0
ADD HL,DE
JP (HL)
#endasm
#else
#asm
putch
PUSH HL
LD C,2
LD E,L
CALL 5
POP HL
RET
#endasm
#endif
/**
* @fn int getch(void)
* @brief Get character from the console without echo.
*
* Waits until a character is available.
*
* @return character
*/
#ifdef CC_CONIO_BIOS
#asm
getch
LD E,6
CALL xbios
LD H,0
LD L,A
RET
#endasm
#else
#asm
getch
LD C,6
LD E,255
CALL 5
OR A
JR Z,getch
LD H,0
LD L,A
RET
#endasm
#endif
/**
* @fn int kbhit(void)
* @brief Tests console input status.
* @return != 0 if a character is available, else 0.
*/
#ifdef CC_CONIO_BIOS
#asm
kbhit
LD E, 3
CALL xbios
LD H,A
LD L,A
RET
#endasm
#else
#asm
kbhit
LD C,11
CALL 5
LD H,A
LD L,A
RET
#endasm
#endif
/**
* @fn int getchar(void)
* @brief Get character from the console or stdin.
*
* Waits until a character is available.
*
* #ifdef CC_STDIO: Returns a character from stdin, or EOF on end of file or error.
* #ifndef CC_STDIO: Returns a character from the console. Echoes the character.
*
* @return character on success, else EOF.
*/
getchar()
{
#ifdef CC_STDIO
return fgetc(stdin);
#else
return putchar(getch());
#endif
}
/**
* @fn int putchar(int ch)
* @brief Send character to the console or stdout.
*
* #ifdef CC_STDIO: Returns ch, or EOF on error.
* #ifndef CC_STDIO: Returns ch.
*
* @param ch - character
* @return ch on success, else EOF.
*/
putchar(ch)
int ch;
{
#ifdef CC_STDIO
return fputc(ch, stdout);
#else
if(ch == '\n')
putch('\r');
return putch(ch);
#endif
}
/**
* @fn int putstr(char *s)
* @brief Send string to the console or stdout.
*
* #ifdef CC_STDIO: Returns the number of characters sent, or EOF on error.
* #ifndef CC_STDIO: Returns a non-negative value to indicate success.
*
* @param s - string
* @return number of characters sent on success, else EOF.
*/
putstr(s)
char *s;
{
#ifdef CC_STDIO
/* FIXME : Better if call to fputs (if available) */
int i, c;
i = 0;
while(*s)
{
/* FIXME : -1 hardcoded -- < 0 causes strange
behaviour if ch > 0x7F */
if((c = putchar(*s++)) == -1)
return c;
++i;
}
return i;
#else
while(*s)
putchar(*s++);
return 0;
#endif
}
/**
* @fn int puts(char *s)
* @brief Send string + '\n' to the console or stdout.
*
* #ifdef CC_STDIO: Returns the number of characters sent, or EOF on error.
* #ifndef CC_STDIO: Returns a non-negative value to indicate success.
*
* @param s - string
* @return number of characters sent on success, else EOF.
*/
puts(s)
char *s;
{
putstr(s);
return putchar('\n'); /* FIXME */
}
#endif