home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / KBPLACE.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-03-31  |  3.6 KB  |  147 lines

  1. ;
  2. ; Name        KBPLACE -- Place one keystroke in keyboard buffer.
  3. ;
  4. ; Synopsis    ret = kbplace (where, value, scan);
  5. ;
  6. ;        int  ret      0 (KB_OK) if successful, 1 (KB_FULL)
  7. ;                  if buffer full, or 2 (KB_PLACE) if
  8. ;                  the place specified was neither
  9. ;                  the head nor the tail of the queue.
  10. ;
  11. ;        int  where      KB_HEAD if keystroke is to be placed
  12. ;                  at head of queue, KB_TAIL if at tail.
  13. ;
  14. ;        char value      ASCII value of keystroke (0 if not
  15. ;                  an ASCII character).
  16. ;
  17. ;        char scan      Scan code of keystroke.
  18. ;
  19. ; Description    This function places one keystroke at the head or tail
  20. ;        of the BIOS type-ahead buffer if there is room for it.
  21. ;
  22. ;        KBPLACE assumes that the BIOS type-ahead buffer is at
  23. ;        the standard location of 0x40:0x1e.  It will probably
  24. ;        malfunction if such is not the case.
  25. ;
  26. ;        The BIOS keyboard buffer is arranged as a circular queue
  27. ;        of fifteen pairs of bytes (plus one pair for overhead).
  28. ;        For extended key sequences the character value is 0 and
  29. ;        the scan code is as described in the IBM Technical
  30. ;        Reference.  For ASCII characters, on the other hand, the
  31. ;        first byte of the pair is the ASCII code for the
  32. ;        character and the second is the keystroke's scan code
  33. ;        (if the key was generated by a normal scan code) or 0
  34. ;        (if the key was generated by ALT plus the numeric ASCII
  35. ;        code on the numeric keypad).  For instance,
  36. ;
  37. ;            pressing 'A' generates character 65, scan code 30,
  38. ;
  39. ;        but
  40. ;
  41. ;            pressing ALT + 65 generates character 65, scan code 0.
  42. ;
  43. ;        (Virtually all application programs treat those two
  44. ;        cases identically.)
  45. ;
  46. ; Returns    ret        0 (KB_OK) if successful, 1 (KB_FULL) if
  47. ;                    buffer full, or 2 (KB_PLACE) if the
  48. ;                    place specified was neither the
  49. ;                    head nor the tail of the queue.
  50. ;
  51. ; Version    6.00 (C)Copyright Blaise Computing Inc. 1987,1989
  52. ;
  53.  
  54.     include beginasm.mac       ; Specifies the C compiler
  55.     beginProg kbplace
  56.  
  57. KB_HEAD equ  0
  58. KB_TAIL equ  1
  59.  
  60. where    equ   word ptr [bp + stkoff + 0] ; Addresses of arguments
  61. value    equ   byte ptr [bp + stkoff + 2]
  62. scan    equ   byte ptr [bp + stkoff + 4]
  63.  
  64. buffer_head   equ word ptr ds:1ah
  65. buffer_tail   equ word ptr ds:1ch
  66. kb_buffer     equ          1eh
  67. kb_buffer_end equ          3eh
  68.  
  69.     ; Beginning of actual code
  70.     push    bp
  71.     mov    bp, sp
  72.     push    ds
  73.     pushf               ; Save interrupt state.
  74.  
  75.     mov    ax, 40h        ; Segment address of BIOS data
  76.     mov    ds, ax
  77.     assume    ds:nothing
  78.  
  79.     cli               ; Disable interrupts.
  80.  
  81.     cmp    where, KB_HEAD
  82.     jz    try_at_head
  83.  
  84.     cmp    where, KB_TAIL
  85.     jz    try_at_tail
  86.  
  87.     mov    ax, 2
  88.     jmp    short exit
  89.  
  90. try_at_head:
  91.     mov    bx, buffer_head
  92.     dec    bx           ; Tentatively step head back.
  93.     dec    bx
  94.     cmp    bx, kb_buffer
  95.     jae    k5h
  96.                    ; Went past beginning of buffer,
  97.     mov    bx, kb_buffer_end-2; so wrap to end.
  98. k5h:
  99.                    ; Now BX has new head pointer.
  100.     cmp    bx, buffer_tail
  101.     je    no_room
  102.  
  103.     mov    al, value
  104.     mov    ah, scan
  105.     mov    [bx], ax       ; Store value & scan code
  106.                    ; at buffer head.
  107.     mov    buffer_head, bx    ; New head pointer.
  108.  
  109.     jmp    short success
  110.  
  111. try_at_tail:
  112.     mov    bx, buffer_tail
  113.     mov    cx, bx
  114.     inc    cx           ; Tentatively advance new tail.
  115.     inc    cx
  116.     cmp    cx, kb_buffer_end
  117.     jb    k5
  118.                    ; Went past end of buffer,
  119.     mov    cx, kb_buffer       ; so wrap to beginning.
  120. k5:
  121.                    ; Now CX has new tail.
  122.     cmp    cx, buffer_head
  123.     je    no_room
  124.  
  125.     mov    al, value
  126.     mov    ah, scan
  127.     mov    [bx], ax       ; Store value & scan code
  128.                    ; at buffer tail.
  129.     mov    buffer_tail, cx    ; New tail pointer.
  130.  
  131. success:
  132.     xor    ax, ax
  133.     jmp    short exit
  134.  
  135. no_room:
  136.     mov    ax, 1
  137.  
  138. exit:
  139.     popff               ; Restore interrupt state.
  140.  
  141.     pop    ds
  142.     pop    bp
  143.     ret
  144.  
  145.     endProg kbplace
  146.     end
  147.