console.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <kernel/console.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 __uint16_t *fb = FB_ADDR;
  30. __uint8_t x, y;
  31. struct tty tty0;
  32. struct tty stdin;
  33. struct tty stdout;
  34. struct tty stderr;
  35. extern struct fifo kbd_fifo;
  36. int nc_putc(struct tty *, char);
  37. errno_t console_init(void) {
  38. schk_init();
  39. x = bootarg->x, y = bootarg->y;
  40. /* setup the frame buffer address fb */
  41. /* note that `fb` is a 16-bits pointer, so we don't need to '<< 1' */
  42. fb += y * VGA_X + x;
  43. tty0.input_fifo = NULL;
  44. tty0.mode = T_COOKED | T_NORD;
  45. tty0.ctl = 0;
  46. /* this is virtual */
  47. tty0.vx = x;
  48. tty0.vy = y;
  49. tty0.putc = nc_putc;
  50. stdin.input_fifo = &kbd_fifo;
  51. stdin.mode = T_COOKED;
  52. stdin.ctl = 0;
  53. stdin.vx = x;
  54. stdin.vy = y;
  55. memcpy(&stdout, &tty0, sizeof(struct tty));
  56. memcpy(&stdout, &tty0, sizeof(struct tty));
  57. /* well, this is the end */
  58. schk_leave();
  59. return 0;
  60. }
  61. int nc_putc(struct tty *tty, char c) {
  62. if (c == '\n') {
  63. tty->vy++;
  64. tty->vx = 0;
  65. /* no roll screen for tty->vy */
  66. putc('\n', white);
  67. return 0;
  68. }
  69. else if (c == '\b') {
  70. if (tty->vx)
  71. putc('\b', white);
  72. else
  73. return 0;
  74. }
  75. else if (c == '\t') {
  76. __uint64_t ax = (tty->vx + 3) & ~3;
  77. if (ax == tty->vx)
  78. tty->vx += TAB_SIZE;
  79. else
  80. tty->vx = ax;
  81. putc('\t', white);
  82. return 0;
  83. }
  84. else if (c == '\r') {
  85. tty->vx = 0;
  86. putc('\r', white);
  87. return 0;
  88. }
  89. putc(c, white);
  90. tty->vx++;
  91. return 0;
  92. }
  93. int printk(__uint8_t color, char *s, ...) {
  94. va_list args;
  95. int i;
  96. /* I think 256 is enough in kernel */
  97. /* too long, return it. */
  98. va_start(args, s);
  99. /* it maybe overflow, but that's in .data section and we did check. */
  100. i = vsprintf(buffer, s, args);
  101. print(color, buffer);
  102. memset(buffer, 0, 256);
  103. va_end(args);
  104. return i;
  105. }
  106. void print(__uint8_t color, char *s) {
  107. while (*s)
  108. putc(*s++, color);
  109. return;
  110. }
  111. void putc(char c, __uint8_t color) {
  112. if (c == '\n') {
  113. /* we do it like \r\n */
  114. x = 0;
  115. if (++y == VGA_Y) {
  116. roll_screen();
  117. y = 24;
  118. }
  119. update_cursor();
  120. return;
  121. }
  122. else if (c == '\r') {
  123. x = 0;
  124. update_cursor();
  125. return;
  126. }
  127. if (c == '\b') {
  128. if (x) {
  129. x--;
  130. update_cursor();
  131. *fb = 0x0720;
  132. }
  133. return;
  134. }
  135. else if (c == '\t') {
  136. __uint64_t ax = (x + TAB_SIZE - 1) & ~(TAB_SIZE - 1);
  137. if (ax == x)
  138. x += TAB_SIZE;
  139. else
  140. x = ax;
  141. if (x == VGA_X) {
  142. y++;
  143. x = TAB_SIZE;
  144. }
  145. update_cursor();
  146. return;
  147. }
  148. __asm__ __volatile__ (
  149. /* rdi is the frame buffer, and rax is the */
  150. "cld\n\t"
  151. "movb %%cl, %%ah\n\t"
  152. "stosw\n\t"
  153. : "+D" (fb)
  154. : "a" (c), "c" (color)
  155. );
  156. if (++x == VGA_X) {
  157. x = 0;
  158. if (++y == VGA_Y) {
  159. roll_screen();
  160. y = 24;
  161. }
  162. }
  163. update_cursor();
  164. return;
  165. }
  166. __uint16_t read_cursor(void) {
  167. __uint16_t pos = 0;
  168. outb(CMD_POS_LO, CURSOR_INDEX);
  169. pos |= inb(CURSOR_DATA);
  170. outb(CMD_POS_HI, CURSOR_INDEX);
  171. pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
  172. return pos;
  173. }
  174. void update_cursor(void) {
  175. if (y > VGA_Y) {
  176. roll_screen();
  177. y = 24;
  178. }
  179. __uint16_t pos = y * bootarg->vga_x + x;
  180. fb = FB_ADDR + pos;
  181. outb(CMD_POS_LO, CURSOR_INDEX);
  182. outb(pos & 0xff, CURSOR_DATA);
  183. outb(CMD_POS_HI, CURSOR_INDEX);
  184. outb((pos >> 8) & 0xff, CURSOR_DATA);
  185. return;
  186. }
  187. void roll_screen(void) {
  188. /* roll down the screen */
  189. __asm__ __volatile__ (
  190. "cld\n\t"
  191. "movq $960, %%rcx\n\t"
  192. "movq $0xb80a0, %%rsi\n\t"
  193. "movq $0xb8000, %%rdi\n\t"
  194. "rep\n\t"
  195. "movsq\n\t"
  196. "movq $0xb8f00, %%rdi\n\t"
  197. "movq $0x0720, %%rax\n\t"
  198. "movq $80, %%rcx\n\t"
  199. "rep\n\t"
  200. "stosw\n\t"
  201. :::
  202. );
  203. return;
  204. }