console.c 3.7 KB

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