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
|
#include "x86_64/vga/text.hpp"
#include "kapi/boot.hpp"
#include "kapi/cio.hpp"
#include "x86_64/boot/boot.hpp"
#include "x86_64/boot/ld.hpp"
#include "x86_64/vga/crtc.hpp"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <span>
#include <string_view>
#include <utility>
namespace teachos::vga::x86_64::text
{
namespace
{
constexpr auto BUFFER_BASE_ADDRESS = std::uintptr_t{0xb8000};
constexpr auto DEFAULT_TEXT_BUFFER_WIDTH = 80z;
constexpr auto DEFAULT_TEXT_BUFFER_HEIGHT = 25z;
constexpr auto CURSOR_ENABLED_BIT = 5U;
} // namespace
std::span<device::glyph> const device::buffer =
std::span{std::bit_cast<device::glyph *>(BUFFER_BASE_ADDRESS +
std::bit_cast<std::uintptr_t>(&teachos::boot::x86_64::TEACHOS_VMA)),
DEFAULT_TEXT_BUFFER_WIDTH * DEFAULT_TEXT_BUFFER_HEIGHT};
device::device()
: m_position{boot::bootstrap_information.vga_buffer_index}
{}
auto device::clear(attribute attribute) -> void
{
m_position = 0;
std::ranges::fill(buffer, std::pair{' ', std::bit_cast<std::byte>(attribute)});
}
auto device::cursor(bool enabled) -> void
{
auto cursor_disable_byte = std::byte{!enabled} << CURSOR_ENABLED_BIT;
crtc::address::write(crtc::registers::cursor_start);
crtc::data::write(crtc::data::read() | cursor_disable_byte);
}
[[nodiscard]] auto device::column() const noexcept -> std::ptrdiff_t
{
return m_position % DEFAULT_TEXT_BUFFER_WIDTH;
}
[[nodiscard]] auto device::line() const noexcept -> std::ptrdiff_t
{
return m_position / DEFAULT_TEXT_BUFFER_WIDTH;
}
auto device::newline() -> void
{
auto free_glyphs_in_line = DEFAULT_TEXT_BUFFER_WIDTH - column();
m_position += free_glyphs_in_line;
}
auto device::scroll(std::ptrdiff_t nof_lines) -> void
{
auto scroll_count = std::min(nof_lines, DEFAULT_TEXT_BUFFER_HEIGHT);
auto scroll_start = buffer.begin() + (scroll_count * DEFAULT_TEXT_BUFFER_WIDTH);
std::ranges::move(scroll_start, buffer.end(), buffer.begin());
auto clear_start = buffer.begin() + (DEFAULT_TEXT_BUFFER_HEIGHT - scroll_count) * DEFAULT_TEXT_BUFFER_WIDTH;
std::ranges::fill(clear_start, buffer.end(), glyph{});
m_position = (line() - scroll_count) * DEFAULT_TEXT_BUFFER_WIDTH;
}
auto device::write(cio::output_stream stream, std::string_view text) -> void
{
auto attributes = [&] -> attribute {
switch (stream)
{
case cio::output_stream::stderr:
return common_attributes::red_on_black;
default:
return common_attributes::green_on_black;
}
}();
write(text, attributes);
}
auto device::write(std::string_view code_points, attribute attribute) -> void
{
std::ranges::for_each(code_points, [&](auto code_point) -> void { write(code_point, attribute); });
}
auto device::write(char code_point, attribute attribute) -> void
{
if (m_position + 1 > DEFAULT_TEXT_BUFFER_HEIGHT * DEFAULT_TEXT_BUFFER_WIDTH)
{
scroll();
}
if (code_point == '\n')
{
if (column())
{
newline();
}
return;
}
buffer[m_position++] = std::pair{code_point, std::bit_cast<std::byte>(attribute)};
};
} // namespace teachos::vga::x86_64::text
|