blob: 0cf04b275875c5a0e38283e37dc3133fa5bc799e [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];
swissChilif5448622021-03-08 20:17:36 -08007char special_key_mappings[LAST_KBD_KEY - FIRST_KBD_KEY] = {
swissChili19ef4182021-02-21 17:45:51 -08008 [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',
swissChili19ef4182021-02-21 17:45:51 -080012};
swissChilid8137922021-02-17 15:34:07 -080013
swissChilid8137922021-02-17 15:34:07 -080014void outb(ushort port, uchar val)
15{
swissChili825d46b2021-02-21 10:14:16 -080016 asm volatile("outb %1, %0" : : "dN"(port), "a"(val));
swissChilid8137922021-02-17 15:34:07 -080017}
18
19uchar inb(ushort port)
20{
21 uchar ret;
swissChili825d46b2021-02-21 10:14:16 -080022 asm volatile("inb %1, %0" : "=a"(ret) : "dN"(port));
swissChilid8137922021-02-17 15:34:07 -080023 return ret;
24}
25
26ushort inw(ushort port)
27{
28 ushort ret;
swissChili825d46b2021-02-21 10:14:16 -080029 asm volatile("inw %1, %0" : "=a"(ret) : "dN"(port));
swissChilid8137922021-02-17 15:34:07 -080030 return ret;
31}
32
33void *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 }
swissChili825d46b2021-02-21 10:14:16 -080039 return s;
swissChilid8137922021-02-17 15:34:07 -080040}
41
42void *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 }
swissChili825d46b2021-02-21 10:14:16 -080048 return dest;
swissChilid8137922021-02-17 15:34:07 -080049}
swissChili9b3584b2021-02-18 13:57:27 -080050
swissChilif5448622021-03-08 20:17:36 -080051uint strlen(char *a)
52{
53 int i = 0;
54 for (; *a; i++)
55 {
56 }
57
58 return i;
59}
60
61int strcmp(char *a, char *b)
62{
63 int al = strlen(a), bl = strlen(b);
64
65 if (al != bl)
66 return bl - al;
67
68 for (int i = 0; i < al; i++)
69 {
70 if (a[i] != b[i])
71 return -1;
72 }
73 return 0;
74}
75
swissChili9b3584b2021-02-18 13:57:27 -080076void io_wait()
77{
swissChili825d46b2021-02-21 10:14:16 -080078 asm volatile("outb %0, $0x80" ::"a"(0));
swissChili9b3584b2021-02-18 13:57:27 -080079}
swissChili19ef4182021-02-21 17:45:51 -080080
81uchar kbd_scan_code()
82{
83 return inb(KBD_DATA_PORT);
84}
85
86static 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
92void 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
110void init_kbd()
111{
112 memset(pressed_keys, 0, LAST_KBD_KEY);
113 add_interrupt_handler(33, kbd_handle_input);
114}