console.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <kernel/tty.h>
  26. #include <lib/string.h>
  27. #include <arch/io.h>
  28. #include <stdarg.h>
  29. static __uint16_t *fb = FB_ADDR;
  30. __uint8_t x, y;
  31. struct tty tty0;
  32. extern struct fifo kbd_fifo;
  33. int nc_putc(struct tty *, char);
  34. errno_t console_init(void) {
  35. schk_init();
  36. /* stdout will be the tty1 if com0 exists */
  37. stdout = &tty0;
  38. x = bootarg->x, y = bootarg->y;
  39. /* setup the frame buffer address fb */
  40. /* note that `fb` is a 16-bits pointer, so we don't need to '<< 1' */
  41. fb += y * VGA_X + x;
  42. tty0.input_fifo = NULL;
  43. tty0.mode = T_COOKED | T_NORD;
  44. tty0.ctl = 0;
  45. /* this is virtual */
  46. tty0.vx = x;
  47. tty0.vy = y;
  48. tty0.putc = nc_putc;
  49. tty0.next = NULL;
  50. /* well, this is the end */
  51. schk_leave();
  52. return 0;
  53. }
  54. int nc_putc(struct tty *tty, char c) {
  55. if (c == '\n') {
  56. tty->vy++;
  57. tty->vx = 0;
  58. /* no roll screen for tty->vy */
  59. putc('\n', white);
  60. return 0;
  61. }
  62. else if (c == '\b') {
  63. if (tty->vx)
  64. putc('\b', white);
  65. else
  66. return 0;
  67. }
  68. else if (c == '\t') {
  69. __uint64_t ax = (tty->vx + TAB_SIZE) & ~TAB_SIZE;
  70. if (ax == tty->vx)
  71. tty->vx += TAB_SIZE;
  72. else
  73. tty->vx = ax;
  74. putc('\t', white);
  75. return 0;
  76. }
  77. else if (c == '\r') {
  78. tty->vx = 0;
  79. putc('\r', white);
  80. return 0;
  81. }
  82. putc(c, white);
  83. tty->vx++;
  84. return 0;
  85. }
  86. void putc(char c, __uint8_t color) {
  87. if (c == '\n') {
  88. /* we do it like \r\n */
  89. x = 0;
  90. if (++y == VGA_Y) {
  91. roll_screen();
  92. y = 24;
  93. }
  94. update_cursor();
  95. return;
  96. }
  97. else if (c == '\r') {
  98. x = 0;
  99. update_cursor();
  100. return;
  101. }
  102. if (c == '\b') {
  103. if (x) {
  104. x--;
  105. update_cursor();
  106. *fb = 0x0720;
  107. }
  108. return;
  109. }
  110. else if (c == '\t') {
  111. __uint64_t ax = (x + TAB_SIZE - 1) & ~(TAB_SIZE - 1);
  112. if (ax == x)
  113. x += TAB_SIZE;
  114. else
  115. x = ax;
  116. if (x == VGA_X) {
  117. y++;
  118. x = TAB_SIZE;
  119. }
  120. update_cursor();
  121. return;
  122. }
  123. __asm__ __volatile__ (
  124. /* rdi is the frame buffer, and rax is the */
  125. "cld\n\t"
  126. "movb %%cl, %%ah\n\t"
  127. "stosw\n\t"
  128. : "+D" (fb)
  129. : "a" (c), "c" (color)
  130. );
  131. if (++x == VGA_X) {
  132. x = 0;
  133. if (++y == VGA_Y) {
  134. roll_screen();
  135. y = 24;
  136. }
  137. }
  138. update_cursor();
  139. return;
  140. }
  141. __uint16_t read_cursor(void) {
  142. __uint16_t pos = 0;
  143. outb(CMD_POS_LO, CURSOR_INDEX);
  144. pos |= inb(CURSOR_DATA);
  145. outb(CMD_POS_HI, CURSOR_INDEX);
  146. pos |= ((__uint16_t) inb(CURSOR_DATA)) << 8;
  147. return pos;
  148. }
  149. void update_cursor(void) {
  150. if (y > VGA_Y) {
  151. roll_screen();
  152. y = 24;
  153. }
  154. __uint16_t pos = y * bootarg->vga_x + x;
  155. fb = FB_ADDR + pos;
  156. outb(CMD_POS_LO, CURSOR_INDEX);
  157. outb(pos & 0xff, CURSOR_DATA);
  158. outb(CMD_POS_HI, CURSOR_INDEX);
  159. outb((pos >> 8) & 0xff, CURSOR_DATA);
  160. return;
  161. }
  162. void roll_screen(void) {
  163. /* roll down the screen */
  164. __asm__ __volatile__ (
  165. "cld\n\t"
  166. "movq $960, %%rcx\n\t"
  167. "movq $0xb80a0, %%rsi\n\t"
  168. "movq $0xb8000, %%rdi\n\t"
  169. "rep\n\t"
  170. "movsq\n\t"
  171. "movq $0xb8f00, %%rdi\n\t"
  172. "movq $0x0720, %%rax\n\t"
  173. "movq $80, %%rcx\n\t"
  174. "rep\n\t"
  175. "stosw\n\t"
  176. :::
  177. );
  178. return;
  179. }