Computer / Programmazione / Z80 · 26 February 2019 0

LM80C: BASIC!

The next, logic,  step for my LM80C, once I’ve been able to set up a functioning serial comm between my Z80 and the host (represented by my computer), is (of course) trying to do something useful with this little piece of hardware. So, what is the most useful thing that an 8-bits computer, built to go back to the roots of the home computing, could do? Obviously… BASIC!! Back in ‘late ’70s and for all the ’80s BASIC has been the most used and known programming language: its widespread use has been helped by the fact that almost every home computer had a BASIC interpreter in it. Moreover, almost every BASIC interpreter was derived by Microsoft BASIC: this was an element that helped the spreading of BASIC since almost every interpreter was ble to run the same code, with very little changes (due to the different hardware the software was written for).

The first home computers weren’t powerful machines: since at the time the RAM was very expensive, often more expensive than the CPU itself, those machines were assembled with little amount of memory. It was not uncommon to have only 1, 2, or 4 KB of RAM: the Sinclair ZX80 and ZX81 had only 1 KB of memory, so the Commodore VIC-20. The first versions of Apple II came out with 4 KB that could be expanded to 48 KB: awesome, isn’t it? The first BASIC that Microsoft wrote was the Altair BASIC for the Altair 8800: it fitted into 4 KB of RAM (it, of course, needed an expansion card since the 8800 didn’t have such amount of RAM). If more more functionality were needed, one could buy a more sophisticated version, that needed the 8 KB expansion. Since these RAM cards were very expensive, some users started writing their own BASIC interpreters: one of the most successfull interpreter was the Tiny BASIC, so called because it was “tiny”. In fact, it fitted in 2/3 KB of RAM. Microsoft wrote different versions of its interpreter for a lot of machines: NASCOM BASIC was written for the NASCOM computers, a couple of machines based on the Z80 CPU, and derived from the Microsoft BASIC plus some improvements. Grant Searle made a fantastic job by adapting this BASIC for its Z80 computer: he got the NASCOM BASIC source, he adapted it to compile with a modern assembler and cut off some useless features. The result is an interpreter that resides in less than 8 KB of ROM: in this calculation it is included an init code, that sets up the hardware of his computer. I got these codes and adapted them to run on my hardware, that is slighty different from the Grant’s computer: he used a Motorola 68B50 as a serial peripheral, connected to a MAX232 to convert signals from TTL levels to serial levels. In fact, Grant’s computer was built over a decade ago, when PCs still had the serial port. Today, USB has supplanted the old RS232 interface, so I adopted a more modern FT232 module, to convert serial signals into something that a USB port can handle. Moreover, I decided to opt for a Z80 SIO since this peripheral can handle the signals of the interrupt daisy chain of the Z80 family chips.

After I burned the code into the EEPROM and connected the FT232 module, I opened a serial channel to the LM80C and was very excited when I saw on the terminal that the computer was asking to me to choose between a cold or a warm start! A warm start keeps into RAM the previous program the user had typed into the system while a cold start is like a power-off-then-power-up start: the system forgets what was inside its memory and set the environment for a clean up restart.

NASCOM BASIC is a very interesting interpreter: in less than 8 KB it fits a lot of different commands. The following are the ones integrated into our version:

SGN, INT, ABS, USR, FRE, INP, POS, SQR, RND, LOG, EXP, COS, SIN, TAN, ATN, PEEK, DEEK, LEN, STR$, VAL, ASC, CHR$, LEFT$, RIGHT$, MID$, END, FOR, NEXT, DATA, INPUT, DIM, READ, LET, GOTO, RUN, IF, RESTORE, GOSUB, RETURN, REM, STOP, OUT, ON, NULL, WAIT, DEF, POKE, DOKE, LINES, CLS, WIDTH, MONITOR, PRINT, CONT, LIST, CLEAR, NEW, TAB, TO, FN, SPC, THEN, NOT, STEP, +, -, *, /, ^, AND, OR, >, <, =

If you want to know more, you can read the NASCOM manual of its BASIC. Please keep in mind that this BASIC handles every number as a SIGNED integer in the range -32,768 / +32,767, in every part of it, functions incuded. So, every number that is bigger than 32,767 will appear as a negative number, and every number passed to, i.e., a function like POKE or something else must be in 2’s complement format. A quick conversion can be made by simply subracting the desired number from 65,536: i.e., to point to cell in RAM with address 49,152 you will have to enter 49,152-65,536= -16,384.

In my repository you will find 2 files: bootloader.asm and bas32k.asm. Both are required, since the first one is the boot loader code that sets up the serial comm between the system and the remote host and then asks for the kind of start. The second one is the actual BASIC interpreter: after it starts, it will ask for the top of the RAM to be used by the interpreter. You can press ENTER to let the interpreter use the whole free memory. After a while the free available memory will be shown and our LM80C will be ready to accept commands and execute them:

LM80C: BASIC

LM80C: BASIC

Since the code has been splitted into 2 files you need 2 burnings to write the code into the EEPROM: bootloader.asm, once compiled, must be burnt from position $0000 while bas32k.asm at the moment must be burnts from position $01B0. This can change in the future since the size of the bootloader may vary due to code changes, so before writing the BASIC check the current starting cell into the asm file.

You can see a little test in this video:

Code will be available soon in my repository on GitHub.