I/O and YOU
What happens when you press a key
by MARK GRICE
Proposition: After many hours of computer analysis, you have finally succeeded in solving a problem which has troubled the human race for 500 years. Your data might even win you the Nobel Prize. There's just one hitch - there's no way to save the data!
Input/Output (often abbreviated I/O) is one of the most important aspects of computing, yet it is one of the last skills a programmer perfects. I think the reason for this is simply that I/O scares people. That's too bad, because I/O is easy to understand.
I/O AND THE COMPUTER:
As a computer goes about its business, you must interrupt it to get it to pay attention to you. Let's look at a simple I/O procedure: Keyboard Input.
Turn on your ATARI and touch a key. Listen closely. Do you hear a click? That click means that an interrupt has occurred. You have pressed a key, the computer has recognized that action, and now must respond to it. You have just participated in computer input. Your end of that input is now finished, but for the computer, the whole thing is just beginning.
First, it decides which key has been pressed by scanning its keyboard. This generates a code number for that key. At this point, the computer is ready to proceed, but there is a problem. The number that it has in its hot little hands is not a standard code. That number is a special internal keyboard code unique to the ATARI.
So big deal, right? Well, it is a big deal. In order for computers to communicate with each other, some standardization is needed. Also, stanardization means that certain peripherals will work with almost any computer. You can imagine how much an Epson printer would cost if Epson had to have a different model for every computer. The point is that we need a standard, and we have one: the American Standard Code for Information Interchange, ASCII for short. Atari developed its own code, called ATASCII, meaning ATARI ASCII. ATASCII is a superset of ASCII, containing all of the ASCII characters plus extra characters found only on the ATARI.
Let us look once again at the problem our computer faces every time we hit a key. It now knows which key is involved, but it's not allowed to use the keyboard code to print the character on the screen. So, it hands the keyboard code to the Operating System. The Operating System changes the number to its ATASCII equivalent, and gives it back to our poor distraught computer. Now our computer can print the character. It does this each and every time you press a key!
Now let's try to apply this. Every time you hit a key, the keyboard code (interrupt value) is placed in memory location 764 (decimal). This is not the ATASCII value, it is the keyboard value. The computer checks location 764 sixty times per second to determine if a key has been pressed.
This information can be used, for example, to monitor the keyboard for input by a user, as in selection from a menu. We can PEEK(764) to get the code for the most recent keypress, eliminating the need for an INPUT Statement and the associated [RETURN]. Here is a quick program to demonstrate this:
10 A = PEEK(764)
20 IF A = 63 THEN GOTO 100
30 GOTO 10
40 END
100 PRINT "AN 'A' WAS HIT!"
Similarly, you could write a game using location 764 instead of joysticks. Enter this short program:
10 A = STICK(0)
20 IF A=7 THEN X=X+1
30 IF X >38 THEN X = 37
40 IF A = 11 THEN X = X-1
50 IF X<1 THEN X=1
60 POSITION X,10:PRINT "0"
70 GOTO 10
If the joystick in Port 1 is moved to the right, a string of 0's will be drawn to the right; and if the joystick is moved to the left, a string of 0's is drawn to the left. Now change the program to this:
10 A= PEEK(764)
20 IF A=7 THEN X=X+1
30 IF X>38 THEN X = 37
40 IF A = 6 THEN X = X-l
50 IF Xt 1 THEN X = X-l
60 POSITION X,10: PRINT "0"
70 POKE 764,255: GOTO 10
80 REM Poking 764,255 clears register 764
In the second example, the same thing is accomplished with the left and right arrow keys, respectively, without the Control key. I would like to point out to all those who are afraid of Input/Output that the process of reading a joystick is considered to be input.
Before we leave location 764, suppose we are writing a program and want to keep unwanted eyes from prying into our files. One obvious way to create a protection scheme is to request the user to INPUT a password and have the program verify the password:
5 DIM PASSWORD$(5)
10 PRINT "ENTER PASSW0RD"
20 INPUT PASSW0RD$
30 IF PASSWORD$ = "ATARI" THEN GOTO 100
40 IF PASSWORD$ <> "ATARI" THEN GOTO 200
100 REM: Here is the rest of the program
150 END
200 PRINT "A SPY! A SPY!"
There are better ways. If you really want to restrict access, don't advertise that a password is needed. A person looking over the shoulder of the operator could discover the password. Time again for Agent 764:
10 SUM=0
15 GRAPHICS 0: POKE 764,0
20 A = PEEK(764)
25 CODE = 224: IF A = B THEN GOTO 20
30 SUM = SUM + A
40 IF SUM = CODE THEN GOTO 100
50 IF SUM > CODE THEN GOTO 200
55 B = A: GOTO 20
100 PRINT "THAT'S RIGHT"
110 POKE 764,255: END
200 SOUND 0,100,8,10: IF PEEK(764)=12 THEN GOTO 310
300 GOTO 200
310 SOUND 0,0,0,0: GOTO 10
I will briefly explain the above program. Basically, we are adding together all the keyboard codes of the keys that spell the world "ATARI". The sum is 224.
First, we set location 764 to 0. When a key is pressed, it changes the value of 764. We store this new value into our temporary storehouse, A. We add this to our SUM. Now we check it against the final CODE. If SUM is equal to CODE, the operator is either authorized, or very lucky. If SUM is larger, the operator is unauthorized or stupid. Either way, we will act accordingly.
Now we take A and put it into B. Why? Because we are now operating at machine speed, not human speed. And before the fastest typist can hit two keys, the computer has checked location 764 about 32 times! Needless to say, that adds up fast. So we check to make sure the value of 764 has changed. Otherwise, we ignore it (IF A=B THEN GOTO 20) and keep checking it until it does change. This presents a small problem -- you cannot have a password with the same letter used twice in succession, like the password "AAAAY", for example (sorry, Fonz). This is a small disadvantage considering all of the secrecy we gain.
To close this introduction to Input/Output, here is a short program that will show the value that is placed in location 764 by any keypress:
10 OPEN #1,4,0,"K:" REM Opens IOCB #1 for input from the keyboard
20 Y = PEEK(764): IF Y = 255 THEN GOTO 20: REM. Check for keypress
30 GET #1,X: REM Obtain ATASCII value
40 PRINT "YOU PRESSED ";CHR$(X)", PEEK(764)= ";Y
50 POKE 764, 255: REM Clear register
60 GOTO 20: REM Do it again