Skip to content

Commit a718089

Browse files
committed
[driver][keyboard] Keyboard is sending ScanCodes
1 parent 7683939 commit a718089

File tree

12 files changed

+206
-62
lines changed

12 files changed

+206
-62
lines changed

Makefile

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ BUILD_DIR = build
33
SRC_BOOTLOADER = src/bootloader
44
SRC_KERNEL = src/kernel
55
SRC_DRIVERS = src/drivers
6+
SRC_LIB_DS = src/lib/ds
67
SRC_LIB_UTILS = src/lib/utils
78
SRC_LIB = src/lib
89
SRC_APP = src/app
910

1011
BUILD_BOOTLOADER = build/bootloader
1112
BUILD_KERNEL = build/kernel
1213
BUILD_DRIVERS = build/drivers
14+
BUILD_LIB_DS = build/lib/ds
1315
BUILD_LIB_UTILS = build/lib/utils
1416
BUILD_LIB = build/lib
1517
BUILD_APP = build/app
@@ -111,7 +113,7 @@ $(kernel_core): $(SRC_KERNEL)/core.asm $(SRC_KERNEL)/core.c $(SRC_KERNEL)/essent
111113
nasm -o $(BUILD_KERNEL)/core_asm.o -f elf32 $(SRC_KERNEL)/core.asm
112114
nasm -o $(BUILD_KERNEL)/interrupts_asm.o -f elf32 $(SRC_KERNEL)/interrupts.asm
113115
gcc -m32 -fno-pie -c -Isrc -D KERNEL_MEMORY_LOCATION=$(KERNEL_MEMORY_LOCATION) -o $(BUILD_KERNEL)/core_c.o $(SRC_KERNEL)/core.c
114-
ld --oformat binary -m elf_i386 --trace -Ttext 0x0000 --strip-all -o $(kernel_core) $(BUILD_KERNEL)/core_asm.o $(BUILD_KERNEL)/core_c.o $(BUILD_KERNEL)/interrupts_asm.o $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/keyboard/libkeyboard $(BUILD_DRIVERS)/display/libtm_vga
116+
ld --oformat binary -m elf_i386 --trace -Ttext 0x0000 --strip-all -o $(kernel_core) $(BUILD_KERNEL)/core_asm.o $(BUILD_KERNEL)/core_c.o $(BUILD_KERNEL)/interrupts_asm.o $(BUILD_DRIVERS)/keyboard/libkeyboard $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga $(BUILD_LIB_DS)/libds
115117
truncate --size=%512 $(kernel_core)
116118

117119
# Libraries
@@ -133,11 +135,11 @@ $(BUILD_DRIVERS)/display/libtm_vga: $(SRC_DRIVERS)/display/text_mode_vga.c $(SRC
133135
nasm -o $(SRC_DRIVERS)/display/text_mode_vga_asm.o -f elf32 $(SRC_DRIVERS)/display/text_mode_vga.asm
134136
ar rc $@ $(BUILD_DRIVERS)/display/text_mode_vga_c.o $(SRC_DRIVERS)/display/text_mode_vga_asm.o
135137

136-
$(BUILD_DRIVERS)/keyboard/libkeyboard: $(SRC_DRIVERS)/keyboard/keyboard.cpp $(SRC_DRIVERS)/keyboard/keyboard.asm $(SRC_DRIVERS)/keyboard/keyboard.h $(SRC_LIB_UTILS)/time.h
138+
$(BUILD_DRIVERS)/keyboard/libkeyboard: $(SRC_DRIVERS)/keyboard/keyboard.c $(SRC_DRIVERS)/keyboard/keyboard.asm $(SRC_DRIVERS)/keyboard/keyboard.h $(SRC_DRIVERS)/keyboard/scancode_handler.c $(SRC_LIB_UTILS)/time.h $(SRC_LIB_DS)/queue.h $(BUILD_LIB_DS)/libds
137139
mkdir -p $(BUILD_DRIVERS)/keyboard/
138-
g++ -m32 -fno-pie -c -Isrc -o $(BUILD_DRIVERS)/keyboard/keyboard_c.o $(SRC_DRIVERS)/keyboard/keyboard.cpp
139-
nasm -o $(SRC_DRIVERS)/keyboard/keyboard_asm.o -f elf32 $(SRC_DRIVERS)/keyboard/keyboard.asm
140-
ar rc $@ $(BUILD_DRIVERS)/keyboard/keyboard_c.o $(SRC_DRIVERS)/keyboard/keyboard_asm.o
140+
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_DRIVERS)/keyboard/keyboard_c.o $(SRC_DRIVERS)/keyboard/keyboard.c
141+
nasm -o $(BUILD_DRIVERS)/keyboard/keyboard_asm.o -f elf32 $(SRC_DRIVERS)/keyboard/keyboard.asm
142+
ar rc $@ $(BUILD_DRIVERS)/keyboard/keyboard_c.o $(BUILD_DRIVERS)/keyboard/keyboard_asm.o
141143

