Model

How to convert Apple programs to on a TRS-80

For all readers

David Lewis

I believe that the TRS-80s are superior to the Apple II or Apple II+, but those machines have sold better than Tandy's best. Why? Well, it has been said that the software sells the hardware, and there are more programs available for the Apple than for any other microcomputer.

You may have thought that all that software was out of reach, but both the Apple and the TRS-80 run versions of Microsoft BASIC. There are differences between the two dialects, but many can be overcome. So, then, here is a comprehensive guide to converting "Applesoft" programs to run on your Model I, III, or 4, or on your LNW, LOBO, or PMC. (Some of the guide is also appropriate for the Model 100, for the Models 11/12/16, for the Color Computer, and MC-10.) You might even learn more about your own system while doing it.

Input/Output Commands

The Apple can send or receive data through the keyboard, through expansion slots similar to those on the Model 12, or through input and output ports.

The Keyboard

Even such an innocuous command as INPUT has its differences. The Apple does not automatically insert a question mark when text to be printed follows the INPUT. When translating a line such as: 10 INPUT "WHAT IS YOUR NAME? ";NM$ be sure to remove the question mark, or you'll wind up with two.

The Apple also has a command similar to INKEY$. The GET statement, however, waits for a single key to be pressed. If you see a line such as 10 GET A$, convert it with something like: 10 A$=INKEY$: IF A$="" THEN 10

