Classic Computer Magazine Archive COMPUTE! ISSUE 8 / JANUARY 1981 / PAGE 50

THE apple® GAZETTE

Load PET Program Tapes Into The APPLE II

Keith Falkner

Applesoft BASIC and COMMODORE BASIC are very similar languages. For this reason it is feasible to load PET programs into an APPLE II running Applesoft BASIC in either ROM or RAM. There are problems every step of the way, but this program solves most of them, with the result that the APPLE can actually run very many of the fine programs available for the PET. Of course there are some profound differences between the two computers, and programs which exploit features of the PET which are absent from the APPLE may be impossible to run.

I call this program "PET Loader", and it requires an APPLE II with Applesoft BASIC. There are two versions, one for the APPLE II PLUS as shown in the listing, and one for an APPLE II with Applesoft in RAM locations $0800-$3000. To load a program which just filled an 8K PET, an APPLE II PLUS uses 10K, and an APPLE II uses 20K. The PET program can then be saved and loaded exactly as an Applesoft program. PET Loader is not needed again for that program.

The operating instructions for PET Loader are in Table 1. If it detects an error, it prints a single digit to identify the error, followed by the message "ERR" and a beep, then it quits. Table 2 shows what has caused the error, and what to do then. Because the PET writes tape very dependably, and because there are two copies of the program on the tape, poor reads are rare, and the usual result is a cheerful 'OK', just after the traditional beep signalling 'stop the tape'. PET Loader then returns to Applesoft, and the "J" prompt appears.

Now the fun begins, or so the saying goes. There are many differences between the PET and the APPLE, which the program could not resolve. When real intelligence is needed, it all depends on you! A PET program loaded into an APPLE is still not an APPLE program. Here are the major differences which you will have to consider, when you try to produce a usable APPLE program:

1. No Equivalent Verb in the APPLE.

OPEN, CLOSE, VERIFY, and CMD are commands in COMMODORE BASIC. Each of these is translated "STOP", and you will need to decipher the programer's intent and program the equivalent for the APPLE. Refer to COMMODORE's excellent manuals for descriptions of these commands.

2. Specific Device Reference.

Programs containing OPEN and CLOSE will also contain either PRINT# or INPUT#, which are simply translated to PR# and IN# respectively, and will require substantial rework. The devices, by number, are conventionally these:

#0 the keyboard
#1 first cassette drive
#2 second cassette drive
#3 the screen
#4 the printer (or maybe not)
#8 the dual disk drive

3. Reference to Actual Memory Locations.

PEEK, POKE, CALL (SYS in the PET), WAIT, and USR refer to specific locations in memory, and you will need more help than I can offer here.

4. Keys to Move the Cursor.

The PET has ten keys to control the position or action of the cursor. Eight of these are missing from the APPLE. PET Loader translates these as follows: Two functional equivalents-

CURSOR-LEFT becomes BACKSPACE, and appears as

"GR" in the program.

CURSOR-DOWN becomes LINE-FEED, and appears as

"PR#" in the program.

(Odd as they appear, these actually move the cursor exactly as stated.)

One destructive approximation:

CURSOR-RIGHT becomes SPACE, which obliterates what it should space past. This appears as "COLOR = " in the program.

Seven non-functional comments:

When the program is listed, these are visible, looking like genuine verbs, and it looks as if the name of the key will be printed. For example,

100 PRINT "CLEAR"
110 INPUT "INVERSE INSTRUCTIONS NORMAL";Z$

In fact, line 100 will neither clear the screen nor print "CLEAR". It will merely print an equal-sign (=). Line 110 will print "INSTRUCTIONS", and no trace will be seen of the INVERSE and NORMAL commands shown in the listing. This behavior can be perplexing, because usually with Applesoft, what you see is what you get. The purpose of these translations is to disclose the intent of the program, so the cursor-keys of the PET are translated thus:

PET key: RUV OFF HOME CLR UP DEL INST
Lists as: INVERSE NORMAL HOME CLEARN VLIN DEL IN#
Result: nil nil nil "=" nil nil nil

...so when you list the program and discover:

300 INPUT "HOME PLAY INVERSE AGAIN NORMAL"; X$