142144
$(BUILD_LIB_UTILS)/libutils_16: $(SRC_LIB_UTILS)/io.c $(SRC_LIB_UTILS)/io.h $(SRC_LIB_UTILS)/string.c $(SRC_LIB_UTILS)/string.h $(SRC_LIB_UTILS)/disk.c $(SRC_LIB_UTILS)/disk.asm $(SRC_LIB_UTILS)/disk.h $(SRC_LIB_UTILS)/panic.c $(SRC_LIB_UTILS)/panic.h $(SRC_LIB_UTILS)/panic.asm $(SRC_LIB_UTILS)/time.c $(SRC_LIB_UTILS)/time.h $(SRC_LIB_UTILS)/time.asm $(SRC_LIB_UTILS)/color.c $(SRC_LIB_UTILS)/color.h
143145
mkdir -p $(BUILD_LIB_UTILS)/
@@ -165,6 +167,11 @@ $(BUILD_LIB_UTILS)/libutils: $(SRC_LIB_UTILS)/io.c $(SRC_LIB_UTILS)/io.h $(SRC_L
165167
nasm -o $(BUILD_LIB_UTILS)/time_asm.o -f elf32 $(SRC_LIB_UTILS)/time.asm
166168
ar rc $@ $(BUILD_LIB_UTILS)/io.o $(BUILD_LIB_UTILS)/string.o $(BUILD_LIB_UTILS)/color.o $(BUILD_LIB_UTILS)/disk_c.o $(BUILD_LIB_UTILS)/disk_asm.o $(BUILD_LIB_UTILS)/panic_c.o $(BUILD_LIB_UTILS)/panic_asm.o $(BUILD_LIB_UTILS)/time_c.o $(BUILD_LIB_UTILS)/time_asm.o
167169

170+
$(BUILD_LIB_DS)/libds: $(SRC_LIB_DS)/queue.h $(SRC_LIB_DS)/queue.c
171+
mkdir -p $(BUILD_LIB_DS)/
172+
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_LIB_DS)/queue.o $(SRC_LIB_DS)/queue.c
173+
ar rc $@ $(BUILD_LIB_DS)/queue.o
174+
168175
# User Applications
169176
$(app_calc): $(app_entry) $(SRC_APP)/calc.c $(SRC_LIB_UTILS)/io.h $(SRC_LIB_UTILS)/time.h $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga # And dependecies :/
170177
mkdir -p $$(dirname $(app_calc))

src/drivers/keyboard/keyboard.asm

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
global port_write
44
global port_read
5-
global enable_interrupts
6-
global disable_interrupts
75

86
[SECTION .text]
97
port_write:
@@ -29,11 +27,3 @@ global disable_interrupts
2927
mov esp, ebp
3028
pop ebp
3129
ret
32-
33-
enable_interrupts:
34-
STI
35-
ret
36-
37-
disable_interrupts:
38-
CLI
39-
ret
Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// UnderDevelopment: Not in functional state.
2-
// It's NOT Working :(...
3-
// Also keyboard is depending lib/utils which depends on drivers :(
41
#include <drivers/keyboard/keyboard.h>
2+
#include <lib/ds/queue.h>
53
#include <lib/utils/io.h>
64
#include <lib/utils/time.h>
75
#include <lib/utils/panic.h>
86

7+
#include "scancode_handler.c"
8+
99
#define DRIVERS_KEYBOARD_PORT_DATA 0x60
1010
#define DRIVERS_KEYBOARD_PORT_STATUS 0x64
1111
#define DRIVERS_KEYBOARD_PORT_COMMAND 0x64
@@ -16,14 +16,11 @@
1616
#define STATUS_SYSTEM_FLAG 0x4
1717
#define STATUS_CMD_DATA 0x8 // 0:cmd 1:data
1818

