console.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* modules/console.c
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #include <sys/types.h>
  21. #include <kernel/init.h>
  22. #include <kernel/module.h>
  23. #include <kernel/kmlog.h>
  24. #include <kernel/tty.h>
  25. #include <modules/console.h>
  26. #include <arch/string.h>
  27. #include <arch/io.h>
  28. __uint16_t pos = 0;
  29. __uint16_t x = 0, y = 0;
  30. #define POS2XY(pos) x = pos % 80; y = pos / 80;
  31. static __uint16_t read_cursor(void);
  32. static void update_cursor(void);
  33. static void roll_screen(void);
  34. void raw_putc(__uint16_t, __uint8_t, char);
  35. int cano_putc(char);
  36. static void put_kmlog(void);
  37. struct tty console_tty = {
  38. .dev_name = "console",
  39. .priv_data = 0,
  40. .flags = FLAGS_COLORS,
  41. .putc = cano_putc,
  42. .getc = NULL,
  43. };
  44. int __init init_console(void) {
  45. pos = read_cursor();
  46. POS2XY(pos);
  47. cano_putc('\n');
  48. put_kmlog();
  49. return tty_register(&console_tty);
  50. }
  51. static __uint16_t read_cursor(void) {
  52. __uint16_t rv = 0;
  53. outb(CMD_POS_LO, VGA_INDEX);
  54. rv |= (__uint16_t) inb(VGA_DATA);
  55. outb(CMD_POS_HI, VGA_INDEX);
  56. rv |= ((__uint16_t) inb(VGA_DATA)) << 8;
  57. return rv;
  58. }
  59. int cano_putc(char c) {
  60. if (c == '\n') {
  61. y++;
  62. x = 0;
  63. goto out;
  64. }
  65. if (c == '\r') {
  66. x = 0;
  67. goto out;
  68. }
  69. if (c == '\t') {
  70. __uint32_t t = (x + TABSIZE - 1) & ~(TABSIZE - 1);
  71. if (t == x)
  72. x += TABSIZE;
  73. else
  74. x = t;
  75. if (x == COLMS) {
  76. y++;
  77. x = TABSIZE;
  78. }
  79. goto out;
  80. }
  81. raw_putc(pos++, 7, c);
  82. x++;
  83. if (x == COLMS) {
  84. y++;
  85. x = 0;
  86. }
  87. out:
  88. update_cursor();
  89. return 0;
  90. }
  91. void raw_putc(__uint16_t pos, __uint8_t color, char c) {
  92. __uint16_t *fb = FB_ADDR + pos;
  93. *fb = (color << 8) + c;
  94. return;
  95. }
  96. void update_cursor(void) {
  97. if (y > LINES)
  98. roll_screen();
  99. pos = y * COLMS + x;
  100. outb(CMD_POS_LO, VGA_INDEX);
  101. outb(pos & 0xff, VGA_DATA);
  102. outb(CMD_POS_HI, VGA_INDEX);
  103. outb((pos >> 8) & 0xff, VGA_DATA);
  104. return;
  105. }
  106. void roll_screen(void) {
  107. /* well, in old versions of xnix this was implemented
  108. * in assembly, that's really fast.
  109. *
  110. * but ... now we can only use C. */
  111. __uint16_t *s;
  112. memcpy(FB_ADDR, FB_ADDR + 80, 7680);
  113. /* fill the last line with '0720', white space */
  114. for (s = FB_ADDR + 1920; s < (__uint16_t *) 0xb9000; s++)
  115. *s = 0x0720;
  116. y = 24;
  117. return;
  118. }
  119. static void put_kmlog(void) {
  120. char *s;
  121. for (s = kmlog_buf; *s; s++) {
  122. if (s > kmlog_buf + KMLOG_SIZE)
  123. s = kmlog_buf;
  124. cano_putc(*s);
  125. }
  126. return;
  127. }
  128. module_init(init_console);