io.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. [0x01] = 0x1B, /* Escape */
  25. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  26. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  27. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  28. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  29. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  30. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  31. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  32. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  33. [0x28] = '\'', [0x29] = '`',
  34. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  35. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  36. [0x35] = '/', [0x39] = ' ',
  37. };
  38. static int shift_pressed;
  39. static unsigned int cursor;
  40. static unsigned short cursor_cell;
  41. static unsigned char last_scancode;
  42. static int scroll_occurred;
  43. static int kbd_extended; /* 1 after 0xE0, next byte is extended scancode */
  44. #define KBD_BUF_SIZE 16
  45. static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
  46. static volatile unsigned int kbd_head, kbd_tail;
  47. void kbd_irq(void)
  48. {
  49. unsigned char sc = inb(KB_DATA);
  50. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  51. if (next != kbd_tail) {
  52. kbd_buf[kbd_head] = sc;
  53. kbd_head = next;
  54. }
  55. outb(0x20, 0x20);
  56. }
  57. static int kbd_buf_get(unsigned char *sc)
  58. {
  59. if (kbd_tail == kbd_head)
  60. return 0;
  61. *sc = kbd_buf[kbd_tail];
  62. kbd_tail = (kbd_tail + 1) % KBD_BUF_SIZE;
  63. return 1;
  64. }
  65. static char apply_shift(char c)
  66. {
  67. if (!shift_pressed) return c;
  68. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  69. switch ((unsigned char)c) {
  70. case '1': return '!'; case '2': return '@'; case '3': return '#';
  71. case '4': return '$'; case '5': return '%'; case '6': return '^';
  72. case '7': return '&'; case '8': return '*'; case '9': return '(';
  73. case '0': return ')'; case '-': return '_'; case '=': return '+';
  74. case '[': return '{'; case ']': return '}'; case ';': return ':';
  75. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  76. case ',': return '<'; case '.': return '>'; case '/': return '?';
  77. default: return c;
  78. }
  79. }
  80. static void hide_cursor_at(unsigned int pos)
  81. {
  82. VGA_BASE[pos] = cursor_cell;
  83. }
  84. void io_init(void)
  85. {
  86. disable_hw_cursor();
  87. io_clear_screen();
  88. cursor = IO_CONTENT_OFFSET;
  89. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  90. io_drain_keyboard();
  91. }
  92. void io_clear_screen(void)
  93. {
  94. unsigned int i;
  95. for (i = 0; i < IO_VGA_ROWS * IO_VGA_COLS; i++)
  96. VGA_BASE[i] = (unsigned short)(VGA_ATTR << 8);
  97. }
  98. int io_did_scroll(void)
  99. {
  100. if (!scroll_occurred)
  101. return 0;
  102. scroll_occurred = 0;
  103. return 1;
  104. }
  105. void io_putchar(char c)
  106. {
  107. if (c == '\n') {
  108. hide_cursor_at(cursor);
  109. cursor = (cursor / IO_VGA_COLS + 1) * IO_VGA_COLS;
  110. if (cursor >= IO_CONTENT_OFFSET + IO_CONTENT_SIZE) {
  111. unsigned int i;
  112. for (i = 0; i < (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i++)
  113. VGA_BASE[IO_CONTENT_OFFSET + i] = VGA_BASE[IO_CONTENT_OFFSET + i + IO_VGA_COLS];
  114. for (i = (IO_CONTENT_ROWS - 1) * IO_VGA_COLS; i < IO_CONTENT_SIZE; i++)
  115. VGA_BASE[IO_CONTENT_OFFSET + i] = (unsigned short)(VGA_ATTR << 8);
  116. cursor = IO_CONTENT_OFFSET + (IO_CONTENT_ROWS - 1) * IO_VGA_COLS;
  117. scroll_occurred = 1;
  118. }
  119. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  120. return;
  121. }
  122. if (c == '\b') {
  123. if (cursor > IO_CONTENT_OFFSET) {
  124. hide_cursor_at(cursor);
  125. cursor--;
  126. VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | ' '); /* erase char on screen */
  127. cursor_cell = (unsigned short)((VGA_ATTR << 8) | ' ');
  128. }
  129. return;
  130. }
  131. if (cursor < IO_CONTENT_OFFSET + IO_CONTENT_SIZE) {
  132. VGA_BASE[cursor] = (unsigned short)((VGA_ATTR << 8) | (unsigned char)c);
  133. cursor++;
  134. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  135. }
  136. }
  137. void io_putstr(const char *s)
  138. {
  139. while (*s) io_putchar(*s++);
  140. }
  141. unsigned int io_get_cursor(void)
  142. {
  143. return cursor;
  144. }
  145. void io_set_cursor(unsigned int pos)
  146. {
  147. if (pos >= IO_VGA_ROWS * IO_VGA_COLS)
  148. return;
  149. hide_cursor_at(cursor);
  150. cursor = pos;
  151. cursor_cell = VGA_BASE[cursor];
  152. }
  153. #define CURSOR_BLOCK ((unsigned short)((VGA_ATTR << 8) | 0xDB))
  154. void io_cursor_show_block(void)
  155. {
  156. VGA_BASE[cursor] = CURSOR_BLOCK;
  157. }
  158. void io_cursor_hide_block(void)
  159. {
  160. VGA_BASE[cursor] = cursor_cell;
  161. }
  162. void io_vga_write(unsigned int pos, unsigned short value)
  163. {
  164. VGA_BASE[pos] = value;
  165. }
  166. void io_drain_keyboard(void)
  167. {
  168. while (inb(KB_STATUS) & 1)
  169. (void)inb(KB_DATA);
  170. kbd_head = kbd_tail = 0;
  171. kbd_extended = 0;
  172. }
  173. int io_poll_key(void)
  174. {
  175. unsigned char sc;
  176. if (!kbd_buf_get(&sc))
  177. return 0;
  178. if (sc == 0xE0) {
  179. kbd_extended = 1;
  180. return 0;
  181. }
  182. if (kbd_extended) {
  183. kbd_extended = 0;
  184. if (sc & 0x80)
  185. return 0; /* key release */
  186. if (sc == 0x4B) return IO_KEY_LEFT;
  187. if (sc == 0x4D) return IO_KEY_RIGHT;
  188. return 0;
  189. }
  190. if (sc & 0x80) {
  191. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  192. return 0;
  193. }
  194. if (sc == 0x2A || sc == 0x36) {
  195. shift_pressed = 1;
  196. return 0;
  197. }
  198. {
  199. char c = (char)scancode_to_ascii[sc];
  200. if (c) {
  201. last_scancode = sc;
  202. return (unsigned char)apply_shift(c);
  203. }
  204. }
  205. return 0;
  206. }
  207. unsigned char io_last_scancode(void)
  208. {
  209. return last_scancode;
  210. }