-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.c
More file actions
239 lines (223 loc) · 4.77 KB
/
io.c
File metadata and controls
239 lines (223 loc) · 4.77 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
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
/*******************************************************
* io.c - Simple program for my boot loader. *
*******************************************************
* Create by Philip R. Simonson (aka 5n4k3) (2018) *
*******************************************************
*/
#include "code16gcc.h"
#include "system.h"
#include "disk.h"
/* program for my boot loader to run */
void
main (void)
{
extern void graphics(void);
extern void graphics2(void);
extern void typing(void);
extern void test_getline(void);
extern void test_getline2(void);
extern void test_getch(void);
unsigned char ch;
for (;;) {
print("Press 'q' to reboot system...\r\n"
"Press 'e' to wipe CMOS!\r\n"
"Press 'g' for graphics.\r\n"
"Press 't' for more graphics.\r\n"
"Press 'h' for timed typing.\r\n"
"Press 'o' for testing my getline.\r\n"
"Press 'p' for testing my getline again.\r\n"
"Press 'm' for testing my getchar.\r\n");
ch = getch();
switch (ch) {
case 'q':
case 'Q':
reboot();
break;
case 'e':
case 'E':
print("Wiping CMOS...\r\n");
clear_cmos();
break;
case 'g':
case 'G':
graphics();
break;
case 't':
case 'T':
graphics2();
break;
case 'h':
case 'H':
typing();
break;
case 'o':
case 'O':
test_getline();
break;
case 'p':
case 'P':
test_getline2();
break;
case 'm':
case 'M':
test_getch();
break;
default:
print("Invalid key pressed\r\n");
break;
}
}
}
/* graphics: testing simple graphics */
void
graphics (void)
{
unsigned short x, y;
init_graphics(0x12);
for (y = 130; y <= 200; y++)
for (x = 25; x <= 277; x++)
draw_pixel(y, x+3, 0xfb);
move(9, 9);
print_color(" By PRS (aka 5n4k3) ", 0xfb);
move(10, 10);
print_color(" Hello world ! ! ! ", 0xfb);
move(3, 11);
print_color(" What happens when you're good ? ", 0xfb);
move(10, 13);
print_color(" Press a key . . . ", 0x0f);
getch();
init_graphics(0x02);
}
/* graphics2: testing simple graphics */
void
graphics2 (void)
{
unsigned short x, y;
init_graphics(0x12);
for (y = 0; y <= 480; y += 2)
for (x = 0; x <= 640; x += 2)
draw_pixel(y, x, 0x5a);
move(10, 10);
print_color(" Hello ! ! ! ", 0x0A);
getch();
init_graphics(0x02);
}
/* test_getline: function to test my getline function */
void
test_getline (void)
{
char buf[64];
init_graphics(0x02);
print("Enter your name: ");
getline(buf, sizeof(buf));
trim(buf);
init_graphics(0x02);
if (!strcmp(buf, "Philip Simonson"))
print("Hello, master.\r\n");
else
print("I don't know you.\r\n");
print("Press any key to quit...");
getch();
init_graphics(0x02);
}
void
test_getline2 (void)
{
char buf[64];
print("Enter your name: ");
getline(buf, sizeof buf);
trim(buf);
print("\r\n");
if (!strcmp(buf, ""))
print("Please enter your name.\r\n");
else {
print("Hello, ");
print(buf);
print(".");
}
print("\r\nPress any key to quit...\r\n");
getch();
}
char
is_alpha (char ch)
{
return ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z'));
}
char
is_num (char ch)
{
return (ch >= '0' && ch <= '9');
}
#define NAME 0
char
get_token (char *token)
{
char ch;
while ((ch = getchar()) == ' ' || ch == '\t' || ch == '\b');
if (ch == '\r' || ch == '\n') {
ungetch(ch);
return -1;
}
if (is_alpha(ch)) {
for (*token++ = ch; is_alpha(ch = getchar()); )
*token++ = ch;
*token = '\0';
ungetch(ch); /* if not push, back onto the buffer */
return NAME;
} else
return ch;
}
void
test_getch (void)
{
char buf[12], tname;
int i;
init_gbuf();
init_graphics(0x03);
i = 3;
do {
--i;
print("Password: ");
if ((tname = get_token(buf)) == 0) {
if (strcmp(buf, "pass") == 0)
print("\r\nAccess granted!\r\n");
else
print("\r\nAccess denied!\r\n");
} else if (tname == EOF)
break;
} while (i > 0);
if (!i)
print("\r\nRan out of tries..\r\n");
getch();
init_graphics(0x03);
}
#define TYPING_MESSAGE \
"This is going to be typed on the screen...\r\n"\
"\r\n"\
"It's pretty cool that you can make a bootloader and program almost\r\n"\
"all in C, using just a bit of assembler to start off the bootloader.\r\n"\
"All the rest is in C, but all standard functions in libraries have\r\n"\
"to be written in inline assembler. Then the actual main functions\r\n"\
"and what not, all in C. That is the only way you can write any boot\r\n"\
"code in C. Unfortunately, you have to have assembler mixed with C.\r\n"\
"Just plain C alone will NOT work!\r\n"
/* typing: simple function to type text on the screen */
void
typing (void)
{
int i;
init_graphics(0x02);
for (i = 0; i < strlen(TYPING_MESSAGE); i++) {
putchar(TYPING_MESSAGE[i]);
play_sound(0x13fb);
timer(0x0001, 0x8040);
stop_sound();
}
move(34, 15);
print("- Philip R. Simonson (aka 5n4k3)");
move(0, 24);
print("Press any key to continue . . .");
getch();
init_graphics(0x02);
}