blob: 55bf7a73ca901fdd9add8ade1183d90ef7044e27 [file] [log] [blame]
swissChili9b3584b2021-02-18 13:57:27 -08001#include "io.h"
swissChili19ef4182021-02-21 17:45:51 -08002#include "kbd.h"
3#include "log.h"
4#include "pic.h"
5
6bool pressed_keys[LAST_KBD_KEY];
7char 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};
swissChilid8137922021-02-17 15:34:07 -080015
swissChilid8137922021-02-17 15:34:07 -080016void outb(ushort port, uchar val)
17{
swissChili825d46b2021-02-21 10:14:16 -080018 asm volatile("outb %1, %0" : : "dN"(port), "a"(val));
swissChilid8137922021-02-17 15:34:07 -080019}
20
21uchar inb(ushort port)
22{
23 uchar ret;
swissChili825d46b2021-02-21 10:14:16 -080024 asm volatile("inb %1, %0" : "=a"(ret) : "dN"(port));
swissChilid8137922021-02-17 15:34:07 -080025 return ret;
26}
27
28ushort inw(ushort port)
29{
30 ushort ret;
swissChili825d46b2021-02-21 10:14:16 -080031 asm volatile("inw %1, %0" : "=a"(ret) : "dN"(port));
swissChilid8137922021-02-17 15:34:07 -080032 return ret;
33}
34
35void *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 }
swissChili825d46b2021-02-21 10:14:16 -080041 return s;
swissChilid8137922021-02-17 15:34:07 -080042}
43
44void *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 }
swissChili825d46b2021-02-21 10:14:16 -080050 return dest;
swissChilid8137922021-02-17 15:34:07 -080051}
swissChili9b3584b2021-02-18 13:57:27 -080052
53void io_wait()
54{
swissChili825d46b2021-02-21 10:14:16 -080055 asm volatile("outb %0, $0x80" ::"a"(0));
swissChili9b3584b2021-02-18 13:57:27 -080056}
swissChili19ef4182021-02-21 17:45:51 -080057
58uchar kbd_scan_code()
59{
60 return inb(KBD_DATA_PORT);
61}
62
63static 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
69void 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
87void init_kbd()
88{
89 memset(pressed_keys, 0, LAST_KBD_KEY);
90 add_interrupt_handler(33, kbd_handle_input);
91}