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]; |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 7 | char special_key_mappings[LAST_KBD_KEY - FIRST_KBD_KEY] = { |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 8 | [KBD_ENTER - FIRST_KBD_KEY] = '\n', |
| 9 | [KBD_SPACEBAR - FIRST_KBD_KEY] = ' ', |
| 10 | [KBD_TAB - FIRST_KBD_KEY] = '\t', |
| 11 | [KBD_BACKSPACE - FIRST_KBD_KEY] = '\b', |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 12 | }; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 13 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 14 | void outb(ushort port, uchar val) |
| 15 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 16 | asm volatile("outb %1, %0" : : "dN"(port), "a"(val)); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | uchar inb(ushort port) |
| 20 | { |
| 21 | uchar ret; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 22 | asm volatile("inb %1, %0" : "=a"(ret) : "dN"(port)); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 23 | return ret; |
| 24 | } |
| 25 | |
| 26 | ushort inw(ushort port) |
| 27 | { |
| 28 | ushort ret; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 29 | asm volatile("inw %1, %0" : "=a"(ret) : "dN"(port)); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 30 | return ret; |
| 31 | } |
| 32 | |
| 33 | void *memset(void *s, int c, size_t n) |
| 34 | { |
| 35 | for (size_t i = 0; i < n; i++) |
| 36 | { |
| 37 | ((uchar *)s)[i] = c; |
| 38 | } |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 39 | return s; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void *memcpy(void *dest, const void *src, size_t n) |
| 43 | { |
| 44 | for (size_t i = 0; i < n; i++) |
| 45 | { |
| 46 | ((uchar *)dest)[i] = ((uchar *)src)[i]; |
| 47 | } |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 48 | return dest; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 49 | } |
swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame] | 50 | |
swissChili | e9289ee | 2021-03-20 21:54:28 -0700 | [diff] [blame] | 51 | void strcpy(char *dest, char *src) |
| 52 | { |
| 53 | memcpy(dest, src, strlen(src) + 1); |
| 54 | } |
| 55 | |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 56 | uint strlen(char *a) |
| 57 | { |
swissChili | cfd3c3c | 2021-04-03 15:04:24 -0700 | [diff] [blame] | 58 | uint i = 0; |
| 59 | for (; a[i]; i++) |
swissChili | f544862 | 2021-03-08 20:17:36 -0800 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
| 63 | return i; |
| 64 | } |
| 65 | |
| 66 | int strcmp(char *a, char *b) |
| 67 | { |
| 68 | int al = strlen(a), bl = strlen(b); |
| 69 | |
| 70 | if (al != bl) |
| 71 | return bl - al; |
| 72 | |
| 73 | for (int i = 0; i < al; i++) |
| 74 | { |
| 75 | if (a[i] != b[i]) |
| 76 | return -1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 81 | uchar kbd_scan_code() |
| 82 | { |
| 83 | return inb(KBD_DATA_PORT); |
| 84 | } |
| 85 | |
| 86 | static bool kbd_shift_pressed() |
| 87 | { |
| 88 | return pressed_keys[KBD_LEFT_SHIFT] || pressed_keys[KBD_RIGHT_SHIFT] || |
| 89 | pressed_keys[KBD_CAPS_LOCK]; |
| 90 | } |
| 91 | |
| 92 | void kbd_handle_input(struct registers *registers) |
| 93 | { |
| 94 | uchar byte = kbd_scan_code(); |
| 95 | |
| 96 | struct kbd_scan_codes code = scan_code_set_1[byte]; |
| 97 | |
| 98 | if (code.ascii && !code.brk) |
| 99 | { |
| 100 | kprintf("%c", kbd_shift_pressed() && code.up_symbol ? code.up_symbol |
| 101 | : code.symbol); |
| 102 | } |
| 103 | else if (!code.brk && special_key_mappings[code.symbol - FIRST_KBD_KEY]) |
| 104 | { |
| 105 | kprintf("%c", special_key_mappings[code.symbol - FIRST_KBD_KEY]); |
| 106 | } |
| 107 | pressed_keys[code.symbol] = !code.brk; |
| 108 | } |
| 109 | |
| 110 | void init_kbd() |
| 111 | { |
| 112 | memset(pressed_keys, 0, LAST_KBD_KEY); |
| 113 | add_interrupt_handler(33, kbd_handle_input); |
| 114 | } |