home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FASKBHIT.HOW < prev    next >
Text File  |  1997-07-05  |  2KB  |  48 lines

  1. +++Date last modified: 05-Jul-1997
  2.  
  3.  #define FastClearbuf() \
  4.  (*(unsigned far*)0x0040001AL = *(unsigned far*)0x0040001CL)
  5.  
  6.  #define FastKBhit() \
  7.  (*(unsigned far*)0x0040001AL != *(unsigned far*)0x0040001CL)
  8.  
  9. Q: Please explain the logic behind these statements. I have not seen
  10.    statements like these before and my textbooks have nothing like this in
  11.    them. Is 0x0040001A a long address cast as a far pointer to a pointer? Now
  12.    I'm really lost...
  13.  
  14. A: In a PC using DOS (this is obviously *very* environment-specific code!),
  15. the BIOS maintains the keyboard buffer in an area of page 40h. The actualy
  16. 16-bit key codes are stored in a 32-bit cirular buffer using head and tail
  17. pointers to point to new characters waiting to be fetched. Using head and
  18. tail pointers allows this buffer to be relocated to other areas of RAM and/or
  19. resized.
  20.  
  21.   A convention of PC C compilers is that real mode addresses (such as those
  22. on page 40h) are represented as a concatenation of their segment and offset
  23. addresses, expressed as a long int. The far pointers to the head and tail of
  24. the circular buffer are...
  25.  
  26. Head pointer storage location = 0040:001Ah (cast to unsigned long=0x0040001aL)
  27. Tail pointer storage location = 0040:001Ch (cast to unsigned long=0x0040001cL)
  28.  
  29. ...Since the key codes are all 16 bits wide, these must be cast to far
  30. pointers to unsigned ints or unsigned shorts.
  31.  
  32.   Since the keyboard buffer is a circular buffer, in order to quickly tell if
  33. there are any keypresses waiting, all you need to do is simply compare the
  34. value of the head and tail pointers. If they're both pointing at the same
  35. place, there's nothing in the buffer. Similarly, in order to clear the
  36. buffer, simply set them equal to the same value and all pending keypresses
  37. disappear.
  38.  
  39.   Now let's put it all together, using the FastKBhit() macro as an example...
  40. First of all, we cast the long int values shown above into far pointers to
  41. unsigned ints. We then dereference each to obtain the value located at each
  42. storage location. Finally, we #define the macro as the inequality of the two
  43. values, i.e. if they're different, there are one or more keypresses waiting.
  44.  
  45.   Two notes... These are functions rather than macros in later versions of
  46. SNIPPETS, and these will obviously not work in protected or flat mode or any
  47. 32-bit environment.
  48.