This TRS-80 line will continually loop until a key is pressed. (GET and INPUT are also used with text files; they are essentially equivalent to INPUT#. More on this later.)

The Apple makes up for the deficiency in the GET command by providing a keyboard latch, which does respond instantly. PEEK (-16384) returns a value of 128 plus the ASCII value of the character pressed. If the value is less than 128, no key has been pressed; be sure to delete the line that checks for this. Because of its responsiveness, PEEK (-16384) is frequently used in keyboard-controlled games. Another use is with the WAIT command. WAIT X,Y,Z waits until the contents of location X, when XORed with Z and ANDed with Y, gives non-zero results. In practice, WAIT -16384, 128 pauses until a key is pressed. It can be simulated by: 10 IF INKEY$="" THEN 10.

PEEK (-16384) can also be translated by using INKE Y$: 10 X= ASC (INKEY$) + 128.

Whenever address -16384 is used on the Apple, the keyboard strobe must be cleared. You can ignore the POKE -16368,0 that accomplishes this.

Paddles and Joysticks

The Apple has special joystick and paddle ports. (Oh, to have bought a business machine!) PDL(x) returns the current value (from 0 to 255) of the game control numbered x. Two separate readings, usually when x=0 and x=l, are needed for a joystick. This function can be simulated by using the input ports on the TRS-80. The manufacturers of your joystick may say that INP(236), for example, is equal to PDL(0).

In addition, the Apple can determine if a fire button is being pressed. If PEEK (X-16287) is larger than 127, then the button on control x is being pressed. This, too, is simulated by using INP(port) on the TRS-80.

Expansion Slots

The Apple has 8 slots, numbered 0 to 7. IN#X causes all input to be received from slot X, and PR#X transfers all output to slot X. Traditionally, the printer is in slot 1. If you see a PR#1, it means that whatever is printed on the screen will also go to the printer. This is one major advantage of the Apple over the TRS-80. We have to make do with LPRINT. The easiest thing to do, though by no means the most efficient, is to duplicate a set of statements, using PRINT once so that output is to the screen, and LPRINT so that the printer receives the data. PR#0 turns the printer off.

If you ever see an IN#6 or PR#6 in a program, it means that the author was being terribly sneaky. Slot 6 contains the disk drive, so a PR#6 reboots the system. You needn't translate it, but the closest thing would be a CMD"S", which returns to TRSDOS on my Model III.

Read, Data, Restore

The READ statement, which allows access to data within the program, can be converted directly. The DATA is also the same, and RESTORE, which moves the pointer to the first line of data, also causes no trouble.

Cassette

Old Apples can use LOAD and SAVE to access programs on cassette tape. These are just like the CLOAD and CSAVE you are familiar with. However, you will probably never come across them. You'll also probably never see STORE and RECALL, which save and load an array from cassette tape. Apples are sold only with 48K and disk drives.

Sound

The last major form of output is the making of sound. The Apple has a built-in speaker that many programmers take demoniacal joy in clicking. The statement X=PEEK (-16336) generates a frequency of about 72 Hz. Another method that you will come across is to use CHR$(7) or CTRL-G, which causes a longer tone and which can be embedded within quotes. You might look at something like: 10 PRINT "YOU WIN!" 20 FOR G=1 TO 10 30 PRINT "" 40 NEXT

and not be able to figure out what's happening in line 30. Model I users might be more familiar with embedded control codes. There is a control-g within the quotes.

A machine language program, typically located 768 decimal, can be used to produce a wider range of sounds. You might see something like: 10 POKE 768, DURA 20 POKE 769, FREQ 30 CALL 770

which pokes the duration and frequency into memory and then calls upon the sound driver. Don't translate a routine that is poked into memory byte-by-byte. The Apple uses a 6502 chip which is terminally incompatible with the Z80.

The TRS-80 has no built-in speaker, but you can get sound by hooking the cassette cable into the AUX line of your cassette player. OUT 255,2 then produces a sound, of sorts. A machine language program might also be used. There's a decent one in a book Radio Shack sells called TRS-80 Graphics, by Kater and Thomas.

Flow of Control

The GOTO, GOSUB, RETURN, ON X GOTO, and ON

X GOSUB commands can be translated without any changes. So can NEW, END, STOP, and RUN. The range of linenumbers and the length of the lines on the Apple are smaller, so no problem with the size of the program exists.

There is one command that cannot be translated. It is POP, which "pops" a return address off the stack. It is used to exit from a GOSUB routine. Try to avoid getting yourself into a position in which you must leave an address on the stack. If you do it very often, you could overwrite your variables. You might GOTO the routine and jump back; you might also see if you can't put the subroutine into a large IF-THEN-ELSE statement. If you absolutely can't change a few lines, then don't lose any sleep.

One reason that the Apple has the POP statement is because it has no ELSE, and so major decision-making subroutines can't be coded into one line but must instead form a subroutine. You can directly translate any IF-THEN statement, however. The FOR-NEXT functions as it does on the TRS-80.

A slight difference between the two versions of BASIC is in error-handling. The Apple blankets all mistakes that follow with something like: 10 ONERR GOTO 100. The TRS-80 uses: 10 ON ERROR GOTO 1.00.

You will frequently see a POKE 216,0. This clears the error flag. PEEK(222) gives the number of the error. On the Apple, it is used to determine how to fix the error. It should be translated with ERR and ERL. The Apple also has only a RESUME statement. It branches back to the beginning of the statement in which the error occurred. You can translate this directly or, with minor restructuring of the program, use the more powerful RESUME NEXT or RESUME XXX.

Graphics

This is the one area of programming in which the Apple is better than the TRS-80.

Do not try to translate high-resolution graphics unless you have an LNW or Color Computer, or own a high-resolution graphics board. You will probably be able to convert a program that makes only passing use of high-resolution graphics by ignoring HCOLOR=, which sets the color to be used, HGR or HGR2, which turn the screen into graphics format, and HPLOT X,Y (to M,N), which plots a point or draws a line. Also ignore any POKEs in the range -16297 to 16304 or from -1953 to -3106, where switches for the high-resolution pages are located. The actual memory used for high-resolution graphics is 2000H to 5FFFH.

Low-resolution graphics can more easily be translated. In fact, TRS-80 graphics can access nearly four times as many blocks as Apple low-resolution can; also, we have the added advantage of being able to mix text with graphics blocks. The first thing to do is to ignore the GR and COLOR= statement, which set the mode and the color.

The Apple screen is 40 by 40, and the points are numbered 0 to 39 in both directions. The horizontal point is mentioned first. PLOT X,Y on the Apple is directly translatable into SET (X,Y). To erase a point on the

Apple, the Color is set to the background color (black is 0) and the point is replotted. If you see that this is done, substitute RESET (X,Y). SCRN(X,Y) returns the color of a point x,y. We can use POINT (X,Y) to find out if the point is lit. Both BASICs allow something like: 10 IF SCRN(X,Y) THEN PRINT "POINT X,Y IS LIT.", in which the color is determined only to exist.

Drawing lines is somewhat more difficult. A lot of speed must be sacrificed in order to achieve the same thing on the TRS-80. The Apple allows vertical and horizontal lines to be drawn. The statement VLIN 10,20 AT 30 draws a vertical line from 30,10 to 30,20, and HLIN 10,20 AT 30 draws a horizontal line from 10,30 to 20,30. Here's how to translate them. Assume the points are in the form xLIN A,B AT C: 10 'VERTICAL LINE 20 FOR D= A TO B 30 SET (C,D) 40 NEXT

10 'HORIZONTAL LINE 20 FOR D=A TO B 30 SET (D,C) 40 NEXT

Shape Tables

Unless you have GRBASIC, shape tables are hard to duplicate on the TRS-80. Basically, the Apple allows you to create a shape by giving it directions coded into numbers that will allow it to quickly redraw a shape. Instead of plotting each point every time you want to make the shape, you can just say DRAW 1 AT X, Y. Now, the original shape (a spaceship, for example) can be packed into a string on the TRS-80 and manipulated that way. However, it is very hard indeed to ROTate it or change its SCALE. Delete all references to ROT, SCALE, DRAW, and XDRAW.

SHLOAD loads a shape table definition from tape. More common is something like: 10 PRINT CHR$(4);"BLOAD CUTESHAPE" which probably loads a binary shape table from disk into memory. If it is followed by POKEs into addresses 232 and 233, which save the address for the table, erase the whole thing.

Screen Formatting

The Apple's screen is divided into 40 columns and 24 rows. It is not memory-mapped. Because the screen doubles as the area for graphics, TEXT is used to reset it. You may omit it in your translation.

The HOME statement is equivalent to CLS. You might also see CALL -936, a leftover from the days of the Apple I and Apple II.

The REM statement can be translated directly. Don't entirely delete these remarks; you should leave an apostrophe or the word REM at the linenumber in case it is the destination of a GOTO or GOSUB. The PRINT statement also can be translated directly. The TAB(x), associated with it, is the same.

Model II users will be familiar with SPC(x). On the Apple, it is used in conjunction with PRINT to leave a blank block of x spaces. Use STRING$(x,32) to achieve the same effect. 22 Basic Computing

POS(O) returns the cursor offset from the left margin in the range 0 to 39. On the TRS-80, the cursor address is stored in addresses 16416 and 16417. To find the offset from the margin in the range 0 to 63, when X begins as 0 to 255:

10 X= PEEK( 16416) 20 Y= X- INT(X/64) * 64 This is the same as X MOD 64.

The Apple uses HTAB and VTAB to PRINT at a specified point on the screen. The argument of HTAB, which understandably stands for Horizontal TAB, is 1 to 40. VTAB has a range of 1 to 24. They can be converted to PRINT AT (or PRINT @) by scaling them to a 64 by 16 screen, though it is better to remove the blank lines. Given HTAB H and VTAB V: 10 X= INT (H * 64/40 + V* 16/24) and PRINT @ X to get the desired location.

There are several other commands associated with PRINT. Unless you have Model 4 BASIC, with its reverse video, ignore INVERSE and FLASH. They cause what is printed to be in inverse video or flashing in time with the cursor. NORMAL, which resets the mode, can also be ignored. SPEED=x sets a printing speed. Ignore it, but consider putting a delay loop (such as 10 FOR G= 1 TO X: NEXT) at the end of each block of printed lines.

Variables

Applesoft integers are in the range -32767 to 32767. You can translate them exactly as they are — followed by a percent sign. The real numbers are in the range (positive or negative) 1E38 to 1E-38; use double-precision if necessary. The Apple uses no type-declaration tags other than % for integer and $ for string, so you can use DEFDBL A-Z. This is needed because the Apple assumes variables are real and the TRS-80 assumes that they are single-precision, so the range may not be large enough for all applications.

LET need not be translated. CLEAR is used on both the Apple and the TRS-80 to clear all variables. The variables on both have only two significant characters, so no change is needed. However, you will have to make sure that the variables you are converting do not contain any TRS-80 reserved words, such as CLS or MKI.

String lengths on the Apple do not exceed those on the TRS-80, so you may convert them directly. However, you must be sure to clear string space at the beginning of the program with something like CLEAR 500.

All string operators, including >, =, <, and + are directly translatable. There is one thing you must be extremely careful of: numeric variables on the Apple do not have leading spaces. If X$=STR$(X) on the TRS-80, you must peel off the leading space: X$=RIGHT$(X$, LEN(X$)-1).

The Cast of Characters

The Apple has no lowercase letters. Instead, the ASCII character set is duplicated. The ESCape key is CHR$(27); it is frequently used for printer commands and may be translated directly. The keyboard contains only the left and right arrows; they are given ASCII values of 8 and 9 and should cause no problem. The commands X=ASC(X$) and X$=CHR$(X) may be translated directly.

Operators

Relational Operators

These are operators such as =, >, <, and <>. They may be translated directly. The AND on the Apple is not a bit-compare but can be used only for statements such as: 10 IF X=1 AND Y=2 THEN PRINT "X=l AND Y=2" You can translate this AND and the OR directly.

Now, there's another tricky thing about the Apple. Zero is FALSE, as it is on the TRS-80, but TRUE is 1, not -1. If actual numeric values are used, be sure to change the sig;n. Also, change something like Y=(X=10) to Y=-(X=10). NOT on the Apple switches between 1 and 0; NOT 0 is 1, for example. This may also be translated, but be wary of Boolean relationships. They are frequently handled differently on different computers — but not always obviously so.

Algebraic Operators

Again, no problem. All are the same. The hyphen is used for negation as well as subtraction. Strings can be concatenated with the "+" sign.

Math Functions

All math functions are directly transferable. The DEF FN, which allows you to create your own functions, is so weak on the Apple that it may be translated directly. About the only function that requires explanation is SGN. This returns the sign of thé number (not its Sine): -i if it's negative, 0 or 1 if it's positive. To simulate Y=SGN(X), use Y= (X<0) - (X>0).

The other area that needs to be explained is that of random numbers. The Apple's RND(l) is our RND(O); both produce random decimals. (And the Apple's RND(O) displays the most-recently chosen random decimal. It can be duplicated only by copying the most recent TRS-80 RND(O) into a variable and then using that.) The Apple has no RND(X). In order to display random integers between 1 and X inclusive, it uses Y= INT (RND(1)*X +1). You can replace the RND(l) with RND(O) or replace the entire expression with Y=RND(X).

