Skip to content

Commit 93bdd22

Browse files
author
Louis Jenkins
committed
Going to call it a day for today, I don't think it is as simple as I
thought... a shell I think will require processes, and can't be faked no matter what I want to do. I'll need to communicate between the driver and caller as well, and all of this will need to be done on the current stack as is. Meaning, I can't just "poll for input" because there is no other place BUT a variable on the stack to poll on. I was going to have the driver invoke a callback with the input received... but this is needlessly complicated. As well, this handler invoking handler after handler means that I will have interrupts disabled the ENTIRE time that the higher-level callback is invoked for, which also means that if it relies on another service that requires interrupts to run, it will just block indefinitely. So pretty much I will be implementing memory management next, most likely, and go from there.
1 parent bc47954 commit 93bdd22

File tree

6 files changed

+197
-179
lines changed

6 files changed

+197
-179
lines changed

src/kernel/drivers/kbd.c

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,120 @@
11
#include <include/kbd.h>
22
#include <include/vga.h>
3-
#include <stdbool.h>
43
#include <include/idt.h>
54
#include <include/io_port.h>
5+
#include <include/helpers.h>
6+
#include <stdbool.h>
7+
#include <stdio.h>
8+
9+
// Below are the two scan tables for normal scan codes, used to provide a mapping from scan code to character code.
10+
// A scan table entry of [0x01] = KBD_KEY_ESC would have a scan code of 0x01, and a character code of KBD_KEY_ESC.
11+
static const uint8_t KBD_SCAN_TABLE[KBD_NSCANS] = {
12+
[0x01] = KBD_KEY_ESC,
13+
[0x02] = KBD_KEY_1,
14+
[0x03] = KBD_KEY_2,
15+
[0x04] = KBD_KEY_3,
16+
[0x05] = KBD_KEY_4,
17+
[0x06] = KBD_KEY_5,
18+
[0x07] = KBD_KEY_6,
19+
[0x08] = KBD_KEY_7,
20+
[0x09] = KBD_KEY_8,
21+
[0x0A] = KBD_KEY_9,
22+
[0x0B] = KBD_KEY_0,
23+
[0x0C] = KBD_KEY_MINUS,
24+
[0x0D] = KBD_KEY_EQUALS,
25+
[0x0E] = KBD_KEY_BACKSPACE,
26+
[0x0F] = KBD_KEY_TAB,
27+
[0x10] = KBD_KEY_Q,
28+
[0x11] = KBD_KEY_W,
29+
[0x12] = KBD_KEY_E,
30+
[0x13] = KBD_KEY_R,
31+
[0x14] = KBD_KEY_T,
32+
[0x15] = KBD_KEY_Y,
33+
[0x16] = KBD_KEY_U,
34+
[0x17] = KBD_KEY_I,
35+
[0x18] = KBD_KEY_O,
36+
[0x19] = KBD_KEY_P,
37+
[0x1A] = KBD_KEY_LBRACKET,
38+
[0x1B] = KBD_KEY_RBRACKET,
39+
[0x1C] = KBD_KEY_ENTER,
40+
[0x1D] = KBD_KEY_LCTRL,
41+
[0x1E] = KBD_KEY_A,
42+
[0x1F] = KBD_KEY_S,
43+
[0x20] = KBD_KEY_D,
44+
[0x21] = KBD_KEY_F,
45+
[0x22] = KBD_KEY_G,
46+
[0x23] = KBD_KEY_H,
47+
[0x24] = KBD_KEY_J,
48+
[0x25] = KBD_KEY_K,
49+
[0x26] = KBD_KEY_L,
50+
[0x27] = KBD_KEY_SEMICOLON,
51+
[0x28] = KBD_KEY_APOSTROPH,
52+
[0x29] = KBD_KEY_GRAVE_ACCENT,
53+
[0x2A] = KBD_KEY_LSHIFT,
54+
[0x2B] = KBD_KEY_BACK_SLASH,
55+
[0x2C] = KBD_KEY_Z,
56+
[0x2D] = KBD_KEY_X,
57+
[0x2E] = KBD_KEY_C,
58+
[0x2F] = KBD_KEY_V,
59+
[0x30] = KBD_KEY_B,
60+
[0x31] = KBD_KEY_N,
61+
[0x32] = KBD_KEY_M,
62+
[0x33] = KBD_KEY_COMMA,
63+
[0x34] = KBD_KEY_PERIOD,
64+
[0x35] = KBD_KEY_SLASH,
65+
[0x36] = KBD_KEY_RSHIFT,
66+
[0x37] = KBD_KP_KEY_ASTERISK,
67+
[0x38] = KBD_KEY_LALT,
68+
[0x39] = KBD_KEY_SPACE,
69+
[0x3A] = KBD_KEY_CAPS_LOCK,
70+
[0x3B] = KBD_KEY_F1,
71+
[0x3C] = KBD_KEY_F2,
72+
[0x3D] = KBD_KEY_F3,
73+
[0x3E] = KBD_KEY_F4,
74+
[0x3F] = KBD_KEY_F5,
75+
[0x40] = KBD_KEY_F6,
76+
[0x41] = KBD_KEY_F7,
77+
[0x42] = KBD_KEY_F8,
78+
[0x43] = KBD_KEY_F9,
79+
[0x44] = KBD_KEY_F10,
80+
[0x45] = KBD_KEY_NUMLOCK,
81+
[0x46] = KBD_KEY_SCROLL_LOCK,
82+
[0x47] = KBD_KP_KEY_7,
83+
[0x48] = KBD_KP_KEY_8,
84+
[0x49] = KBD_KP_KEY_9,
85+
[0x4A] = KBD_KP_KEY_MINUS,
86+
[0x4B] = KBD_KP_KEY_4,
87+
[0x4C] = KBD_KP_KEY_5,
88+
[0x4D] = KBD_KP_KEY_6,
89+
[0x4E] = KBD_KP_KEY_PLUS,
90+
[0x4F] = KBD_KP_KEY_1,
91+
[0x50] = KBD_KP_KEY_2,
92+
[0x51] = KBD_KP_KEY_3,
93+
[0x52] = KBD_KP_KEY_0,
94+
[0x53] = KBD_KP_KEY_PERIOD,
95+
[0x57] = KBD_KEY_F11,
96+
[0x58] = KBD_KEY_F12
97+
};
98+
99+
// Escaped scan code sequences are multibyte, and begin with '0xE0'.
100+
static const uint8_t KBD_ESCAPED_SCAN_TABLE[KBD_NSCANS] = {
101+
[0x1C] = KBD_KP_KEY_ENTER,
102+
[0x1D] = KBD_KEY_RCTRL,
103+
[0x35] = KBD_KP_KEY_SLASH,
104+
[0x38] = KBD_KEY_RALT,
105+
[0x48] = KBD_KEY_UP,
106+
[0x49] = KBD_KEY_PAGE_UP,
107+
[0x4B] = KBD_KEY_LEFT,
108+
[0x4D] = KBD_KEY_RIGHT,
109+
[0x4F] = KBD_KEY_PAGE_DOWN,
110+
[0x50] = KBD_KEY_DOWN,
111+
[0x51] = KBD_KEY_PAGE_DOWN,
112+
[0x52] = KBD_KEY_INSERT,
113+
[0x53] = KBD_KEY_DELETE
114+
};
6115

