Page 1 of 1
Keyboards and Hardware Interrupts
Posted: Sat Dec 01, 2012 9:36 pm
by Geometrian
Hi,
I'm trying to get basic keyboard input working in x86 protected mode.
I'm primarily confused about how this works. My first guess was to assume that there was a special hardware interrupt--the interrupt handler would save some state in some globals, and then the OS would process the key when it got a chance later.
However, I have not found any such examples of this happening, and in the examples I have found, I'm having trouble seeing how interrupts fit into it at all. For example, one OS simple asks for a key with inportb (as defined
here):
Code: Select all
while (!(inportb(0x64)&0x01)); // 0x64 is for reading the 8042 status register
rawkey = inportb(0x60);
Is this how it's generally supposed to be done? How do interrupts enter into it? Is there a code example that demonstrates?
Thanks,
Re: Keyboards and Hardware Interrupts
Posted: Sun Dec 02, 2012 3:59 am
by rdos
You can do it both ways. A single-tasking OS / application might just busy-poll the keyboard IO-ports instead of relying on a functional IRQ-system (I use this in the crash-debugger, where interrupts might be non-functional). In a multitasking OS, you need to use the interrupt method in order to be able to do other things than waiting for keyboard input.
Re: Keyboards and Hardware Interrupts
Posted: Sun Dec 02, 2012 7:22 am
by cxzuk
I assumed recently that interrupts were more complex than they really are; I found a recent quote which said
"We know that I/O device interface generates an interrupt request when SIN or SOUT bit in its status register is =1."[1]
So what the interrupt is doing is offloading this busy loop
while (!(inportb(0x64)&0x01)); // 0x64 is for reading the 8042 status register
rdos wrote: use this in the crash-debugger, where interrupts might be non-functional
+1, Good point. I like it.
[1]
http://en.wikipedia.org/wiki/Advanced_P ... Controller
Re: Keyboards and Hardware Interrupts
Posted: Sun Dec 02, 2012 2:16 pm
by mrvn
Geometrian wrote:Hi,
I'm trying to get basic keyboard input working in x86 protected mode.
I'm primarily confused about how this works. My first guess was to assume that there was a special hardware interrupt--the interrupt handler would save some state in some globals, and then the OS would process the key when it got a chance later.
However, I have not found any such examples of this happening, and in the examples I have found, I'm having trouble seeing how interrupts fit into it at all. For example, one OS simple asks for a key with inportb (as defined
here):
Code: Select all
while (!(inportb(0x64)&0x01)); // 0x64 is for reading the 8042 status register
rawkey = inportb(0x60);
Is this how it's generally supposed to be done? How do interrupts enter into it? Is there a code example that demonstrates?
Thanks,
Well, keyboard is a bit confusing and you need to do several things to use it. First you have to realize that the keyboard is connected to the PS/2 controler (or USB, but I wonb't go there). And the PS/2 controler is connected to the PIC (programmable interrupt controler) and the PIC is connected to the CPU. When all is setup correctly the PS/2 controler sends a clock pulse to the keyboard, the keyboard checks if you have pressed any key. If so it sends the scan code to the PS/2 controler. The PS/2 controller sends an interrupt to the PIC, the PIC sends an interrupt to the CPU and the CPU invokes the programmed interrupt. The ISR (interrupt service routine) then needs to read the keyboard scancode from the PS/2 controler (rawkey = inportb(0x60);) and needs to signal end-of-interrupt to the PIC before calling iret.
Look for the articles about keyboard, PS/2 and PIC on
http://wiki.osdev.org. But note one left out bit: You need to enable the clock signal for the port on the PS/2 controler. The wiki assumes it is on which isn't always the case,