Page 1 of 1

help getting user input from keyboard, and reading writing..

Posted: Sat Dec 02, 2006 3:32 pm
by R2_
Hi, Im currently developing a small kernel. So far I got printf() function but I do not know how to get keyboard input, nor get my kernel to install into the hard drive. I have written small kernels for PDA devices before never for x86 so any help would be greatly appreciated.

I seen in a lot of tutorials the use of outb() or something like that functions which they say can be used to access the keyboard but they never explain it nor show how they work so this leads to another question... is it a gcc built in function header that gets added when compiled (I doubt it) or how do you code it ?

Im good at C and assembler, but I do not want to use BIOS for keyboard input.

Posted: Sat Dec 02, 2006 4:10 pm
by Dex
This will get a key press

Code: Select all

        xor   eax,eax
      	in    al,0x60
But you need to have your keyboard irq, link in to your keyboard code, for a proper OS setup, have you got your idt setup yet ?.

Posted: Sat Dec 02, 2006 4:18 pm
by R2_
sorry I posted this in wrong section..


how would I implement this into C ?

asm( xor eax,eax );
asm( in al,0x60 );

?

sorry as I said Im a newbie
thnx for response.

Posted: Sat Dec 02, 2006 4:52 pm
by Brynet-Inc
1) You have to install a IRQ handler for the keyboard...
2) And you need a Scancode table..
3) Handle every event like the shift key.. caps lock, keyboard lights, etc.

To simplify reading from I/O ports, A function like this can be used with GCC.

Code: Select all

/*
This is used for reading I/O ports to get data from devices such as the
keyboard.
*/
inline unsigned char inportb(unsigned short p)
{
	unsigned char rv;
	__asm__ __volatile__("inb %%dx, %%al" : "=a" (rv) : "d" (p));
	return rv;
}
For simply reading from the keyboard, It can be done sorta like this..

Code: Select all

unsigned char scancode;
scancode = inportb(0x60);
if (scancode & 0x80)
{
	....
}
You should read a few tutorials...

In any senses it does take time, learning about the languages and the architecture your writing for is usually something you should do first.

Posted: Sat Dec 02, 2006 5:00 pm
by R2_
thanks a lot that really helped, about this tutorials can you post any ? I haven't been able to find any on OS dev or I/O on the Intel architecture.

Posted: Sat Dec 02, 2006 5:03 pm
by Brynet-Inc
Just noticed you posted you don't know what IDT/IRQ is.. Your beyond all help.. :lol:

Read here - http://www.osdev.org/phpBB2/viewtopic.php?p=85475#85475

Posted: Sat Dec 02, 2006 7:28 pm
by R2_
I read the
http://www.osdev.org/osfaq2/index.php/S ... eFunctions
website but I can't figure out how to enable the IRQ

static __inline__
void irqUnlock(int no)
{
/* Val, Port */
if (no>7) outb(0x20,0xa0);
outb(0x20,0x20);
}

^^ I was thinking that was the way to do it but my IRQ check function still says it's disabled... I passed number 8 to it.. don't know if thats right..

Can anyone recommend any books that cover all this ?

Posted: Tue Dec 05, 2006 11:16 am
by TheQuux
This took me forever as well...

To enable IRQ's, you need to write an 8-bit mask to port 0x21 (for the lower 8 IRQs) and another to 0xA1 (for the upper 8 IRQs).

For each mask, if a bit is clear (0), the IRQ is enabled

Then, to enable interrupts, use "sti"

Now, if your computer reboots, or bochs complains about a "third chance exception without resolution", you forgot your IDT...

About documentation; google is your friend.

For intel processors (and AMD, for that matter), go to http://www.intel.com/products/processor ... /index.htm

Download all of them; they are all useful.

For other hardware, good luck; I STILL haven't found anything on my NVidia graphics card...