7-
static uint8_t (*scan_to_char(uint8_t));
8116

117+
// Converts a character code to string
9118
static const char *to_string(uint8_t code) {
10119
const char *str;
11120
switch (code) {
@@ -287,6 +396,7 @@ static const char *to_string(uint8_t code) {
287396
return str;
288397
}
289398

399+
// Converts escaped character codes to string
290400
static const char *escaped_to_string(uint8_t code) {
291401
const char *str;
292402
switch (code) {
@@ -331,9 +441,14 @@ static const char *escaped_to_string(uint8_t code) {
331441
return str;
332442
}
333443

444+
// Keyboard state, which drives the statement machine in handler
334445
static uint8_t state = 0;
335446

336-
static void keyboard_irq_handler(struct registers *regs) {
447+
// A simple (and temporary) handler that reads input from the hardware (keyboard)
448+
// and converts it into it's respective character codes.
449+
// Temporary: It currently prints out the key being pressed but that is because
450+
// I hav enot implemented a more adequate way of doing this.
451+
static void keyboard_irq_handler(struct registers *UNUSED(regs)) {
337452
// Get what key has been pressed
338453
uint8_t scancode = inb(KBD_PORT);
339454

@@ -343,26 +458,29 @@ static void keyboard_irq_handler(struct registers *regs) {
343458
return;
344459
}
345460

461+
// All scan codes signify that a key has been released by setting the most significant bit
346462
bool released = scancode & 0x80;
347463
scancode &= ~0x80;
348464

465+
// On each key press, we write prettily to the same line.
349466
vga_clear();
350467
vga_set_x(0);
351468
printf("Scancode: %x, You %s: ", scancode, released ? "Released" : "Pressed");
352469

470+
// State-Based machine, which determines which scan table to decode from.
353471
switch (state) {
354472
// Waiting for first byte
355473
case 0:
356474
printf("%s", to_string(KBD_SCAN_TABLE[scancode]));
357475
return;
358-
// Wait
359-
ing for second byte of multibyte sequence
476+
// Waiting for second byte of multibyte sequence
360477
case 1:
361478
printf("%s", escaped_to_string(KBD_ESCAPED_SCAN_TABLE[scancode]));
362479
state = 0;
363480
}
364481
}
365482

483+
// Simple initializer that registers IRQ handler
366484
void keyboard_init() {
367485
register_interrupt_handler(IRQ1, keyboard_irq_handler);
368486
}

src/kernel/drivers/rtc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static uint8_t as_binary(uint8_t bcd) {
1313
}
1414

1515
void rtc_init() {
16-
// TODO
16+
// TODO: Register IRQ handler (IRQ8)
1717
}
1818

1919
uint8_t rtc_get_second() {

src/kernel/include/helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define MOLTAROS_HELPERS_H
33

44
#define CEILING(x,y) (((x) + (y) - 1) / (y))
5+
#define UNUSED(x) (__attribute__((__unused__)) x)
56

67
#endif /* end MLTAROS_HELPERS_H */

src/kernel/include/kbd.h

Lines changed: 4 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
#define KBD_NSCANS 0x80
99
#define KBD_ESCAPED_NSCANS 0xE0
1010

11+
1112
/*
12-
Character Codes: The layout is based on my work laptop's keyboard layout.
13+
A minimal keyboard driver that conforms to my laptop's keyboard layout.
1314
*/
15+
16+
// Below is all character codes for the given buttons.
1417
enum {
1518
KBD_KEY_ESC = 0x1,
1619
KBD_KEY_F1,
@@ -116,110 +119,6 @@ enum {
116119
KBD_KP_KEY_PERIOD
117120
};
118121

119-
static const uint8_t KBD_SCAN_TABLE[KBD_NSCANS] = {
120-
[0x01] = KBD_KEY_ESC,
121-
[0x02] = KBD_KEY_1,
122-
[0x03] = KBD_KEY_2,
123-
[0x04] = KBD_KEY_3,
124-
[0x05] = KBD_KEY_4,
125-
[0x06] = KBD_KEY_5,
126-
[0x07] = KBD_KEY_6,
127-
[0x08] = KBD_KEY_7,
128-
[0x09] = KBD_KEY_8,
129-
[0x0A] = KBD_KEY_9,
130-
[0x0B] = KBD_KEY_0,
131-
[0x0C] = KBD_KEY_MINUS,
132-
[0x0D] = KBD_KEY_EQUALS,
133-
[0x0E] = KBD_KEY_BACKSPACE,
134-
[0x0F] = KBD_KEY_TAB,
135-
[0x10] = KBD_KEY_Q,
136-
[0x11] = KBD_KEY_W,
137-
[0x12] = KBD_KEY_E,
138-
[0x13] = KBD_KEY_R,
139-
[0x14] = KBD_KEY_T,
140-
[0x15] = KBD_KEY_Y,
141-
[0x16] = KBD_KEY_U,
142-
[0x17] = KBD_KEY_I,
143-
[0x18] = KBD_KEY_O,
144-
[0x19] = KBD_KEY_P,
145-
[0x1A] = KBD_KEY_LBRACKET,
146-
[0x1B] = KBD_KEY_RBRACKET,
147-
[0x1C] = KBD_KEY_ENTER,
148-
[0x1D] = KBD_KEY_LCTRL,
149-
[0x1E] = KBD_KEY_A,
150-
[0x1F] = KBD_KEY_S,
151-
[0x20] = KBD_KEY_D,
152-
[0x21] = KBD_KEY_F,
153-
[0x22] = KBD_KEY_G,
154-
[0x23] = KBD_KEY_H,
155-
[0x24] = KBD_KEY_J,
156-
[0x25] = KBD_KEY_K,
157-
[0x26] = KBD_KEY_L,
158-
[0x27] = KBD_KEY_SEMICOLON,
159-
[0x28] = KBD_KEY_APOSTROPH,
160-
[0x29] = KBD_KEY_GRAVE_ACCENT,
161-
[0x2A] = KBD_KEY_LSHIFT,
162-
[0x2B] = KBD_KEY_BACK_SLASH,
163-
[0x2C] = KBD_KEY_Z,
164-
[0x2D] = KBD_KEY_X,
165-
[0x2E] = KBD_KEY_C,
166-
[0x2F] = KBD_KEY_V,
167-
[0x30] = KBD_KEY_B,
168-
[0x31] = KBD_KEY_N,
169-
[0x32] = KBD_KEY_M,
170-
[0x33] = KBD_KEY_COMMA,
171-
[0x34] = KBD_KEY_PERIOD,
172-
[0x35] = KBD_KEY_SLASH,
173-
[0x36] = KBD_KEY_RSHIFT,
174-
[0x37] = KBD_KP_KEY_ASTERISK,
175-
[0x38] = KBD_KEY_LALT,
176-
[0x39] = KBD_KEY_SPACE,
177-
[0x3A] = KBD_KEY_CAPS_LOCK,
178-
[0x3B] = KBD_KEY_F1,
179-
[0x3C] = KBD_KEY_F2,
180-
[0x3D] = KBD_KEY_F3,
181-
[0x3E] = KBD_KEY_F4,
182-
[0x3F] = KBD_KEY_F5,
183-
[0x40] = KBD_KEY_F6,
184-
[0x41] = KBD_KEY_F7,
185-
[0x42] = KBD_KEY_F8,
186-
[0x43] = KBD_KEY_F9,
187-
[0x44] = KBD_KEY_F10,
188-
[0x45] = KBD_KEY_NUMLOCK,
189-
[0x46] = KBD_KEY_SCROLL_LOCK,
190-
[0x47] = KBD_KP_KEY_7,
191-
[0x48] = KBD_KP_KEY_8,
192-
[0x49] = KBD_KP_KEY_9,
193-
[0x4A] = KBD_KP_KEY_MINUS,
194-
[0x4B] = KBD_KP_KEY_4,
195-
[0x4C] = KBD_KP_KEY_5,
196-
[0x4D] = KBD_KP_KEY_6,
197-
[0x4E] = KBD_KP_KEY_PLUS,
198-
[0x4F] = KBD_KP_KEY_1,
199-
[0x50] = KBD_KP_KEY_2,
200-
[0x51] = KBD_KP_KEY_3,
201-
[0x52] = KBD_KP_KEY_0,
202-
[0x53] = KBD_KP_KEY_PERIOD,
203-
[0x57] = KBD_KEY_F11,
204-
[0x58] = KBD_KEY_F12
205-
};
206-
207-
static const uint8_t KBD_ESCAPED_SCAN_TABLE[KBD_NSCANS] = {
208-
[0x1C] = KBD_KP_KEY_ENTER,
209-
[0x1D] = KBD_KEY_RCTRL,
210-
[0x35] = KBD_KP_KEY_SLASH,
211-
[0x38] = KBD_KEY_RALT,
212-
[0x48] = KBD_KEY_UP,
213-
[0x49] = KBD_KEY_PAGE_UP,
214-
[0x4B] = KBD_KEY_LEFT,
215-
[0x4D] = KBD_KEY_RIGHT,
216-
[0x4F] = KBD_KEY_PAGE_DOWN,
217-
[0x50] = KBD_KEY_DOWN,
218-
[0x51] = KBD_KEY_PAGE_DOWN,
219-
[0x52] = KBD_KEY_INSERT,
220-
[0x53] = KBD_KEY_DELETE
221-
};
222-
223122
void keyboard_init();
224123

225124
#endif /* endif MOLTAROS_KEYBOARD_H */

src/tmp/MoltarOS.iso

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)