How i use keyboard in my 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.
Krisu

How i use keyboard in my OS

Post 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.
Omegaice

Re:How i use keyboard in my OS

Post by Omegaice »

iv done this before in assembler and c++, it is in my code somewhere.

[attachment deleted by admin]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How i use keyboard in my OS

Post 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.
Omegaice

Re:How i use keyboard in my OS

Post 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
Krisu

Re:How i use keyboard in my OS

Post by Krisu »

I'am making OS with C. I forgot tell it in my first message.
richie

Re:How i use keyboard in my OS

Post 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.
Krisu

Re:How i use keyboard in my OS

Post 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");
};
richie

Re:How i use keyboard in my OS

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How i use keyboard in my OS

Post 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
Krisu

Re:How i use keyboard in my OS

Post by Krisu »

Is it possibly to use keyboard in protected mode (without interrupts enabled)?
Whatever5k

Re:How i use keyboard in my OS

Post 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?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:How i use keyboard in my OS

Post 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;
Krisu

Re:How i use keyboard in my OS

Post 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?
richie

Re:How i use keyboard in my OS

Post 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.
Mastermind

outportb

Post 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?
Post Reply