-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.c
196 lines (171 loc) · 3.84 KB
/
console.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
#include <stdint.h>
#include "console.h"
#include "address.h"
#include "framebuffer.h"
#include "serial.h"
#define FONT_PPM_WIDTH 240
#define FONT_PPM_HEIGHT 240
#define FONT_CHAR_WIDTH 15
#define FONT_CHAR_HEIGHT 15
#define FONT_PPM_HEADER_SIZE 15
#define FONT_MATRIX_WIDTH 16
#define FONT_MATRIX_HEIGHT 16
typedef struct {
uint8_t *buffer;
int width;
int height;
} Pixmap;
extern char _binary_font_ppm_start;
extern char _binary_font_ppm_end;
static Framebuffer *framebuffer;
static Pixmap fontbm;
static int console_i = 0;
static int console_j = 0;
static int console_w = 0;
static int console_h = 0;
static uint32_t bmpx(const Pixmap *bm, int x, int y)
{
union {
uint8_t b[4];
uint32_t i;
} u;
uint8_t *px = bm->buffer + (y * bm->width + x) * 3;
u.b[0] = px[0];
u.b[1] = px[1];
u.b[2] = px[2];
u.b[3] = 0xFF;
return u.i;
}
void putpx(int x, int y, uint32_t px)
{
int32_t *fbb = (int32_t *) framebuffer->base;
int32_t *pxa = fbb + y * framebuffer->pitch + x;
*pxa = px;
}
static void sputpx(int x, int y, uint32_t px)
{
if(x >= 0 && x < framebuffer->width && y >= 0 && x < framebuffer->height) {
putpx(x, y, px);
}
}
static void blt(int dx, int dy, int sx, int sy, int sw, int sh, const Pixmap *bm)
{
for(int i = 0; i < sh; ++i) {
for(int j = 0; j < sw; ++j) {
uint32_t px = bmpx(bm, sx + j, sy + i);
sputpx(dx + j, dy + i, px);
}
}
}
static void blt_char(int di, int dj, uint8_t c) {
int i = c / FONT_MATRIX_WIDTH;
int j = c % FONT_MATRIX_HEIGHT;
int ipx = i * FONT_CHAR_HEIGHT;
int jpx = j * FONT_CHAR_WIDTH;
blt(dj * FONT_CHAR_WIDTH, di * FONT_CHAR_HEIGHT,
jpx, ipx,
FONT_CHAR_WIDTH, FONT_CHAR_HEIGHT,
&fontbm);
}
static void clean_line(int i) {
for(int j = 0; j < console_w; ++j) {
blt_char(i, j, ' ');
}
}
static void print_char(uint8_t c) {
if(c == '\n') {
console_i = (console_i + 1) % console_h;
clean_line(console_i);
console_j = 0;
} else {
blt_char(console_i, console_j, c);
++console_j;
}
}
void console_init(Framebuffer *_framebuffer)
{
framebuffer = _framebuffer;
fontbm.buffer = (uint8_t *)(&_binary_font_ppm_start + FONT_PPM_HEADER_SIZE);
fontbm.width = FONT_PPM_WIDTH;
fontbm.height = FONT_PPM_HEIGHT;
console_w = framebuffer->width / FONT_CHAR_WIDTH;
console_h = framebuffer->height / FONT_CHAR_HEIGHT;
}
void console_print(const char *str)
{
const uint8_t *p = (const uint8_t *) str;
while(*p) {
print_char(*p);
++p;
}
}
static char hex(uint8_t v) {
if (v >= 0 && v <= 9) {
return '0' + v;
} else if(v >= 0xA && v <= 0xF) {
return 'A' + (v - 0xA);
} else {
return '?';
}
}
static char hexh(uint8_t b)
{
uint8_t h = b >> 4;
return hex(h);
}
static char hexl(uint8_t b)
{
uint8_t l = b & 0x0F;
return hex(l);
}
void console_print_u8(uint8_t v)
{
char buf[] = {hexh(v), hexl(v), 0};
console_print(buf);
}
void console_print_u16(uint16_t v)
{
const uint8_t *b = (const uint8_t *) &v;
for(int i = sizeof(v) - 1; i >= 0; --i) {
char buf[] = {hexh(b[i]), hexl(b[i]), 0};
console_print(buf);
}
}
void console_print_u64(uint64_t v)
{
const uint8_t *b = (const uint8_t *) &v;
for(int i = sizeof(v) - 1; i >= 0; --i) {
char buf[] = {hexh(b[i]), hexl(b[i]), 0};
console_print(buf);
}
}
void console_print_ptr(const void *ptr)
{
console_print_u64((uint64_t) ptr);
}
void console_print_byte(uint8_t b)
{
char buf[] = {'0', 'x', hexh(b), hexl(b), 0};
console_print(buf);
}
void console_print_mem(const void *mem, int n)
{
const uint8_t *b = (const uint8_t *) mem;
for(int i = 0; i < n; ++i) {
console_print(":");
console_print_byte(b[i]);
}
console_print("\n");
}
void console_print_linaddr(const void *addr)
{
const LinAddr *linaddr = (const LinAddr *) &addr;
console_print_u16(linaddr->pml4);
console_print(":");
console_print_u16(linaddr->pdpt);
console_print(":");
console_print_u16(linaddr->pd);
console_print(":");
console_print_u16(linaddr->pt);
console_print("\n");
}