aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/src/vga/text/device.cpp
blob: 2da9e0646cb97d3f9467f8926611c1d7bf3e4c35 (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
#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