System and Utility

FRE(O) on the Apple returns the amount of memory available to the user. Translate it into MEM. If you are specifically interested in the amount of string memory, use FRE(X$).

Now, the memory available to you on the Apple is set by HIMEM: and LOMEM:. They establish boundaries on the area that the program can use. You will frequently see HIMEM:8192. This sets the memory limit just below the high-resolution page one, so that a program does not overlap video memory. To translate it, ignore it but set the memory when you enter BASIC. Because Applesoft programs begin at 800H, add about 18000 (or 28000 for disk systems) to the number used in the program.

When you enter BASIC, you should also set the number of disk buffers that the Apple's MAXFILES requires. However, ignore any command with MON, which duplicates output to the disk and the screen.

Peeking and Poking

These commands are strange on the Apple. You may use either a positive number or its negative equivalent (difference from 65536); that means that POKE 49168,0 is equivalent to POKE -16368,0. This explains a lot; the high numbers are used because the Apple's ROM begins at D000H and continues up to FFFFH, and the pokes are to pressure points within it.

A second place pokes are used is to establish a machine language program — in low memory. The 256 bytes beginning at 768 are unused by the Apple, so most sound routines and other short subroutines are stuck there. A third place is on "page zero," the first 256 bytes; POKE 32,5 indents all text 5 spaces, for example.

