-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.h
More file actions
176 lines (157 loc) · 3.09 KB
/
io.h
File metadata and controls
176 lines (157 loc) · 3.09 KB
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
#ifndef _IO_H_
#define _IO_H_
#define EOF -1
#define BUFSIZ 128
static char g_buf[BUFSIZ];
static int g_bufp;
/* putch: puts a character on the screen */
static void
putch (char c, unsigned char color)
{
__asm__ __volatile__ ("int $0x10"::"a"(0x0e00 | c), "b"(0x0000 | color));
}
/* putchar: puts a character on the screen; with color gray on black */
#define putchar(c) putch(c, 0x07)
/* move: moves cursor to x,y position */
static void
move (unsigned char x, unsigned char y)
{
__asm__ __volatile__ ("int $0x10"
::"a"(0x0200), "b"(0x0007), "d"((y << 8) | x));
}
/* print_color: prints a string in color */
static void
print_color (const char *s, unsigned char color)
{
while (*s) {
putch(*s++, color);
}
}
/* print: prints a string normal color */
#define print(msg) print_color(msg, 0x07)
/* strlen: count length of string; to null terminator */
static int
strlen (const char *s)
{
const char *p = s;
while (*p++);
return (p-s-1);
}
/* getch: get character from keyboard */
static char
getch (void)
{
char ch;
__asm__ __volatile__ ("int $0x16":"=a"(ch):"a"(0x0000));
__asm__ __volatile__ ("int $0x16"::"a"(0x0100));
return ch;
}
/* getche_color: get character from keyboard; put to screen in color */
static char
getche_color (unsigned char color)
{
char ch;
ch = getch();
putch(ch, color);
return ch;
}
#define getche() getche_color(0x07)
/* init_gbuf: initialize and clear buffer */
static void
init_gbuf (void)
{
int i;
for (i=0; i<BUFSIZ; i++)
g_buf[i] = '\0';
g_bufp = 0;
}
/* print_gbuf: prints contents of buffer */
static void
print_gbuf (void)
{
print(g_buf);
}
/* getchar: get character into buffer */
static char
getchar (void)
{
char ch = getche();
if (g_bufp >= BUFSIZ) {
print("Too many characters...\r\n");
return -1;
} else
g_buf[g_bufp++] = ch;
return ch;
}
/* ungetch: push char back to standard input */
static void
ungetch (char ch)
{
if (g_bufp > 0) {
g_buf[--g_bufp] = ch;
} else
print("No characters on buffer.\r\n");
}
/* getline: get string of characters; size of lim-1 */
static int
getline (char *s, int lim)
{
char *p = s;
char c = -1;
while (lim-- > 2 && (c = getche()) != '\r')
switch (c) {
case '\b':
*--s = '\0';
print(" \b");
break;
case '\r':
putchar('\n');
break;
default:
*s++ = c;
break;
}
if (c == '\r') {
*s++ = c;
*s++ = '\n';
}
*s = '\0';
return s-p;
}
/* trim: trim new line off string s */
static void
trim(char *s)
{
while (*s) {
if (*s == '\r' || *s == '\n')
break;
s++;
}
*s = '\0';
}
/* strcmp: compare t to s; returns 0 if match, anything else if false */
static int
strcmp (const char *s, const char *t)
{
while (*s == *t++)
if (*s++ == '\0')
return 0;
return *s-*t;
}
/* bufcmp: compare getchar buffer to a string */
static int
bufcmp (const char *s)
{
char *t = g_buf;
while (*s == *t++)
if (*s++ == '\0')
return 0;
return *s-*t;
}
/* draw_pixel: draws a pixel at specified location */
static void
draw_pixel (unsigned short x, unsigned short y, short unsigned int color)
{
__asm__ __volatile__ ("int $0x10"::"a"(0x0c00 | color), "c"(y), "d"(x));
}
#endif