console.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <modules/console.h>
  23. #include <arch/io.h>
  24. __uint16_t pos = 0;
  25. __uint16_t x = 0, y = 0;
  26. #define POS2XY(pos) x = pos % 80; y = pos / 80;
  27. static __uint16_t read_cursor(void);
  28. static void update_cursor(void);
  29. void raw_putc(__uint16_t, __uint8_t, char);
  30. void cano_putc(char);
  31. static void put_kmlog(void);
  32. int __init init_console(void) {
  33. pos = read_cursor();
  34. POS2XY(pos);
  35. cano_putc('\n');
  36. put_kmlog();
  37. return 0;
  38. }
  39. static __uint16_t read_cursor(void) {
  40. __uint16_t rv = 0;
  41. outb(CMD_POS_LO, VGA_INDEX);
  42. rv |= (__uint16_t) inb(VGA_DATA);
  43. outb(CMD_POS_HI, VGA_INDEX);
  44. rv |= ((__uint16_t) inb(VGA_DATA)) << 8;
  45. return rv;
  46. }
  47. void cano_putc(char c) {
  48. if (c == '\n') {
  49. y++;
  50. x = 0;
  51. goto out;
  52. }
  53. if (c == '\r') {
  54. x = 0;
  55. goto out;
  56. }
  57. if (c == '\t') {
  58. __uint32_t t = (x + TABSIZE - 1) & ~(TABSIZE - 1);
  59. if (t == x)
  60. x += TABSIZE;
  61. else
  62. x = t;
  63. if (x == COLMS) {
  64. y++;
  65. x = TABSIZE;
  66. }
  67. goto out;
  68. }
  69. raw_putc(pos++, 7, c);
  70. x++;
  71. if (x == COLMS) {
  72. y++;
  73. x = 0;
  74. }
  75. out:
  76. update_cursor();
  77. return;
  78. }
  79. void raw_putc(__uint16_t pos, __uint8_t color, char c) {
  80. __uint16_t *fb = FB_ADDR + pos;
  81. *fb = (color << 8) + c;
  82. return;
  83. }
  84. void update_cursor(void) {
  85. pos = y * COLMS + x;
  86. outb(CMD_POS_LO, VGA_INDEX);
  87. outb(pos & 0xff, VGA_DATA);
  88. outb(CMD_POS_HI, VGA_INDEX);
  89. outb((pos >> 8) & 0xff, VGA_DATA);
  90. return;
  91. }
  92. static void put_kmlog(void) {
  93. char *s;
  94. for (s = kmlog_buf; *s; s++) {
  95. if (s > kmlog_buf + KMLOG_SIZE)
  96. s = kmlog_buf;
  97. cano_putc(*s);
  98. }
  99. return;
  100. }
  101. module_init(init_console);