tty.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 <kernel/args.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(char color, 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. int argno;
  59. int newlines;
  60. state = 0;
  61. newlines = 0;
  62. /* argno = 3 because first argument is 's' */
  63. for (i = 0, l = 0, argno = 3; 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, color);
  80. l++;
  81. }
  82. continue;
  83. }
  84. }
  85. /* it's a formater */
  86. else {
  87. if (c == 'd') {
  88. l += printint((int) get_arg(int, argno), 10, 1, color);
  89. argno++;
  90. }
  91. else if (c == 'x') {
  92. /* `x` is 64-bits, I think it's */
  93. l += printint((long) get_arg(long, argno), 16, 0, color);
  94. argno++;
  95. }
  96. else if (c == 'p') {
  97. /* zeros at high bits should display, not like `%x` or `%d` */
  98. l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16, color);
  99. argno++;
  100. }
  101. /* put a byte */
  102. else if (c == 'b') {
  103. l += printptr((__uint64_t) get_arg(__uint32_t, argno), 2, color);
  104. argno++;
  105. }
  106. /* put a word */
  107. else if (c == 'w') {
  108. l += printptr((__uint64_t) get_arg(__uint32_t, argno), 4, color);
  109. argno++;
  110. }
  111. else if (c == 'l') {
  112. l += printptr((__uint64_t) get_arg(__uint32_t, argno), 8, color);
  113. argno++;
  114. }
  115. else if (c == 'q') {
  116. l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16, color);
  117. argno++;
  118. }
  119. else if (c == 's') {
  120. fmt_s = (char *) get_arg(char *, argno);
  121. argno++;
  122. if (!fmt_s)
  123. fmt_s = "(null)";
  124. /* put while end of fmt_s */
  125. while (*fmt_s) {
  126. putc(*fmt_s, color);
  127. fmt_s++;
  128. l++;
  129. }
  130. }
  131. else if (c == 'c') {
  132. /* `char` aligned to `int` in GCC */
  133. putc((char) get_arg(int, argno), color);
  134. argno++;
  135. l++;
  136. }
  137. else if (c == '%') {
  138. /* put % on screen */
  139. putc(c, color);
  140. l++;
  141. }
  142. else {
  143. /* don't forget to put % */
  144. putc('%', color);
  145. putc(c, color);
  146. l++;
  147. }
  148. state = 0;
  149. }
  150. }
  151. /* update cursor position */
  152. x += l;
  153. if (x > TEXT_VGA_X) {
  154. x = TEXT_VGA_X - x;
  155. y++;
  156. }
  157. y += newlines;
  158. if (newlines)
  159. x = 0;
  160. if (y > TEXT_VGA_Y) {
  161. y = 0;
  162. clear_line(0);
  163. }
  164. update_cursor();
  165. return i;
  166. }
  167. static char digits[] = {"0123456789abcdef"};
  168. __uint64_t printint(long n, int base, int sign, char color) {
  169. char buf[32];
  170. /* neg: x < 0 && signed interger */
  171. int i, neg, j;
  172. /* x: real number should put */
  173. __uint64_t x;
  174. if (n < 0 && sign) {
  175. neg = 1;
  176. x = -n;
  177. }
  178. else {
  179. neg = 0;
  180. x = n;
  181. }
  182. i = 0;
  183. do {
  184. /* get string of based-number */
  185. /* the first bit of 10-based digit is at buf[15] */
  186. buf[i++] = digits[x % base];
  187. } while (x /= base);
  188. if (neg)
  189. buf[i++] = '-';
  190. j = i;
  191. while (--j >= 0)
  192. putc(buf[j], color);
  193. return i;
  194. }
  195. __uint64_t printptr(__uint64_t p, int width, char color) {
  196. int i;
  197. for (i = width - 1; i >= 0; i--) {
  198. char c = (p >> (i * 4)) & 0x0f;
  199. if (c > 9)
  200. c = c - 10 + 'a';
  201. else
  202. c += '0';
  203. putc(c, color);
  204. }
  205. return width;
  206. }
  207. #define LINE_ADDR(l) ((char *) (__uint64_t) (0xb8000 + (l) * TEXT_VGA_X * 2))
  208. void clear_line(int line) {
  209. char *p = LINE_ADDR(line);
  210. memsetw(p, 0x0720, TEXT_VGA_X * 2);
  211. return;
  212. }