Now with a working PS2 driver, kinda!

alentours-dev
Sergiusz Bazanski 2011-03-13 18:32:05 +01:00
parent c25939efc6
commit 42361f56cb
6 changed files with 187 additions and 11 deletions

View File

@ -0,0 +1,23 @@
#ifndef __KBD_LAYOUT_H__
#define __KBD_LAYOUT_H__
#include "types.h"
enum E_KBD_SPECIAL {
E_KBD_ESC,
E_KBD_ENTER,
E_KBD_LCTRL,
E_KBD_RCTRL,
E_KBD_LSHIFT,
E_KBD_RSHIFT,
E_KBD_LALT,
E_KBD_RALT,
} typedef T_KBD_KEY;
typedef u8 * T_KBD_LAYOUT;
void kbd_layout_set(T_KBD_LAYOUT Layout, u8 Length);
void kbd_layout_set_default(void);
s8 kbd_layout_translate(u8 Key, u8 Special);
#endif

View File

@ -1,6 +1,13 @@
#ifndef __PS2_H__
#define __PS2_H__
#define PS2_SPECIAL_CTRL 1
#define PS2_SPECIAL_ALT 2
#define PS2_SPECIAL_SHIFT 4
#define PS2_SPECIAL_META 8
#define PS2_SPECIAL_HYPER 16
s8 ps2_getc(void);
u8 ps2_wait_key(void);
u8 ps2_poll_key(void);
void ps2_keyboard_isr(void);

127
src/Tier0/kbd_layout.c Normal file
View File

@ -0,0 +1,127 @@
#include "Tier0/kbd_layout.h"
#include "Tier0/ps2.h"
u8 g_kbd_layout_default[] = {
// 0x00
0xFF, 0xFF,
0xFF, 0xFF,
'1', '!',
'2', '@',
'3', '#',
'4', '$',
'5', '%',
'6', '^',
'7', '&',
'8', '*',
'9', '(',
'0', ')',
'-', '_',
'=', '+',
0xFF, 0xFF,
' ', ' ',
// 0x10
'q', 'Q',
'w', 'W',
'e', 'E',
'r', 'R',
't', 'T',
'y', 'Y',
'u', 'U',
'i', 'I',
'o', 'O',
'p', 'P',
'[', '{',
']', '}',
'\n', '\n',
0xFF, 0xFF,
'a', 'A',
's', 'S',
// 0x20
'd', 'D',
'f', 'F',
'g', 'G',
'h', 'H',
'j', 'J',
'k', 'K',
'l', 'L',
';', ':',
'\'', '"',
'`', '~',
0xFF, 0xFF,
'\\', '|',
'z', 'Z',
'x', 'X',
'c', 'C',
'v', 'V',
// 0x30
'b', 'B',
'n', 'N',
'm', 'M',
',', '<',
'.', '>',
'/', '?',
0xFF, 0xFF,
'*', '*',
0xFF, 0xFF,
' ', ' ',
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
// 0x40
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
'7', '7',
'8', '8',
'9', '9',
'-', '-',
'4', '4',
'5', '5',
'6', '6',
'+', '+',
'1', '1',
// 0x50
'2', '2',
'3', '3',
'0', '0',
'.', '.',
0xFF, 0xFF
};
T_KBD_LAYOUT g_kbd_layout_current;
u8 g_kbd_layout_current_length;
void kbd_layout_set(T_KBD_LAYOUT Layout, u8 Length)
{
g_kbd_layout_current = Layout;
g_kbd_layout_current_length = Length;
}
void kbd_layout_set_default(void)
{
kbd_layout_set(g_kbd_layout_default, 0x54 * 2);
}
s8 kbd_layout_translate(u8 Key, u8 Special)
{
if (Key > g_kbd_layout_current_length)
return 0x00;
u8 Shift = (Special & PS2_SPECIAL_SHIFT) > 0;
s8 Mapped = g_kbd_layout_current[Key * 2 + Shift];
return (Mapped == 0xFF ? 0x00 : Mapped);
}

View File

@ -7,6 +7,7 @@
#include "Tier0/ps2.h"
#include "Tier0/system.h"
#include "Tier0/pic.h"
#include "Tier0/kbd_layout.h"
void interrupts_irq_sample(void);
@ -50,13 +51,16 @@ void kmain(void *MultibootHeader, u32 Magic)
interrupts_init_simple();
pic_init(0, 0);
ps2_init_simple();
kbd_layout_set_default();
__asm__ volatile("sti");
kprintf("[i] Hardware interrupts are now enabled.\n");
while(1)
{
u8 Key = ps2_wait_key();
kprintf("[i] Received key %u.\n", Key);
s8 c = ps2_getc();
kprintf("%c", c);
}
LOOPFOREVER;

View File

@ -197,7 +197,7 @@ void kputch(s8 Character)
VideoMemory[Offset] = Character;
VideoMemory[Offset+1] = 0x0F;
if (g_kstdio_cur_x + 1 >= 80)
kmove_cursor(0, g_kstdio_cur_y);
kmove_cursor(0, g_kstdio_cur_y + 1);
else
kmove_cursor(g_kstdio_cur_x + 1, g_kstdio_cur_y);
}

View File

@ -5,6 +5,7 @@
#include "Tier0/ps2.h"
#include "Tier0/kstdio.h"
#include "Tier0/interrupts.h"
#include "Tier0/kbd_layout.h"
#define PS2_KBD_IRQ
#define PS2_KBD_DATA 0x60
@ -14,13 +15,24 @@
#define PS2_D_TO_P(x) (x - 128)
#define PS2_P_TO_D(x) (x | 128)
u8 ps2_shift_pressed = 0;
u8 ps2_ctrl_pressed = 0;
u8 ps2_special = 0;
u8 ps2_key_pressed = 0;
u8 ps2_key = 0;
u8 ps2_key_new = 0;
s8 ps2_getc(void)
{
while(1)
{
u8 Key = ps2_wait_key();
s8 Character = kbd_layout_translate(Key, ps2_special);
if (Character != 0)
return Character;
}
}
u8 ps2_poll_key(void)
{
if (ps2_key_pressed)
@ -30,8 +42,11 @@ u8 ps2_poll_key(void)
u8 ps2_wait_key(void)
{
while (!ps2_key_pressed && !ps2_key_new)
{}
while(1)
{
if (ps2_key_pressed && ps2_key_new)
break;
}
ps2_key_new = 0;
return ps2_key;
}
@ -39,8 +54,8 @@ u8 ps2_wait_key(void)
void _ps2_keyboard_key_pressed(u8 Code)
{
if (Code == PS2_KEY_SHIFT)
ps2_shift_pressed = 1;
else if (ps2_key_pressed == 0)
ps2_special |= PS2_SPECIAL_SHIFT;
else// if (ps2_key_pressed == 0)
{
ps2_key_pressed = 1;
ps2_key = Code;
@ -51,8 +66,8 @@ void _ps2_keyboard_key_pressed(u8 Code)
void _ps2_keyboard_key_depressed(u8 Code)
{
if (Code == PS2_KEY_SHIFT)
ps2_shift_pressed = 0;
else if (ps2_key_pressed == 1 && Code == ps2_key)
ps2_special &= ~PS2_SPECIAL_SHIFT;
else if (Code == ps2_key)
ps2_key_pressed = 0;
}