-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
187 lines (162 loc) · 3.17 KB
/
io.c
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
/*
* io.c -
*/
#include <io.h>
#include <types.h>
/**************/
/** Screen ***/
/**************/
#define NUM_COLUMNS 80
#define NUM_ROWS 25
Byte x, y = 19;
char buffer[12];
/* Read a byte from 'port' */
Byte inb(unsigned short port)
{
Byte v;
__asm__ __volatile__("inb %w1,%0"
: "=a"(v)
: "Nd"(port));
return v;
}
void printc(char c)
{
__asm__ __volatile__("movb %0, %%al; outb $0xe9" ::"a"(c)); /* Magic BOCHS debug: writes 'c' to port 0xe9 */
if (c == '\n')
{
x = 0;
y = (y + 1) % NUM_ROWS;
}
else
{
Word ch = (Word)(c & 0x00FF) | 0x0200;
Word *screen = (Word *)0xb8000;
screen[(y * NUM_COLUMNS + x)] = ch;
if (++x >= NUM_COLUMNS)
{
x = 0;
y = (y + 1) % NUM_ROWS;
}
}
}
void printc_color(Byte color, char c)
{
__asm__ __volatile__("movb %0, %%al; outb $0xe9" ::"a"(c)); /* Magic BOCHS debug: writes 'c' to port 0xe9 */
if (c == '\n')
{
x = 0;
y = (y + 1) % NUM_ROWS;
}
else
{
Word ch = (Word)(c & 0x00FF) | (color << 8);
Word *screen = (Word *)0xb8000;
screen[(y * NUM_COLUMNS + x)] = ch;
if (++x >= NUM_COLUMNS)
{
x = 0;
y = (y + 1) % NUM_ROWS;
}
}
}
void printc_scroll(char c, Byte color)
{
__asm__ __volatile__("movb %0, %%al; outb $0xe9" ::"a"(c)); /* Magic BOCHS debug: writes 'c' to port 0xe9 */
if (y == NUM_ROWS)
{
Word *screen = (Word *)0xb8000;
for (int i = 0; i < NUM_ROWS - 1; i++)
{
for (int j = 0; j < NUM_COLUMNS; j++)
{
screen[(i * NUM_COLUMNS + j)] = screen[((i + 1) * NUM_COLUMNS + j)];
}
}
for (int j = 0; j < NUM_COLUMNS; j++)
{
screen[((NUM_ROWS - 1) * NUM_COLUMNS + j)] = 0;
}
--y;
}
if (c == '\n')
{
x = 0;
y++;
}
else
{
Word ch = (Word)(c & 0x00FF) | (color << 8); // (Word)(c & 0x00FF) | 0x0200;
Word *screen = (Word *)0xb8000;
screen[(y * NUM_COLUMNS + x)] = ch;
if (++x >= NUM_COLUMNS)
{
x = 0;
y++;
}
}
}
void printc_xy(Byte mx, Byte my, char c)
{
Byte cx, cy;
cx = x;
cy = y;
x = mx;
y = my;
printc(c);
x = cx;
y = cy;
}
void printk(char *string)
{
int i;
for (i = 0; string[i]; i++)
printc_scroll(string[i], 0x02);
}
void println(char *string)
{
printk("\n");
printk(string);
}
void printk_color(char *string, Byte color)
{
int i;
for (i = 0; string[i]; i++)
printc_scroll(string[i], color);
}
void panic(char *string)
{
printk_color("\n[ERROR]: ", 0x04);
printk_color(string, 0x04);
breakpoint;
}
void int_to_string(int a, char *b)
{
int i, i1;
char c;
if (a == 0)
{
b[0] = '0';
b[1] = 0;
return;
}
i = 0;
while (a > 0)
{
b[i] = (a % 10) + '0';
a = a / 10;
i++;
}
for (i1 = 0; i1 < i / 2; i1++)
{
c = b[i1];
b[i1] = b[i - i1 - 1];
b[i - i1 - 1] = c;
}
b[i] = 0;
}
void printvar(int var)
{
int_to_string(var, buffer);
printk_color("\n[VARIABLE]: ", 0x03);
printk_color(buffer, 0x03);
}