One of the interesting things you can do with TINY BASIC on your ELF II is to control the various input and output devices of the computer. For example, TINY already displays all its PRINT output on the screen of your TV set; with a little cleverness and a special command you can also draw pictures and graphs. In this chapter you will also learn how to interact with the hex keypad and display.

Let us start with the TV. Every dot on the screen which can be either white or black is represented by a bit in memory. TINY BASIC has a special command to allow you to set any point to white or black. The command is normally used to plot graphs on the screen, so it is called the PLOT comrnand. To use it you need to specify which dot on the screen is to be affected, and what its color will be. The position is given in vertical (between 0 and 41) and horizontal (between 0 and 63) coordinates, and the color is either black (value 0) or white (value 1). Unlike the notation you learned in school, position 0,0 is in the top left corner. Try this program out to get a feel for how the PLOT command positions the dot:

When you run this program, give it two numbers (separated by a comma) between 0 and 40. There are two things about this program I have not yet explained. Line 40 is a special form of the PLOT command which clears the whole screen (like when you type control-L). The other thing is that no matter where you plot the dot, exactly two lines under it is the next message asking for another coordinate pair. This is because the PLOT command positions the cursor at the coordinates you selected, then the two PRINT commands moved it down two lines. Experiment with the program a little. See where it places the little dot (the one above the word "COORDINATES". not the blinking cursor) as you give it various numbers. What happens if the numbers you give it are negative or greater than 41 (or 63)? What happens if you take the two PRINT commands in lines 70 and 80 out? What happens if you delete line 40? If you do that, you will see another effect: nothing erases the screen (to blacks) except the scroll-up when you get too near the bottom, so it becomes more and more covered with white.10 PRINT "COORDINATES";20 INPUT X,Y30 REM CLEAR SCREEN40 PLOT 1250 REM PLOT THE POINT60 PLOT X,Y,170 PRINT80 PRINT90 GO TO 10

Let's use the PLOT command to draw a graph for some equation. So that the program will not die if it gets a point out of range, we will write a subroutine to check the range before plotting (P.S. CLEAR out the old program first):

Notice that we have two IFs on the same line! You saw this before In the checkbook program. If the first is false, the rest of the line is ignored; if it is true, then the command on the rest of the line is executed. This command happens to be another IF, which if it is false the rest of the line is ignored, and so on. The PLOT command has to have coordinates in the range 0-41, 0-63; it would be nicer if they went from -20 to +20 and -30 to +30, so we compute the necessary offset to make it seem so. Also, we normally expect more positive Y-values to plot at the top of the graph, so Y is subtracted from rather than added to the offset. The next line (line 820) is a trick to let us look at the graph for 1 /4 second before continuing.800 REM PLOT X,Y IN RANGE810 IF Y>-21 IF Y<21 PLOT 20-Y,X+32,1820 PLOT 0,0,10830 RETURN

We need to clear the screen, then draw coordinate axes:

If you want to see the program so far, add a line "400 END" and RUN it; you can watch the axes grow right before your very eyes! Can you figure out how it works?100 REM FORMULA PLOTTER110 PLOT 12120 C=0130 X=C140 Y=0150 GOSUB 800160 X=-X170 Y=-Y180 IF X<0 GOTO 150190 IF Y<0 GOTO 150200 Y=X210 X=0220 IF Y>0 GOTO 150230 C=C+2240 IF C<32 GOTO 130

Now we are ready for the function to plot. The equation will be a subroutine at line 1000. This is the driver for it:

Let's try a simple parabola:300 X=-30320 GOSUB 1000330 GOSUB 800340 X=X+1350 IF X<31 GOTO 320400 END

When you RUN this, you will see a solid line across the bottom of the parabola. This results from the squares of low values of X (which is then divided by 10). After you RUN that one, you might want to try a trickier formula:1000 Y=X*X/10-101010 RETURN

