console.c 3.7 KB

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