console.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* modules/console.c
  2. *
  3. * This file is part of evx.
  4. *
  5. * evx is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * evx 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. * You should have received a copy of the GNU General Public License
  16. * along with evx. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <sys/types.h>
  19. #include <kernel/init.h>
  20. #include <kernel/module.h>
  21. #include <kernel/kmlog.h>
  22. #include <kernel/tty.h>
  23. #include <modules/console.h>
  24. #include <arch/io.h>
  25. __uint16_t pos = 0;
  26. __uint16_t x = 0, y = 0;
  27. #define POS2XY(pos) x = pos % 80; y = pos / 80;
  28. static __uint16_t read_cursor(void);
  29. static void update_cursor(void);
  30. void raw_putc(__uint16_t, __uint8_t, char);
  31. int cano_putc(char);
  32. static void put_kmlog(void);
  33. struct tty console_tty = {
  34. .dev_name = "console",
  35. .priv_data = 0,
  36. .flags = FLAGS_COLORS,
  37. .putc = cano_putc,
  38. .getc = NULL,
  39. };
  40. int __init init_console(void) {
  41. pos = read_cursor();
  42. POS2XY(pos);
  43. cano_putc('\n');
  44. put_kmlog();
  45. return tty_register(&console_tty);
  46. }
  47. static __uint16_t read_cursor(void) {
  48. __uint16_t rv = 0;
  49. outb(CMD_POS_LO, VGA_INDEX);
  50. rv |= (__uint16_t) inb(VGA_DATA);
  51. outb(CMD_POS_HI, VGA_INDEX);
  52. rv |= ((__uint16_t) inb(VGA_DATA)) << 8;
  53. return rv;
  54. }
  55. int cano_putc(char c) {
  56. if (c == '\n') {
  57. y++;
  58. x = 0;
  59. goto out;
  60. }
  61. if (c == '\r') {
  62. x = 0;
  63. goto out;
  64. }
  65. if (c == '\t') {
  66. __uint32_t t = (x + TABSIZE - 1) & ~(TABSIZE - 1);
  67. if (t == x)
  68. x += TABSIZE;
  69. else
  70. x = t;
  71. if (x == COLMS) {
  72. y++;
  73. x = TABSIZE;
  74. }
  75. goto out;
  76. }
  77. raw_putc(pos++, 7, c);
  78. x++;
  79. if (x == COLMS) {
  80. y++;
  81. x = 0;
  82. }
  83. out:
  84. update_cursor();
  85. return 0;
  86. }
  87. void raw_putc(__uint16_t pos, __uint8_t color, char c) {
  88. __uint16_t *fb = FB_ADDR + pos;
  89. *fb = (color << 8) + c;
  90. return;
  91. }
  92. void update_cursor(void) {
  93. pos = y * COLMS + x;
  94. outb(CMD_POS_LO, VGA_INDEX);
  95. outb(pos & 0xff, VGA_DATA);
  96. outb(CMD_POS_HI, VGA_INDEX);
  97. outb((pos >> 8) & 0xff, VGA_DATA);
  98. return;
  99. }
  100. static void put_kmlog(void) {
  101. char *s;
  102. for (s = kmlog_buf; *s; s++) {
  103. if (s > kmlog_buf + KMLOG_SIZE)
  104. s = kmlog_buf;
  105. cano_putc(*s);
  106. }
  107. return;
  108. }
  109. module_init(init_console);