If X is zero you would get an error stop from the division (you can't divide by zero, you know), so the formula is evaluated only if X is not equal to (ie. "less or greater than") zero. The one time the formula is not evaluated the previous value for Y (which was -100) is used.1000 IF X<>0 Y=100/X

Now try some of your own equations. What can you do to shift the coordinate axes, say if you want more of the first quadrant? Hint: do it in the plotting routine (lines 800+). Experiment!

There are two very obvious Input/Output devices on your ELF II: the hexadecimal keypad and the two-digit hexadecimal display. And TINY is able to talk to them. Start with the display. This is output port number 4 in the machine, so the command to output to it is (obviously enough) OUT 4,(data). For data you can use any number or computed value. Try this:

Do you know why the display shows "63", not "99"? The 99 that you typed was in decimal, while the display is hexadecimal. Maybe this program will help you to learn the difference:OUT 4,99

Run this program, and give it various numbers. What happens if you type in 255? 256? 0? You see, only the low eight bits of the number you type in are displayed. This is the same as saying that only the remainder after dividing by 256 is displayed.10 INPUT H20 OUT 4,H30 GOTO 10

Input from the keypad is a little harder. This is because you cannot just tell it to Input the keyboard value without telling TINY what to do with the number it gets from the keys. So there is a special phrase you use to read the keypad, but this phrase is used only where TINY might reasonably expect to find a number. For example, the number might be PRINTed:

Or you could use it in a calculation and store the result in a variable:PRINT INP(4)

This computation, incidentally, converts the Input so that TINY can think of it as a decimal number instead of hexadecimal. Try it in a program, the rest of which might be:10 LET J=INP(4)-INP(4)/16*6

Run it and watch the printout as you push various keys on the hex keypad. You might even notice that some funny values turn up just as you push a key. This is because the program looks at the keys twice, expecting both times to give the same result. This is not necessarily so, especially if you are pushing keys at the time. You can remedy this problem by modifying the program to read the keys into a variable (with a LET), then computing with the variable instead of multiple INP references. See if you can make it work.20 PRINT J30 GOTO 10

You may have guessed by now that if INP(4) and OUT4 refer to the hex keyboard and the display, what about INP(1), INP(2), OUT5, OUT6, etc.? Well they do exist. INP(1) turns the TV Interface circuit on (but don't expect to see much!). The others relate to various input and output ports you may or may not have attached. This is not really the place to discuss what they can do. Experiment!

There is one other function which you will find interesting. A function In TINY BASIC is one of the special forms consisting of a function name followed by an argument (some numerical value) in parentheses. INP(4) is a function. The other interesting one is used to create an unpredictable number. We call it a random number, but it is not truly random, because it repeats after 32768 times. However, that is seldom enough that you will not notice it. The number that TINY creates is between zero and some upper limit which you specify in the argument. For example, RND(100) has a value somewhere between 0 and 99, inclusive. Here is a little program I like to leave My ELF doing when it has nothing better to do:

After a half hour or so the TV takes on a kind of starry look.10 REM TWIWLE20 OUT 4,RND(256)30 LET T = RND(5)/440 PLOT RND(42),RND(64),T50 IF T=0 GOTO 2060 PRINT70 GOTO 30

A friend of mine uses TINY BASIC to teach his kids their arithmetic.. The program generates a problem, and asks for the answer. If it is right the kid gets some sort of brownie points. The following is only an outline of how the program might work:

A few lines in this program deserve some discussion. Line number 160 prints nothing, in quotes (open quote followed immediately by close quote), then has a comma before the variable name A. The purpose of the comma is to center the next item in the middle of the TV screen (in this case; if you have already PRINTed past the center, It goes to the left margin of the next line). But the comma cannot be the first thing in the PRINT statement.100 REM TUTOR120 LET A=RND(9)+l130 LET B=RND(9)+l140 LET F=RND(3)150 PRINT160 PRINT "",A170 PRINT " ";180 GOSUB 500+F*100200 PRINT210 PRINT " ";220 PLOT 95230 PLOT 95240 PRINT250 PRINT260 PRINT "RESULT";300 INPUT G310 IF G=R GOTO 400320 PRINT "NO, ";A;330 GOSUB 500+F*100350 PRINT "=";R;", NOT ";G360 E=E+1370 GOTO 120400 PRINT "CORRECT!"410 GOTO 120500 REM ADD510 PRINT "+";B;520 LET R=A+B530 RETURN600 REM SUBTRACT610 PRINT "-";B,620 LET R=A-B630 RETURN700 REM MULTIPLY710 PRINT "*";B,720 LET R=A*B730 RETURN

Line 180 looks a little strange until you realize it computes a line number. If F is zero, the GOSUB goes to line 5W, if F is 1 it goes to 600, if F it 2 it goes to line 700. F can only be 0, 1, or 2 because of line 140.

I think you should be able to figure the rest of this program out yourself. You probably already noticed that it is too big for your 4K memory as it is, but you know how to squeeze it in (and it will fit, with trimming). However, be sure you leave lines 500, 600, and 700 in (you can trim them to just REM) or you will get error number 46.

This program, as it stands, is probably not suitable for kids. It should check to be sure the subtractions do not result in negative numbers, and you may want to grade the difficulty of the problems. Computer Assisted Instruction (called "CAI" for short) turns out to be as big a can of worms as financial problems.

Now that you understand how TINY BASIC works, the next step in learning to write programs is to write programs. What is it you want your computer to do for you? Lay out on paper (in English) step by step, exactly what you want the computer to do. Then rewrite those steps in rules that explain (to you or another human; do not use TINY BASIC yet) exactly how it is to be done. Finally, convert these rules into the TINY BASIC statements to do the job. Type them in and try it out. Programming is like driving: you have to know where you want to go, but the best way to learn is by doing it. Don't worry about making mistakes; TINY doesn't care, and who else will know, if you discover and fix them?

Now that you have a good feel for using TINY BASIC, you will probably want to go back and read the User's Manual. There are some things in it I did not tell you about, and it is a little easier to find things in it than in this tutorial.

If this sounds like the end, it should. The second part of this book deals with esoteric programming techniques which are probably to complicated for you to handle right now. First get some experience writing programs, then come back and read the rest of this book.