keyboard driver - need help! 
Author Message
 keyboard driver - need help!


Subject: KEYBOARD DRIVER
Date: Tuesday, March 23, 1999 2:28 PM

HI-
I'M MAKING A KEYBOARD DRIVER TO REPLACE THE DOS ONE - HOWEVER I'M HAVING
ERRORS THAT I JUST CAN'T SEEM TO FIND - I'M USING ASSEMBLY LANGUAGE
PROCEDURES BUT MY MAIN PROGRAM IS WRITTEN IN C - ANY HELP WOULD BE
APPRECIATED

"KEY.C"

#define MAKE_ESC    1                       // SCAN CODE

extern void KeyInst(int,int);
extern void KeyRemov();
extern _interrupt _far KeyDrivr();

int key_state[128]={0};
int keys_active = 0;

void main()
{
 KeyInst(&key_state[0],&keys_active);       // INSTALL IT
                                            // USING WATCOM SO AX =
KEY_STATE
           // DX = KEYS_aCTIVE
 while(key_state[MAKE_ESC] != 1)            // PRINT HAPPY FACE UNTIL ESC
 {
  _asm mov ah,02h
  _asm mov dl,01h
  _asm int 21h
  }

 KeyRemov();                                // REMOVE DRIVER

Quote:
}

// CNTRL.ASM

;;  TYLER TIGUE
;; VERSION 0.1

          PAGE    60,132
          TITLE   CONTROL LIBRARY
          .286
          .MODEL  SMALL

          .CODE
          PUBLIC  KeyInst_,KeyRemov_,KeyDrivr_

          .DATA
          PUBLIC  OLDINTR,KSTADDR,KACADDR
OLDINTR   DD      ?                 ; HOLDS DOS INTERRUPT
KSTADDR   DW      ?                 ; HOLDS KEYSTATE ARRAY ADDRESS
KACADDR   DW      ?                 ; HOLDS KEYS ACTIVE INTEGER

          .CODE

KeyInst_  PROC
          MOV     KSTADDR,AX        ; STORE ADDRESSES IN PROPER VARIABLES
          MOV     KACADDR,DX

          MOV     BX,AX             ; CLEAR OUT KEYSTATE ARRAY
          MOV     CX,128

CLRKEY:   MOV     WORD PTR [BX],0
          INC     BX
          INC     BX
          LOOP    CLRKEY

          MOV     AX,3509H          ; SAVE OLD DOS INTERRUPT
          INT     21H
          MOV     WORD PTR OLDINTR[0],ES
          MOV     WORD PTR OLDINTR[2],BX

          PUSH    DS                ; SET NEW DRIVER
          MOV     AX,CS
          MOV     DS,AX
          MOV     AX,2509H
          LEA     DX,KeyDrivr_
          INT     21H
          POP     DS
          RET
KeyInst_  ENDP

KeyRemov_ PROC                       ; RESTORE OLD DRIVER
          PUSH    DS
          MOV     DX,WORD PTR OLDINTR[2]
          MOV     AX,WORD PTR OLDINTR[0]
          MOV     DS,AX
          MOV     AX,2509H
          INT     21H
          POP     DS
          RET
KeyRemov_ ENDP

          .DATA

RAWCODE   DW      ?

          .CODE

