Keyboards and Hardware Interrupts

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
Geometrian
Member
Member
Posts: 77
Joined: Tue Nov 20, 2012 4:45 pm
Contact:

Keyboards and Hardware Interrupts

Post 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,
rdos
Member
Member
Posts: 3276
Joined: Wed Oct 01, 2008 1:55 pm

Re: Keyboards and Hardware Interrupts

Post 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.
cxzuk
Member
Member
Posts: 164
Joined: Mon Dec 21, 2009 6:03 pm

Re: Keyboards and Hardware Interrupts

Post 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
mrvn
Member
Member
Posts: 43
Joined: Tue Mar 11, 2008 6:56 am

Re: Keyboards and Hardware Interrupts

Post 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,
Life - Don't talk to me about LIFE!
So long and thanks for all the fish.
Post Reply