aboutsummaryrefslogtreecommitdiff
path: root/arch
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-01-13 14:16:50 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-01-13 14:16:50 +0100
commit5a41c39169e45de71263c2419309244665cfa890 (patch)
tree2f1a09d36d03bff98facb88f90ffe2bb9d39209e /arch
parent67f4809a04999be393e5b10e924bcb0f685c721c (diff)
downloadteachos-5a41c39169e45de71263c2419309244665cfa890.tar.xz
teachos-5a41c39169e45de71263c2419309244665cfa890.zip
x86_64/vga: extract buffer type
Diffstat (limited to 'arch')
-rw-r--r--arch/x86_64/CMakeLists.txt3
-rw-r--r--arch/x86_64/include/x86_64/boot/boot.hpp2
-rw-r--r--arch/x86_64/include/x86_64/vga/text/buffer.hpp101
-rw-r--r--arch/x86_64/include/x86_64/vga/text/device.hpp69
-rw-r--r--arch/x86_64/src/vga/text.cpp148
-rw-r--r--arch/x86_64/src/vga/text/buffer.cpp101
-rw-r--r--arch/x86_64/src/vga/text/device.cpp60
7 files changed, 268 insertions, 216 deletions
diff --git a/arch/x86_64/CMakeLists.txt b/arch/x86_64/CMakeLists.txt
index 54f04cb..2505bb6 100644
--- a/arch/x86_64/CMakeLists.txt
+++ b/arch/x86_64/CMakeLists.txt
@@ -32,7 +32,8 @@ target_sources("x86_64" PRIVATE
"src/memory/scoped_mapping.cpp"
# VGA text mode
- "src/vga/text.cpp"
+ "src/vga/text/buffer.cpp"
+ "src/vga/text/device.cpp"
)
file(GLOB_RECURSE ARCH_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "include/**.hpp")
diff --git a/arch/x86_64/include/x86_64/boot/boot.hpp b/arch/x86_64/include/x86_64/boot/boot.hpp
index 0db2396..2c44659 100644
--- a/arch/x86_64/include/x86_64/boot/boot.hpp
+++ b/arch/x86_64/include/x86_64/boot/boot.hpp
@@ -60,7 +60,7 @@ namespace teachos::boot
multiboot2::information_view const * mbi;
//! The index of the next character to be written in the VGA text buffer after handoff.
- std::ptrdiff_t vga_buffer_index;
+ std::size_t vga_buffer_index;
};
} // namespace teachos::boot
diff --git a/arch/x86_64/include/x86_64/vga/text/buffer.hpp b/arch/x86_64/include/x86_64/vga/text/buffer.hpp
new file mode 100644
index 0000000..9e93af3
--- /dev/null
+++ b/arch/x86_64/include/x86_64/vga/text/buffer.hpp
@@ -0,0 +1,101 @@
+#ifndef TEACHOS_X86_64_VGA_TEXT_BUFFER_HPP
+#define TEACHOS_X86_64_VGA_TEXT_BUFFER_HPP
+
+// IWYU pragma: private, include "x86_64/vga/text.hpp"
+
+#include "x86_64/vga/text/attribute.hpp"
+
+#include <cstddef>
+#include <span>
+#include <string_view>
+#include <utility>
+
+namespace teachos::vga::x86_64::text
+{
+ //! A VGA text buffer.
+ //!
+ //! VGA text mode presents a linear buffer of so-called cells. Each cell consists of a single code point and a
+ //! rendering attribute. The codepoint determines the character being rendered in a specific cell, while the attribute
+ //! determines the visual style of that cell.
+ //!
+ //! @see text::attribute
+ struct buffer
+ {
+ using cell = std::pair<char, std::byte>;
+
+ //! Create a new buffer.
+ //!
+ //! @param width The width of the buffer
+ //! @param height The height of the buffer
+ //! @param start A pointer to the first byte of the buffer.
+ //! @param position The starting position for the first write to the buffer
+ buffer(std::size_t width, std::size_t height, cell * start, std::size_t position = 0);
+
+ //! Clear the buffer.
+ //!
+ //! Clearing the buffer ensures it is filled with zeroes, effectively erasing all data and resetting the output
+ //! position to the start of the buffer.
+ auto clear() -> void;
+
+ //! Write a string of formatted code points to the buffer.
+ //!
+ //! @param code_points A string of (8-bit) code points to write to the buffer.
+ //! @param attribute The formatting to apply to the written sequence of code points.
+ auto write(std::string_view code_points, attribute attribute) -> void;
+
+ //! Write a single, formatted code point to the buffer.
+ //!
+ //! @param code_point A single (8-bit) code point
+ //! @param attribute The formatting to apply to the code point.
+ auto write(char code_point, attribute attribute) -> void;
+
+ //! Move the output position to a new line and scroll the buffer if necessary.
+ auto newline() -> void;
+
+ //! Scroll the buffer contents.
+ //!
+ //! @param nof_lines The number of lines to scroll up.
+ auto scroll(std::size_t nof_lines = 1) -> void;
+
+ private:
+ //! Get column number of the current cell.
+ [[nodiscard]] auto column() const noexcept -> std::ptrdiff_t;
+
+ //! Get the line number of the current cell.
+ [[nodiscard]] auto line() const noexcept -> std::ptrdiff_t;
+
+ //! Process the semantics of special code points, for example newlines and carriage returns.
+ //!
+ //! @param code_point The code point to process.
+ //! @param attribute The attribute to use when writing to the text buffer.
+ //! @return @p true iff. the code point was handled, @p false otherwise.
+ auto handle_special_code_point(char code_point, attribute attribute) -> bool;
+
+ //! Perform the actual output to the buffer.
+ //!
+ //! @param code_points The code points to output..
+ //! @param attribute The attribute to use when writing to the text buffer.
+ auto do_write(std::string_view code_points, attribute attribute) -> void;
+
+ //! Perform the actual output to the buffer.
+ //!
+ //! @param code_point The code point to output.
+ //! @param attribute The attribute to use when writing to the text buffer.
+ auto do_write(char code_point, attribute attribute) -> void;
+
+ //! The width, in cells, of the buffer.
+ std::size_t m_width{};
+
+ //! The height, in cells, of the buffer.
+ std::size_t m_height{};
+
+ //! The text mode data buffer.
+ std::span<cell> m_buffer;
+
+ //! The position of the next cell to be written to.
+ std::size_t m_position{};
+ };
+
+} // namespace teachos::vga::x86_64::text
+
+#endif \ No newline at end of file
diff --git a/arch/x86_64/include/x86_64/vga/text/device.hpp b/arch/x86_64/include/x86_64/vga/text/device.hpp
index 6cb7a45..f5c69a1 100644
--- a/arch/x86_64/include/x86_64/vga/text/device.hpp
+++ b/arch/x86_64/include/x86_64/vga/text/device.hpp
@@ -5,12 +5,9 @@
#include "kapi/cio.hpp"
-#include "x86_64/vga/text/attribute.hpp"
+#include "x86_64/vga/text/buffer.hpp"
-#include <cstddef>
-#include <span>
#include <string_view>
-#include <utility>
namespace teachos::vga::x86_64::text
{
@@ -25,7 +22,7 @@ namespace teachos::vga::x86_64::text
{
device();
- //! Clear the buffer.
+ //! Clear the screen.
//!
//! Clearing the screen ensures the text mode buffer is filled with zeroes, effectively erasing all displayed data
//! and resetting the output position to the start of the buffer.
@@ -40,67 +37,7 @@ namespace teachos::vga::x86_64::text
auto write(cio::output_stream stream, std::string_view text) -> void override;
private:
- using cell = std::pair<char, std::byte>;
-
- //! Get column number of the current cell.
- [[nodiscard]] auto column() const noexcept -> std::ptrdiff_t;
-
- //! Get the line number of the current cell.
- [[nodiscard]] auto line() const noexcept -> std::ptrdiff_t;
-
- //! Process the semantics of special code points, for example newlines and carriage returns.
- //!
- //! @param code_point The code point to process.
- //! @param attribute The attribute to use when writing to the text buffer.
- //! @return @p true iff. the code point was handled, @p false otherwise.
- auto handle_special_code_point(char code_point, attribute attribute) -> bool;
-
- //! Perform the actual output to the buffer.
- //!
- //! @param code_points The code points to output..
- //! @param attribute The attribute to use when writing to the text buffer.
- auto put(std::string_view code_points, attribute attribute) -> void;
-
- //! Perform the actual output to the buffer.
- //!
- //! @param code_point The code point to output.
- //! @param attribute The attribute to use when writing to the text buffer.
- auto put(char code_point, attribute attribute) -> void;
-
- //! Move the cursor to a new line and scroll the buffer if necessary.
- auto newline() -> void;
-
- //! Scroll the screen.
- //!
- //! @param nof_lines The number of lines to scroll up.
- auto scroll(std::size_t nof_lines = 1) -> void;
-
- //! Write a string of code points to the text buffer.
- //!
- //! @note This function also updates the text mode buffer pointer.
- //!
- //! @param code_points A string of (8-bit) code points to write to the VGA text mode buffer.
- //! @param attribute The attribute to apply to the written sequence of code points.
- //! @see vga::text::attribute
- auto write(std::string_view code_points, attribute attribute) -> void;
-
- //! Write a single code point to the VGA text buffer.
- //!
- //! @param code_point A single (8-bit) code point
- //! @param attribute The #attribute to apply to the code point.
- auto write(char code_point, attribute attribute) -> void;
-
- //! The width, in cells, of the buffer.
- std::size_t m_width{};
-
- //! The height, in cells, of the buffer.
- std::size_t m_height{};
-
- //! The text mode data buffer.
- std::span<cell> m_buffer;
-
- //! The position of the next cell to be written to.
- std::ptrdiff_t m_position{};
+ buffer m_buffer;
};
} // namespace teachos::vga::x86_64::text
diff --git a/arch/x86_64/src/vga/text.cpp b/arch/x86_64/src/vga/text.cpp
deleted file mode 100644
index 1ee69f6..0000000
--- a/arch/x86_64/src/vga/text.cpp
+++ /dev/null
@@ -1,148 +0,0 @@
-#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 default_buffer_address = std::uintptr_t{0xb8000};
- constexpr auto default_buffer_width = 80z;
- constexpr auto default_buffer_height = 25z;
-
- constexpr auto bit_cursor_enabled = 5U;
-
- [[maybe_unused]] constexpr auto black_on_black = attribute{.foreground_color = color::black,
- .foreground_flag = foreground_flag::none,
- .background_color = color::black,
- .background_flag = background_flag::none};
- } // namespace
-
- device::device()
- : m_width{default_buffer_width}
- , m_height{default_buffer_height}
- , m_buffer{std::bit_cast<device::cell *>(default_buffer_address +
- std::bit_cast<std::uintptr_t>(&teachos::boot::x86_64::TEACHOS_VMA)),
- m_width * m_height}
- , m_position{boot::bootstrap_information.vga_buffer_index}
- {
- clear();
- }
-
- auto device::clear() -> void
- {
- m_position = 0;
- std::ranges::fill(m_buffer, std::pair{' ', std::bit_cast<std::byte>(black_on_black)});
- }
-
- auto device::cursor(bool enabled) -> void
- {
- auto cursor_disable_byte = std::byte{!enabled} << bit_cursor_enabled;
-
- 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 % m_width;
- }
-
- [[nodiscard]] auto device::line() const noexcept -> std::ptrdiff_t
- {
- return m_position / m_width;
- }
-
- auto device::handle_special_code_point(char code_point, attribute attribute) -> bool
- {
- switch (code_point)
- {
- case '\n':
- newline();
- return true;
- case '\r':
- m_position -= column();
- return true;
- case '\t':
- put(" ", attribute);
- return true;
- default:
- return false;
- }
- }
-
- auto device::put(std::string_view code_points, attribute attribute) -> void
- {
- std::ranges::for_each(code_points, [&](auto code_point) -> void { put(code_point, attribute); });
- }
-
- auto device::put(char code_point, attribute attribute) -> void
- {
- m_buffer[m_position++] = std::pair{code_point, std::bit_cast<std::byte>(attribute)};
- }
-
- auto device::newline() -> void
- {
- auto free_glyphs_in_line = m_width - column();
- m_position += free_glyphs_in_line;
- }
-
- auto device::scroll(std::size_t nof_lines) -> void
- {
- auto scroll_count = std::min(nof_lines, m_height);
-
- auto scroll_start = m_buffer.begin() + (scroll_count * m_width);
- std::ranges::move(scroll_start, m_buffer.end(), m_buffer.begin());
-
- auto clear_start = m_buffer.begin() + (m_height - scroll_count) * m_width;
- std::ranges::fill(clear_start, m_buffer.end(), cell{});
-
- m_position = (line() - scroll_count) * m_width;
- }
-
- auto device::write(cio::output_stream stream, std::string_view text) -> void
- {
- auto attributes = [&] -> attribute {
- switch (stream)
- {
- case cio::output_stream::stderr:
- return red_on_black;
- default:
- return 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 > static_cast<std::ptrdiff_t>(m_height * m_width))
- {
- scroll();
- }
-
- if (!handle_special_code_point(code_point, attribute))
- {
- put(code_point, attribute);
- }
- };
-
-} // namespace teachos::vga::x86_64::text
diff --git a/arch/x86_64/src/vga/text/buffer.cpp b/arch/x86_64/src/vga/text/buffer.cpp
new file mode 100644
index 0000000..2dcf084
--- /dev/null
+++ b/arch/x86_64/src/vga/text/buffer.cpp
@@ -0,0 +1,101 @@
+#include "x86_64/vga/text/buffer.hpp"
+
+#include "x86_64/vga/text/attribute.hpp"
+
+#include <algorithm>
+#include <bit>
+#include <cstddef>
+#include <span>
+#include <string_view>
+#include <utility>
+
+namespace teachos::vga::x86_64::text
+{
+ buffer::buffer(std::size_t width, std::size_t height, cell * start, std::size_t position)
+ : m_width{width}
+ , m_height{height}
+ , m_buffer{start, m_width * m_height}
+ , m_position{position}
+ {}
+
+ auto buffer::clear() -> void
+ {
+ m_position = 0;
+ std::ranges::fill(m_buffer, std::pair{'\0', static_cast<std::byte>(0x00)});
+ }
+
+ auto buffer::write(std::string_view code_points, attribute attribute) -> void
+ {
+ std::ranges::for_each(code_points, [&](auto code_point) -> void { write(code_point, attribute); });
+ }
+
+ auto buffer::write(char code_point, attribute attribute) -> void
+ {
+ if (m_position + 1 > m_height * m_width)
+ {
+ scroll();
+ }
+
+ if (!handle_special_code_point(code_point, attribute))
+ {
+ do_write(code_point, attribute);
+ }
+ };
+
+ auto buffer::newline() -> void
+ {
+ auto free_glyphs_in_line = m_width - column();
+ m_position += free_glyphs_in_line;
+ }
+
+ auto buffer::scroll(std::size_t nof_lines) -> void
+ {
+ auto scroll_count = std::min(nof_lines, m_height);
+
+ auto scroll_start = m_buffer.begin() + (scroll_count * m_width);
+ std::ranges::move(scroll_start, m_buffer.end(), m_buffer.begin());
+
+ auto clear_start = m_buffer.begin() + (m_height - scroll_count) * m_width;
+ std::ranges::fill(clear_start, m_buffer.end(), cell{});
+
+ m_position = (line() - scroll_count) * m_width;
+ }
+
+ auto buffer::column() const noexcept -> std::ptrdiff_t
+ {
+ return m_position % m_width;
+ }
+
+ auto buffer::line() const noexcept -> std::ptrdiff_t
+ {
+ return m_position / m_width;
+ }
+ auto buffer::handle_special_code_point(char code_point, attribute attribute) -> bool
+ {
+ switch (code_point)
+ {
+ case '\n':
+ newline();
+ return true;
+ case '\r':
+ m_position -= column();
+ return true;
+ case '\t':
+ do_write(" ", attribute);
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ auto buffer::do_write(std::string_view code_points, attribute attribute) -> void
+ {
+ std::ranges::for_each(code_points, [&](auto code_point) -> void { do_write(code_point, attribute); });
+ }
+
+ auto buffer::do_write(char code_point, attribute attribute) -> void
+ {
+ m_buffer[m_position++] = std::pair{code_point, std::bit_cast<std::byte>(attribute)};
+ }
+
+} // namespace teachos::vga::x86_64::text
diff --git a/arch/x86_64/src/vga/text/device.cpp b/arch/x86_64/src/vga/text/device.cpp
new file mode 100644
index 0000000..2da9e06
--- /dev/null
+++ b/arch/x86_64/src/vga/text/device.cpp
@@ -0,0 +1,60 @@
+#include "kapi/cio.hpp"
+
+#include "x86_64/boot/boot.hpp"
+#include "x86_64/boot/ld.hpp"
+#include "x86_64/vga/crtc.hpp"
+#include "x86_64/vga/text.hpp"
+
+#include <bit>
+#include <cstddef>
+#include <cstdint>
+#include <string_view>
+
+namespace teachos::vga::x86_64::text
+{
+ namespace
+ {
+ constexpr auto default_buffer_address = std::uintptr_t{0xb8000};
+ constexpr auto default_buffer_width = 80z;
+ constexpr auto default_buffer_height = 25z;
+
+ constexpr auto bit_cursor_enabled = 5U;
+ } // namespace
+
+ device::device()
+ : m_buffer{default_buffer_width, default_buffer_height,
+ std::bit_cast<buffer::cell *>(default_buffer_address +
+ std::bit_cast<std::uintptr_t>(&teachos::boot::x86_64::TEACHOS_VMA)),
+ boot::bootstrap_information.vga_buffer_index}
+ {
+ clear();
+ }
+
+ auto device::clear() -> void
+ {
+ m_buffer.clear();
+ }
+
+ auto device::cursor(bool enabled) -> void
+ {
+ auto cursor_disable_byte = std::byte{!enabled} << bit_cursor_enabled;
+
+ crtc::address::write(crtc::registers::cursor_start);
+ crtc::data::write(crtc::data::read() | cursor_disable_byte);
+ }
+
+ auto device::write(cio::output_stream stream, std::string_view text) -> void
+ {
+ auto attributes = [&] -> attribute {
+ switch (stream)
+ {
+ case cio::output_stream::stderr:
+ return red_on_black;
+ default:
+ return green_on_black;
+ }
+ }();
+ m_buffer.write(text, attributes);
+ }
+
+} // namespace teachos::vga::x86_64::text