VGATextModeConsole.cpp 4.3 KB

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