Console.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "Console.h"
  2. #include "VGA.h"
  3. #include "IO.h"
  4. // Bytes output to 0xE9 end up on the Bochs console. It's very handy.
  5. #define CONSOLE_OUT_TO_E9
  6. static Console* s_the;
  7. Console& Console::the()
  8. {
  9. return *s_the;
  10. }
  11. Console::Console()
  12. {
  13. s_the = this;
  14. }
  15. Console::~Console()
  16. {
  17. }
  18. bool Console::hasDataAvailableForRead() const
  19. {
  20. return false;
  21. }
  22. ssize_t Console::read(byte* buffer, size_t bufferSize)
  23. {
  24. // FIXME: Implement reading from the console.
  25. // Maybe we could use a ring buffer for this device?
  26. // A generalized ring buffer would probably be useful.
  27. return 0;
  28. }
  29. void Console::putChar(char ch)
  30. {
  31. #ifdef CONSOLE_OUT_TO_E9
  32. IO::out8(0xe9, ch);
  33. #endif
  34. switch (ch) {
  35. case '\n':
  36. m_cursorColumn = 0;
  37. if (m_cursorRow == (m_rows - 1)) {
  38. vga_scroll_up();
  39. } else {
  40. ++m_cursorRow;
  41. }
  42. vga_set_cursor(m_cursorRow, m_cursorColumn);
  43. return;
  44. }
  45. vga_putch_at(m_cursorRow, m_cursorColumn, ch);
  46. ++m_cursorColumn;
  47. if (m_cursorColumn >= m_columns) {
  48. if (m_cursorRow == (m_rows - 1)) {
  49. vga_scroll_up();
  50. } else {
  51. ++m_cursorRow;
  52. }
  53. m_cursorColumn = 0;
  54. }
  55. vga_set_cursor(m_cursorRow, m_cursorColumn);
  56. }
  57. ssize_t Console::write(const byte* data, size_t size)
  58. {
  59. if (!size)
  60. return 0;
  61. for (size_t i = 0; i < size; ++i)
  62. putChar(data[i]);
  63. return 0;
  64. }