-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb.c
245 lines (200 loc) · 4.35 KB
/
fb.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
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
#include "fb.h"
#include "io.h"
#include "common.h"
#include "vfs.h"
#include "log.h"
#define FB_MEMORY KERNEL_START_VADDR + 0x000B8000
#define FB_NUM_COLS 80
#define FB_NUM_ROWS 25
#define FB_CURSOR_DATA_PORT 0x3D5
#define FB_CURSOR_INDEX_PORT 0x3D4
#define FB_HIGH_BYTE 14
#define FB_LOW_BYTE 15
#define BLACK_ON_WHITE 0x0F
#define TO_ADDRESS(row, col) (fb + 2 * (row * FB_NUM_COLS + col))
#define FB_BACKSPACE_ASCII 8
static uint8_t *fb = (uint8_t *) FB_MEMORY;
static uint16_t cursor_pos;
static vnodeops_t vnodeops;
static uint8_t read_cell(uint32_t row, uint32_t col)
{
uint8_t *cell = TO_ADDRESS(row, col);
return *cell;
}
static void write_cell(uint8_t *cell, uint8_t b)
{
cell[0] = b;
cell[1] = BLACK_ON_WHITE;
}
static void write_at(uint8_t b, uint32_t row, uint32_t col)
{
uint8_t *cell = TO_ADDRESS(row, col);
write_cell(cell, b);
}
static void set_cursor(uint16_t loc)
{
outb(FB_CURSOR_INDEX_PORT, FB_HIGH_BYTE);
outb(FB_CURSOR_DATA_PORT, loc >> 8);
outb(FB_CURSOR_INDEX_PORT, FB_LOW_BYTE);
outb(FB_CURSOR_DATA_PORT, loc);
}
static void move_cursor_forward(void)
{
cursor_pos++;
set_cursor(cursor_pos);
}
static void move_cursor_back(void)
{
if (cursor_pos != 0) {
cursor_pos--;
set_cursor(cursor_pos);
}
}
static void move_cursor_down()
{
cursor_pos += FB_NUM_COLS;
set_cursor(cursor_pos);
}
static void move_cursor_start()
{
cursor_pos -= cursor_pos % FB_NUM_COLS;
set_cursor(cursor_pos);
}
static void scroll()
{
uint32_t r, c;
for (r = 1; r < FB_NUM_ROWS; ++r) {
for (c = 0; c < FB_NUM_COLS; ++c) {
write_at(read_cell(r, c), r - 1, c);
}
}
for (c = 0; c < FB_NUM_COLS; ++c) {
write_at(' ', FB_NUM_ROWS - 1, c);
}
}
void fb_put_b(uint8_t b)
{
if (b != '\n' && b != '\t' && b != FB_BACKSPACE_ASCII) {
uint8_t *cell = fb + 2 * cursor_pos;
write_cell(cell, b);
}
if (b == '\n') {
move_cursor_down();
move_cursor_start();
} else if (b == FB_BACKSPACE_ASCII) {
move_cursor_back();
uint8_t *cell = fb + 2 * cursor_pos;
write_cell(cell, ' ');
} else if (b == '\t') {
int i;
for (i = 0; i < 4; ++i) {
fb_put_b(' ');
}
} else {
move_cursor_forward();
}
if (cursor_pos >= FB_NUM_COLS * FB_NUM_ROWS) {
scroll();
fb_move_cursor(24, 0);
}
}
void fb_put_s(char const *s)
{
while (*s != '\0') {
fb_put_b(*s++);
}
}
void fb_put_ui(uint32_t i)
{
/* FIXME: please make this code more beautiful */
uint32_t n, digit;
if (i >= 1000000000) {
n = 1000000000;
} else {
n = 1;
while (n*10 <= i) {
n *= 10;
}
}
while (n > 0) {
digit = i / n;
fb_put_b('0'+digit);
i %= n;
n /= 10;
}
}
void fb_put_ui_hex(unsigned int n)
{
char *chars = "0123456789ABCDEF";
unsigned char b = 0;
int i;
fb_put_s("0x");
for (i = 7; i >= 0; --i) {
b = (n >> i*4) & 0x0F;
fb_put_b(chars[b]);
}
}
void fb_clear()
{
uint8_t i, j;
for (i = 0; i < FB_NUM_ROWS; ++i) {
for (j = 0; j < FB_NUM_COLS; ++j) {
write_at(' ', i, j);
}
}
fb_move_cursor(0, 0);
}
void fb_move_cursor(uint16_t row, uint16_t col)
{
uint16_t loc = row*FB_NUM_COLS + col;
cursor_pos = loc;
set_cursor(loc);
}
static int fb_open(vnode_t *n)
{
UNUSED_ARGUMENT(n);
return 0;
}
static int fb_lookup(vnode_t *n, char const *p, vnode_t *o)
{
UNUSED_ARGUMENT(n);
UNUSED_ARGUMENT(p);
UNUSED_ARGUMENT(o);
return -1;
}
static int fb_read(vnode_t *n, void *buf, uint32_t count)
{
UNUSED_ARGUMENT(n);
UNUSED_ARGUMENT(buf);
UNUSED_ARGUMENT(count);
/* TODO: this can actually be implemented by copying the console memory */
return -1;
}
static int fb_getattr(vnode_t *n, vattr_t *attr)
{
UNUSED_ARGUMENT(n);
UNUSED_ARGUMENT(attr);
return -1;
}
static int fb_write(vnode_t *n, char const *str, size_t len)
{
UNUSED_ARGUMENT(n);
UNUSED_ARGUMENT(len);
fb_put_s(str);
return 0;
}
int fb_init(void)
{
vnodeops.vn_open = &fb_open;
vnodeops.vn_lookup = &fb_lookup;
vnodeops.vn_read = &fb_read;
vnodeops.vn_getattr = &fb_getattr;
vnodeops.vn_write = &fb_write;
return 0;
}
int fb_get_vnode(vnode_t *out)
{
out->v_op = &vnodeops;
out->v_data = 123456;
return 0;
}