Using Keyboard in OS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
HRR

Using Keyboard in OS

Post by HRR »

I have written a kernel in C which can display text. How can I use kayboard in my Kernel ? I also want help about how to port (or duplicate) printf. Please help. I am 15 years old.
Ranieri

Re: Using Keyboard in OS

Post by Ranieri »

You can poll port 0x60 and read the character that is currently being pressed or released.
There's plenty of documentation on that floating around, just remember that keyboard scancodes != ASCII.

What you eventually really need to do (considering the wastefulness of busy looping) is build an ISR for interrupt 8 (== IRQ 1).
This handler will get called every time soemthing happens to the keyboard.

Easiest way by far to deal with it is then update an internal memory image of the keyboard to reflect what keys have been pressed/released. THen you can basically treat the keyboard as a memory mapped device in the rest of your OS.

Hope this helps a bit, ranieri
hrr

Re: Using Keyboard in OS

Post by hrr »

Thanks for your reply. But my kernel is in Protected Mode. How can I use keyboard in protected mode ?
Chris Giese

Re: Using Keyboard in OS

Post by Chris Giese »

>I have written a kernel in C which can display text. How can I use kayboard in my Kernel ?

Hook the IRQ 1 keyboard interrupt. IRQ 1 is INT 9 unless
you reprogram the 8259 interrupt controller chips. (You
should reprogram these chips in a pmode OS.)

You _must_ read I/O port 60h after each keyboard interrupt,
or you will not get any more keyboard interrupts.

The value you read from port 60h depends on the key
pressed, key up or down, "scancode set", NumLock state,
phase of the moon, etc. Here are some tables and code:

http://www.execpc.com/~geezer/osd/kbd/index.htm#scan
http://www.execpc.com/~geezer/osd/kbd/kbd.c

>I also want help about how to port (or duplicate) printf.

If you get rid of floating point and locales (e.g. different
languages), then printf() is a lot simpler.

http://www.execpc.com/~geezer/osd/libc/ ... tm#problem
hrr

Re: Using Keyboard in OS

Post by hrr »

I tried to use the port 0x60. The code was as follows: (It is a part of the main file)

Code: Select all

unsigned char ky,ky1;
ky1=inportb(0x60);
do
{
 ?ky=inportb(0x60);
}while(ky==ky1);
This code returns the same value of the variable ky whichever key is pressed. Is this code correct ? What modifications should I make so that I can get the scancode of the key pressed ?
Please Help.
hrr

Re: Using Keyboard in OS

Post by hrr »

The code now works properly. The problem was with another part of the kernel.  :)
Post Reply