io.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* I/O library: VGA text output and PS/2 keyboard. */
  2. #include "io.h"
  3. #define VGA_BASE ((volatile unsigned short *)0xB8000)
  4. #define VGA_ATTR 0x0A
  5. #define KB_DATA 0x60
  6. #define KB_STATUS 0x64
  7. static inline unsigned char inb(unsigned short port)
  8. {
  9. unsigned char v;
  10. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  11. return v;
  12. }
  13. static inline void outb(unsigned short port, unsigned char v)
  14. {
  15. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  16. }
  17. static void disable_hw_cursor(void)
  18. {
  19. outb(0x3D4, 0x0A);
  20. outb(0x3D5, 0x20);
  21. }
  22. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
  23. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  24. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  25. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  26. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  27. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  28. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  29. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  30. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  31. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  32. [0x28] = '\'', [0x29] = '`',
  33. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  34. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  35. [0x35] = '/', [0x39] = ' ',
  36. };
  37. static int shift_pressed;
  38. static unsigned int cursor;
  39. static unsigned short cursor_cell;
  40. static unsigned char last_scancode;
  41. static int scroll_occurred;
  42. static char apply_shift(char c)
  43. {
  44. if (!shift_pressed) return c;
  45. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  46. switch ((unsigned char)c) {
  47. case '1': return '!'; case '2': return '@'; case '3': return '#';
  48. case '4': return '$'; case '5': return '%'; case '6': return '^';
  49. case '7': return '&'; case '8': return '*'; case '9': return '(';
  50. case '0': return ')'; case '-': return '_'; case '=': return '+';
  51. case '[': return '{'; case ']': return '}'; case ';': return ':';
  52. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  53. case ',': return '<'; case '.': return '>'; case '/': return '?';
  54. default: return c;
  55. }
  56. }
  57. static void hide_cursor_at(unsigned int pos)
  58. {
  59. VGA_BASE[pos] = cursor_cell;
  60. }
  61. void io_init(void)
  62. {
  63. disable_hw_cursor();
  64. io_clear_screen();
  65. cursor = 0;
  66. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  67. io_drain_keyboard();
  68. }
  69. void io_clear_screen(void)
  70. {
  71. unsigned int i;
  72. for (i = 0; i < IO_VGA_ROWS * IO_VGA_COLS; i++)
  73. VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
  74. }
  75. int io_did_scroll(void)
  76. {
  77. if (!scroll_occurred)
  78. return 0;
  79. scroll_occurred = 0;
  80. return 1;
  81. }
  82. void io_putchar(char c)
  83. {
  84. if (c == '\n') {
  85. hide_cursor_at(cursor);
  86. cursor = (cursor / IO_VGA_COLS + 1) * IO_VGA_COLS;
  87. if (cursor >= IO_CONTENT_SIZE) {
  88. unsigned int i;
  89. for (i = 0; i < (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i++)
  90. VGA_BASE[i] = VGA_BASE[i + IO_VGA_COLS];
  91. for (i = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i < IO_CONTENT_SIZE; i++)
  92. VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
  93. cursor = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS;
  94. scroll_occurred = 1;
  95. }
  96. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  97. return;
  98. }
  99. if (c == '\b') {
  100. if (cursor > 0) {
  101. hide_cursor_at(cursor);
  102. cursor--;
  103. cursor_cell = VGA_BASE[cursor];
  104. }
  105. return;
  106. }
  107. if (cursor < IO_CONTENT_SIZE) {
  108. VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
  109. cursor++;
  110. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  111. }
  112. }
  113. void io_putstr(const char *s)
  114. {
  115. while (*s) io_putchar(*s++);
  116. }
  117. unsigned int io_get_cursor(void)
  118. {
  119. return cursor;
  120. }
  121. #define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
  122. void io_cursor_show_block(void)
  123. {
  124. VGA_BASE[cursor] = CURSOR_BLOCK;
  125. }
  126. void io_cursor_hide_block(void)
  127. {
  128. VGA_BASE[cursor] = cursor_cell;
  129. }
  130. void io_vga_write(unsigned int pos, unsigned short value)
  131. {
  132. VGA_BASE[pos] = value;
  133. }
  134. void io_drain_keyboard(void)
  135. {
  136. while (inb(KB_STATUS) & 1)
  137. (void)inb(KB_DATA);
  138. }
  139. char io_poll_key(void)
  140. {
  141. if (!(inb(KB_STATUS) & 1))
  142. return 0;
  143. {
  144. unsigned char sc = inb(KB_DATA);
  145. if (sc & 0x80) {
  146. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  147. return 0;
  148. }
  149. if (sc == 0x2A || sc == 0x36) {
  150. shift_pressed = 1;
  151. return 0;
  152. }
  153. {
  154. char c = (char)scancode_to_ascii[sc];
  155. if (c) {
  156. last_scancode = sc;
  157. return apply_shift(c);
  158. }
  159. }
  160. }
  161. return 0;
  162. }
  163. unsigned char io_last_scancode(void)
  164. {
  165. return last_scancode;
  166. }