Porting Programs to Sinclair BASIC

The ZX81 and ZX Spectrum were both ridiculously popular computers back in the 1980s. Especially in Europe. However, the variant of BASIC they used was... quirky, to say the least. It's extremely difficult and time consuming to port programs to Sinclair BASIC. In this post, I'll describe how to do it. Also, this video is mostly about this process, so it might be worth watching.

The first quirk is that the LET statement is not optional. Anytime you assign a value to a variable, you have to precede it with LET. This includes IF something THEN X=something types of statements. Of course, you can do that fairly easy by pasting a LET before assignments.

The second quirk is that you must have a GOTO before the line number when you're GOTOing in an IF statement. Other versions of BASIC consider a GOTO to be optional in those cases. So, instead of IF something THEN <line number>, it'll be IF something THEN GOTO <line number>. Again, pasting is your friend here.

There is no END statement - only STOP. So, just replace END with STOP if you use that. Also, there are functions like SGN, ABS, SQR, INT, VAL, and RND that don't have parentheses after them. So, instead of C=VAL(A$), it would be C=VAL A$. Additionally, functions don't play well when there's a formula after them. If you have the following line:

1910 D=INT(D*(S+(RND(Y)*N))/B)

You'll need to assign it to a temp variable first, like so:

1909 LET TE=D*(S+(RND*N))/B
1910 LET D=INT TE

Finally, arrays in Sinclair BASIC are indexed starting at 1, instead of 0. This is incredibly annoying. And there's no option to change it to the correct way. So, a good way to fix it is to add 1 to all of your array subscripts. If you use multiplication to determine a subscript, you'll want to add parentheses around the subscript, and add 1 to that, in order to make the math work out the same.

What I've described up to now will implement the common changes you'll need to make something sort of work for both the ZX81 and ZX Spectrum. There are additional changes you'll need to make in order to get programs working. For the ZX Spectrum, you'll need to do a find and replace to change GOTO to GO TO, and GOSUB to GO SUB. For some dumb reason, they added a space there. Also, you might want to put POKE 23692,255 in your main program loop to disable the scroll prompt that will inevitably show up.


For the ZX81, things get worse. You don't need to do any of the stuff in the preceding paragraph, but there are other changes you must make. First, the !, %, &, and apostrophe keys cannot be used as they don't exist on the keyboard. Secondly, your program will stop if the screen fills up. There are two ways around that: SCROLL and CLS. If you want to use CLS, you should add it at a point where it will be triggered before the screen fills up. A LET A$=INKEY loop should be used to ensure that the user can read the text before proceeding. You could use SCROLL if you want to, but it tends to cause serious bugs if you're not careful, and I avoid using it.

The other really serious problem for the ZX81 is the complete lack of DATA statements. Yes, there are no DATA statements, but you can simulate them with the following code:

9985 LET D$="314,314,54,54,38,96,24,1,"  <- this is what would be in your DATA statements, with a comma at the end
9986 LET C=Z
9987 LET E=Y
9988 LET I=45  <- this is the number of elements in the array you want to fill
9989 LET T$=""
9990 LET C=C+Y
9991 IF D$(C)="," THEN GOTO 9994
9992 LET T$=T$+D$(C)
9993 GOTO 9990
9994 LET A(E)=VAL T$
9995 LET E=E+Y
9996 IF E>I THEN GOTO <the rest of your code>
9997 GOTO 2550

That code will work for numbers - you'll have to modify it if you want to have strings in there. The book I grabbed that code from suggested using a semi-colon to mark the end of strings. But it'll work fine as is, as long as you want to fill one single-dimensional array. You'll probably have to duplicate the code if you have to fill more than one array.

One last thing. If you want to save a ZX81 program as an auto-loading program, you'll want to add the following lines to the very end of your program:

9998 SAVE "<program name here>"
9999 GOTO 1

Once you're program is loaded in, you might want to type FAST to set Fast Mode, and then you should type POKE 16389,68 to shrink the display file, because otherwise you'll get a 700-ish byte blank section that will have to load every time. Then, you GOTO 9998, and it will save your program, and then it will go to the first line of your program. Hopefully, you can now port programs to the ZX81 and ZX Spectrum.

Comments

Popular posts from this blog

CoCo 3 NitrOS-9 Emulator VHD Install Disks

Overclocking the Raspberry Pi 4 for Fun and Games

Mac Emulator Videos