console.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/string.h>
  25. #include <arch/io.h>
  26. __uint16_t pos = 0;
  27. __uint16_t x = 0, y = 0;
  28. #define POS2XY(pos) x = pos % 80; y = pos / 80;
  29. static __uint16_t read_cursor(void);
  30. static void update_cursor(void);
  31. static void roll_screen(void);
  32. void raw_putc(__uint16_t, __uint8_t, char);
  33. int cano_putc(char);
  34. static void put_kmlog(void);
  35. struct tty console_tty = {
  36. .dev_name = "console",
  37. .priv_data = 0,
  38. .flags = FLAGS_COLORS,
  39. .putc = cano_putc,
  40. .getc = NULL,
  41. };
  42. int __init init_console(void) {
  43. pos = read_cursor();
  44. POS2XY(pos);
  45. cano_putc('\n');
  46. put_kmlog();
  47. return tty_register(&console_tty);
  48. }
  49. static __uint16_t read_cursor(void) {
  50. __uint16_t rv = 0;
  51. outb(CMD_POS_LO, VGA_INDEX);
  52. rv |= (__uint16_t) inb(VGA_DATA);
  53. outb(CMD_POS_HI, VGA_INDEX);
  54. rv |= ((__uint16_t) inb(VGA_DATA)) << 8;
  55. return rv;
  56. }
  57. int cano_putc(char c) {
  58. if (c == '\n') {
  59. y++;
  60. x = 0;
  61. goto out;
  62. }
  63. if (c == '\r') {
  64. x = 0;
  65. goto out;
  66. }
  67. if (c == '\t') {
  68. __uint32_t t = (x + TABSIZE - 1) & ~(TABSIZE - 1);
  69. if (t == x)
  70. x += TABSIZE;
  71. else
  72. x = t;
  73. if (x == COLMS) {
  74. y++;
  75. x = TABSIZE;
  76. }
  77. goto out;
  78. }
  79. raw_putc(pos++, 7, c);
  80. x++;
  81. if (x == COLMS) {
  82. y++;
  83. x = 0;
  84. }
  85. out:
  86. update_cursor();
  87. return 0;
  88. }
  89. void raw_putc(__uint16_t pos, __uint8_t color, char c) {
  90. __uint16_t *fb = FB_ADDR + pos;
  91. *fb = (color << 8) + c;
  92. return;
  93. }
  94. void update_cursor(void) {
  95. if (y > LINES)
  96. roll_screen();
  97. pos = y * COLMS + x;
  98. outb(CMD_POS_LO, VGA_INDEX);
  99. outb(pos & 0xff, VGA_DATA);
  100. outb(CMD_POS_HI, VGA_INDEX);
  101. outb((pos >> 8) & 0xff, VGA_DATA);
  102. return;
  103. }
  104. void roll_screen(void) {
  105. /* well, in old versions of xnix this was implemented
  106. * in assembly, that's really fast.
  107. *
  108. * but ... now we can only use C. */
  109. __uint16_t *s;
  110. memcpy(FB_ADDR, FB_ADDR + 80, 7680);
  111. /* fill the last line with '0720', white space */
  112. for (s = FB_ADDR + 1920; s < (__uint16_t *) 0xb9000; s++)
  113. *s = 0x0720;
  114. y = 24;
  115. return;
  116. }
  117. static void put_kmlog(void) {
  118. char *s;
  119. for (s = kmlog_buf; *s; s++) {
  120. if (s > kmlog_buf + KMLOG_SIZE)
  121. s = kmlog_buf;
  122. cano_putc(*s);
  123. }
  124. return;
  125. }
  126. module_init(init_console);