home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / asm_programming / KB32.ASM < prev    next >
Assembly Source File  |  1993-01-09  |  7KB  |  253 lines

  1.         .386p
  2. code32  segment para public use32
  3.         assume cs:code32, ds:code32, ss:code32
  4.  
  5. include start32.inc
  6.  
  7. public  _kbhand, _ksstat
  8. public  _init_kb, _reset_kb, _getch, _reset_hand
  9.  
  10. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  11. ; DATA
  12. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  13. _kbhand         dd      ?
  14. kbword  label word
  15. kbbyte          db      ?
  16. _ksstat         db      ?       ; bits: 0-shift, 1-alt, 2-ctrl, 3-key hit
  17. kbpausec        db      ?
  18. kbxtbl          db      0,14,'1234567890-=',16,15,'qwertyuiop[]',13,0
  19.                 db      'asdfghjkl;''`',0,'\zxcvbnm,./',0,'*',0,32,0
  20.                 db      1,2,3,4,5,6,7,8,9,10,0,0,19,25,21,'-',23,'5'
  21.                 db      24,'+',20,26,22,17,18,0,0,0,11,12
  22.  
  23. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  24. ; CODE
  25. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  26.  
  27. ;─────────────────────────────────────────────────────────────────────────────
  28. waitforkb:                              ; Wait for keyboard ready
  29.         mov ecx,20000h
  30. waitforkbl:
  31.         in al,64h
  32.         test al,2
  33.         loopnz waitforkbl
  34.         ret
  35.  
  36. ;─────────────────────────────────────────────────────────────────────────────
  37. setkb:                                  ; Set KB, LOCKS=BL
  38.         mov al,0edh
  39.         call outtokb
  40.         mov al,bl
  41.         call outtokb
  42.         mov al,0f4h
  43. ;─────────────────────────────────────────────────────────────────────────────
  44. outtokb:
  45.         mov ah,al
  46.         mov bh,3
  47. outtokbl0:
  48.         call waitforkb
  49.         mov al,ah
  50.         out 60h,al
  51.         mov ecx,4000h
  52. outtokbl1:
  53.         in al,61h
  54.         test al,10h
  55.         jz $-4
  56.         in al,61h
  57.         test al,10h
  58.         jnz $-4
  59.         in al,64h
  60.         test al,1
  61.         loopz outtokbl1
  62.         in al,60h
  63.         cmp al,0fah
  64.         je _ret
  65.         dec bh
  66.         jnz outtokbl0
  67.         ret
  68.  
  69. ;═════════════════════════════════════════════════════════════════════════════
  70. kbirq:                                  ; Primary keyboard IRQ
  71.         pushad
  72.         push ds
  73.         push es
  74.         mov ax,ss
  75.         mov ds,ax
  76.         mov es,ax
  77.         call waitforkb
  78.         mov al,0adh
  79.         out 64h,al
  80.         call waitforkb
  81.         in al,60h
  82.         mov bl,al
  83.         call waitforkb
  84.         mov al,0aeh
  85.         out 64h,al
  86.         call waitforkb
  87.         cmp kbpausec,0
  88.         jne kbirqpc
  89.         mov al,bl
  90.         cmp al,0e0h
  91.         je kbirqd
  92.         cmp al,0e1h
  93.         je kbirqp
  94.         call _kbhand
  95. kbirqd:
  96.         pop es
  97.         pop ds
  98.         mov al,20h
  99.         out 20h,al
  100.         popad
  101.         iretd
  102. kbirqp:
  103.         mov kbpausec,5
  104.         jmp kbirqd
  105. kbirqpc:
  106.         dec kbpausec
  107.         jmp kbirqd
  108.  
  109. ;═════════════════════════════════════════════════════════════════════════════
  110. kbhand:                                 ; Default keyboard handler
  111.         test al,80h                     ;  Break code???
  112.         jnz short kbbreak
  113.         mov ah,1                        ;  KB make code, or value of shift
  114.         cmp al,2ah                      ;  == left shift?
  115.         jb short kbmakecc
  116.         je short kbmakes
  117.         cmp al,36h                      ;  == right shift?
  118.         jb short kbmake
  119.         je short kbmakes
  120.         cmp al,38h                      ;  == alt?
  121.         jne short kbmake
  122.         mov ah,2                        ;  or in alt
  123. kbmakes:
  124.         or _ksstat,ah
  125.         ret
  126. kbmakecc:
  127.         mov ah,4
  128.         cmp al,1dh                      ;  == ctrl?
  129.         je kbmakes
  130. kbmake:                                 ;  not a shift state make code
  131.         movzx eax,al
  132.         mov al,kbxtbl[eax]
  133.         or al,al
  134.         jz short kbhandd
  135.         or _ksstat,8
  136.         mov kbbyte,al
  137.         ret
  138. kbbreak:                                ;  KB break code
  139.         mov ah,0ffh
  140.         cmp al,0aah                     ;  == left shift?
  141.         jb short kbbreakcc
  142.         je short kbbreaks
  143.         cmp al,0b6h                     ;  == right shift?
  144.         jb short kbbreakd
  145.         je short kbbreaks
  146.         cmp al,0b8h                     ;  == alt?
  147.         jne short kbbreakd
  148.         mov ah,0fdh                     ;  and out alt
  149.         jmp short kbbreakd
  150. kbbreakcc:
  151.         cmp al,9dh                      ;  == ctrl?
  152.         jne kbbreakd
  153.         mov ah,0fbh                     ;  and out ctrl
  154.         jmp kbbreakd
  155. kbbreaks:                               ;  and out shift
  156.         mov ah,0feh
  157. kbbreakd:
  158.         and _ksstat,ah
  159. kbhandd:
  160.         ret
  161.  
  162. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  163. ; Initialize primary keyboard IRQ and secondary handler
  164. ; In:
  165. ;   None
  166. ; Out:
  167. ;   None
  168. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  169. _init_kb:
  170.         pushf
  171.         cli
  172.         push eax
  173.         push bx
  174.         push ecx
  175.         mov bl,21h
  176.         mov eax,offset kbirq
  177.         call _setvect
  178.         xor bl,bl
  179.         call setkb
  180.         in al,21h
  181.         and al,0fdh
  182.         out 21h,al
  183.         call _reset_hand
  184.         pop ecx
  185.         pop bx
  186.         pop eax
  187.         popf
  188.         ret
  189.  
  190. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  191. ; Reset KB and uninstall handlers
  192. ; In:
  193. ;   None
  194. ; Out:
  195. ;   None
  196. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  197. _reset_kb:
  198.         pushf
  199.         cli
  200.         push eax
  201.         push bx
  202.         push ecx
  203.         push es
  204.         mov ax,18h
  205.         mov es,ax
  206.         mov bl,es:[417h]
  207.         shr bl,4
  208.         and bl,0fh
  209.         call setkb
  210.         in al,21h
  211.         or al,2
  212.         out 21h,al
  213.         pop es
  214.         pop ecx
  215.         pop bx
  216.         pop eax
  217.         popf
  218.         ret
  219.  
  220. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  221. ; Reset to default KB handler
  222. ; In:
  223. ;   None
  224. ; Out:
  225. ;   None
  226. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  227. _reset_hand:
  228.         mov _ksstat,0
  229.         mov _kbhand,offset kbhand
  230.         mov kbpausec,0
  231.         ret
  232.  
  233. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  234. ; Get keystroke or wait for 1 (on default 2nd handler)
  235. ; In:
  236. ;   None
  237. ; Out:
  238. ;   AL - ASCII key or special code
  239. ;   AH - bits: 0 - Shift
  240. ;              1 - Alt
  241. ;              2 - Ctrl
  242. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  243. _getch:
  244.         btr word ptr _ksstat,3
  245.         jnc _getch
  246.         mov ax,kbword
  247.         ret
  248.  
  249.  
  250. code32  ends
  251.         end
  252.  
  253.