KeyDrivr_ PROC    FAR
          PUSH    DS                    ; SAVE DS
          MOV     AX,DGROUP             ; MAKE SURE IT POINTS TO DATA SEG
          MOV     DS,AX                 ; (I'M USING WATCOM)
          STI
          IN      AL,60H                ; ENABLE INTERRUPT
          XOR     AH,AH                 ; GET KEY AND STORE IT
          MOV     RAWCODE,AX
          IN      AL,61H                ; RESET KEYBOARD
          OR      AL,82H
          OUT     61H,AL
          AND     AL,7FH
          OUT     61H,AL
          MOV     AL,20H
          OUT     20H,AL

          MOV     BX,KSTADDR            ; TO ACCESS KEYSTATE AND KEYSACTIVE
          MOV     SI,KACADDR
          CMP     RAWCODE,128           ; MAKE OR BREAK CODE
          JA      BRKCODE

          CMP     [BX+RAWCODE],0        ; IF ITS ALREADY SET WERE DONE
          JNE     DONE
          MOV     [BX+RAWCODE],1        ; SET IT
          MOV     CX,[SI]               ; INCREMENT KEYSACTIVE
          INC     CX
          MOV     [SI],CX
          JMP     DONE

BRKCODE:  SUB     RAWCODE,128
          CMP     [BX+RAWCODE],1        ; CLEAR IT
          JNE     DONE
          MOV     [BX+RAWCODE],0
          MOV     CX,[SI]
          DEC     CX
          MOV     [SI],CX

DONE:     POP     DS
          IRET
KeyDrivr_ ENDP
          END

EVERYTIME I RUN IT ONCE I HIT ESC IT TURNS THE SCREEN INTO GARBAGE BUT
CONTINUES TO PRINT MORE GARBAGE AND IT DOES NOT EXIT - THANKS FOR ANY HELP -
I'VE DEBUGGED IT BUT I CAN'T FIND WHERE I'M GOING WRONG

TYLER



Sun, 09 Sep 2001 03:00:00 GMT  
 keyboard driver - need help!

|Subject: KEYBOARD DRIVER
|Date: Tuesday, March 23, 1999 2:28 PM
|
|HI-
|I'M MAKING A KEYBOARD DRIVER TO REPLACE THE DOS ONE - HOWEVER I'M HAVING
|ERRORS THAT I JUST CAN'T SEEM TO FIND - I'M USING ASSEMBLY LANGUAGE
|PROCEDURES BUT MY MAIN PROGRAM IS WRITTEN IN C - ANY HELP WOULD BE
|APPRECIATED

LET ME GUESS, YOUR KEYBOARD CAN ONLY PRINT IN UPPERCASE AND NEED
A NEW DRIVER THAT CAN PRINT IN LOWERCASE?

[...]

|KeyInst_  PROC
|          MOV     KSTADDR,AX        ; STORE ADDRESSES IN PROPER VARIABLES
|          MOV     KACADDR,DX

What C compiler are you using? They usually pass parameters on the stack
and *return* stuff in AL (char) AX (short) DX:AX (long) (16-bit compilers).

[...]

|KeyRemov_ PROC                       ; RESTORE OLD DRIVER
|          PUSH    DS
|          MOV     DX,WORD PTR OLDINTR[2]
|          MOV     AX,WORD PTR OLDINTR[0]
|          MOV     DS,AX

Just a note: most people save the segment in the second word and the
offset in the first word. That way, you can use LDS/LES/LSS/LETC...

|          MOV     AX,2509H
|          INT     21H
|          POP     DS
|          RET
|KeyRemov_ ENDP
|
|          .DATA
|
|RAWCODE   DW      ?
|
|          .CODE
|
|KeyDrivr_ PROC    FAR
|          PUSH    DS                    ; SAVE DS
|          MOV     AX,DGROUP             ; MAKE SURE IT POINTS TO DATA SEG
|          MOV     DS,AX                 ; (I'M USING WATCOM)

Aha, Watcom C... Have only used BC++

|          STI
|          IN      AL,60H                ; ENABLE INTERRUPT
|          XOR     AH,AH                 ; GET KEY AND STORE IT
|          MOV     RAWCODE,AX
|          IN      AL,61H                ; RESET KEYBOARD
|          OR      AL,82H
|          OUT     61H,AL
|          AND     AL,7FH
|          OUT     61H,AL

Unless this is for a pre-AT system, you shouldn't do the above
(Set 61h[7], clear 61h[7]).

And why do you set the SPKR_EN (bit 1)?

|          MOV     AL,20H
|          OUT     20H,AL

You shouldn't send an EOI if you haven't stopped your IRQ
from being generated again. Use CLI, disable the keyboard
or mask IRQ1 by setting IMR[1] to 1. Otherwise, your IRQ
handler can be interrupted again...

|
|          MOV     BX,KSTADDR            ; TO ACCESS KEYSTATE AND KEYSACTIVE
|          MOV     SI,KACADDR
|          CMP     RAWCODE,128           ; MAKE OR BREAK CODE
|          JA      BRKCODE

Should be JAE (bit 7 set is break code)

|
|          CMP     [BX+RAWCODE],0        ; IF ITS ALREADY SET WERE DONE
|          JNE     DONE
|          MOV     [BX+RAWCODE],1        ; SET IT

See what can happen here? Your IRQ can be interrupted after the CMP and
both IRQ handlers will think the byte's not set. When the new IRQ handler
exits, it has set the byte to 1. When it returns, the CPU continues from
"JNE DONE", which will NOT jmp...

|          MOV     CX,[SI]               ; INCREMENT KEYSACTIVE
|          INC     CX
|          MOV     [SI],CX

Can be interrupted here too, and anywhere else.

|          JMP     DONE
|
|BRKCODE:  SUB     RAWCODE,128
|          CMP     [BX+RAWCODE],1        ; CLEAR IT
|          JNE     DONE
|          MOV     [BX+RAWCODE],0
|          MOV     CX,[SI]
|          DEC     CX
|          MOV     [SI],CX
|
|DONE:     POP     DS
|          IRET

Ok, you save DS, but you don't save the other regs you destroy.



Sun, 09 Sep 2001 03:00:00 GMT  
 keyboard driver - need help!

Quote:
>You shouldn't send an EOI if you haven't stopped your IRQ
>from being generated again. Use CLI, disable the keyboard
>or mask IRQ1 by setting IMR[1] to 1. Otherwise, your IRQ
>handler can be interrupted again...

The Interrupt flag is cleared by default when an interrupt is called. Unless he
specifically uses STI he shouldn't worry about it.

  - vulture a.k.a. Sean stanek



Sun, 09 Sep 2001 03:00:00 GMT  
 keyboard driver - need help!
|
|>You shouldn't send an EOI if you haven't stopped your IRQ
|>from being generated again. Use CLI, disable the keyboard
|>or mask IRQ1 by setting IMR[1] to 1. Otherwise, your IRQ
|>handler can be interrupted again...
|
|The Interrupt flag is cleared by default when an interrupt is called. Unless he
|specifically uses STI he shouldn't worry about it.

I know. He did use STI, that's why I wrote the above...



Mon, 10 Sep 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. HELP!!! I need help with keyboard INT

2. HELP!!! I need help with the keyboard INT

3. need help with keyboard hook

4. Hardware keyboard handling... help needed

5. keyboard handler,... need a little help

6. CD-ROM and Keyboard led help needed

7. Hardware keyboard handling... help needed

8. keyboard handler,... need a little help

9. Need help on low level keyboard routines

10. I need your help : Keyboard in Assembler

11. I need help for my keyboard handler !!!

12. I need help with keyboard input functions....

 

 
Powered by phpBB® Forum Software