tty.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. XNIX is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <kernel/init.h>
  20. #include <kernel/types.h>
  21. #include <kernel/errno.h>
  22. #include <modules/tty.h>
  23. #include <kernel/vsprintf.h>
  24. #include <kernel/schk.h>
  25. #include <lib/string.h>
  26. #include <arch/io.h>
  27. #include <stdarg.h>
  28. static char buffer[256];
  29. static int syslog_index = 0;
  30. static __uint16_t *fb = FB_ADDR;
  31. static __uint8_t x, y;
  32. errno_t tty_init(void) {
  33. x = bootarg->x, y = bootarg->y;
  34. /* setup the frame buffer address fb */
  35. /* note that `fb` is a 16-bits pointer, so we don't need to '<< 1' */
  36. fb += y * VGA_X + x;
  37. /* well, this is the end */
  38. return 0;
  39. }
  40. void syslog(const char *s) {
  41. /* we can't use schk in this function, */
  42. /* because this will make a die-loop (schk_init calls this function) */
  43. int i;
  44. for (i = 0; s[i]; i++) {
  45. if (syslog_index == 32768)
  46. syslog_index = 0;
  47. *(syslog_index + SYSLOG_BUFFER) = s[i];
  48. syslog_index++;
  49. }
  50. return;
  51. }
  52. int printk(__uint8_t color, char *s, ...) {
  53. schk_init();
  54. va_list args;
  55. int i;
  56. /* I think 256 is enough in kernel */
  57. /* too long, return it. */
  58. va_start(args, s);
  59. /* it maybe overflow, but that's in .data section and we did check. */
  60. i = vsprintf(buffer, s, args);
  61. print(color, buffer);
  62. memset(buffer, 0, 256);
  63. va_end(args);
  64. schk_leave();
  65. return i;
  66. }
  67. void print(__uint8_t color, char *s) {
  68. while (*s)
  69. putc(*s++, color);
  70. return;
  71. }
  72. void putc(char c, __uint8_t color) {
  73. if (c == '\n') {
  74. /* we do it like \r\n */
  75. x = 0;
  76. if (++y == VGA_Y) {
  77. roll_screen();
  78. y = 24;
  79. }
  80. update_cursor();
  81. return;
  82. }
  83. else if (c == '\r') {
  84. x = 0;
  85. update_cursor();
  86. return;
  87. }
  88. else if (c == '\t') {
  89. __uint64_t ax = (x + TAB_SIZE - 1) & ~(TAB_SIZE - 1);
  90. if (ax == x)
  91. x += TAB_SIZE;
  92. else
  93. x = ax;
  94. update_cursor();
  95. return;
  96. }
  97. __asm__ __volatile__ (
  98. /* rdi is the frame buffer, and rax is the */
  99. "cld\n\t"
  100. "movb %%cl, %%ah\n\t"
  101. "stosw\n\t"
  102. : "+D" (fb)
  103. : "a" (c), "c" (color)
  104. );
  105. if (++x == VGA_X) {
  106. x = 0;
  107. if (++y == VGA_Y) {
  108. roll_screen();
  109. y = 24;
  110. }
  111. }
  112. update_cursor();
  113. return;
  114. }
  115. __uint16_t read_cursor(void) {
  116. __uint16_t pos = 0;
  117. outb(CMD_POS_LO, CURSOR_INDEX);
  118. pos |= inb(CURSOR_DATA);
  119. outb(CMD_POS_HI, CURSOR_INDEX);
  120. pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
  121. return pos;
  122. }
  123. void update_cursor(void) {
  124. __uint16_t pos = y * bootarg->vga_x + x;
  125. fb = FB_ADDR + pos;
  126. outb(CMD_POS_LO, CURSOR_INDEX);
  127. outb(pos & 0xff, CURSOR_DATA);
  128. outb(CMD_POS_HI, CURSOR_INDEX);
  129. outb((pos >> 8) & 0xff, CURSOR_DATA);
  130. return;
  131. }
  132. void roll_screen(void) {
  133. /* roll down the screen */
  134. __asm__ __volatile__ (
  135. "cld\n\t"
  136. "movq $960, %%rcx\n\t"
  137. "movq $0xb80a0, %%rsi\n\t"
  138. "movq $0xb8000, %%rdi\n\t"
  139. "rep\n\t"
  140. "movsq\n\t"
  141. "movq $0xb8f00, %%rdi\n\t"
  142. "movq $0x0720, %%rax\n\t"
  143. "movq $80, %%rcx\n\t"
  144. "rep\n\t"
  145. "stosw\n\t"
  146. :::
  147. );
  148. return;
  149. }