tty.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of Cunix. */
  3. /*
  4. Cunix 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. Cunix 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 Cunix. 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 <arch/io.h>
  24. #include <arch/sys.h>
  25. #include <lib/string.h>
  26. #include <stdarg.h>
  27. /* variables in .data will initialize by value 0 */
  28. int x, y;
  29. extern __uint64_t framebuffer;
  30. __uint16_t read_cursor(void) {
  31. __uint16_t pos = 0;
  32. outb(CURSORCTL_POSLO, CURSOR_INDEX);
  33. pos |= inb(CURSOR_DATA);
  34. outb(CURSORCTL_POSHI, CURSOR_INDEX);
  35. pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
  36. return pos;
  37. }
  38. void write_cursor(__uint16_t pos) {
  39. outb(CURSORCTL_POSLO, CURSOR_INDEX);
  40. outb((__uint8_t) (pos & 0xff), CURSOR_DATA);
  41. outb(CURSORCTL_POSHI, CURSOR_INDEX);
  42. outb((__uint8_t) ((pos >> 8) & 0xff), CURSOR_DATA);
  43. return;
  44. }
  45. void update_cursor(void) {
  46. __uint16_t pos = y * TEXT_VGA_X + x;
  47. write_cursor(pos);
  48. framebuffer = 0xb8000 + pos * 2;
  49. return;
  50. }
  51. __uint64_t printf(const char *s, ...) {
  52. /* l: character puts (no '%') */
  53. __uint64_t i, l;
  54. /* if prev character is '%', state = '%' */
  55. char state, c;
  56. /* fmt_s only use in %s, but we cannot define variables in switch-case */
  57. char *fmt_s;
  58. va_list values;
  59. int newlines;
  60. va_start(values, s);
  61. state = 0;
  62. newlines = 0;
  63. for (i = 0, l = 0; s[i]; i++) {
  64. c = s[i];
  65. /* check state */
  66. if (!state) {
  67. if (c == '%') {
  68. state = '%';
  69. continue;
  70. }
  71. /* c != '%' (it's not a formater), put it */
  72. else {
  73. /* if it's \n */
  74. if (c == '\n') {
  75. /* new line, and reset x */
  76. newlines++;
  77. }
  78. else {
  79. putc(c);
  80. l++;
  81. }
  82. continue;
  83. }
  84. }
  85. /* it's a formater */
  86. else {
  87. if (c == 'd') {
  88. l += printint((int) va_arg(values, int), 10, 1);
  89. }
  90. else if (c == 'x') {
  91. /* `x` is 64-bits, I think it's */
  92. l += printint((long) va_arg(values, long), 16, 0);
  93. }
  94. else if (c == 'p') {
  95. /* zeros at high bits should display, not like `%x` or `%d` */
  96. l += printptr((__uint64_t) va_arg(values, __uint64_t));
  97. }
  98. else if (c == 's') {
  99. fmt_s = (char *) va_arg(values, char *);
  100. if (!fmt_s)
  101. fmt_s = "(null)";
  102. /* put while end of fmt_s */
  103. while (*fmt_s) {
  104. putc(*fmt_s);
  105. fmt_s++;
  106. l++;
  107. }
  108. }
  109. else if (c == 'c') {
  110. /* `char` aligned to `int` in GCC */
  111. putc((char) va_arg(values, int));
  112. l++;
  113. }
  114. else if (c == '%') {
  115. /* put % on screen */
  116. putc(c);
  117. l++;
  118. }
  119. else {
  120. /* don't forget to put % */
  121. putc('%');
  122. putc(c);
  123. l++;
  124. }
  125. state = 0;
  126. }
  127. }
  128. /* update cursor position */
  129. x += l;
  130. if (x > TEXT_VGA_X) {
  131. x = TEXT_VGA_X - x;
  132. y++;
  133. }
  134. y += newlines;
  135. if (newlines)
  136. x = 0;
  137. if (y > TEXT_VGA_Y) {
  138. y = 0;
  139. clear_line(0);
  140. }
  141. update_cursor();
  142. va_end(values);
  143. return i;
  144. }
  145. static char digits[] = {"0123456789abcdef"};
  146. __uint64_t printint(long n, int base, int sign) {
  147. char buf[32];
  148. /* neg: x < 0 && signed interger */
  149. int i, neg, j;
  150. /* x: real number should put */
  151. __uint64_t x;
  152. if (n < 0 && sign) {
  153. neg = 1;
  154. x = -n;
  155. }
  156. else {
  157. neg = 0;
  158. x = n;
  159. }
  160. i = 0;
  161. do {
  162. /* get string of based-number */
  163. /* the first bit of 10-based digit is at buf[15] */
  164. buf[i++] = digits[x % base];
  165. } while (x /= base);
  166. if (neg)
  167. buf[i++] = '-';
  168. j = i;
  169. while (--j >= 0)
  170. putc(buf[j]);
  171. return i;
  172. }
  173. __uint64_t printptr(__uint64_t p) {
  174. int i;
  175. for (i = 15; i >= 0; i--) {
  176. char c = (p >> (i * 4)) & 0x0f;
  177. if (c > 9)
  178. c = c - 10 + 'a';
  179. else
  180. c += '0';
  181. putc(c);
  182. }
  183. return 16;
  184. }
  185. #define LINE_ADDR(l) ((char *) (__uint64_t) (0xb8000 + (l) * TEXT_VGA_X * 2))
  186. void clear_line(int line) {
  187. char *p = LINE_ADDR(line);
  188. memsetw(p, 0x0720, TEXT_VGA_X * 2);
  189. return;
  190. }