Skip to content

Commit 187ccbb

Browse files
committed
Trying to add PS/2 keyboard but not yet functional :(
1 parent 1537cb2 commit 187ccbb

File tree

12 files changed

+226
-41
lines changed

12 files changed

+226
-41
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ $(bt_stage2): $(SRC_BOOTLOADER)/stage2.asm $(SRC_BOOTLOADER)/stage2.c $(SRC_BOOT
105105
ld --oformat binary -m elf_i386 -Ttext 0x8000 --strip-all -o $@ $(BUILD_BOOTLOADER)/stage2_asm.o $(BUILD_BOOTLOADER)/stage2_c.o $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_bios
106106
truncate --size=%512 $@
107107

108-
$(kernel_core): $(SRC_KERNEL)/core.asm $(SRC_KERNEL)/core.c $(SRC_KERNEL)/essentials.c $(SRC_LIB_UTILS)/io.h $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga # And other io.h dependecies -_-
108+
$(kernel_core): $(SRC_KERNEL)/core.asm $(SRC_KERNEL)/core.c $(SRC_KERNEL)/essentials.c $(SRC_LIB_UTILS)/io.h $(SRC_DRIVERS)/keyboard/keyboard.h $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/keyboard/libkeyboard $(BUILD_DRIVERS)/display/libtm_vga # And other io.h dependecies -_-
109109
mkdir -p $$(dirname $(kernel_core))
110110
nasm -o $(BUILD_KERNEL)/core_asm.o -f elf32 $(SRC_KERNEL)/core.asm
111-
gcc -m16 -fno-pie -c -D__SOURCE_SNAPSHOT__=$(SOURCE_SNAPSHOT) -Isrc -o $(BUILD_KERNEL)/core_c.o $(SRC_KERNEL)/core.c
112-
ld --oformat binary -m elf_i386 -Ttext 0xC000 --strip-all -o $(kernel_core) $(BUILD_KERNEL)/core_asm.o $(BUILD_KERNEL)/core_c.o $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga
111+
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_KERNEL)/core_c.o $(SRC_KERNEL)/core.c
112+
ld --oformat binary -m elf_i386 --trace -Ttext 0xC000 --strip-all -o $(kernel_core) $(BUILD_KERNEL)/core_asm.o $(BUILD_KERNEL)/core_c.o $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/keyboard/libkeyboard $(BUILD_DRIVERS)/display/libtm_vga
113113
truncate --size=%512 $(kernel_core)
114114

115115
# Libraries
@@ -130,7 +130,7 @@ $(BUILD_DRIVERS)/display/libtm_vga: $(SRC_DRIVERS)/display/text_mode_vga.c $(SRC
130130
nasm -o $(SRC_DRIVERS)/display/text_mode_vga_asm.o -f elf32 $(SRC_DRIVERS)/display/text_mode_vga.asm
131131
ar rc $@ $(BUILD_DRIVERS)/display/text_mode_vga_c.o $(SRC_DRIVERS)/display/text_mode_vga_asm.o
132132

133-
$(BUILD_DRIVERS)/keyboard/libkeyboard: $(SRC_DRIVERS)/keyboard/keyboard.c $(SRC_DRIVERS)/keyboard/keyboard.asm
133+
$(BUILD_DRIVERS)/keyboard/libkeyboard: $(SRC_DRIVERS)/keyboard/keyboard.c $(SRC_DRIVERS)/keyboard/keyboard.asm $(SRC_DRIVERS)/keyboard/keyboard.h $(SRC_LIB_UTILS)/time.h
134134
mkdir -p $(BUILD_DRIVERS)/keyboard/
135135
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_DRIVERS)/keyboard/keyboard_c.o $(SRC_DRIVERS)/keyboard/keyboard.c
136136
nasm -o $(SRC_DRIVERS)/keyboard/keyboard_asm.o -f elf32 $(SRC_DRIVERS)/keyboard/keyboard.asm
@@ -143,7 +143,7 @@ $(BUILD_LIB_UTILS)/libutils: $(SRC_LIB_UTILS)/io.c $(SRC_LIB_UTILS)/io.h $(SRC_L
143143
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/color.o $(SRC_LIB_UTILS)/color.c
144144
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/disk_c.o $(SRC_LIB_UTILS)/disk.c
145145
nasm -o $(BUILD_LIB_UTILS)/disk_asm.o -f elf32 $(SRC_LIB_UTILS)/disk.asm
146-
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/panic_c.o $(SRC_LIB_UTILS)/panic.c
146+
gcc -m16 -fno-pie -c -D__SOURCE_SNAPSHOT__=$(SOURCE_SNAPSHOT) -Isrc -o $(BUILD_LIB_UTILS)/panic_c.o $(SRC_LIB_UTILS)/panic.c
147147
nasm -o $(BUILD_LIB_UTILS)/panic_asm.o -f elf32 $(SRC_LIB_UTILS)/panic.asm
148148
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_LIB_UTILS)/time_c.o $(SRC_LIB_UTILS)/time.c
149149
nasm -o $(BUILD_LIB_UTILS)/time_asm.o -f elf32 $(SRC_LIB_UTILS)/time.asm

src/app/dashboard.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int call_main(unsigned short cs,unsigned short ip, int argc, char *argv[]) {
3939
return ((int (*) (int, char *[]))(unsigned int)ip)(argc, argv);
4040
}
4141

42-
int run_dashboard(int argc,char **argv) {
42+
int main(int argc,char **argv) {
4343
while(1) {
4444
print_board();
4545
move_xy(3+sizeof(query_app_number)-1,3);

src/bootloader/stage2.asm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ global label_exit
3939
mov eax, cr0
4040
or eax, 0x00000001
4141
mov cr0, eax
42+
;jmp 0x08:0x0000 ; address to kernel in memory
4243
jmp 0x08:long_jump_enter_protected
4344

4445
label_exit:
@@ -53,13 +54,12 @@ global label_exit
5354

5455
[BITS 16]
5556
long_jump_enter_protected:
56-
57+
; this won't be required here in future.
5758
mov ax, 0x10
5859
mov ds, ax
5960
mov ss, ax
6061
mov es, ax
6162
mov fs, ax
6263
mov gs, ax
63-
6464
; Hardcoded Kernel Load Address
6565
jmp 0x08:0xC000

src/bootloader/stage2.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ int populate_gdt_table() {
5252
populate_gct_entry(
5353
&gdt_table[0],
5454
0,0,0,0);
55+
// Kernel Code Segment Selector
5556
populate_gct_entry(
5657
&gdt_table[1],
5758
0x00000000,0x0fffffff,
59+
// 0x0000C000,0x0fffffff,
5860
0b00000000,
5961
0x9a);
62+
// Kernel Data Segment Selector
6063
populate_gct_entry(
6164
&gdt_table[2],
6265
0x00000000,0x0fffffff,
66+
// 0x000000C000,0x0fffffff,
6367
0b00000000,
6468
0x92);
6569

src/drivers/keyboard/keyboard.c

Lines changed: 191 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
// UnderDevelopment: Not in functional state.
2+
// It's NOT Working :(...
23
// Also keyboard is depending lib/utils which depends on drivers :(
4+
#include <drivers/keyboard/keyboard.h>
35
#include <lib/utils/io.h>
6+
#include <lib/utils/time.h>
47
#include <lib/utils/panic.h>
58

69
#define DRIVERS_KEYBOARD_PORT_DATA 0x60
710
#define DRIVERS_KEYBOARD_PORT_STATUS 0x64
811
#define DRIVERS_KEYBOARD_PORT_COMMAND 0x64
912

13+
14+
#define STATUS_OUTPUT_BUFFER 0x1
15+
#define STATUS_INPUT_BUFFER 0x2
16+
#define STATUS_SYSTEM_FLAG 0x4
17+
#define STATUS_CMD_DATA 0x8 // 0:cmd 1:data
18+
19+
1020
// Using USB Legacy Support
1121
extern void port_write(unsigned short port, unsigned char value);
1222
extern unsigned char port_read(unsigned short port);
@@ -24,36 +34,195 @@ void print_status(const char *message, int status) {
2434
}
2535
}
2636

37+
void sleep_mini(int s) {
38+
while(s>0) {
39+
s--;
40+
}
41+
}
42+
void wait_for_status_flag(unsigned char flag, unsigned char check_true) {
43+
while((((port_read(DRIVERS_KEYBOARD_PORT_STATUS))&flag)!=0)^check_true);
44+
}
45+
void wait_for_status_flag_timeout(unsigned char flag, unsigned char check_true, int timeout) {
46+
while(timeout>0) {
47+
if((((port_read(DRIVERS_KEYBOARD_PORT_STATUS))&flag)!=0)^check_true) {
48+
// waiting
49+
} else {
50+
// poll done
51+
return;
52+
}
53+
timeout--;
54+
}
55+
if(check_true) {
56+
PANIC(flag,"TIMEOUT: status bit set");
57+
} else {
58+
PANIC(flag,"TIMEOUT: status bit reset");
59+
}
60+
}
61+
62+
unsigned char send_command(unsigned char cmd, int want_reply) {
63+
unsigned char out;
64+
sleep_mini(100000);
65+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, cmd);
66+
sleep_mini(100000);
67+
if(!want_reply) {
68+
return 0;
69+
}
70+
wait_for_status_flag(STATUS_OUTPUT_BUFFER, 1);
71+
// wait_for_status_flag(STATUS_INPUT_BUFFER, 0);
72+
sleep_mini(100000);
73+
unsigned char data = port_read(DRIVERS_KEYBOARD_PORT_DATA);
74+
return data;
75+
}
76+
77+
unsigned char send_command_with_data(unsigned char cmd, unsigned char data, int want_reply) {
78+
unsigned char out;
79+
80+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, cmd);
81+
sleep_mini(1000000);
82+
wait_for_status_flag(STATUS_INPUT_BUFFER, 0);
83+
port_write(DRIVERS_KEYBOARD_PORT_DATA, data);
84+
sleep_mini(1000000);
85+
if(!want_reply) {
86+
return 0;
87+
}
88+
sleep_mini(1000000);
89+
wait_for_status_flag(STATUS_OUTPUT_BUFFER, 1);
90+
data = port_read(DRIVERS_KEYBOARD_PORT_DATA);
91+
return data;
92+
}
93+
unsigned char read_reply() {
94+
sleep_mini(1000000);
95+
wait_for_status_flag_timeout(STATUS_OUTPUT_BUFFER, 1, 1000000);
96+
return port_read(DRIVERS_KEYBOARD_PORT_DATA);
97+
}
98+
99+
unsigned char write_to_ps2_first_port(unsigned char byte, int want_reply) {
100+
unsigned char out;
101+
for(int i=0;i<3;i++) {
102+
wait_for_status_flag_timeout(STATUS_INPUT_BUFFER, 0, 1000000);
103+
port_write(DRIVERS_KEYBOARD_PORT_DATA, byte);
104+
sleep_mini(1000000);
105+
if(!want_reply) {
106+
return 0;
107+
}
108+
wait_for_status_flag_timeout(STATUS_OUTPUT_BUFFER, 1, 1000000);
109+
out = port_read(DRIVERS_KEYBOARD_PORT_DATA);
110+
if(out!=0xFE) {
111+
break;
112+
}
113+
}
114+
return out;
115+
}
116+
void write_to_ps2_second_port(unsigned char byte) {
117+
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0x64);
118+
sleep_mini(1000000);
119+
wait_for_status_flag_timeout(STATUS_INPUT_BUFFER, 0, 1000000);
120+
port_write(DRIVERS_KEYBOARD_PORT_DATA, byte);
121+
sleep_mini(1000000);
122+
}
123+
27124
void keyboard_init() {
28-
print_status("Loading Keyboard Drivers...", 0);
125+
sleep_mini(30000000);
29126
unsigned char out;
30-
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xAD);
31-
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xA7);
32-
while(1) {
33-
// clear the buffer
34-
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
35-
if(!(out&1)) break;
36-
}
37-
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0x20);
38-
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0x60);
39-
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
127+
128+
// disable PS/2 first port
129+
send_command(0XAD, 0);
130+
// disable PS/2 second port
131+
send_command(0XA7, 0);
132+
// flush output buffer
133+
wait_for_status_flag(STATUS_OUTPUT_BUFFER, 0);
134+
135+
out = send_command(0X20, 1);
136+
out = out&(~0b1000011);
40137
int second_port_exists = 0;
41-
if(out&(0b10000)) {
138+
if (out & (0b10000)) {
42139
second_port_exists = 1;
43-
print_status("Loading Keyboard Drivers: Second Port Exists", 0);
44140
}
45-
// clear 0, 1 and 6.
46-
// test bit 5
47-
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xAA);
48-
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
141+
print_status("PS2 Port Count is ", 1+second_port_exists);
142+
send_command_with_data(0x60, out, 0);
143+
out = send_command(0XAA, 1);
49144
if (out != 0x55) {
50145
PANIC(out, "PS2 self test failed.");
51146
}
52-
// enable PS2 first port.
53-
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xAE);
54-
port_write(DRIVERS_KEYBOARD_PORT_COMMAND, 0xFF);
55-
out = port_read(DRIVERS_KEYBOARD_PORT_STATUS);
147+
148+
if(second_port_exists) {
149+
send_command(0XA8, 0);
150+
out = send_command(0X20, 1);
151+
if (!(out & (0b10000))) {
152+
// should be clear
153+
second_port_exists = 0;
154+
send_command(0xA7, 0);
155+
print_status("PS2 second port disabled DNWAIS.", 0);
156+
} else {
157+
// can't have dual channel
158+
print_status("PS2 can't have dual channel.", 0);
159+
}
160+
}
161+
out = send_command(0XAB, 1);
162+
if (out != 0x00) {
163+
PANIC(out, "PS2 first port test failed.");
164+
}
165+
if(second_port_exists) {
166+
out = send_command(0XA9, 1);
167+
if (out != 0x00) {
168+
PANIC(out, "PS2 second port test failed.");
169+
}
170+
}
171+
// enable first port
172+
write_to_ps2_first_port(0xAE, 0);
173+
if(second_port_exists) {
174+
// enable second port
175+
write_to_ps2_second_port(0xA8);
176+
}
177+
// reset device
178+
out = write_to_ps2_first_port(0xFF, 1);
56179
if (out != 0xFA) {
57-
PANIC(out, "PS2 reset failed.");
180+
PANIC(out, "reset ps/2 first failed");
58181
}
182+
// if(second_port_exists) {
183+
// // reset second port
184+
// write_to_ps2_first_port(0xFF);
185+
// out = read_reply();
186+
// if (out != 0xFA) {
187+
// PANIC(out, "reset ps/2 second failed");
188+
// }
189+
// }
190+
191+
// scan code
192+
// write_to_ps2_first_port(0xF0, 0);
193+
// out = write_to_ps2_first_port(0, 1);
194+
// if (out != 0xFA) {
195+
// PANIC(out, "failed to get scan code");
196+
// } else {
197+
// PANIC(out, "got get scan code");
198+
// }
199+
// write_to_ps2_first_port(0xF4);
200+
201+
// Caps
202+
write_to_ps2_first_port(0xED, 0);
203+
out = write_to_ps2_first_port(7, 1);
204+
if (out != 0xFA) {
205+
PANIC(out, "caps failed");
206+
}
207+
// write_to_ps2_first_port(0xF4);
208+
// out = read_reply();
209+
// if (out != 0xFA) {
210+
// PANIC(out, "scan failed");
211+
// }
212+
213+
// // detect device type
214+
out = write_to_ps2_first_port(0xF5, 1);
215+
if (out != 0xFA) {
216+
PANIC(out, "disable scanning failed.");
217+
}
218+
out = write_to_ps2_first_port(0xF2, 1);
219+
if (out != 0xFA) {
220+
PANIC(out, "device identity failed.");
221+
}
222+
// 0xab
223+
unsigned char dev_d0 = read_reply();
224+
unsigned char dev_d1 = read_reply();
225+
PANIC(dev_d0, "device id");
226+
227+
PANIC(1, "end");
59228
}