substitute the equivalent APPLE code, which in this case is:

300 VTAB 1: PRINT "PLAY": INVERSE:
PRINT "AGAIN";: NORMAL: INPUT "?";X$

For all those keys except INST and DEL, the equivalent in Applesoft is easy to devise, but logic to produce the function of PET's INSERT and DELETE keys is extremely difficult, and APPLE's convoluted screen addressing makes this task truly hairy. Fortunately very few PET programs print INSERT or DELETE characters.

5. Printing of numbers is slightly different.

290 X = 4 : Y = - 6
300 PRINT "X IS"; X ; "Y IS" ; Y ; "."

The PET prints: X IS 4 Y IS-6.

THE APPLE PRINTS: X IS4Y IS-6.

The PET prints a blank before positive numbers, and a CURSOR-RIGHT after all numbers; the APPLE does neither. By the way, all four semicolons (;) in line 300 above are optional in both computers.

6. A side effect of TAB.

A PET can TAB over data already on the screen; the APPLE wipes it out, so use an HTAB verb in place of a TAB phrase if this difference mars the display.

PET: 40 PRINT TAB(11) "XYZ"
APPLE: 40 HTAB 12 : PRINT "XYZ"

7. Computations in Boolean Arithmetic.

In the following lines,

400 X = 11 : Y = 6 : Z = X > Y
410 PRINT Z : IF Z THEN 500

The PET will set Z to -1, and line 410 will print this result, then go to 500. The APPLE will set Z to +1, and print this different result, then go to 500. In the above example, the difference may not be crucial, but it often can be. Because the PET does bit-by-bit evaluation of the operators OR and AND, in

700 X = 11 : Y = 6 : Z = X AND Y

PET's result will be Z = 2, because the bit pattern of 11 is 0000 1011 and the bit pattern of 6 is 0000 0110, and these two patterns, ANDed, give 0000 0010, arithmetically 2. APPLE, on the other hand, merely sees that neither X nor Y is FALSE (zero), calls the result TRUE, and gets Z equal to 1. This can be a very subtle pitfall.

8. Random numbers.

RND (0) gives a genuine random number each time in the PET, but in the APPLE, it repeats the previous random number. Simply replace the 0 with a 1.

9. The GET command.

In the PET, GET does not wait for a key to be struck, so the sequence

333 GET P$ : IF P$ = "" THEN 333

is the customary way for a PET program to wait for a key. Oddly enough, this is completely appropriate in the APPLE, because if the key struck is CTRL-@, then P$ will be the null string. Ignorance of this is an obscure bug in some Applesoft programs. When the PET program is testing for a key but not waiting when no key has been struck, a different approach is needed. For example,

PET: 60 GET A$ : IF A$ = "" THEN 100 APPLE: 60 ON PEEK (-16384) < 128 GOTO 100: GET A$ : IF A$ = "" THEN 60

10. Graphics Characters and Lower Case.

The PET can produce lower-case letters and many graphic symbols which the APPLE cannot, and there are two display-modes in the PET. $C1 might mean "a" (old PET), "A" (new PET), or the symbol for the Spade suit (any PET). PET Loader looks for $CF, probably a lower-case "o", in the PET program. If $CF is found, all letters are translated to upper case; if not, graphic symbols are translated into a similar character the APPLE can produce.

11. Direct Screen Addressing.

In both computers, the video screen occupies a part of memory, and a POKE to a storage location in the screen memory will produce a character on the screen. The relationship between memory location and screen position in the PET is straight-forward, but it is quite complicated in the APPLE, and therefore not often used. Nevertheless, it is worth mastering, because there are hordes of PET programs which use this technique, and a lot of them are attractive games. The PET has 25 lines of 40 columns, and the memory location of each byte of the screen can be computed thus (the expression is not written in BASIC):

LOCATION = 32768 + 40 * LINE + COLUMN

where the upper left corner is LINE 0, COLUMN 0.

The APPLE has 24 lines of 40 columns, and the memory location of each byte can be calculated by:

LOCATION = XL% (LINE) + COLUMN

where the array XL% has been initialized thus:

