io.c 7.3 KB

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