; ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
; ³ (c) Lord Julus - 2000                Vxtasy #1 - Magazine source code ³
; ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

;==========( Procedure to set the new keyboard handler )=====================

setnewint9 proc near                       ; Set new keyboard handler
         pusha                             ;
         push ds                           ;
         push si                           ;
         push es                           ;
         cli                               ;
         mov ax,3509h                      ; get old INT 9
         int 21h                           ;
         mov si,offset oldint9             ;
         mov word ptr cs:[si],bx           ; save offset
         mov word ptr cs:[si+2],es         ; save segment
         mov ax,2509h                      ; set new INT 9
         mov dx,seg NewInt9                ;
         mov ds,dx                         ;
         mov dx,offset NewInt9             ;
         int 21h                           ;
         sti                               ;
         pop es                            ;
         pop si                            ;
         pop ds                            ;
         popa                              ;
         ret                               ;
setnewint9 endp                            ;

;===========( Restore the old keyboard handler )=============================

setoldint9 proc near                       ; Restore old keyboard handler
         push ds                           ;
         push es                           ;
         push si                           ;
         cli                               ;
         mov si,offset oldint9             ;
         mov dx,word ptr cs:[si]           ; load offset
         mov ds,word ptr cs:[si + 2]       ; load segment
         mov ax,2509h                      ; set new INT 9
         int 21h                           ;
         sti                               ;
         pop si                            ;
         pop es                            ;
         pop ds                            ;
         ret                               ;
setoldint9 endp                            ;

;=========( The new keyboard handler )=======================================

NewInt9 proc far                           ;
         push ax                           ; save some regs
         push bx                           ;
                                           ;
         in al,60h                         ; get the key status
         cmp al,0E0h                       ; was it an E0 key?
         jne setscancode                   ;
                                           ;
         mov cs:[e0flag],128               ; if so, then
         mov al,20h                        ; send generic EOI to PIC
         out 20h,al                        ;
         pop bx                            ; restore regs
         pop ax                            ;
         iret                              ; and get out
                                           ;
setscancode:                               ;
         mov bl,al                         ; save scan code
         and bl,01111111b                  ;
         add bl,cs:[e0flag]                ;
         xor bh,bh                         ; clear for index use
         and al,10000000b                  ; keep break bit, if set
         xor al,10000000b                  ; flip bit - 1 means pressed
                                           ;          - 0 means released
         rol al,1                          ; put it in bit 0
         mov cs:keys[bx],al                ; set index for key
         mov cs:[e0flag],0                 ; set E0 to 0
         shl al,1                          ; set to 2 or 0
         dec al                            ; 1 = press, -1 = release
         add cs:[keynumpress],al           ; inc or dec keypress
                                           ;
         mov al,20h                        ; Send generic EOI to PIC
         out 20h,al                        ;
         pop bx                            ; restore regs
         pop ax                            ;
                                           ;
                                           ;
         cmp cs:do_old9, 0                 ;
         je dont_do_old                    ;
         pushf                             ;
         call dword ptr cs:[oldint9]       ;
                                           ;
dont_do_old:                               ;
         iret                              ; and exit
NewInt9 endp                               ;
                                           ;
keys           db      256 dup(0)          ; matrix with the keyboard status
keynumpress    db      0                   ;
oldint9        dd      0                   ;
e0flag         db      0                   ;
do_old9        db      0                   ;
;============================================================================