1000 DIM XL% (23): FOR I © 0 TO 7:
XL% (1) = 1024 + 128 * 1:
XL% (1 + 8) = 1064 + 128 * 1:
XL% (1 + 16) = 1104 + 128 * 1: NEXT

As before, the upper left corner is LINE 0, COLUMN 0. In applying this tactic take care not to let COLUMN exceed 39, or you will cause destruction of some important values in memory. For example, a POKE to valid LINE 23, and invalid COLUMN 49, will likely cause loss of your BASIC program.

12. Numeric Keypad.

Programs which use screen-POKEs to move pieces of a game around the screen use the keys 1-9 to indicate the direction of motion. This is satisfactory on the PET, because these keys form a square, with 7,8,9 above 4,5,6 above 1,2,3 and it is natural that, if the 5-key means ‘stop’, the 8-key means ‘up’, and the 3-key means ‘down and right’. APPLE'S numeric keys do not form a square, so some substitute must be devised. None is immediately obvious, but perhaps the parallelogram formed by R, T, Y above F, G, H above V, B, N would serve, since the ‘BELL’ on the G-key can be easily remembered as being a ‘home’ position. Often the game can be improved by substituting use of the game paddles for these keys; why should you accept the limitations of the PET?

13. Sound.

Without an accessory, the PET is silent, but a simple way of making noises has become very popular. Discovery of a POKE to 59466 is strong evidence that this technique is in use. Here is how to deduce the programmer's intent:

POKE 59467,16 startmaking noises (musical tones)
POKE 59467,0 stop making noise altogether
POKE 59466,15 typical values to define an octave (or 51 or 85)
POKE 59464, X high X is a low note, and vice versa.

It is important to remember that once the PET issues three POKEs, the sound is continuous, whereas the APPLE must continually address the speaker to keep making noise. This part of the conversion can be left for last, because the POKEs address APPLE's ReadOnly Memory, which is unaffected by POKE.

14. Real-time clock.

The PET has a genuine timer, which programs can address in two ways. Variable TI increases by 60 every second, and can be read but not written; string TI$ is six numeric characters, in the format HHM-MSS, and can be read and written.

400 PRINT "THE TIME IS" ; TI$

TI$ is computed from the instantaneous value of TI, and formatted as up to six digits. If you try running a PET program in your APPLE, and it just stalls, doing nothing at all, press CTRL-C to stop it, and you may find lines like

700 X = TI + 60
710 IF TI < X THEN 710

Line 710 is merely waiting for a second to elapse, and in the timeless APPLE, it never will. Substitute a FOR-NEXT loop of the appropriate duration.

15. PI.

A single key on the PET provides the number PI, 3.14159265. No such facility exists in the APPLE, so PI is translated into a character which prints as "UNDEF'D FUNCTION". This causes "?SYNTAX ERROR", and is simple to correct.

16. The INPUT statement has subtle differences.

The PET provides a question mark after the prompt for INPUT, and for neatness, you should supply one. If the PET issues an INPUT statement and the user merely presses RETURN, execution of the program ceases at once. This is such a nuisance that programs with any elegance guard against it in a variety of ways, for example:

50 INPUT "WHAT NOW > > + < < <"; X$

where ‘>’ and ‘<’ stand for keys to move the cursor right and left. You can tidy this up as you supply the question mark for the prompt.

As you ponder all the differences and incompatibilities listed, and the other differences which you will find, you may wonder if the resulting program will be worth the work. In fact, PET Loader is a potent and reliable tool, and you will appreciate it. The tape-reading process has proven particularly sound, and has error-recovery procedures even better than those in the PET.

I received a lot of help in this project, all of it from Gordon Campbell of Toronto, who provided manuals, clippings, advice, and many PET programs. He received some Applesoft programs in exchange, and he loaded them into his PET! But that is his story, and you can read it in the Sep/Oct 1980 issue of COMPUTE.

To get the source and object of both versions of PET Loader, as well as two accessory programs I have developed, send me ten dollars, and I will mail them to you on a cassette, together with printed copies of Table 1 and Table 2.

