swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame] | 1 | #include "io.h" |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 2 | #include "kbd.h" |
| 3 | #include "log.h" |
| 4 | #include "pic.h" |
| 5 | |
| 6 | bool pressed_keys[LAST_KBD_KEY]; |
| 7 | char special_key_mappings[LAST_KBD_KEY - FIRST_KBD_KEY] = |
| 8 | { |
| 9 | [KBD_ENTER - FIRST_KBD_KEY] = '\n', |
| 10 | [KBD_SPACEBAR - FIRST_KBD_KEY] = ' ', |
| 11 | [KBD_TAB - FIRST_KBD_KEY] = '\t', |
| 12 | [KBD_BACKSPACE - FIRST_KBD_KEY] = '\b', |
| 13 | 0 |
| 14 | }; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 15 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 16 | void outb(ushort port, uchar val) |
| 17 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 18 | asm volatile("outb %1, %0" : : "dN"(port), "a"(val)); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | uchar inb(ushort port) |
| 22 | { |
| 23 | uchar ret; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 24 | asm volatile("inb %1, %0" : "=a"(ret) : "dN"(port)); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 25 | return ret; |
| 26 | } |
| 27 | |
| 28 | ushort inw(ushort port) |
| 29 | { |
| 30 | ushort ret; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 31 | asm volatile("inw %1, %0" : "=a"(ret) : "dN"(port)); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 32 | return ret; |
| 33 | } |
| 34 | |
| 35 | void *memset(void *s, int c, size_t n) |
| 36 | { |
| 37 | for (size_t i = 0; i < n; i++) |
| 38 | { |
| 39 | ((uchar *)s)[i] = c; |
| 40 | } |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 41 | return s; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void *memcpy(void *dest, const void *src, size_t n) |
| 45 | { |
| 46 | for (size_t i = 0; i < n; i++) |
| 47 | { |
| 48 | ((uchar *)dest)[i] = ((uchar *)src)[i]; |
| 49 | } |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 50 | return dest; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 51 | } |
swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame] | 52 | |
| 53 | void io_wait() |
| 54 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 55 | asm volatile("outb %0, $0x80" ::"a"(0)); |
swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame] | 56 | } |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 57 | |
| 58 | uchar kbd_scan_code() |
| 59 | { |
| 60 | return inb(KBD_DATA_PORT); |
| 61 | } |
| 62 | |
| 63 | static bool kbd_shift_pressed() |
| 64 | { |
| 65 | return pressed_keys[KBD_LEFT_SHIFT] || pressed_keys[KBD_RIGHT_SHIFT] || |
| 66 | pressed_keys[KBD_CAPS_LOCK]; |
| 67 | } |
| 68 | |
| 69 | void kbd_handle_input(struct registers *registers) |
| 70 | { |
| 71 | uchar byte = kbd_scan_code(); |
| 72 | |
| 73 | struct kbd_scan_codes code = scan_code_set_1[byte]; |
| 74 | |
| 75 | if (code.ascii && !code.brk) |
| 76 | { |
| 77 | kprintf("%c", kbd_shift_pressed() && code.up_symbol ? code.up_symbol |
| 78 | : code.symbol); |
| 79 | } |
| 80 | else if (!code.brk && special_key_mappings[code.symbol - FIRST_KBD_KEY]) |
| 81 | { |
| 82 | kprintf("%c", special_key_mappings[code.symbol - FIRST_KBD_KEY]); |
| 83 | } |
| 84 | pressed_keys[code.symbol] = !code.brk; |
| 85 | } |
| 86 | |
| 87 | void init_kbd() |
| 88 | { |
| 89 | memset(pressed_keys, 0, LAST_KBD_KEY); |
| 90 | add_interrupt_handler(33, kbd_handle_input); |
| 91 | } |