Skip to content

Commit

Permalink
Start implementing keyboard driver
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgian committed Apr 6, 2023
1 parent 91ad83d commit eef4092
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
2 changes: 1 addition & 1 deletion disk.layout
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ device: disk.img
unit: sectors
sector-size: 512

disk.img1 : start= 2048, size= 2048, type=0, bootable
disk.img1 : start= 2048, size= 2048, type=0
disk.img2 : start= 4096, size= 32768, type=0
disk.img3 : start= 36864, size= 94208, type=6
2 changes: 2 additions & 0 deletions kernel/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub extern "C" fn exception_handler(int: u32, eip: u32, cs: u32, eflags: u32) {
}
}
println!("EIP: {:X}, CS: {:X}, EFLAGS: {:b}", eip, cs, eflags);

loop {}
}

#[naked]
Expand Down
67 changes: 64 additions & 3 deletions kernel/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ use crate::pic::PICS;
use core::arch::asm;

pub const KEYBOARD_INT: u8 = 33;
pub const KEYBAORD_CONTROLLER: u8 = 0x60;

pub static mut KEYBOARD: Keyboard = Keyboard { lshift: false };

pub struct Keyboard {
lshift: bool,
}

//keyboard handler
#[naked]
Expand All @@ -16,10 +23,64 @@ pub extern "C" fn keyboard_handler() {
//read scancode from keyboard controller
let scancode: u8;
unsafe {
asm!("in al, dx", out("al") scancode, in("dx") 0x60 as u16);
asm!("in al, dx", out("al") scancode, in("dx") KEYBAORD_CONTROLLER as u16);
}

println!("KEYBOARD INTERRUPT! Scancode: {:X} ", scancode);

//notify pics end of interrupt
PICS.end_interrupt(KEYBOARD_INT);

unsafe {
if scancode == 0x2a {
KEYBOARD.lshift = true;
return;
}

if scancode == 0xaa {
KEYBOARD.lshift = false;
return;
}
}

//print char
let key = scancode_to_char(scancode);

if key != '\0' {
print!("{}", key);
}
}

fn scancode_to_char(scancode: u8) -> char {
let chars = "1234567890qwertyuiopasdfghjklzxcvbnm".as_bytes();

let diff;
match scancode {
0x02..=0x0b => diff = 2,
0x10..=0x19 => diff = 6,
0x1e..=0x26 => diff = 10,
0x2c..=0x32 => diff = 15,
_ => diff = 0,
}

let index = (scancode - diff) as usize;

let mut key;

if index < 36 {
key = chars[index];

unsafe {
if KEYBOARD.lshift {
key -= 0x20;
}
}
} else {
key = 0x00;
}

//space
if scancode == 0x39 {
key = 0x20;
}

key as char
}
4 changes: 4 additions & 0 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ pub extern "C" fn _start() -> ! {
asm!("sti");
}

/*unsafe {
asm!("ud2");
}*/

//halt cpu while waiting for interrupts
loop {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/pic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Pic {

//check if pic is handling interrupt
pub fn handles_interrupt(&self, interupt: u8) -> bool {
self.offset <= interupt && interupt < self.offset + 8
self.offset <= interupt && interupt < self.offset + IRQ_COUNT
}
}

Expand Down

0 comments on commit eef4092

Please sign in to comment.