home *** CD-ROM | disk | FTP | other *** search
- Convenient Timing in BASIC Programs
-
- Wayne Hammond
- St. Louis IBM PC Club
-
- While doing some programming in
- BASIC, I had the need to set a
- variable with a value representing
- the time of some future event. I
- wanted to hang in a loop until the
- current time reached my
- pre-determined time. I wanted this
- in the form of a variable rather
- than a timer interrupt so that logic
- could be added to the loop and a
- branch out based on keyboard input
- as well as time expired.
-
- I worked out a way to do it in
- BASIC. In the Technical Reference
- manual I noticed that four bytes of
- low main storage are constantly
- updated by the timer. These four
- bytes contain a 32-bit binary number
- that is initialized at IPL to zero,
- and is incremented by 1
- approximately 18 times per second.
- These bytes are at hex locations 46C
- through 46F. The bytes are stored
- in reverse order; the byte at 46C is
- the low-order, the byte at 46F the
- high-order.
-
- I can get at these bytes from a
- BASIC program with the PEEK
- function. Remember that, since
- these bytes are outside the segment
- that BASIC handles, I first have to
- specify the segment address before
- doing the PEEK, and I must store
- the segment address after doing it.
- The following BASIC subroutine will
- access the complete timer value:
-
- 10000 DEF SEG=0
- 'Set to segment address base 0
- 10010 TO=PEEK(&h46C):
- T1=PEEK(&H46D):T2=PEEK(&h46E):
- T3=PEEK(&h46F) 'GET 4 BYTES
- 10020
- TVAL=TO+256*(T1+256*(T2+256*T3))
- ' Giving each byte its positional
- value
- 10030 DEG SEG
- 'Restore to BASIC segment address
- 10040 RETURN
- 'Return from subroutine
- 10040
-
- If I want to retain the precision of
- TVAL regardless of the timer's
- value, I'd better define TVAL as
- double-precision (using the DEFDBL
- statement), because BASIC's
- single-precision real numbers have
- less than 32 bits of accuracy. To
- set up to test for the end of a time
- interval, I would get the current
- timer value, add to it a value that
- corresponds to the time I want to
- allow to elapse (add 18 for each
- second), store that result in a
- variable, then continually get the
- time value again until the value I
- get equals the value stored in the
- variable. (By the way, it is
- possible for the timer to be updated
- during this subroutine, and if I am
- really unlucky I could access a
- low-order byte that has not yet been
- updated to zero and a next
- high-order byte that has already
- been updated. Do any readers have
- an answer to that one?)