How i use keyboard in my OS
How i use keyboard in my OS
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.
I can write text to screen, but i don't know, how I could use keyboard.
Re:How i use keyboard in my OS
iv done this before in assembler and c++, it is in my code somewhere.
[attachment deleted by admin]
[attachment deleted by admin]
- Pype.Clicker
- 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
@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.
@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
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
I'am making OS with C. I forgot tell it in my first message.
Re:How i use keyboard in my OS
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.
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
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");
};
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
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.
- Pype.Clicker
- 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
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
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
Is it possibly to use keyboard in protected mode (without interrupts enabled)?
Re:How i use keyboard in my OS
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?
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?
- Pype.Clicker
- 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
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
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?
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
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:
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.
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;
};
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.