src/drivers/keyboard/keyboard.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void keyboard_init();

src/kernel/core.asm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ extern entry_core
44
global __low_va_args
55

66
[SECTION .text]
7+
mov ax, 0x10
8+
mov ds, ax
9+
mov ss, ax
10+
mov es, ax
11+
mov fs, ax
12+
mov gs, ax
713
jmp entry_core
814

915
__low_va_args:

src/kernel/core.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <drivers/keyboard/keyboard.h>
12
#include <kernel/essentials.c>
23
#include <lib/utils/io.h>
34
#include <lib/utils/panic.h>
@@ -27,5 +28,6 @@ void entry_core() {
2728

2829
move_xy(6,22);
2930
print_line("Initializing Kernel...");
30-
PANIC(501, "Kernel is under construction!!!");
31+
keyboard_init();
32+
PANIC(501, "Kernel is under development!!!");
3133
}

src/lib/utils/disk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// TODO: Fix diff 1 value in DISK_KERNEL_SECTOR_START
44
#define DISK_KERNEL_SECTOR_START 78
5-
#define DISK_KERNEL_SECTOR_COUNT 25
5+
#define DISK_KERNEL_SECTOR_COUNT 25 //33
66

77
int load_sectors(unsigned short address,
88
unsigned char drive,

src/lib/utils/panic.asm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[BITS 16]
22

3-
global __low_panic_halt
3+
global panic_just_halt
44

55
[SECTION .text]
6-
__low_panic_halt:
6+
panic_just_halt:
77
HLT
8-
JMP __low_panic_halt
8+
JMP panic_just_halt

0 commit comments

Comments
 (0)