console.c 2.1 KB

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