vgalog.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <kernel/system.h>
  2. #include <kernel/printf.h>
  3. #include <kernel/module.h>
  4. #include <kernel/logging.h>
  5. #include <kernel/types.h>
  6. #include "../lib/termemu.c"
  7. static unsigned short * textmemptr = (unsigned short *)0xB8000;
  8. static void placech(unsigned char c, int x, int y, int attr) {
  9. unsigned short *where;
  10. unsigned att = attr << 8;
  11. where = textmemptr + (y * 80 + x);
  12. *where = c | att;
  13. }
  14. static char vga_to_ansi[] = {
  15. 0, 4, 2, 6, 1, 5, 3, 7,
  16. 8,12,10,14, 9,13,11,15
  17. };
  18. static int current_fg = 0x07;
  19. static int current_bg = 0x10;
  20. static int cur_x = 0;
  21. static int cur_y = 0;
  22. term_state_t * ansi_state = NULL;
  23. static int write_string(char * s) {
  24. int written = 0;
  25. while (*s) {
  26. switch (*s) {
  27. case '\n':
  28. cur_x = 0;
  29. cur_y++;
  30. break;
  31. case '\b':
  32. if (cur_x > 0) cur_x--;
  33. placech(' ', cur_x, cur_y, (vga_to_ansi[current_fg] & 0xF) | (vga_to_ansi[current_bg] << 4));
  34. break;
  35. default:
  36. placech(*s, cur_x, cur_y, (vga_to_ansi[current_fg] & 0xF) | (vga_to_ansi[current_bg] << 4));
  37. cur_x++;
  38. break;
  39. }
  40. if (cur_x == 80) {
  41. cur_x = 0;
  42. cur_y++;
  43. }
  44. if (cur_y == 25) {
  45. memmove(textmemptr, (textmemptr + 80), sizeof(unsigned short) * 80 * 24);
  46. memset(textmemptr + 80 * 24, 0x00, 80 * sizeof(unsigned short));
  47. cur_y = 24;
  48. }
  49. s++;
  50. written++;
  51. }
  52. return written;
  53. }
  54. static void term_write(char c) {
  55. char foo[] = {c,0};
  56. write_string(foo);
  57. }
  58. static uint32_t vga_write(fs_node_t * node, uint64_t offset, uint32_t size, uint8_t *buffer) {
  59. /* XXX do some terminal processing like we did in the old days */
  60. size_t i = 0;
  61. while (*buffer && i < size) {
  62. ansi_put(ansi_state, *buffer);
  63. buffer++;
  64. i++;
  65. }
  66. return i;
  67. }
  68. static fs_node_t _vga_fnode = {
  69. .name = "vga_log",
  70. .write = vga_write,
  71. };
  72. static void term_scroll(int how_much) {
  73. for (int i = 0; i < how_much; ++i) {
  74. memmove(textmemptr, (textmemptr + 80), sizeof(unsigned short) * 80 * 24);
  75. memset(textmemptr + 80 * 24, 0x00, 80 * sizeof(unsigned short));
  76. }
  77. }
  78. static void term_set_cell(int x, int y, uint32_t c) {
  79. placech(c, x, y, (vga_to_ansi[current_fg] & 0xF) | (vga_to_ansi[current_bg] << 4));
  80. }
  81. static void term_set_csr(int x, int y) {
  82. cur_x = x;
  83. cur_y = y;
  84. }
  85. static int term_get_csr_x() {
  86. return cur_x;
  87. }
  88. static int term_get_csr_y() {
  89. return cur_y;
  90. }
  91. static void term_set_csr_show(int on) {
  92. return;
  93. }
  94. static void term_set_colors(uint32_t fg, uint32_t bg) {
  95. current_fg = fg;
  96. current_bg = bg;
  97. }
  98. static void term_redraw_cursor() {
  99. return;
  100. }
  101. static void input_buffer_stuff(char * str) {
  102. return;
  103. }
  104. static void set_title(char * c) {
  105. /* Do nothing */
  106. }
  107. static void term_clear(int i) {
  108. memset(textmemptr, 0x00, sizeof(unsigned short) * 80 * 25);
  109. }
  110. int unsupported_int(void) { return 0; }
  111. void unsupported(int x, int y, char * data) { }
  112. term_callbacks_t term_callbacks = {
  113. term_write,
  114. term_set_colors,
  115. term_set_csr,
  116. term_get_csr_x,
  117. term_get_csr_y,
  118. term_set_cell,
  119. term_clear,
  120. term_scroll,
  121. term_redraw_cursor,
  122. input_buffer_stuff,
  123. set_title,
  124. unsupported,
  125. unsupported_int,
  126. unsupported_int,
  127. term_set_csr_show,
  128. NULL,
  129. };
  130. static int vgadbg_init(void) {
  131. memset(textmemptr, 0x00, sizeof(unsigned short) * 80 * 25);
  132. ansi_state = ansi_init(ansi_state, 80, 25, &term_callbacks);
  133. debug_file = &_vga_fnode;
  134. debug_level = 1;
  135. write_string("VGA Debug Logging is enabled.\n");
  136. return 0;
  137. }
  138. static int vgadbg_fini(void) {
  139. return 0;
  140. }
  141. MODULE_DEF(vgalog, vgadbg_init, vgadbg_fini);