• Please review our updated Terms and Rules here

Writing ZX-81 Basic on PC?

Divarin

Veteran Member
Joined
Mar 9, 2019
Messages
565
Location
Cleveland, OH
Hi all I'm looking for a way I could try writing a simple basic program for the ZX-81 on a PC (maybe just use notepad++, or notepadqq in my case since I'm using Linux) and then something that will convert that to the tokens the ZX-81 would be expecting and make a file I could put into an emulator for testing and eventually convert that to a wav file for recording to cassette.

I have found various ZX-81 emulators but working within those is not any easier than using a real ZX-81 for example I want to type "P-R-I-N-T" not hit "P" and have the keyword "PRINT" come out because I'm going to be spending more time hunting around for the key that has the desired keyword than if I had just typed the word.

Also a lot of emulators lack support for saving since they're really just designed as a way to run already existing software.
 
Well, anything is possible in software...

The actual BASIC program part itself should not be that difficult. You can use tools (such as FLEX and BISON) to scan and parse the text of the BASIC program and to translate it into a form suitable for the ZX-81. See: https://web.iitd.ac.in/~sumeet/flex__bison.pdf.

However, the problem I can see is that the SAVEd file also has a number of system variables attached to the front of it.

I would have to read up on those...

Dave
 
Could you use a Spectrum 128 emulator? That should allow pasting the text into the emulator without needing special tokens. Then save from the emulator and reload the saved BASIC file in 48K mode.
 
If you can't find one, you can always write one in BASIC :)

If you already know BASIC then using a modern BASIC like FreeBASIC is a lot of fun also, and will allow you to compare and contrast, as well as format the data structure however you want.
 
Could you use a Spectrum 128 emulator? That should allow pasting the text into the emulator without needing special tokens. Then save from the emulator and reload the saved BASIC file in 48K mode.
Hmm maybe but I do want my program to run on a zx-81, not a spectrum. If necessary with a 16k ram expansion.
 
I didn't follow the above link but there are several ZX81 emulators available, all of which obviously run on PCs. Being ZX81 emulators they will obviously emulate the tortuous single-key keyword entry of the original machine whereas I'm wondering if what you really want is something which lets you type ZX81 programs the 'normal' way in a PC text editor and then have something, a sort of compiler, turn your plain text BASIC program into the tokensised format actually used by the ZX81. Is that what you are really looking for?
 
P.S. with the Spectrum scene still alive and well I'd expect cross-compilers, PC "SDKs" and whatnot to be available, however might not be available for model and language combination you're after.
 
>>> So a BASIC cross-compiler. Quick google gave me this...

Not strictly true.

When a line of BASIC is entered, the line number is converted from ASCII text into a 2-byte binary number. Likewise, keywords (such as PRINT, INPUT, FOR, etc.) are converted into 1-byte tokens. The resultant line is then stored into the machine's memory. This makes the code (supposedly) quicker to execute and means that the program can be backlisted relatively quickly also. No attempt is made to convert the entered code into 'machine code'.

A BASIC compiler (or cross-compiler) on the other hand converts the text program into machine code to be executed.

Of course, there are many 'variations' on a theme - such as just-in-time compilation and a pre-pass on the text to compile it into machine code to start with.

Dave
 
That is what I was also assuming in my post above.

Yep, I was at work then and only glanced at the OP's post at the time. That obviously was what he meant.

I think the problem is that you would ideally want an intelligent editor which does the same sort of real time syntax checking as the ZX81's own editor so you can't enter a syntaxially incorrect line of code, otherwise you'll spend a lot of time shoving your plain BASIC text file through the ... let's call it a 'translator' ... and being informed of simple mistakes all the time.

That said, that is what a lot of people have put up with for all of these years writing 'C' programs with plain text editors and hoping the compiler won't find too many errors - I guess it just takes discipline and practice to make as few initial errors as possible.

Regarding getting the code into the ZX81, I have an interface for my MK14 which reads Intel Hex files and converts them into the keypresses required to enter the code into the machine, via optcouplers connected to the keypad matrix, via the 'external keypad' connector on that machine.

There is no reason why the same approach can't work on any computer with a matrix keypad including a ZX81, so what you could have is a program running on a raspberry Pi which accepts entry of single longhand lines of ZX81 code and, when you press 'Newline', converts the entered line to the awkward keypresses required to enter that line on a ZX81 keyboard and sends those keypresses via an optocoupler matrix to the ZX81's keyboard rows and columns.

I prefer the opto approach because it keeps the possibly valuable target machine isolated from the machine being used for programming, but if isolation is not a concern then a pair of back to back connected 4051s could be used to connect keypad rows to keypad columns - in the case of the ZX81 you would need a couple of extra electronic switches (4016? 4066?) or optocouplers across the SHIFT key and the NEWLINE / FUNCTION key so that the program 'enterer' could press those specific keys at the same time as any of the other keys on the keyboard.

There are a couple of things that I like about this approach - one is that you get to watch the code being typed in, as though there's a ghost controlling the machine - the other thing is that you don't need to understand the way the ZX81 stores its lines of BASIC in its own RAM because the ZX81's own editor does that translation to the tokenised form as the 'loader' enters the line.
 
Last edited:
Hmm the physical key-presser sounds interesting but I was thinking I would run my code incrementally through an emulator as that would make for a faster turn-around with testing and only get it running on a real zx-81 further down the road after it's pretty much done.
 
Entering the BASIC code line by line into the ZX-81 would be (effectively) the same as whizzing all of a plain text BASIC source file through an offline 'tokeniser' utility. Any syntax errors etc. should be caught by the utility in the same way as the real ROMs - but across all of the program (assuming the translator is written not to halt on the first error and to attempt a restart - possibly resynchronising on the newline).

Dave
 
The initial line-translator / line enterer, once got working, could then be the core subroutine in a program which reads multiple lines from a text file and parses them one after the other.

A 'soft' translator from 'English' ZX81 code to Tokenized ZX81 code sounds to me like a job for some PC runnable version of BASIC (maybe even Qbasic running under DOSBOX) or Python, the latter being available on a wide variety of current platforms including the Raspberry Pi.

You could write it in C if you absolutely must write it in a write-only language. ;)
 
I had success with http://freestuff.grok.co.uk/zxtext2p/index.html.

I typed in a game from a magazine using Visual Studio Code (Norepad++ will work fine) and used ZXText2P to convert it to a .p file that I can open in a ZX81 emulator (EightyOne).

Actually that sounds exactly like what I'm looking for! Thanks, I'll play around with this today!
 
Back
Top