forked from includeos/IncludeOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvga.cpp
More file actions
123 lines (101 loc) · 3.27 KB
/
vga.cpp
File metadata and controls
123 lines (101 loc) · 3.27 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
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <kernel/vga.hpp>
#include <cstring>
#include <x86intrin.h>
#include <memstream>
static inline uint16_t
make_vgaentry(const char c, const uint8_t color) noexcept {
uint16_t c16 = c;
uint16_t color16 = color;
return c16 | color16 << 8;
}
const uint16_t ConsoleVGA::DEFAULT_ENTRY =
make_vgaentry(32, make_color(COLOR_LIGHT_GREY, COLOR_BLACK));
ConsoleVGA::ConsoleVGA() noexcept:
row{0}, column{0}
{
this->color = make_color(COLOR_WHITE, COLOR_BLACK);
this->buffer = reinterpret_cast<uint16_t*>(0xB8000);
clear();
}
uint16_t ConsoleVGA::get(uint8_t x, uint8_t y)
{
const size_t index = y * VGA_WIDTH + x;
return this->buffer[index];
}
void ConsoleVGA::put(const char c, uint8_t color, uint8_t x, uint8_t y) noexcept {
putent(make_vgaentry(c, color), x, y);
}
void ConsoleVGA::put(const char c, uint8_t x, uint8_t y) noexcept {
putent(make_vgaentry(c, this->color), x, y);
}
void ConsoleVGA::set_cursor(uint8_t x, uint8_t y) noexcept {
this->column = x;
this->row = y;
}
inline void ConsoleVGA::putent(uint16_t entry, uint8_t x, uint8_t y) noexcept {
const size_t index = y * VGA_WIDTH + x;
this->buffer[index] = entry;
}
void ConsoleVGA::increment(const int step) noexcept {
this->column += step;
if (this->column >= VGA_WIDTH) {
newline();
}
}
void ConsoleVGA::newline() noexcept {
// Reset back to left side
this->column = 0;
// And finally move everything up one line, if necessary
if (++this->row == VGA_HEIGHT) {
this->row--;
unsigned total {VGA_WIDTH * (VGA_HEIGHT - 1)};
__m128i scan;
// Copy rows upwards
for (size_t n {0}; n < total; n += 8) {
scan = _mm_load_si128(reinterpret_cast<__m128i*>(&buffer[n + VGA_WIDTH]));
_mm_store_si128(reinterpret_cast<__m128i*>(&buffer[n]), scan);
}
// Clear out the last row
scan = _mm_set1_epi16(DEFAULT_ENTRY);
for (size_t n {0}; n < VGA_WIDTH; n += 8) {
_mm_store_si128(reinterpret_cast<__m128i*>(&buffer[total + n]), scan);
}
}
}
void ConsoleVGA::clear() noexcept {
this->row = 0;
this->column = 0;
streamset16(buffer, DEFAULT_ENTRY, VGA_WIDTH * VGA_HEIGHT * 2);
}
void ConsoleVGA::write(const char c) noexcept {
static const char CARRIAGE_RETURN = '\r';
static const char LINE_FEED = '\n';
if (c == LINE_FEED) {
newline();
} else if (c == CARRIAGE_RETURN) {
// skip
} else {
put(c, this->color, this->column, this->row);
increment(1);
}
}
void ConsoleVGA::write(const char* data, const size_t len) noexcept {
for (size_t i = 0; i < len; ++i)
write(data[i]);
}