Sinclair BASIC

IMPORTANT: This article uses some of the material from the Sinclair BASIC History article on the ZX Spectrum technical information wiki at Fandom (formerly Wikia), written by Andrew Owen, and is released under the Creative Commons Attribution-Share Alike License.

Sinclair BASIC was created by Nine Tiles Networks Ltd in Cambridge, UK, back in 1979 to fit into the 4K of ROM available in the Sinclair ZX80. It started out as an incomplete version of the 1978 ANSI minimal BASIC standard, but evolved through the 8K ROM in the ZX81 and Timex-Sinclair TS1000, and eventually became and almost complete version in the 16K ROM of the ZX Spectrum.

In May 1979, Clive Sinclair's engineers began work on the machine that would become the ZX80. Sinclair was inspired to create the machine after seeing how much his son enjoyed using a TRS-80 but guessing that many people would be put off buying one because of the high price - just under £500. Unlike Sinclair's previous foray in to the computer hobbyist market, the MK14, this machine would ship with BASIC, based on the ANSI standard. But the aim was to keep costs down and that precluded paying a licence fee to Microsoft. To this end, Sinclair had already met with John Grant of Nine Tiles in April 1979 to discuss the software requirements of the ZX80.

Given the tiny R&D budget, Nine Tiles stood to make hardly any money out of the deal, but the feeling was that the project was exciting and worthwhile, and one the company would benefit from being associated with.

To achieve the launch price of £79.95 in kit-form, RAM was limited to 1K and the integer BASIC had to be crammed into a 4K ROM. Grant wrote the bulk of the ROM between June and July 1979. But the resulting program was 5K in length so Grant spent that August trimming the code.

Grant laid the path for things to come, introducing many unique features of Sinclair BASIC, such as the way it refuses to allow most syntax errors to be entered into the program, instead pointing out where the error is in the line before it is entered, making it much easier to learn and use than any other version of BASIC.

Each key on the keyboard (or keypad in the case of the ZX80 and ZX81) held upwards of 3 or 4 commands, depending on the 'mode' you were in. You could access these through the use of CAPS SHIFT and SYMBOL SHIFT keys, or a combination of them both. Commands are entered via Sinclair's slightly odd entry system, where a single keypress if often all that's needed to input an entire command keyword. For example, hitting the P key results in PRINT being displayed. Based on the context of the line of code you're writing, the system understands when to do this, and when hitting 'p' just results in 'P' being displayed. This mechanism was later removed in favour of the traditional letter-by-letter system from the Spectrum 128 and onwards.

For the ZX81, again Nine Tiles was called upon to provide the new BASIC, but this time there was 8K to play with. Steve Vickers, who had joined Nine Tiles in January 1980, added the floating-point arithmetic, including trigonometric and other functions.

For the Spectrum BASIC, Vickers was again asked to provide the BASIC. What started out as an expansion of the ZX81 BASIC soon turned into a large 16K program. Sinclair wanted as few changes to the ZX81 code as possible but at Nine Tiles the feeling was that software designed for a machine with 1K was inappropriate for a machine with 16K and that problems would occur later on. They were right.

"Certainly with the Spectrum we wanted to rewrite the code, but there wasn't the time and there definitely weren't the resources," said Grant in an interview on September 8, 1985. "At every point [in the development of the ZX range] Clive wanted the maximum new facilities for the minimum money."

After the best part of a year's work the BASIC was almost finished. While it was greatly enhanced, it was also depressingly slow, but more problems were to follow. The main problem was providing support for the planned peripherals because no working prototypes were available to Vickers until near the end of 1981. But then, in February 1982 Nine Tiles began to have financial disagreements with Sinclair over royalties which it became apparent would not be forthcoming.

To make matters worse, Steve Vickers and Richard Altwasser (who designed the hardware for the Spectrum) both handed in their resignations in order to form their own company, Cantab, which went on to produce the Jupiter Ace, essentially a ZX80 with the Forth language built-in in place of BASIC. The result of the delays these problems caused was that when Sinclair launched the machine, it did so with an incomplete ROM. Nine Tiles continued working on the ROM for three months after the launch in April 1982, but by then too many units had been sold and the program was never finished.

The original plan was to issue only a limited number of Spectrums with the incomplete ROM and provide an upgrade, much in the way the bug in the ZX81 ROM had been handled, except that by the time Sinclair got its act together, around 75,000 units had been sold and the plan became unworkable. This is the reason why the microdrive commands don't work in the standard ROM, and hence led to the development by Ian Logan of the shadow ROM in the Interface 1 in order to handle peripherals which should have been supported directly by BASIC.

Those who are interested in what the finished ROM might have looked like should visit Geoff Wearmouth's website at http://www.wearmouth.demon.co.uk/, where you can download the latest version of his Sea Change ROM, complete with source code. Wearmouth's version of Sinclair BASIC cunningly includes RS232 and network support in the main ROM, although sadly it is incompatible with the majority of commercial Spectrum software.

