interrupt example

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.
Post Reply
Poseidon

interrupt example

Post by Poseidon »

Could somebody give me an example of a keyboard interrupt?

Thanx in advance
pini

Re:interrupt example

Post by pini »

It depends on what you want to store. Do you want scancodes only or do you want ASCII chars ?
I'm using the following scheme (the buffer is ring-buffer):

Code: Select all

Check if buffer is not full
Read scancode (port 0x60)
Check if scancode is extended value (0xE0 or 0xE1)
1) If so, store it and exit
2) else, use the scancode and a previously stored extended scancode to find an ascii equivalent in the keymap.
If the equivalent is zero, store the scancode
If the equivalent is non-zero, store the scancode and the ascii char
I also check for special keys, like 'shift', 'ctrl' or 'alt gr' to produce special ASCII chars
Mr, L

Re:interrupt example

Post by Mr, L »

I'm also stuck on this subject, but instead of using ports, I want to use the PIC and IDTR.

Does anybody have any examples of interrupts using the PIC and IDTR?

Thanks
IRBMe

Re:interrupt example

Post by IRBMe »

Mr, L, "the PIC and IDTR" and ports are completely different things and are usually used in conjunction with each other. You reprogram the PIC and then it'll handle interrupt requests for you. You add interrupts to your interrupt table, and then write handlers for them. Then in the handlers, you'll often do port io. This applies to the keyboard too. The keyboard interrupt just tells you that the keyboard has *some* information. In order to see what, you need to read the ports once a keyboard interrupt occurs. Setting up the interrupt handlers, reprogramming the PIC, creating an IDT etc are all seperate topics. What pini gave was a good example of what you would write in a keyboard interrupt handler, as asked for. There are seperate tutorials showing you how to set up interrupts.
ASHLEY4

Re:interrupt example

Post by ASHLEY4 »

The simplest possable, to get you started:

Code: Select all

key_board:
                                                     ; Push register
        pushad
        cli
        cld
        in    AL,60h
write_text:
        cmp   al,128                  ; if below 128, then there is a key
   jae   nokey
   mov   edi,eax
        mov   al,[edi+normal_keymap]
next3:
        mov   [keybuffer],al
nokey:
        mov   AL,20h                                ; generate End of Interrupt
        out   20h,AL
        sti
        popad
        ret
normal_keymap

Code: Select all

normal_keymap:

     db   '6',27
     db   '!@#$%^&*()_+',8,9
     db   'QWERTYUIOP{}',13
     db   '~ASDFGHJKL:"~',0,'|ZXCVBNM<>?',0,'45 '
     db   '@234567890123',180,178,184,'6',176,'7'
     db   179,'8',181,177,183,185,182
     db   'AB>D',255,'FGHIJKLMNOPQRSTUVWXYZ'
     db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     db   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
This is from a simple GameOS, i made so was just for top score etc.
so upper case only and no ctrl ,alt etc.

\\\\||////
(@@)
ASHLEY4.

Batteries not included, Some assembly required.
Mr. L

Re:interrupt example

Post by Mr. L »

Mr, L, "the PIC and IDTR" and ports are completely different things and are usually used in conjunction with each other. You reprogram the PIC and then it'll handle interrupt requests for you. You add interrupts to your interrupt table, and then write handlers for them. Then in the handlers, you'll often do port io. This applies to the keyboard too. The keyboard interrupt just tells you that the keyboard has *some* information. In order to see what, you need to read the ports once a keyboard interrupt occurs. Setting up the interrupt handlers, reprogramming the PIC, creating an IDT etc are all seperate topics. What pini gave was a good example of what you would write in a keyboard interrupt handler, as asked for. There are seperate tutorials showing you how to set up interrupts.
Thanks for the reply IRBME, but I thought the question was asking how to get a keyboard interrupt :-[

I have tried getting keyboard interrupts to work for days and I've still had no luck. I have an ISR just waiting to be called upon, but with no interrupt redirection this function is useless.

Does anybody have an example interrupt code that shows you how to get a keyboard interrupt to point to an ISR?

Thanks again.
ASHLEY4

Re:interrupt example

Post by ASHLEY4 »

Maybe if you do a little search in old post you may find what you are looking for.
http://www.mega-tokyo.com/forum/index.p ... 53;start=0
asm code for what you want, by me "ASHLEY4"

\\\\||////
(@@)
ASHLEY4.

Batteries not included, Some assembly required.
Mr. L

Re:interrupt example

Post by Mr. L »

Thanks ASHLEY, I got interrupts working ;D Great Post!!!
Chris Giese

Re:interrupt example

Post by Chris Giese »

ASHLEY4 wrote:

Code: Select all

key_board:
        pushad
        cli
...
        mov   AL,20h  ; generate End of Interrupt
        out   20h,AL
        sti
        popad
        ret
If this is a hardware interrupt handler, you should not
use CLI and STI. What happens if another interrupt
arrives between STI and POPAD? Will the other interrupt
do something that conflicts with the keyboard? Will it
cause a race condition? Will it do something non-reentrant?
Will the kernel stack overflow because of too many PUSHADs?

The interrupt itself will push EFLAGS then clear EFLAGS.IF,
which is the equivalent of CLI. IRET will do the equivalent
of STI by popping the old EFLAGS value. In fact, IRET is
often safer than STI, because it re-enables interrupts
only if they were enabled.

PS - here is a keyboard driver in C that runs under DOS:
[url]http://my.execpc.com/~geezer/osd/kbd/index.htm#snippets/[url]
ASHLEY4

Re:interrupt example

Post by ASHLEY4 »

Chris, thanks for the info, but this was from a old GameOS, made year a go when i was just starting, my OS that i am working on now keyboard int, is nothing like this, but would be not be so easy to under stand, i should have checked it for errors first.

And may i point out that although you should not do this with a hardware int, you can and some time must use this in a software int.

\\\\||////
(@@)
ASHLEY4.

Batteries not included, Some assembly required.
Poseidon

Re:interrupt example

Post by Poseidon »

Thanx guys, but there is one thing i don't get :-[ :
How do i assign the function for the keyboard interrupt to the keyboard interrupt itself?
Mr. L

Re:interrupt example

Post by Mr. L »

You have to reprogram the PIC first and make sure you keep note of what value you have given the keyboard interrupt (i.e. this is usually 0x21). Then you have to populate the IDT table and make sure that the entry that relates to the keyboard interrupt (0x21 or 33rd entry) points to your keyboard ISR.
ASHLEY4

Re:interrupt example

Post by ASHLEY4 »

Here are the the stager's, you can follow the link to see them in context.
http://www.mega-tokyo.com/forum/index.p ... 53;start=0
This is after remapping pic

Code: Select all

;33 interrupt 21h <- IRQ 1
   dw keyboard_irq      ; keyboard
   dw sys_code
   db 0
   db sys_interrupt
   dw 0
Which points to this:

Code: Select all

keyboard_irq:
        pushad
          call key_board
        popad
   iret
Which points to this:

Code: Select all

key_board:
        pushad
        cld
        in    AL,60h
write_text:
        cmp   al,128    ; if below 128, then there is a key
   jae   nokey

   mov   edi,eax
        mov   al,[edi+normal_keymap]
next3:
        mov   [keybuffer],al
nokey:
        mov   AL,20h      ; generate End of Interrupt
        out   20h,AL
        popad
        ret
\\\\||////
(@@)
ASHLEY4.

Batteries not included, Some assembly required.
Post Reply