/
display.cpp
229 lines (207 loc) · 5.9 KB
/
display.cpp
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
// PhoeniX OS Kernel library display functions
// Copyright © 2017 Yury Popov a.k.a. PhoeniX
#include "kernlib.hpp"
#include "efi.hpp"
#include "pagetable.hpp"
#include "font-8x16.hpp"
class ConsoleDisplay: public Display {
private:
static char *const base;
static char *const top;
static const size_t size;
char *display;
void putc(const char c) {
if (c == 0) return;
size_t pos = display - base;
if (c == '\n') {
display += 160 - (pos % 160);
} else if (c == '\t') {
do {
*(display++) = ' ';
*(display++) = 0x0F;
pos += 2;
} while (pos % 8 != 0);
} else {
*(display++) = c;
*(display++) = 0x0F;
}
if (display >= top) {
Memory::copy(base, base + 160, size - 160);
display = top - 160;
Memory::fill(display, 0, 160);
}
}
public:
ConsoleDisplay() {
if (EFI::getSystemTable() != 0) return;
display = base;
clean();
}
void write(const char *str) {
uint64_t t = EnterCritical();
mutex.lock();
while (*str != 0) putc(*(str++));
mutex.release();
LeaveCritical(t);
}
void clean() {
mutex.lock();
Memory::fill(base, 0, size);
mutex.release();
}
};
enum PixelFormat {
PixelFormatRGBX,
PixelFormatBGRX
};
class FramebufferDisplay: public Display {
private:
void *framebuffer;
size_t width, height;
PixelFormat pixelFormat;
size_t offset;
size_t pixelBytes() {
switch (pixelFormat) {
case PixelFormatRGBX: case PixelFormatBGRX: return 4;
}
return 0;
}
size_t bufferSize() { return width * height * pixelBytes(); }
size_t charWidth() { return width / 8; }
size_t charHeight() { return height / 16; }
void putc(const char c) {
if (c == 0) return;
if (c == '\n') {
offset += charWidth() - (offset % charWidth());
} else if (c == '\t') {
offset += 8 - ((offset % charWidth()) % 8);
} else {
const uint8_t *fontsym = fb_font_8x16 + (size_t(c) * 16);
size_t offsetX = offset % charWidth();
size_t offsetY = offset / charWidth();
size_t pb = pixelBytes();
uint8_t *fbptr = static_cast<uint8_t*>(framebuffer)
+ offsetY * 16 * width * pb
+ offsetX * 8 * pb;
for (size_t y = 0; y < 16; y++) {
uint8_t *line = fbptr + y * width * pb;
uint8_t fontline = fontsym[y];
for (size_t x = 0; x < 8; x++) {
if (fontline & (1 << (8 - x))) {
for (size_t px = 0; px < pb; px++)
line[x * pb + px] = 0xFF;
}
}
}
offset++;
}
if (offset >= charHeight() * charWidth()) {
char *fbptr = static_cast<char*>(framebuffer);
size_t pb = pixelBytes();
Memory::copy(fbptr,
fbptr + width * 16 * pb,
bufferSize() - width * 16 * pb);
Memory::zero(fbptr + width * 16 * (charHeight() - 1) * pb,
width * 16 * pb);
offset -= charWidth();
}
}
public:
FramebufferDisplay(void *framebuffer, size_t width, size_t height,
PixelFormat pixelFormat):
framebuffer(framebuffer), width(width), height(height),
pixelFormat(pixelFormat), offset(0) {
for (uintptr_t ptr = uintptr_t(framebuffer);
ptr < uintptr_t(framebuffer) + bufferSize() + 0xFFF;
ptr += 0x1000) {
Pagetable::map(reinterpret_cast<void*>(ptr));
}
clean();
}
void write(const char *str) {
uint64_t t = EnterCritical();
mutex.lock();
while (*str != 0) putc(*(str++));
mutex.release();
LeaveCritical(t);
}
void clean() {
mutex.lock();
Memory::zero(framebuffer, bufferSize());
mutex.release();
}
};
class SerialDisplay: public Display {
private:
static const uint16_t port = 0x3F8;
static const uint16_t baud = 9600;
static const uint16_t divisor = 115200 / baud;
public:
SerialDisplay() {
// Disable interrupts
outportb(port + 1, 0);
// Enable DLAB
outportb(port + 3, 0x80);
// Set divisor
outportb(port + 0, divisor & 0xFF);
outportb(port + 1, (divisor >> 16) & 0xFF);
// Set port mode (8N1), disable DLAB
outportb(port + 3, 0x03);
}
void write(const char *str) {
uint64_t t = EnterCritical();
mutex.lock();
char c;
while ((c = *str++) != 0) outportb(port, c);
mutex.release();
LeaveCritical(t);
}
void clean() {}
};
char *const ConsoleDisplay::base = reinterpret_cast<char*>(0xB8000);
char *const ConsoleDisplay::top = reinterpret_cast<char*>(0xB8FA0);
const size_t ConsoleDisplay::size = ConsoleDisplay::top - ConsoleDisplay::base;
static Display *getSerialDisplay();
static SerialDisplay serialConsole;
Display *Display::instance = getSerialDisplay();
Mutex Display::instanceMutex;
static Display *getSerialDisplay() { return &serialConsole; }
void Display::setup() {
if (instance != &serialConsole) return;
const EFI_SYSTEM_TABLE *ST = EFI::getSystemTable();
if (ST) { // EFI Framebuffer
EFI_GRAPHICS_OUTPUT *graphics_output = 0;
ST->BootServices->LocateProtocol(
&EFI_GRAPHICS_OUTPUT_PROTOCOL, 0,
reinterpret_cast<void**>(&graphics_output));
PixelFormat pixelFormat;
switch (graphics_output->Mode->Info->PixelFormat) {
case EFI_GRAPHICS_PIXEL_FORMAT_RGBX_8BPP:
pixelFormat = PixelFormatRGBX;
break;
case EFI_GRAPHICS_PIXEL_FORMAT_BGRX_8BPP:
pixelFormat = PixelFormatBGRX;
break;
default:
return;
}
instance = new FramebufferDisplay(
graphics_output->Mode->FrameBufferBase,
graphics_output->Mode->Info->HorizontalResolution,
graphics_output->Mode->Info->VerticalResolution,
pixelFormat);
return;
}
// Fallback
instance = new ConsoleDisplay();
}
Display *Display::getInstance() {
if (instance) return instance;
uint64_t t = EnterCritical();
instanceMutex.lock();
if (!instance) setup();
instanceMutex.release();
LeaveCritical(t);
return instance;
}
Display::~Display() {}