Various 'enhancements' were made to the BASIC over the years, including the extra syntax of the shadow ROM introduced with the Sinclair Interface I, and in the United States in 1983 when an attempt was made to overhaul the BASIC by Timex when it launched its TS2068. But again, the version of the ROM launched with the machine was incomplete, and the TS2068 was unable to run the majority of Spectrum software because of hard-coded calls to locations in the ROM which were different in the Spectrum.

In 1985, in a joint venture with its Spanish distributor Investronica, Sinclair launched the Spectrum 128, codenamed Derby, with a new editor bolted on to the original BASIC. This was slightly more compatible than the Timex effort but the editor was bug ridden, and some software refused to work, even in 48 mode, because the empty space at the end of the original ROM, used as a table by some programs, was now overwritten with extra code.

It did introduce some useful new commands and a built-in text editor, although inexplicably these were replaced with a menu system with less functionality in the English version of the machine launched the following year. However, criticism of the 128 Editor must be put in context. The programmers were relying on the Logan & O'Hara disassembly of the original ROM publised by Melbourne House, since if Sinclair ever had a copy of the original source by now it had been lost, and were working on a network of VAX machines running CP/M.

I do not know who wrote this drivel, about VAX, as CP/M most definity did not run on a VAX!  At best there may have been a cross compile environment being used on VMS, Ultrix-32 or BSD/UNIX which did run on a VAX. The boys at Microsoft developed code like this on cross compiler/dev environments on a PDP-10. VAX is a CPU not an operating system!

Fortunately, tracing the development of the 128 Editor is made easier by the fact that the initials of programmers are stored at the beginning of the Spanish ROM and (MB, KM, and AT) at the end of the English ROM (Martin Brennan, Steve Berry, Andrew Cummins, Rupert Goodwins and Kevin Males).

According to Rupert Goodwins, editor of the +2 manual and the person responsible for the Spectrum logo on the menu system, the Sinclair programmers didn't realise that the unused bytes in the original ROM were being used as a table by games programmers. "The TV test screen and other ancillary code was in there for production testing," he said. "As Spectrums came off the production line, they got checked and set up for keyboard, tape, ports, colour, and sound." He recalls there being an Interface 2-style cartridge system at one point but that most of the test code ended up in the ROM. "We had the space and it's obviously cheaper and more efficient that way."

There were also some strange features planned for the 128 which were removed before production as they couldn't be made to work properly. "There were certainly plans to do more with the [external] keypad -- what a bizarre idea that was," he said. "It was originally supposed to have been a mouse as well -- can you imagine!"

Kevin Males worked on both versions of the 128 Editor ROM. "I wrote the music string interpreter for the 128, plus various other bits and pieces that never made it into the ROM," he said. "I also did a lot of work on microdrives, but its a long time since I wrote any Z80 code though!" He may also be the author of the text editor in the original Spanish version. "I recall working on various text editors for the 128 that didn't make it into the ROM," he said.

In addition, he worked on automated test and diagnostic software for both Spectrum & QL microdrives. He was also involved in the notorious Loki project. "Towards the end I started looking at software to control a proposed digital synth for the new games machine but the company was sold before that could be realised."

Martin Brennan, who worked on no-end of projects at Sinclair, wrote the editor with contributions from Steve Berry, and Andrew Cummins probably wrote the tricky number handling code.

Amazingly, Sinclair never owned the rights to the ROM. Amstrad had to acquire them seperately from Nine Tiles in 1986 when it bought out Sinclair. When Spectrum clones began appearing back in late 1984, Sinclair Research boss Nigel Searle found he was powerless to do anything about it because the only really unique part of the Spectrum was the ROM and in the disagreements following the Spectrum's launch, Sinclair had failed to acquire the rights, for which it had originally offered John Grant £5,000. By now the Spectrum had sold more than 2.5 million units.

In fact Amstrad only obtained the rights to the Spectrum and the QL, which they sold on. Nine Tiles Networks retains the rights to the ZX80 and ZX81 ROMs, and Sinclair retains the rights to the Interface 1 ROM. In fact, the developers of the SAM Coupe, a powerful Z80 based machine with a Sinclair-compatible BASIC, approached Nine Tiles with a view to licensing the floating-point routines from the ZX81 ROM. However, at the time the asking price was too high.

Towards the end of 1986, when Amstrad wanted to create a Spectrum with a built in disk drive, it simply took the DOS from its PCW machine and patched the 128 editor to provide simple disk access. In fairness, the DOS, written by Cliff Lawson, was a very good one, although its full power remained untapped by +3 Basic. Unfortunately none of the bugs were fixed in the first version of the +3 and new ones were introduced, but perhaps this is understandable as there was little documentation at the Sinclair Computers division and development had moved from a VAX network running CP/M to a room full of PCWs running CP/M which was less than ideal.

