aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/src')
-rw-r--r--arch/x86_64/src/vga/text.cpp50
1 files changed, 34 insertions, 16 deletions
diff --git a/arch/x86_64/src/vga/text.cpp b/arch/x86_64/src/vga/text.cpp
index c9eee71..d4548a2 100644
--- a/arch/x86_64/src/vga/text.cpp
+++ b/arch/x86_64/src/vga/text.cpp
@@ -19,8 +19,8 @@ namespace teachos::vga::x86_64::text
namespace
{
constexpr auto BUFFER_BASE_ADDRESS = std::uintptr_t{0xb8000};
- constexpr auto DEFAULT_TEXT_BUFFER_WIDTH = 80U;
- constexpr auto DEFAULT_TEXT_BUFFER_HEIGHT = 25U;
+ constexpr auto DEFAULT_TEXT_BUFFER_WIDTH = 80z;
+ constexpr auto DEFAULT_TEXT_BUFFER_HEIGHT = 25z;
constexpr auto CURSOR_ENABLED_BIT = 5U;
} // namespace
@@ -47,22 +47,33 @@ namespace teachos::vga::x86_64::text
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 current_line = m_position / DEFAULT_TEXT_BUFFER_WIDTH;
- auto next_line = current_line + 1;
+ auto free_glyphs_in_line = DEFAULT_TEXT_BUFFER_WIDTH - column();
+ m_position += free_glyphs_in_line;
+ }
- if (std::cmp_greater_equal(next_line, DEFAULT_TEXT_BUFFER_HEIGHT))
- {
- auto begin = buffer.begin() + DEFAULT_TEXT_BUFFER_WIDTH;
- auto end = buffer.begin() + DEFAULT_TEXT_BUFFER_WIDTH * DEFAULT_TEXT_BUFFER_HEIGHT;
- std::ranges::move(begin, end, buffer.begin());
- m_position = current_line * DEFAULT_TEXT_BUFFER_WIDTH;
- }
- else
- {
- m_position = next_line * DEFAULT_TEXT_BUFFER_WIDTH;
- }
+ 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(std::string_view code_points, attribute attribute) -> void
@@ -72,13 +83,20 @@ namespace teachos::vga::x86_64::text
auto device::write(char code_point, attribute attribute) -> void
{
+ if (m_position + 1 > DEFAULT_TEXT_BUFFER_HEIGHT * DEFAULT_TEXT_BUFFER_WIDTH)
+ {
+ scroll();
+ }
buffer[m_position++] = std::pair{code_point, std::bit_cast<std::byte>(attribute)};
};
auto device::writeln(std::string_view code_points, attribute attribute) -> void
{
std::ranges::for_each(code_points, [&](auto code_point) -> void { write(code_point, attribute); });
- newline();
+ if (column())
+ {
+ newline();
+ }
}
} // namespace teachos::vga::x86_64::text