19-
20-
extern "C" {
21-
// Using USB Legacy Support
22-
extern void port_write(unsigned short port, unsigned char value);
23-
extern unsigned char port_read(unsigned short port);
24-
void __stack_chk_fail(void) {
25-
PANIC(0, "Kernel stack overflow!!!");
26-
}
19+
// Using USB Legacy Support
20+
extern void port_write(unsigned short port, unsigned char value);
21+
extern unsigned char port_read(unsigned short port);
22+
void __stack_chk_fail(void) {
23+
PANIC(0, "Kernel stack overflow!!!");
2724
}
2825

2926
void print_status(const char *message, int status) {
@@ -50,26 +47,36 @@ void keyboard_wait() {
5047
}
5148

5249
const int WAIT_FOR_STATUS_TIMEOUT = 1000000;
50+
const int WAIT_FOR_STATUS_KEYSCANCODE_TIMEOUT = 1000;
5351

54-
void wait_for_status_flag_timeout(unsigned char flag, unsigned char check_true, int timeout) {
52+
int wait_for_status_flag_timeout_low(unsigned char flag, unsigned char check_true, int timeout, int canpanic) {
5553
while(timeout>0) {
5654
if((((port_read(DRIVERS_KEYBOARD_PORT_STATUS))&flag)!=0)^check_true) {
5755
// waiting
5856
} else {
5957
// poll done
60-
return;
58+
return 1;
6159
}
6260
timeout--;
6361
}
62+
if(!canpanic) return 0;
6463
if(check_true) {
6564
PANIC(flag,"TIMEOUT: status bit set");
6665
} else {
6766
PANIC(flag,"TIMEOUT: status bit reset");
6867
}
6968
}
70-
71-
void wait_for_status_flag(unsigned char flag, unsigned char check_true) {
72-
wait_for_status_flag_timeout(flag, check_true, WAIT_FOR_STATUS_TIMEOUT);
69+
int wait_for_status_flag_timeout(unsigned char flag, unsigned char check_true, int timeout) {
70+
return wait_for_status_flag_timeout_low(flag, check_true, timeout, 1);
71+
}
72+
int wait_for_status_flag_nopanic(unsigned char flag, unsigned char check_true) {
73+
return wait_for_status_flag_timeout_low(flag, check_true, WAIT_FOR_STATUS_TIMEOUT, 0);
74+
}
75+
int wait_for_status_flag_nopanic_timeout(unsigned char flag, unsigned char check_true, int timeout) {
76+
return wait_for_status_flag_timeout_low(flag, check_true, timeout, 0);
77+
}
78+
int wait_for_status_flag(unsigned char flag, unsigned char check_true) {
79+
return wait_for_status_flag_timeout_low(flag, check_true, WAIT_FOR_STATUS_TIMEOUT, 1);
7380
}
7481

7582
unsigned char ps2_controller_send_command(unsigned char cmd, int want_reply) {
@@ -129,11 +136,45 @@ unsigned char read_data_reply() {
129136
wait_for_status_flag_timeout(STATUS_OUTPUT_BUFFER, 1, WAIT_FOR_STATUS_TIMEOUT);
130137
return port_read(DRIVERS_KEYBOARD_PORT_DATA);
131138
}
132-
extern "C" void enable_interrupts();
133-
extern "C" void disable_interrupts();
134139

140+
#define KEYBOARD_BUFFER_SIZE 64
141+
int keyboard_buffer[KEYBOARD_BUFFER_SIZE+3];
142+
143+
void keyboard_scanner_handle_buffer(int keyboard_buffer_queue[]);
144+
145+
void keyboard_scanner_init() {
146+
ASSERT( queue_init(keyboard_buffer, KEYBOARD_BUFFER_SIZE) );
147+
}
148+
149+
int keyboard_scanner_step() {
150+
int state_change = 0;
151+
int qc = queue_capacity(keyboard_buffer);
152+
int qs = queue_size(keyboard_buffer);
153+
while(qs<qc) {
154+
int got_response = wait_for_status_flag_nopanic_timeout(STATUS_OUTPUT_BUFFER, 1, WAIT_FOR_STATUS_KEYSCANCODE_TIMEOUT);
155+
if(!got_response) return;
156+
unsigned char out = read_data_reply();
157+
if(out == 0) return;
158+
state_change = 1;
159+
160+
queue_push(keyboard_buffer, out);
161+
qs++;
162+
}
163+
keyboard_scanner_handle_buffer(keyboard_buffer);
164+
return state_change;
165+
}
166+
167+
int fake_getch() {
168+
while(queue_size(keyboard_buffer) == 0) {
169+
// busy wait but checking on keyboard replies.
170+
keyboard_scanner_step();
171+
}
172+
int out = queue_front(keyboard_buffer);
173+
queue_pop(keyboard_buffer);
174+
return out;
175+
}
135176

136-
extern "C" void keyboard_init() {
177+
void keyboard_init() {
137178
sleep_mini(3000000);
138179

139180
unsigned char out;
@@ -192,9 +233,9 @@ extern "C" void keyboard_init() {
192233
}
193234

194235
// enable interrupts
195-
// out = ps2_controller_send_command(0X20, 1);
196-
// out |= 0b11;
197-
// ps2_controller_send_command_with_data(0x60, out, 0);
236+
out = ps2_controller_send_command(0X20, 1);
237+
out |= 0b11;
238+
ps2_controller_send_command_with_data(0x60, out, 0);
198239

199240

200241
// reset device
@@ -241,31 +282,28 @@ extern "C" void keyboard_init() {
241282
if (out != 0xFA) {
242283
PANIC(out, "caps failed");
243284
}
285+
// get scan code
286+
write_to_ps2_first_port(0xF0, 0);
287+
out = write_to_ps2_first_port(0, 1);
288+
if (out != 0xFA) {
289+
PANIC(out, "failed to get scan code");
290+
} else {
291+
// Not yet working
292+
// out = read_data_reply();
293+
// PANIC(out, "got get scan code");
294+
}
244295

245296
// set scan code
246297
write_to_ps2_first_port(0xF0, 0);
247-
out = write_to_ps2_first_port(1, 2);
298+
out = write_to_ps2_first_port(2, 1);
248299
if (out != 0xFA) {
249300
PANIC(out, "failed to set scan code");
250301
}
251-
// enable scanning
252-
write_to_ps2_first_port(0xF4, 0);
253-
254-
// get scan code
255-
// write_to_ps2_first_port(0xF0, 0);
256-
// out = write_to_ps2_first_port(0, 1);
257-
// if (out != 0xFA) {
258-
// PANIC(out, "failed to get scan code");
259-
// } else {
260-
// PANIC(out, "got get scan code");
261-
// }
262-
// write_to_ps2_first_port(0xF4, 0);
263302

303+
// enable scanning
264304
out = write_to_ps2_first_port(0xF4, 1);
265305
if (out != 0xFA) {
266306
PANIC(out, "scan failed");
267307
}
268-
269-
270-
PANIC(1, "end");
308+
keyboard_scanner_init();
271309
}

src/drivers/keyboard/keyboard.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#pragma once
2-
#ifdef __cplusplus
3-
extern "C" {
4-
#endif
52

63
void keyboard_init();
74

8-
#ifdef __cplusplus
9-
}
10-
#endif
5+
int fake_getch();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
void keyboard_scanner_handle_buffer(int keyboard_buffer_queue[]) {
2+
// Do Nothing for now.
3+
}

src/kernel/core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,13 @@ void entry_core() {
3636
print_line("Initializing Kernel...");
3737
populate_and_load_idt_table();
3838
kernel_enable_interrupts();
39+
keyboard_init();
40+
41+
move_xy(2,8);
42+
print_line("Keyboard ScanCode: ");
43+
while(1) {
44+
move_xy(21, 8);
45+
print_hex_int(fake_getch());
46+
}
3947
PANIC(501, "Kernel is under development!!!");
4048
}

src/kernel/interrupts.asm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ global kernel_enable_interrupts
66
global kernel_disable_interrupts
77
global idt_table
88

9+
global syscall_interrupt_handler_low
10+
extern syscall_interrupt_handler
11+
912
[SECTION .text]
1013

1114
interrupt_nohup:
@@ -29,3 +32,7 @@ global idt_table
2932
kernel_disable_interrupts:
3033
CLI
3134
ret
35+
36+
syscall_interrupt_handler_low:
37+
call syscall_interrupt_handler
38+
iret

src/kernel/interrupts.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#define IDT_SIZE 16
1+
#define IDT_SIZE 128
22

33
extern void interrupt_nohup();
44
extern void load_idt_table(unsigned int idtr_address);
@@ -53,10 +53,20 @@ void populate_idt_entry_32bit(int id,
5353
);
5454
}
5555

56+
extern void syscall_interrupt_handler_low();
57+
int x = 0;
58+
void syscall_interrupt_handler() {
59+
move_xy(4,7);
60+
print_line("Wierd syscall interrupt 0x64!!!! ");
61+
print_int(x);
62+
x++;
63+
}
64+
5665
void populate_and_load_idt_table() {
5766
for (int i = 0; i < IDT_SIZE; ++i) {
5867
populate_idt_entry_32bit(i, (unsigned int)interrupt_nohup, 0, 1);
5968
}
69+
populate_idt_entry_32bit(0x64, (unsigned int)syscall_interrupt_handler_low, 0, 1);
6070
idtr.size = sizeof(struct IDTEntry)*IDT_SIZE;
6171
idtr.base_address = ((int)idt_table + KERNEL_MEMORY_LOCATION);
6272

0 commit comments

Comments
 (0)