Amstrad stopped selling the last Spectrum model, the +2B, in the early 1990s. For a time it looked as if the SAM Coupe might offer an upgrade path to Sinclair BASIC users, but after two false starts the machine disappeared into obscurity.

However, with the advent of emulation (made possible because Amstrad has kindly given permission for the distribution of its copyrighted material for use with emulators although it retains that copyright) there has been a renaissance in Sinclair BASIC programming.

This has been aided greatly by Paul Dunn's development of a Sinclair BASIC integrated development environment: BASin. As well as providing a superb editor, it includes the entire contents of the original Spectrum BASIC manual, the Spectrum + manual and the relevant parts of the Spectrum 128 manual as online help. Although BASin is developed only for the Microsoft Windows platform, it is open source so in theory it could be ported to other machines. However, it is compatible with Wine, the Windows application layer for Unix-like operating systems, and so in practice it can be used with most machines.

For more information about Sinclair and the actual hardware the five-part article 'Sinclair and the "Sunrise" Technology' by Ian Adamson and Richard Kennedy, is available here on Retro Isle, and makes excellent reading.

 

ZX81 Reserved Words

FAST, SCROLL, SLOW, UNPLOT, GOSUB and GOTO (vs the Spectrum's functionally identical GO SUB, GO TO).

ZX Spectrum 16K/48K Reserved Words

On the ZX Spectrum each reserved word was assigned a character code between 165 and 255 in the latter half of the system character set, and expanded by referencing a token table held in ROM. As a result, any reserved word in a program listing occupied just one byte of memory, a significant saving over traditional letter-by-letter storage. This also meant that the BASIC interpreter could quickly determine any command or function by evaluating a single byte.

Commands
BEEP, BIN, BORDER, BRIGHT, CAT, CIRCLE, CLEAR, CLOSE#, CLS, CONTINUE, COPY, DATA, DEF FN, DIM, DRAW, ERASE, FLASH, FORMAT, FOR, GO SUB, GO TO, IF, INK, INPUT, INVERSE, LET, LIST, LLIST, LOAD, LPRINT, MERGE, MOVE, NEW, NEXT, OPEN#, OUT, OVER, PAPER, PAUSE, PLOT, POKE, PRINT, RANDOMIZE, READ, REM, RESTORE, RETURN, RUN, SAVE, VERIFY
Functions
ABS, ACS, AND,ASN, ATN, ATTR, CHR$, CODE, COS, EXP, FN, INKEY$, INT, IN, LEN, LN, NOT, OR, PEEK, PI, POINT, RND, SCREEN$, SGN, SIN, SQR, STR$, TAN, TO, USR,VAL$,VAL,
Other keywords
AT, LINE, STEP, TAB, THEN

INK, PAPER, FLASH, BRIGHT, OVER and INVERSE set attributes for outputting text and graphics to the screen. They can be used either as commands, to apply to all subsequent output until set again, or within a PRINT statement, to apply only from that point until the end of the statement.

CAT, ERASE, FORMAT and MOVE were originally designed to be used with peripherals, but at the launch of ZX Spectrum, they had not been completely implemented, such that their use generated an error message (Invalid Stream). Later with the aid of the ZX Interface 1 shadow ROM, they were used for the ZX Microdrive. (The shadow ROM was paged when the BASIC interpreter detected a syntax error, which is why most ZX Microdrive commands use a "*").

ZX Spectrum 128, +2, +2A, +2B, and +3 Reserved Words

With the Spectrum 128's enhanced sound chip, the Yamaha AY-3-8910, came a new command:

  • PLAY, which operated the 128k models' AY-3-8910 music chip

You could also switch between 128 BASIC and the original 48K BASIC using this command:

  • SPECTRUM, which switched the 128k Spectrum into a 48k Spectrum compatibility mode

 

Timex-Sinclair TS2000 Reserved Words

The BASIC dialect used on the Spectrum-compatible TS2068, but not the TC2048, which used the ordinary Sinclair BASIC, has the following six keywords as well as the ordinary Sinclair BASIC ones:

  • DELETE deletes BASIC program line ranges. ⇧ Shift+0 with the K cursor produces the command DELETE.
  • FREE is a function that gives the amount of free RAM. PRINT FREE will show how much RAM is free.
  • ON ERR is an error detection function mostly used as ON ERR GO TO or ON ERR CONT.
  • RESET can be used to reset the behaviour of ON ERR. It was also intended to reset peripherals.
  • SOUND controls the AY-3-8192 sound chip.
  • STICK is a function that gives the position of the internal joystick (Timex Sinclair 2090).