TABLE 1. OPERATING INSTRUCTIONS
1. Before loading PET Loader, ensure the BASIC pointers in page zero are normal, by either issuing the DOS command "FP", or powering off and on.
2. Load PET Loader from tape or disk and run it. It will ask you to "PLAY" a PET program tape and press a key. Do so (or press CTRL-C to quit).
3. Any clicks you hear indicate unreadable bytes on the tape. Before or between programs on the tape, these are perfectly normal events.
4. After 10-40 seconds, the name of a program will appear near the top of the screen. If you wish to bypass this program, press CTRL-X promptly, and the name will disappear, then you can press a key to try the next program.
5. Many error conditions are tested at this point. Refer to Table 2 if any of them occurs. The signal is a digit, the letters "ERR", and a "BEEP".
6. The image of the program will be read into memory, and simultaneously be displayed in a 32-character window. Some of it will appear to make sense.
7. Relax. PET programs load slowly, for example 20K in nine minutes. If the speaker clicks during the load, the wait will be doubled, because PET Loader will have to read the second copy of the program as well.
8. When the whole program is loaded, a "BEEP" is issued. Stop the tape.
9. There is a very brief pause while tokens and data are translated, then the message "OK" is issued, and the Applesoft prompt "]" reappears.
10. You now have a PET program in your APPLE. Refer to the article, to turn it into an APPLE program (there's always a catch, isn't there?).
11. To rerun PET Loader, just type "&" You don't need to reload the program.

MAKING IT WORK

The best way to enter PET Loader into your APPLE is to have an Assembler. I used Programma's mighty ASM/65, whose only idiosyncracy is the use of "<" and ">" to denote the low and high halves of a two-byte address. (The ".FILE" lines serve to connect pieces of a program which will not all fit in memory.) Next best is the Mini-Assembler which is part of Integer BASIC. Worst is byte-by-byte keying through the Machine-Language Monitor. If you are capable of doing any of these, you already know how (this is not the program to learn the technique on!).

To package PET Loader as an Applesoft program, execute these instructions after loading it into locations $800-$BF6, using the Machine-Language Monitor:

TABLE 2. ERRORS AND RECOVERY

If PET Loader detects an error it cannot handle, it issues an error message and quits, returning control to Applesoft. The error message is a digit and "ERR"

MESSAGE MEANING AND RECOVERY
1 ERR A long search for a Header Label has not found one. If this really is a PET tape, perhaps the volume is improperly set. PET tapes are louder than APPLE tapes, so adjust the volume by ear and try again.
2 ERR Neither copy of the label could be read correctly. This might also be improper volume, but is probably a bedraggled tape. Either try again, or acquire another copy of the program.
3 ERR The header label was read correctly, and a code in it says that the contents of this file is not a BASIC program, but data. PET Loader reads programs only. Try another file or another tape.
4 ERR This program is too big to fit in memory. Perhaps the pointer at location 115-116 ($73-74) was altered by HIMEN:, so issue the DOS command "FP", or HIMEN:16384 (or what have you), and try again.
5 ERR After the header label was read correctly, the program should follow. It was not found, perhaps because the PET user aborted the "SAVE". If repeated tries cause the same message, that is most likely.
6 ERR This program loads at an unusual address in the PET. Possibilities are: proprietary program, machine-language or partly so, or some attempt to make the program difficult to copy (or use?). Forget it!

To try again, just type "&" and press RETURN. With RAM Applesoft "CALL 1013". Use this tactic even if you have typed "NEW" or "FP", or have pressed RESET!

*67 : 01 08 00 0C 00 0C 00 0C
*AF : 00 0C N E003G
(which gets you back to Applesoft)

then LIST the program; you should get only one line: 65535 CALL 2064 : END PET Loader can now be saved, loaded, and run exactly as an Applesoft program, except that PET Loader cannot be saved after it has been run. The program occupies locations 2048-3071 ($800-$BFF), and makes them unavailable to Applesoft. This costs 1024 bytes of memory, but saves having to reload PET Loader to convert the next program. To restore the pointers to normal, issue the DOS command "FP", or type "CALL - 151" then enter Applesoft "cold" with CTRL-B.

The version of PET Loader which operates with RAM Applesoft occupies memory locations 12288-13311 ($3000-$33FF). The bogus Applesoft program within it says "CALL 12304 : END", and references to locations within the program are $2800 bytes higher than shown in the listing of the version for ROM Applesoft.