aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/video/vga/text.cpp
blob: b070a0a43f1188be84fe5590f4666f359160aa9b (plain)
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
#include "arch/video/vga/text.hpp"

#include "arch/video/vga/io.hpp"
#include "memory/asm_pointer.hpp"

#include <algorithm>
#include <string_view>
#include <utility>

extern "C" std::pair<char, teachos::arch::video::vga::text::attribute> * vga_buffer_pointer;

namespace teachos::arch::video::vga::text
{
  namespace
  {
    auto constexpr DEFAULT_TEXT_BUFFER_WIDTH = 80U;
    auto constexpr DEFAULT_TEXT_BUFFER_HEIGHT = 25U;

    auto constinit text_buffer = teachos::memory::asm_pointer{vga_buffer_pointer};
  }  // namespace

  auto clear(attribute attribute) -> void
  {
    *text_buffer = reinterpret_cast<decltype(text_buffer)::pointer>(DEFAULT_VGA_TEXT_BUFFER_ADDRESS);
    std::ranges::fill_n(*text_buffer, 2000, std::pair{' ', attribute});
  }

  auto cursor(bool enabled) -> void
  {
    auto cursor_disable_byte = std::byte{!enabled} << 5;

    crtc::address_port::write(crtc::registers::cursor_start);
    crtc::data_port::write(vga::crtc::data_port::read() | cursor_disable_byte);
  }

  auto newline() -> void
  {
    auto base = reinterpret_cast<decltype(text_buffer)::pointer>(DEFAULT_VGA_TEXT_BUFFER_ADDRESS);
    auto & raw_buffer = *text_buffer;
    auto current_line = (raw_buffer - base) / DEFAULT_TEXT_BUFFER_WIDTH;
    auto next_line = current_line + 1;

    if (next_line >= DEFAULT_TEXT_BUFFER_HEIGHT)
    {
      auto begin = base + DEFAULT_TEXT_BUFFER_WIDTH;
      auto end = base + DEFAULT_TEXT_BUFFER_WIDTH * DEFAULT_TEXT_BUFFER_HEIGHT;
      std::ranges::move(begin, end, base);
      raw_buffer = base + current_line * DEFAULT_TEXT_BUFFER_WIDTH;
    }
    else
    {
      raw_buffer = base + next_line * DEFAULT_TEXT_BUFFER_WIDTH;
    }
  }

  auto write_char(char code_point, attribute attribute) -> void
  {
    auto & p = *text_buffer;
    (*p++) = std::pair{code_point, attribute};
  };

  auto write(std::string_view code_points, attribute attribute) -> void
  {
    std::ranges::for_each(code_points, [&](auto code_point) { write_char(code_point, attribute); });
  }
}  // namespace teachos::arch::video::vga::text