Page 1 of 2
How i use keyboard in my OS
Posted: Thu Feb 27, 2003 9:43 am
by Krisu
How could I use my keyboard in my OS?
I can write text to screen, but i don't know, how I could use keyboard.
Re:How i use keyboard in my OS
Posted: Thu Feb 27, 2003 9:50 am
by Omegaice
iv done this before in assembler and c++, it is in my code somewhere.
[attachment deleted by admin]
Re:How i use keyboard in my OS
Posted: Thu Feb 27, 2003 10:07 am
by Pype.Clicker
@OmegaIce : hmm ... if i can afford a comment, you should learn about strings (or maybe what you posted was very "Quick'n'Dirty" ...). all these single-char operations ...
@Krisu: basically, you have to read I/O port 0x60 when a keyboard interrupt (IRQ1) is received. Optionally, you can skip the "interrupt" part with a loop that checks the port state hasn't change.
The informations you get from port 0x60 are called SCAN CODES, which is raw numbers affected to each key of your keyboard. if key&0x80 is present, it means key&0x7F has been released, while if it's cleared, key&0x7f has been pressed.
Once you get the scancode, you still have to translate it into an ascii character if you plan to get a user input ... if all you want to do is waiting for [ESC] , [ENTER] , etc then you can skip that translation as well ...
I also suggest you try a search on the forum's archive (see the button at the top of the page) on the following keywords : 'keyboard' 'scancode' '0x60' ... you'll find plenty of older threads, including code, tutorials reference, etc.
Re:How i use keyboard in my OS
Posted: Thu Feb 27, 2003 10:11 am
by Omegaice
pype: I do know about strings but the bootloader i had originaly worked with c++ files but it had big problems with strings, they would not work at all
Re:How i use keyboard in my OS
Posted: Thu Feb 27, 2003 11:06 am
by Krisu
I'am making OS with C. I forgot tell it in my first message.
Re:How i use keyboard in my OS
Posted: Thu Feb 27, 2003 11:21 am
by richie
Is your kernel in Protected Mode or in Real Mode?
I think not your whole kernel is in C. At least the startup should be in asm. Write a simple int-handler for IRQ 1 and enable interrupts. If the user presses any key the int handler for irq 1 is called.
This is the first step. The next is to get the scancode. This is done by port 0x60. Now you have the scancode.
You need a kind of table for getting the ASCII-Value for this key. Move this ASCII-Value in a buffer und after a <ENTER> you have the string the user has typed in your buffer.
BTW: You should print every character that was typed in so that the user can see what he already has typed in. This is called echoing.
Re:How i use keyboard in my OS
Posted: Thu Feb 27, 2003 1:01 pm
by Krisu
I tried to enable interrupts, but it won't work.
When I enable interrupts, bochs give error message (real computer restart).
I tried enable interrupts with this code:
void enable_ints()
{
asm("sti");
};
Re:How i use keyboard in my OS
Posted: Thu Feb 27, 2003 1:20 pm
by richie
If you are in protected mode you first have to set up a IDT (Interrupt Descriptor Table). In real-mode you have to set up a IVT (Interrupt vector Table). You can also reprogram the PIC (Programable Interrupt Controller) to disable some hardware-interrupts.
Re:How i use keyboard in my OS
Posted: Fri Feb 28, 2003 12:51 am
by Pype.Clicker
http://www.mega-tokyo.com/forum/index.p ... eadid=3035
should learn you a bit more about enabling interrupts. If you still need more info, check the "Interrupts" section of
Intel System Programming Manual or
this tutorial @ bona fide OS dev
Re:How i use keyboard in my OS
Posted: Fri Feb 28, 2003 10:35 am
by Krisu
Is it possibly to use keyboard in protected mode (without interrupts enabled)?
Re:How i use keyboard in my OS
Posted: Fri Feb 28, 2003 10:55 am
by Whatever5k
Of course, why not.
When a user presses a key, your keyboard handler is called (that is IRQ1). Your handler reads the scancode from port 0x60, converts it into ASCII and puts it into a circular queue.
What's the problem here?
Re:How i use keyboard in my OS
Posted: Fri Feb 28, 2003 11:34 am
by Pype.Clicker
and if you wish to do it before interrupts are enabled, just do
Code: Select all
char k;
while ((k=inb(0x60))&0x80);
return k;
Re:How i use keyboard in my OS
Posted: Fri Feb 28, 2003 12:18 pm
by Krisu
Trouble again... DJGPP gives error message:
kernel_c.o(.text+0x126):kernel_c.c: undefined reference to `_inb'
What file I need to include or what I need to declare in my kernel?
Re:How i use keyboard in my OS
Posted: Fri Feb 28, 2003 12:38 pm
by richie
Hello
inb is no function in stdlib or somewhere else. What Pype.Clicker wants to say is that you have to read from port 0x60. The name of this function doesn't matter. But you have to write this function on your one. One possible solution would be this:
Code: Select all
unsigned char inb(unsigned short port)
{
unsigned char ret_val;
asm volatile("inb %w1,%b0"
: "=a"(ret_val)
: "d"(port));
return ret_val;
};
This works fine.
But the code Pype.Clicker posted is only useful for first steps in Kernel Programming. Since this loop hangs until a key is pressed you cannot do anything else in this time. This code is only useful if your kernel panics. Then you can disable all interrupts, print a panic massage on the screen and hang in such a loop. When the user preses any key you can then reboot. But for a normal keyboard driver it is better to use interrupts.
outportb
Posted: Sat Mar 01, 2003 11:48 am
by Mastermind
The inb function is good, but how do I use outb? A lot of the example code that I come across has either that or outportb(). Can you help me here?