Do not translate any pokes you do not understand or cannot find the equivalent of. They could do some pretty horrible things.

CALL and USR

The USR(X) command is just like our USR(X). It passes values back and forth from a machine language subroutine. Unless you know exactly how to translate the routine into Z80 machine language, see if there is a BASIC equivalent that will do the same thing a little slower or see if you can do without it entirely.

CALLs are used much more often because they can use ROM routines to do things quickly. In order to help you more easily translate common CALLS, a short table

I—-fr Product; Continuous fan-foldeB v checks with duai-windowed envelopes. checkbook binder and software designed especially for computer use in the home. You can't set continuous checks liKe these anywhere ;n the

Guarantee: Our checks and ac-IVI cessories are guaranteed to please ' you and guaranteed compatible with your bank. In fact, if you order a special package and aren't satisfied for any reason, simply return it for a full refund (including postage) and you can keep the "Checkbook-Checkwriter II" program.

I—-fr Product; Continuous fan-foldeB v checks with duai-windowed envelopes. checkbook binder and software designed especially for computer use in the home. You can't set continuous checks liKe these anywhere ;n the r~-m Savings: Special package — in-V eluding software — to get you L1—1 started. 200 checks, 100 envelopes and binder-$59.95. 500 checks, 300 envelopes and binder-- $79.95. Both with our versatile "Checkbook-Checkwriter II" prosram that will have you printing your checks the day you receive them.

SPECIAL BONUS:

• Each 1000th customer—your order free, and free check refills for life.

Guarantee: Our checks and ac-IVI cessories are guaranteed to please ' you and guaranteed compatible with your bank. In fact, if you order a special package and aren't satisfied for any reason, simply return it for a full refund (including postage) and you can keep the "Checkbook-Checkwriter II" program.

Now CHECK WITH US...Order a special package with moneyback guarantee today! '.lust enclose a voided check for encoding information with your order. Or send today for our free information package. We guarantee you'll be pleased. Visa, M.C.j AiMEX orders welcome. Shipping; S2.00 USA, S5 00 Outside USA.

"12

4715 SHEPHERD RD • MULBERRY, FL 33860

0 0

Post a comment