blob: 0ba13ae806aab93dd4d5b77680b4fc6bc06dd3f6 [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;
swissChilie5adca52021-06-16 21:00:31 -070029 asm("inw %1, %0" : "=a"(ret) : "dN"(port));
swissChilid8137922021-02-17 15:34:07 -080030 return ret;
31}
32
swissChili0d248832021-04-08 18:16:02 -070033void outl(ushort port, uint val)
34{
swissChilie5adca52021-06-16 21:00:31 -070035 asm("outl %1, %0" : : "dN"(port), "a"(val));
swissChili0d248832021-04-08 18:16:02 -070036}
37
38uint inl(ushort port)
39{
40 uint ret;
swissChilie5adca52021-06-16 21:00:31 -070041 asm("inl %1, %0" : "=a"(ret) : "dN"(port));
swissChili0d248832021-04-08 18:16:02 -070042 return ret;
43}
44
swissChilie5adca52021-06-16 21:00:31 -070045void outw(ushort port, ushort val)
46{
47 asm("outw %1, %0" :: "dN"(port), "a"(val));
48}
49
50void __attribute__((noinline)) nop()
51{
52 asm("nop");
53}
54
swissChilid8137922021-02-17 15:34:07 -080055void *memset(void *s, int c, size_t n)
56{
57 for (size_t i = 0; i < n; i++)
58 {
59 ((uchar *)s)[i] = c;
60 }
swissChili825d46b2021-02-21 10:14:16 -080061 return s;
swissChilid8137922021-02-17 15:34:07 -080062}
63
64void *memcpy(void *dest, const void *src, size_t n)
65{
66 for (size_t i = 0; i < n; i++)
67 {
68 ((uchar *)dest)[i] = ((uchar *)src)[i];
69 }
swissChili825d46b2021-02-21 10:14:16 -080070 return dest;
swissChilid8137922021-02-17 15:34:07 -080071}
swissChili9b3584b2021-02-18 13:57:27 -080072
swissChilie9289ee2021-03-20 21:54:28 -070073void strcpy(char *dest, char *src)
74{
75 memcpy(dest, src, strlen(src) + 1);
76}
77
swissChilif5448622021-03-08 20:17:36 -080078uint strlen(char *a)
79{
swissChilicfd3c3c2021-04-03 15:04:24 -070080 uint i = 0;
81 for (; a[i]; i++)
swissChilif5448622021-03-08 20:17:36 -080082 {
83 }
84
85 return i;
86}
87
88int strcmp(char *a, char *b)
89{
90 int al = strlen(a), bl = strlen(b);
91
92 if (al != bl)
93 return bl - al;
94
95 for (int i = 0; i < al; i++)
96 {
97 if (a[i] != b[i])
98 return -1;
99 }
100 return 0;
101}
102
swissChili19ef4182021-02-21 17:45:51 -0800103uchar kbd_scan_code()
104{
105 return inb(KBD_DATA_PORT);
106}
107
108static bool kbd_shift_pressed()
109{
110 return pressed_keys[KBD_LEFT_SHIFT] || pressed_keys[KBD_RIGHT_SHIFT] ||
111 pressed_keys[KBD_CAPS_LOCK];
112}
113
114void kbd_handle_input(struct registers *registers)
115{
116 uchar byte = kbd_scan_code();
117
118 struct kbd_scan_codes code = scan_code_set_1[byte];
119
120 if (code.ascii && !code.brk)
121 {
122 kprintf("%c", kbd_shift_pressed() && code.up_symbol ? code.up_symbol
123 : code.symbol);
124 }
125 else if (!code.brk && special_key_mappings[code.symbol - FIRST_KBD_KEY])
126 {
127 kprintf("%c", special_key_mappings[code.symbol - FIRST_KBD_KEY]);
128 }
129 pressed_keys[code.symbol] = !code.brk;
130}
131
132void init_kbd()
133{
134 memset(pressed_keys, 0, LAST_KBD_KEY);
135 add_interrupt_handler(33, kbd_handle_input);
136}
swissChili9bd74de2021-06-15 20:30:48 -0700137
138bool isdigit(char c)
139{
140 return c >= '0' && c <= '9';
141}
142
143uint parse_int(char *string)
144{
145 uint number = 0;
146
147 for (; isdigit(*string); string++)
148 {
149 number *= 10;
150 number += *string - '0';
151 }
152
153 return number;
154}