home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / DOOG / CTASK.ZIP / TSKKBD.ASM < prev    next >
Assembly Source File  |  1989-12-20  |  6KB  |  327 lines

  1. ;
  2. ;    --- Version 2.0 89-12-13 17:57 ---
  3. ;
  4. ;    CTask - Keyboard handler module.
  5. ;
  6. ;    Public Domain Software written by
  7. ;        Thomas Wagner
  8. ;        Patschkauer Weg 31
  9. ;        D-1000 Berlin 33
  10. ;        West Germany
  11. ;
  12. ;    This module traps the keyboard interrupts to allow task switching
  13. ;    on waiting for a character.
  14. ;    To avoid problems with programs that access the keyboard buffer
  15. ;    directly instead of going through INT 16, the logic has been changed
  16. ;    in version 1.2. The keyboard characters are no longer placed into
  17. ;    a pipe, instead the keyboard hardware interrupt just sets a flag
  18. ;    to signal that there might be something in the buffer. The keyboard
  19. ;    read routines wait on this flag if the original INT 16 status call
  20. ;    indicates that no key is available.
  21. ;
  22. ;    Note that there is a slight chance of this logic leading to busy
  23. ;    waiting in the original INT 16. This could happen if the process
  24. ;    is interrupted between the status check and the actual keyboard
  25. ;    fetch, and the interrupting routine snatches away the keystroke.
  26. ;    Since this is not very likely to occur, and would not be fatal
  27. ;    anyway, it would be overkill to try to avoid this.
  28. ;
  29. ;    If INT 16 is entered via CTask's t_read_key and t_wait_key,
  30. ;    the stack is not switched.
  31. ;    
  32.     name    tskkbd
  33.     .model    large,c
  34. ;
  35.     public    tsk_install_kbd
  36.     public    tsk_remove_kbd
  37. ;
  38.     public    t_read_key
  39.     public    t_wait_key
  40.     public    t_keyhit
  41. ;
  42.     include    tsk.mac
  43. ;
  44. ;
  45.     extrn    create_flag: far
  46.     extrn    delete_flag: far
  47.     extrn    set_flag: far
  48.     extrn    clear_flag: far
  49.     extrn    wait_flag_set: far
  50. ;
  51.     extrn    tsk_dgroup: word
  52. ;
  53. ;
  54. intseg    segment at 0
  55.         org    09h*4
  56. hwdoff        dw    ?    ; keyboard hardware interrupt
  57. hwdseg        dw    ?
  58.         org    16h*4
  59. kbdoff        dw    ?    ; keyboard I/O interrupt
  60. kbdseg        dw    ?
  61. ;
  62. intseg    ends
  63. ;
  64. ;----------------------------------------------------------------------------
  65. ;
  66. ;    Variables
  67. ;
  68.     .tsk_data
  69. ;
  70.     IF    TSK_NAMEPAR
  71. kbd_name    db    "KEYAVAIL",0
  72.     ENDIF
  73. ;
  74. key_avail    flag <>
  75. ;
  76.     .tsk_edata
  77.     .tsk_code
  78. ;
  79.     extrn    tsk_switch_stack: near
  80. ;
  81. ;    Original Interrupt-Entries
  82. ;
  83. savhwd        label    dword        ; original hardware int entry
  84. savhwdoff    dw    ?
  85. savhwdseg    dw    ?
  86. ;
  87. savkbd        label    dword        ; original keyboard I/O entry
  88. savkbdoff    dw    ?
  89. savkbdseg    dw    ?
  90. ;
  91. ;---------------------------------------------------------------------------
  92. ;
  93. ;    void tsk_install_kbd (void)
  94. ;
  95. ;        Install keyboard handler
  96. ;
  97. tsk_install_kbd    proc    near uses ds
  98. ;
  99.     mov    ds,cs:tsk_dgroup
  100. ;
  101.     IF    TSK_NAMEPAR
  102.     mov    ax,offset kbd_name
  103.     push    ds
  104.     push    ax
  105.     ENDIF
  106.     mov    ax,offset key_avail
  107.     push    ds
  108.     push    ax
  109.     call    create_flag
  110.     IF    TSK_NAMEPAR
  111.     add    sp,8
  112.     ELSE
  113.     add    sp,4
  114.     ENDIF
  115. ;
  116. ;    Save old interrupt vectors
  117. ;
  118.         push    es
  119.     xor    ax,ax
  120.     mov    es,ax
  121. ;
  122.         assume  es:intseg
  123. ;
  124.     mov    ax,kbdoff
  125.     mov    savkbdoff,ax
  126.     mov    ax,kbdseg
  127.     mov    savkbdseg,ax
  128. ;
  129.     mov    ax,hwdoff
  130.     mov    savhwdoff,ax
  131.     mov    ax,hwdseg
  132.     mov    savhwdseg,ax
  133. ;
  134. ;    Enter new Interrupt-Entries
  135. ;
  136.     cli
  137.     mov    kbdoff,offset kbdentry
  138.     mov    kbdseg,cs
  139.     mov    hwdoff,offset hwdentry
  140.     mov    hwdseg,cs
  141.     sti
  142.         pop     es
  143. ;
  144.     ret
  145. ;
  146.     assume    es:nothing
  147. ;
  148. tsk_install_kbd    endp
  149. ;
  150. ;
  151. ;    void tsk_remove_kbd (void)
  152. ;
  153. ;        Un-install keyboard handler
  154. ;
  155. tsk_remove_kbd    proc    near uses ds
  156. ;
  157.     mov    ds,cs:tsk_dgroup
  158. ;
  159.     mov    ax,offset key_avail
  160.     push    ds
  161.     push    ax
  162.     call    delete_flag
  163.     add    sp,4
  164. ;
  165.         push    es
  166.     xor    ax,ax
  167.     mov    es,ax
  168. ;
  169.         assume  es:intseg
  170. ;
  171. ;    Restore interrupt entries
  172. ;
  173.     cli
  174. ;
  175.     mov    ax,savkbdoff
  176.     mov    kbdoff,ax
  177.     mov    ax,savkbdseg
  178.     mov    kbdseg,ax
  179. ;
  180.     mov    ax,savhwdoff
  181.     mov    hwdoff,ax
  182.     mov    ax,savhwdseg
  183.     mov    hwdseg,ax
  184. ;
  185.     sti
  186. ;
  187.         pop     es
  188.     ret
  189. ;
  190.     assume    es:nothing
  191. ;
  192. tsk_remove_kbd    endp
  193. ;
  194. ;
  195. ;---------------------------------------------------------------------------
  196. ;
  197. ;    int t_read_key (void)
  198. ;
  199. ;    Waits for key from keyboard. Returns char in lower byte,
  200. ;    Scan-Code in upper byte.
  201. ;
  202. t_read_key    proc
  203. ;
  204.     mov    ax,4112h
  205.     int    16h
  206.     ret
  207. ;    
  208. t_read_key    endp
  209. ;
  210. ;
  211. ;    int t_wait_key (dword timeout)
  212. ;
  213. ;    Waits for key from keyboard. Returns char in lower byte,
  214. ;    Scan-Code in upper byte.
  215. ;
  216. t_wait_key    proc    tout: dword
  217. ;
  218.     mov    cx,word ptr (tout+2)
  219.     mov    dx,word ptr (tout)
  220.     mov    ax,4012h
  221.     int    16h
  222.     ret
  223. ;    
  224. t_wait_key    endp
  225. ;
  226. ;
  227. ;    int t_keyhit (void)
  228. ;
  229. ;    Checks if char is available. Returns -1 if not, else the
  230. ;    character value. The character remains in the buffer.
  231. ;
  232. t_keyhit    proc
  233. ;
  234.     mov    ah,1
  235.     int    16h
  236.     jnz    t_kh_ok
  237.     mov    ax,-1
  238. t_kh_ok:
  239.     ret
  240. ;
  241. t_keyhit    endp
  242. ;
  243. ;---------------------------------------------------------------------------
  244. ;
  245. ;    INT 9 - Keyboard hardware interrupt
  246. ;
  247. hwdentry    proc    far
  248. ;
  249.         call    tsk_switch_stack
  250.     pushf
  251.         cli
  252.     call    cs:savhwd    ; process key
  253. ;
  254.     mov    ax,offset key_avail
  255.     push    ds
  256.     push    ax
  257.     call    set_flag
  258.     add    sp,4
  259.     iret
  260. ;
  261. hwdentry    endp
  262. ;
  263. ;---------------------------------------------------------------------------
  264. ;
  265. ;    INT 16 - Keyboard I/O
  266. ;
  267. kbdentry    proc    far
  268. ;
  269.         pushf
  270.     or    ah,ah
  271.     jz    kbd_read
  272.     cmp    ax,4012h
  273.     jz    kbd_read
  274.     cmp    ax,4112h
  275.     jz    kbd_read
  276.         popf
  277.     jmp    cs:savkbd    ; pass on functions != 0
  278. ;
  279. kbd_read:
  280.         popf
  281.     call    tsk_switch_stack
  282. ;
  283. kbr_loop:
  284.     mov    ax,offset key_avail
  285.     push    ds
  286.     push    ax
  287.     call    clear_flag
  288.     add    sp,4
  289.     mov    ah,1
  290.     pushf
  291.     cli
  292.     call    cs:savkbd
  293.     jnz    kbr_get_key
  294.         mov     cx,save_cx[bp]
  295.         mov     dx,save_dx[bp]
  296.     cmp    byte ptr save_ax+1[bp],40h
  297.     je    tout_x
  298.     xor    cx,cx
  299.     mov    dx,cx
  300. tout_x:
  301.     push    cx
  302.     push    dx
  303.     mov    ax,offset key_avail
  304.     push    ds
  305.     push    ax
  306.     call    wait_flag_set
  307.     add    sp,8
  308.     or    ax,ax
  309.     jz    kbr_loop
  310.     mov    save_ax[bp],-1
  311.     jmp    short kbr_retn
  312. ;
  313. kbr_get_key:
  314.     xor    ah,ah
  315.     pushf
  316.     cli
  317.     call    cs:savkbd
  318.     mov    save_ax[bp],ax
  319. kbr_retn:
  320.     iret
  321. ;
  322. kbdentry    endp
  323. ;
  324.     .tsk_ecode
  325.     end
  326.  
  327.