VGATextModeConsole.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/Graphics/Console/VGATextModeConsole.h>
  7. #include <Kernel/Graphics/GraphicsManagement.h>
  8. #include <Kernel/Sections.h>
  9. namespace Kernel::Graphics {
  10. UNMAP_AFTER_INIT NonnullLockRefPtr<VGATextModeConsole> VGATextModeConsole::initialize()
  11. {
  12. auto vga_window_size = MUST(Memory::page_round_up(0xc0000 - 0xa0000));
  13. auto vga_window_region = MUST(MM.allocate_kernel_region(PhysicalAddress(0xa0000), vga_window_size, "VGA Display"sv, Memory::Region::Access::ReadWrite));
  14. return adopt_lock_ref(*new (nothrow) VGATextModeConsole(move(vga_window_region)));
  15. }
  16. UNMAP_AFTER_INIT VGATextModeConsole::VGATextModeConsole(NonnullOwnPtr<Memory::Region> vga_window_region)
  17. : Console(80, 25)
  18. , m_vga_window_region(move(vga_window_region))
  19. , m_current_vga_window(m_vga_window_region->vaddr().offset(0x18000).as_ptr())
  20. {
  21. for (size_t index = 0; index < height(); index++) {
  22. clear_vga_row(index);
  23. }
  24. dbgln("VGA Text mode console initialized!");
  25. }
  26. enum VGAColor : u8 {
  27. Black = 0,
  28. Blue,
  29. Green,
  30. Cyan,
  31. Red,
  32. Magenta,
  33. Brown,
  34. LightGray,
  35. DarkGray,
  36. BrightBlue,
  37. BrightGreen,
  38. BrightCyan,
  39. BrightRed,
  40. BrightMagenta,
  41. Yellow,
  42. White,
  43. };
  44. [[maybe_unused]] static inline VGAColor convert_standard_color_to_vga_color(Console::Color color)
  45. {
  46. switch (color) {
  47. case Console::Color::Black:
  48. return VGAColor::Black;
  49. case Console::Color::Red:
  50. return VGAColor::Red;
  51. case Console::Color::Brown:
  52. return VGAColor::Brown;
  53. case Console::Color::Blue:
  54. return VGAColor::Blue;
  55. case Console::Color::Magenta:
  56. return VGAColor::Magenta;
  57. case Console::Color::Green:
  58. return VGAColor::Green;
  59. case Console::Color::Cyan:
  60. return VGAColor::Cyan;
  61. case Console::Color::LightGray:
  62. return VGAColor::LightGray;
  63. case Console::Color::DarkGray:
  64. return VGAColor::DarkGray;
  65. case Console::Color::BrightRed:
  66. return VGAColor::BrightRed;
  67. case Console::Color::BrightGreen:
  68. return VGAColor::BrightGreen;
  69. case Console::Color::Yellow:
  70. return VGAColor::Yellow;
  71. case Console::Color::BrightBlue:
  72. return VGAColor::BrightBlue;
  73. case Console::Color::BrightMagenta:
  74. return VGAColor::BrightMagenta;
  75. case Console::Color::BrightCyan:
  76. return VGAColor::BrightCyan;
  77. case Console::Color::White:
  78. return VGAColor::White;
  79. default:
  80. VERIFY_NOT_REACHED();
  81. }
  82. }
  83. void VGATextModeConsole::set_cursor(size_t x, size_t y)
  84. {
  85. SpinlockLocker lock(m_vga_lock);
  86. GraphicsManagement::the().set_vga_text_mode_cursor(width(), x, y);
  87. m_x = x;
  88. m_y = y;
  89. }
  90. void VGATextModeConsole::hide_cursor()
  91. {
  92. SpinlockLocker lock(m_vga_lock);
  93. GraphicsManagement::the().disable_vga_text_mode_console_cursor();
  94. }
  95. void VGATextModeConsole::show_cursor()
  96. {
  97. set_cursor(m_x, m_y);
  98. }
  99. void VGATextModeConsole::clear(size_t x, size_t y, size_t length)
  100. {
  101. SpinlockLocker lock(m_vga_lock);
  102. auto* buf = (u16*)m_current_vga_window.offset((x * 2) + (y * width() * 2)).as_ptr();
  103. for (size_t index = 0; index < length; index++) {
  104. buf[index] = 0x0720;
  105. }
  106. }
  107. void VGATextModeConsole::write(size_t x, size_t y, char ch, bool critical)
  108. {
  109. write(x, y, ch, m_default_background_color, m_default_foreground_color, critical);
  110. }
  111. void VGATextModeConsole::write(size_t x, size_t y, char ch, Color background, Color foreground, bool critical)
  112. {
  113. SpinlockLocker lock(m_vga_lock);
  114. // If we are in critical printing mode, we need to handle new lines here
  115. // because there's no other responsible object to do that in the print call path
  116. if (critical && (ch == '\r' || ch == '\n')) {
  117. // Disable hardware VGA cursor
  118. GraphicsManagement::the().disable_vga_text_mode_console_cursor();
  119. m_x = 0;
  120. m_y += 1;
  121. if (m_y >= max_row())
  122. m_y = 0;
  123. return;
  124. }
  125. auto* buf = (u16*)m_current_vga_window.offset((x * 2) + (y * width() * 2)).as_ptr();
  126. *buf = foreground << 8 | background << 12 | ch;
  127. m_x = x + 1;
  128. if (m_x >= max_column()) {
  129. m_x = 0;
  130. m_y = y + 1;
  131. if (m_y >= max_row())
  132. m_y = 0;
  133. }
  134. }
  135. void VGATextModeConsole::clear_vga_row(u16 row)
  136. {
  137. clear(0, row, width());
  138. }
  139. void VGATextModeConsole::write(char ch, bool critical)
  140. {
  141. write(m_x, m_y, ch, critical);
  142. }
  143. }