home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MISC / TNH_PC.ZIP / HAMMOND.NL < prev    next >
Encoding:
Text File  |  1987-01-14  |  2.5 KB  |  85 lines

  1. Convenient Timing in BASIC Programs
  2.  
  3.             Wayne Hammond
  4.         St. Louis IBM PC Club
  5.  
  6. While doing some programming in
  7. BASIC, I had the need to set a
  8. variable with a value representing
  9. the time of some future event.  I
  10. wanted to hang in a loop until the
  11. current time reached my
  12. pre-determined time.  I wanted this
  13. in the form of a variable rather
  14. than a timer interrupt so that logic
  15. could be added to the loop and a
  16. branch out based on keyboard input
  17. as well as time expired.
  18.  
  19. I worked out a way to do it in
  20. BASIC.  In the Technical Reference
  21. manual I noticed that four bytes of
  22. low main storage are constantly
  23. updated by the timer.  These four
  24. bytes contain a 32-bit binary number
  25. that is initialized at IPL to zero,
  26. and is incremented by 1
  27. approximately 18 times per second.
  28. These bytes are at hex locations 46C
  29. through 46F.  The bytes are stored
  30. in reverse order; the byte at 46C is
  31. the low-order, the byte at 46F the
  32. high-order.
  33.  
  34. I can get at these bytes from a
  35. BASIC program with the PEEK
  36. function.  Remember that, since
  37. these bytes are outside the segment
  38. that BASIC handles,  I first have to
  39. specify the segment address before
  40. doing the PEEK, and I must   store
  41. the segment address after doing it.
  42. The following BASIC subroutine will
  43. access the complete timer value:
  44.  
  45. 10000 DEF SEG=0
  46.    'Set to segment address base 0
  47. 10010 TO=PEEK(&h46C):
  48.    T1=PEEK(&H46D):T2=PEEK(&h46E):
  49.    T3=PEEK(&h46F) 'GET 4 BYTES
  50. 10020
  51.   TVAL=TO+256*(T1+256*(T2+256*T3))
  52.   ' Giving each byte its positional
  53.   value
  54. 10030  DEG SEG
  55.   'Restore to BASIC segment address
  56. 10040 RETURN
  57.   'Return from subroutine
  58. 10040
  59.  
  60. If I want to retain the precision of
  61. TVAL regardless of the timer's
  62. value, I'd better define TVAL as
  63. double-precision (using the DEFDBL
  64. statement), because BASIC's
  65. single-precision real numbers have
  66. less than  32 bits of accuracy.  To
  67. set up to test for the end of a time
  68. interval, I would get the current
  69. timer value, add to it a value that
  70. corresponds to the time I want to
  71. allow to elapse (add 18 for each
  72. second), store that result in a
  73. variable, then continually get the
  74. time value again until the value I
  75. get equals the value stored in the
  76. variable.  (By the way, it is
  77. possible for the timer to be updated
  78. during this subroutine, and if I am
  79. really unlucky I could access a
  80. low-order byte that has not yet been
  81. updated to zero and a next
  82. high-order byte that has already
  83. been updated.  Do any readers have
  84. an answer to that one?)
  85.