blob: 1c10b5333e92e6fd0f8f9795e445413ef8071e08 [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
swissChilid98781b2021-07-25 21:04:17 -070033struct ext2_dir_contains_data
34{
35 char *name;
36 uint len;
37 bool found;
38};
39
swissChili0d248832021-04-08 18:16:02 -070040void outl(ushort port, uint val)
41{
swissChilie5adca52021-06-16 21:00:31 -070042 asm("outl %1, %0" : : "dN"(port), "a"(val));
swissChili0d248832021-04-08 18:16:02 -070043}
44
45uint inl(ushort port)
46{
47 uint ret;
swissChilie5adca52021-06-16 21:00:31 -070048 asm("inl %1, %0" : "=a"(ret) : "dN"(port));
swissChili0d248832021-04-08 18:16:02 -070049 return ret;
50}
51
swissChilie5adca52021-06-16 21:00:31 -070052void outw(ushort port, ushort val)
53{
54 asm("outw %1, %0" :: "dN"(port), "a"(val));
55}
56
57void __attribute__((noinline)) nop()
58{
59 asm("nop");
60}
61
swissChilid8137922021-02-17 15:34:07 -080062void *memset(void *s, int c, size_t n)
63{
64 for (size_t i = 0; i < n; i++)
65 {
66 ((uchar *)s)[i] = c;
67 }
swissChili825d46b2021-02-21 10:14:16 -080068 return s;
swissChilid8137922021-02-17 15:34:07 -080069}
70
71void *memcpy(void *dest, const void *src, size_t n)
72{
73 for (size_t i = 0; i < n; i++)
74 {
75 ((uchar *)dest)[i] = ((uchar *)src)[i];
76 }
swissChili825d46b2021-02-21 10:14:16 -080077 return dest;
swissChilid8137922021-02-17 15:34:07 -080078}
swissChili9b3584b2021-02-18 13:57:27 -080079
swissChilie9289ee2021-03-20 21:54:28 -070080void strcpy(char *dest, char *src)
81{
82 memcpy(dest, src, strlen(src) + 1);
83}
84
swissChilif5448622021-03-08 20:17:36 -080085uint strlen(char *a)
86{
swissChilicfd3c3c2021-04-03 15:04:24 -070087 uint i = 0;
88 for (; a[i]; i++)
swissChilif5448622021-03-08 20:17:36 -080089 {
90 }
91
92 return i;
93}
94
swissChilid98781b2021-07-25 21:04:17 -070095uint strnlen(char *s, size_t len)
96{
97 int i;
98
99 for (i = 0; s[i] && i < len; i++)
100 {
101 }
102
103 return i;
104}
105
106int strncmp(char *a, char *b, size_t len)
107{
108 int al = strnlen(a, len), bl = strnlen(b, len);
109
110 if (al != bl)
111 return bl - al;
112
113 for (int i = 0; i < al; i++)
114 {
115 if (a[i] != b[i])
116 return -1;
117 }
118 return 0;
119}
120
swissChilif5448622021-03-08 20:17:36 -0800121int strcmp(char *a, char *b)
122{
123 int al = strlen(a), bl = strlen(b);
124
125 if (al != bl)
126 return bl - al;
127
128 for (int i = 0; i < al; i++)
129 {
130 if (a[i] != b[i])
131 return -1;
132 }
133 return 0;
134}
135
swissChili19ef4182021-02-21 17:45:51 -0800136uchar kbd_scan_code()
137{
138 return inb(KBD_DATA_PORT);
139}
140
141static bool kbd_shift_pressed()
142{
143 return pressed_keys[KBD_LEFT_SHIFT] || pressed_keys[KBD_RIGHT_SHIFT] ||
144 pressed_keys[KBD_CAPS_LOCK];
145}
146
147void kbd_handle_input(struct registers *registers)
148{
149 uchar byte = kbd_scan_code();
150
151 struct kbd_scan_codes code = scan_code_set_1[byte];
152
153 if (code.ascii && !code.brk)
154 {
155 kprintf("%c", kbd_shift_pressed() && code.up_symbol ? code.up_symbol
156 : code.symbol);
157 }
158 else if (!code.brk && special_key_mappings[code.symbol - FIRST_KBD_KEY])
159 {
160 kprintf("%c", special_key_mappings[code.symbol - FIRST_KBD_KEY]);
161 }
162 pressed_keys[code.symbol] = !code.brk;
163}
164
165void init_kbd()
166{
167 memset(pressed_keys, 0, LAST_KBD_KEY);
168 add_interrupt_handler(33, kbd_handle_input);
169}
swissChili9bd74de2021-06-15 20:30:48 -0700170
171bool isdigit(char c)
172{
173 return c >= '0' && c <= '9';
174}
175
176uint parse_int(char *string)
177{
178 uint number = 0;
179
180 for (; isdigit(*string); string++)
181 {
182 number *= 10;
183 number += *string - '0';
184 }
185
186 return number;
187}