io.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* I/O library: framebuffer text output and PS/2 keyboard. */
  2. #include "io.h"
  3. #include "fbtext.h"
  4. #define VGA_ATTR 0x0A
  5. static unsigned int io_cols = 80;
  6. static unsigned int io_rows = 25;
  7. #define KB_DATA 0x60
  8. #define KB_STATUS 0x64
  9. static inline unsigned char inb(unsigned short port)
  10. {
  11. unsigned char v;
  12. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  13. return v;
  14. }
  15. static inline void outb(unsigned short port, unsigned char v)
  16. {
  17. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  18. }
  19. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
  20. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  21. [0x01] = 0x1B, /* Escape */
  22. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  23. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  24. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  25. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  26. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  27. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  28. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  29. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  30. [0x28] = '\'', [0x29] = '`',
  31. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  32. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  33. [0x35] = '/', [0x39] = ' ',
  34. };
  35. static int shift_pressed;
  36. static unsigned int cursor;
  37. static unsigned short cursor_cell;
  38. static unsigned char last_scancode;
  39. static int scroll_occurred;
  40. static int kbd_extended; /* 1 after 0xE0, next byte is extended scancode */
  41. #define KBD_BUF_SIZE 16
  42. static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
  43. static volatile unsigned int kbd_head, kbd_tail;
  44. void kbd_irq(void)
  45. {
  46. unsigned char sc = inb(KB_DATA);
  47. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  48. if (next != kbd_tail) {
  49. kbd_buf[kbd_head] = sc;
  50. kbd_head = next;
  51. }
  52. outb(0x20, 0x20);
  53. }
  54. static int kbd_buf_get(unsigned char *sc)
  55. {
  56. if (kbd_tail == kbd_head)
  57. return 0;
  58. *sc = kbd_buf[kbd_tail];
  59. kbd_tail = (kbd_tail + 1) % KBD_BUF_SIZE;
  60. return 1;
  61. }
  62. static char apply_shift(char c)
  63. {
  64. if (!shift_pressed) return c;
  65. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  66. switch ((unsigned char)c) {
  67. case '1': return '!'; case '2': return '@'; case '3': return '#';
  68. case '4': return '$'; case '5': return '%'; case '6': return '^';
  69. case '7': return '&'; case '8': return '*'; case '9': return '(';
  70. case '0': return ')'; case '-': return '_'; case '=': return '+';
  71. case '[': return '{'; case ']': return '}'; case ';': return ':';
  72. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  73. case ',': return '<'; case '.': return '>'; case '/': return '?';
  74. default: return c;
  75. }
  76. }
  77. static void hide_cursor_at(unsigned int pos)
  78. {
  79. unsigned int col = pos % io_cols;
  80. unsigned int row = pos / io_cols;
  81. fbtext_set_cursor(col, row);
  82. fbtext_hide_cursor((unsigned char)(cursor_cell & 0xFF), (unsigned char)(cursor_cell >> 8));
  83. }
  84. unsigned int io_get_cols(void)
  85. {
  86. return io_cols;
  87. }
  88. unsigned int io_get_rows(void)
  89. {
  90. return io_rows;
  91. }
  92. void io_init(void)
  93. {
  94. fbtext_init();
  95. io_cols = fbtext_get_cols();
  96. io_rows = fbtext_get_rows();
  97. io_clear_screen();
  98. cursor = IO_CONTENT_OFFSET;
  99. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  100. io_drain_keyboard();
  101. }
  102. void io_clear_screen(void)
  103. {
  104. fbtext_clear();
  105. }
  106. int io_did_scroll(void)
  107. {
  108. if (!scroll_occurred)
  109. return 0;
  110. scroll_occurred = 0;
  111. return 1;
  112. }
  113. void io_putchar(char c)
  114. {
  115. unsigned int ncols = io_cols;
  116. unsigned int content_off = ncols;
  117. unsigned int content_size = (io_rows - 1) * ncols;
  118. if (c == '\n') {
  119. hide_cursor_at(cursor);
  120. cursor = (cursor / ncols + 1) * ncols;
  121. if (cursor >= content_off + content_size) {
  122. fbtext_scroll_up_one_line();
  123. cursor = content_off + (io_rows - 2) * ncols;
  124. scroll_occurred = 1;
  125. }
  126. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  127. return;
  128. }
  129. if (c == '\b') {
  130. if (cursor > content_off) {
  131. hide_cursor_at(cursor);
  132. cursor--;
  133. {
  134. unsigned int col = cursor % ncols;
  135. unsigned int row = cursor / ncols;
  136. fbtext_put_char_at(col, row, ' ', VGA_ATTR);
  137. }
  138. cursor_cell = (unsigned short)((VGA_ATTR << 8) | ' ');
  139. }
  140. return;
  141. }
  142. if (cursor < content_off + content_size) {
  143. {
  144. unsigned int col = cursor % ncols;
  145. unsigned int row = cursor / ncols;
  146. fbtext_put_char_at(col, row, (unsigned char)c, VGA_ATTR);
  147. }
  148. cursor++;
  149. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  150. }
  151. }
  152. void io_putstr(const char *s)
  153. {
  154. while (*s) io_putchar(*s++);
  155. }
  156. void io_puthex(unsigned int n)
  157. {
  158. static const char hex[] = "0123456789ABCDEF";
  159. char buf[9];
  160. unsigned int i = 8;
  161. buf[8] = '\0';
  162. do {
  163. buf[--i] = hex[n & 0xF];
  164. n >>= 4;
  165. } while (i > 0);
  166. io_putstr(buf);
  167. }
  168. unsigned int io_get_cursor(void)
  169. {
  170. return cursor;
  171. }
  172. void io_set_cursor(unsigned int pos)
  173. {
  174. if (pos >= io_rows * io_cols)
  175. return;
  176. hide_cursor_at(cursor);
  177. cursor = pos;
  178. cursor_cell = (unsigned short)(VGA_ATTR << 8);
  179. fbtext_set_cursor(cursor % io_cols, cursor / io_cols);
  180. }
  181. void io_cursor_show_block(void)
  182. {
  183. fbtext_set_cursor(cursor % io_cols, cursor / io_cols);
  184. fbtext_show_cursor();
  185. }
  186. void io_cursor_hide_block(void)
  187. {
  188. fbtext_hide_cursor((unsigned char)(cursor_cell & 0xFF), (unsigned char)(cursor_cell >> 8));
  189. }
  190. void io_vga_write(unsigned int pos, unsigned short value)
  191. {
  192. unsigned int col = pos % io_cols;
  193. unsigned int row = pos / io_cols;
  194. fbtext_put_char_at(col, row, (unsigned char)(value & 0xFF), (unsigned char)(value >> 8));
  195. }
  196. void io_drain_keyboard(void)
  197. {
  198. while (inb(KB_STATUS) & 1)
  199. (void)inb(KB_DATA);
  200. kbd_head = kbd_tail = 0;
  201. kbd_extended = 0;
  202. }
  203. /* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
  204. static int kbd_process_byte(unsigned char sc)
  205. {
  206. if (sc == 0xE0) {
  207. kbd_extended = 1;
  208. return 0;
  209. }
  210. if (kbd_extended) {
  211. kbd_extended = 0;
  212. if (sc & 0x80)
  213. return 0; /* key release */
  214. if (sc == 0x4B) return IO_KEY_LEFT;
  215. if (sc == 0x4D) return IO_KEY_RIGHT;
  216. return 0;
  217. }
  218. if (sc & 0x80) {
  219. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  220. return 0;
  221. }
  222. if (sc == 0x2A || sc == 0x36) {
  223. shift_pressed = 1;
  224. return 0;
  225. }
  226. {
  227. char c = (char)scancode_to_ascii[sc];
  228. if (c) {
  229. last_scancode = sc;
  230. return (unsigned char)apply_shift(c);
  231. }
  232. }
  233. return 0;
  234. }
  235. int io_poll_key(void)
  236. {
  237. unsigned char sc;
  238. if (!kbd_buf_get(&sc))
  239. return 0;
  240. return kbd_process_byte(sc);
  241. }
  242. /* Like io_poll_key but also reads from port 0x60 when buffer is empty.
  243. For use by full-screen programs (e.g. space.bin) where IRQ may be missed on real HW. */
  244. int io_poll_key_with_hw(void)
  245. {
  246. unsigned char sc;
  247. if (kbd_tail == kbd_head) {
  248. if (!(inb(KB_STATUS) & 1))
  249. return 0;
  250. sc = inb(KB_DATA);
  251. } else {
  252. if (!kbd_buf_get(&sc))
  253. return 0;
  254. }
  255. return kbd_process_byte(sc);
  256. }
  257. unsigned char io_last_scancode(void)
  258. {
  259. return last_scancode;
  260. }