Classic Computer Magazine Archive ANTIC VOL. 2, NO. 7 / OCTOBER 1983

STARTING LINE

TIME YOUR FIRE

Reflex action for beginners

by DAVID FAUGHN

Requires 16K RAM

Every ATARI knows how to tell time, at least it keeps time while it's running, and you can use this timer in your programs. An internal clock is necessary for the ATARI to do things in the proper order, and one of the most important of these chores is the regulation of the video screen.

The screen is redrawn sixty times a second. Within that small interval the computer directs the video beam to race back and forth across the screen, from upper left to bottom right, then leap to the upper left for another pass. This leap, called the vertical blank, is generated each 1/60 of a second by a crystal-controlled pulse that also drives the real-time clock.

Just as a wrist watch keeps time inside and shows the time outside, the "face" of the real-time clock reflects the time gone by. This face is in RAM memory at locations 18, 19, and 20; and you can PEEK at these to check the time.

Type in this short BASIC program:

10 A=PEEK(18)
20 B=PEEK(19)
30 C=PEEK(20)
40 PRINT A,B,C
RUN

You should see something like this:

0 53 196

The values will differ, but the first will probably be zero (unless your computer has been on for a long time). Run the program several more times. The middle number will gradually increase, but the last number may be as low as zero or as high as 255. These numbers show ATARI time at the instant you asked for it with the RUN command. The first number is sort of like an "hour hand", the middle one a "minute hand," and the last one like a "second hand," but not exactly. The ATARI doesn't keep hours, minutes, and seconds.

The minimum unit of time for the RTCLOK (as it is called) is the "jiffy," or 1/60 of a second. Memory location number 20 counts jiffies. Since each memory location holds one byte in the ATARI, and one byte can be used to count from 0 to 255 (00000000 to 11111111), location 20 starts again at 0 after the 256th jiffy. To figure the number of seconds required for location 20 to recycle, divide 256 by 6O -- you will get approximately 4.27 seconds.

Every time location 20 gets full it increments location 19 by one (forgive the redundancy, increment means to increase by one, and decrement means to decrease by one). So the middle number tells how many units of 4.27 seconds have gone by -- a strange way to tell time. Location 19, also being one byte, recycles after 255 increments, which is equal to 256 times 4.27 seconds, or about 18.22 minutes.

When location 19 recycles, it increments location 18, which counts periods of about 18.22 minutes (convenient correspondence, eh what.'). Well, obviously we are going to tell you that location 18 counts to 255, after which the whole thing starts over like the odometer on an old car - but that takes more than three days!

It should be clear that easy arithmetic can translate from ATARI to human time. The number in location 20 counts sixtieths of a second; location 19 tallies 4.27 seconds; and location 18 represents periods of 18.22 minutes. And you can PEEK at these values even within a program.

The ATARI starts counting when you turn it on, and keeps counting until you turn it off - it doesn't stop when you look at the counters. Moreover, it doesn't care what the counters say, so you can set them back to zero any time you want. This makes it possible to use the RTCLOK to time a game or anything else.

Trigger Finger is a simpie program to demonstrate this feature. It is also an interesting test of reflexes, or possibly the responsiveness of your joystick. Let's take a look at what the program does.

Essentially, it measures the amount of time it takes you to press the fire button on your joystick a certain number of times. You can choose a number greater or less, but 50 times makes good test. First you are asked for the number of "spaces" you want, i.e., the number of times to press the trigger. When the program runs, every time you push the trigger a one-space block appears on the screen. As soon as you reach the preset number, the program prints out the time it took you.

In the program, timing is performed in lines 80 through 200. Line 80 resets the timers to 0. At the end of the race, line 110 PEEKS memory locations 18,19, and 20, and lines 120 through 150 convert the readings found there to a time interval. Line 125 changes jiffies to hundredths of a second. The time elapsed between resetting the registers and the end of the race is then printed out in lines 190 and 200.

The program rests itself in line 300 by RUNning itself again. The advantage of using these internal timers in a game is that they run completely independently of actions that may be occurring in your own program. If your BASIC program is busy searching through its line numbers for a GOSUB or a GOTO, the counting of these timers continues at an exact rate. The advantage of this for writing timed games, timed math drills, and other programs requiring accurate timing is obvious.

Listing: TIMEFIRE.BAS Download