16

I'm trying to write some programs from an old magazine that were specified to run on an ZX80/ZX81 but I'm using a Sinclair BASIC interpreter for the Spectrum.

Is there any significant changes in BASIC instructions for those computers?

Omar and Lorraine
  • 38,883
  • 14
  • 134
  • 274
d'Arby
  • 161
  • 4
  • 2
    https://www.reddit.com/r/zxspectrum/comments/o2poeo/zx_spectrum_and_zx81_basic_differences/ – Tomas By Apr 18 '22 at 17:45
  • 1
    Some info here also. – Tomas By Apr 18 '22 at 17:55
  • 1
    So presumably you're only interested in changes that break backward compatibility? E.g. the ZX80 BASIC was integer only, but that shouldn't necessarily be a problem. – Tommy Apr 18 '22 at 19:06
  • 1
    @Tommy The lack of floating point could break stuff; consider (3/2) > 1. – Philip Kendall Apr 18 '22 at 22:11
  • Not true that the ZX80 only had integer math. I bought a kit (to avoid waiting for the assembled version) which did indeed come with a ROM with integer arithmetic. However, Sinclair followed up with a free ROM upgrade to floating-point a few months later, which was pluggable. This had the infamous square-root bug, and a further free version of the ROM was also issued. I still have the ZX80, all 3 ROMs, the 16KB extension, the full Technical Manual, and many issues of the ZX80 magazine. – Paul_Pedant Apr 19 '22 at 11:47
  • @Paul_Pedant was that definitely a free ROM? Sinclair definitely engineered the ZX81 ROM so that it also worked in a ZX80 (without SLOW mode, naturally), but I was unaware that they were provided for free. Or maybe I've got the wrong end of the stick and that's not the upgrade you're thinking of? – Tommy Apr 19 '22 at 18:52
  • 1
    @Tommy At the distance of 42 years, I would not bet my life on it, but I think I would remember if I had to pay for it. I suspect kit purchasers were slightly better treated, or maybe were (unsuspecting) Beta testers. I also think I got the upgrade before the ZX81 was released, so not a retro-fit for me. – Paul_Pedant Apr 19 '22 at 23:00
  • Re "write": Don't you mean "transcribe" or "type in"? – Peter Mortensen Apr 20 '22 at 07:10
  • Just like to add that POKE could break things, different memory map and hardware etc. – user19862 Apr 20 '22 at 09:46
  • Yes, Darby, there are significant differences. I don't remember the details and I do Ask you, how is it not obvious there were differences? That Spectrum had more advantages than simple speed over ZX81, as did ZX81 over ZX80?

    If that's not obvious, what's the problem?

    If that is obvious, what's the Question?

    In ordinary English this would be pedantic. In terms of programming languages, you're not trying to write, only to replicate. Is that difference not significant to you?

    In many cases older Sinclair code will be upwards compatible but that's not guaranteed.

    More…

    – Robbie Goodwin Apr 21 '22 at 21:07
  • Further, Sinclair design suffered from two immense faults; either might have killed the marque dead; neither is replicable on modern keyboards.

    To replicate legacy Sinclair coding on a modern machine, please accept Sinclair refused to use, or endorse, standard keyboards. Sir Clive's insistence on his own designs meant Sinclair kit was hugely more difficult to work with, in ways today's Users may find unimaginable.

    Since we cannot now usefully replicate Sinclair's button technology, which today looks cheapskate, there's no possibility of a useful comparison.

    Would you like details?

    – Robbie Goodwin Apr 21 '22 at 21:52

3 Answers3

21

Spectrum BASIC is a superset of the earlier BASICs (all the colour commands, etc), so pedantically the answer to the question is the title of the post is "yes, there are significant differences".

However, if you're asking whether a earlier BASIC program can be run on the Spectrum, the answer is mostly yes. Possibly the most notable changes are:

  • PLOT works differently on the earlier machines (where it functioned on a 64x44 grid) and the Spectrum (where it functioned on a 256x176 grid). If not corrected, PLOT statements will all end up in the bottom-left corner of the Spectrum's screen.
    • UNPLOT does not exist on the Spectrum; it can be emulated with PLOT INVERSE 1 or similar with the same caveats as for PLOT.
  • The Spectrum doesn't have FAST and SLOW mode (as it didn't use the Z80 to render the screen); just ignore those commands.
  • The Spectrum's character set doesn't have the half-tone graphics or the inverse characters which existed on the earlier machines:
    • The half-tone graphics can be emulated with UDGs
    • For output, the inverse characters can be emulated with embedded control codes in the output strings.
  • While the Spectrum's character set was very close to ASCII, the ZX81's character set was entirely custom. This will break anything which makes too many assumptions about the relations between the character set values (although 0 to 9 and A to Z are contiguous ranges in both sets so a lot of stuff will work).
  • The ZX80 had only integer math, while the ZX81 and Spectrum had only floating point math (in the sense that there is no way to force integer math). If a ZX80 program relied on the integer behaviour (e.g. for truncation - (X/4)*4) then you will need to scatter some INT calls in the ZX81/Spectrum code.

There was even a utility back in the day, ZX Slowloader, which emulated the ZX81's tape loading routine so ZX81 tapes could be loaded into the Spectrum.

Philip Kendall
  • 547
  • 2
  • 9
  • I'm not as familiar with the ZX80's BASIC but I believe it was very similar to the ZX81's; please comment or just edit in any additional differences. – Philip Kendall Apr 18 '22 at 21:55
  • 2
    Because of lack of time, just as a comment: AFAIK the ZX80 BASIC has only integer numbers, while the ZX81 BASIC has only floating point numbers. – the busybee Apr 19 '22 at 06:27
  • 1
    I believe the PLOT thing will plot things in the bottom-left corner, as Y coords where bottom-up. My 11-year-old self was really confused - I typed some PLOT-based ZX81 on my Spectrum, and couldn't figure out the point of those tiny drawings. Fun times! – Jonathan Apr 19 '22 at 07:14
  • 1
    One other minor tweak is that the ZX81 used a "quote image" token which displayed as "" in a listing but " when printed; the Spectrum allows you to insert a " in a string by simply doubling it. – Neil Apr 19 '22 at 11:04
  • There is also the SCROLL statement on the ZX81 -- the nearest equivalent on the Spectrum is POKE 23692,255: PRINT AT 21,0: PRINT – john_e Apr 28 '22 at 10:28
7

The Spectrum's character set was based on 7-bit ASCII. The main printing sets (20-7F) are nearly pure ASCII, the only differences are that 60="£" in Speccy, but "`" in ASCII, likewise 7F is "©" instead of DEL. My reference¹ shows 5C is "/" whereas ASCII has "\" but I think this may be a typo since 2F is also "/". The Speccy did, however, use the first 32 codes and all codes after 80 for its own purposes. 0D is mapped to ENTER, which matches with ASCII's CR. 80-8F are block graphics, 90-A4 are user-defined graphics and A5 to FF are the tokenised commands for BASIC.

The ZX80's character set has no relation to ASCII:² 00→space, 01→null string, 02-0B→chunky graphics, 0C-1B→"£$:?()_+*/=><;,.", 1C-25→"0-9", 26-3F→"A-Z", 40-7F (undefined) 80-BF inverses of 00-3F, D4-FF→tokens and some more punctuation.

Unfortunately I have no reliable copy of the ZX81 mappings. I've found the character code tables for the ZX81 between 007E and 01FB in the ROM.³ As pointed out by Tommy in a comment below, they are similar not quite identical, eg "<" is at 0x17 on a ZX80 but 0x12 in the ZX81 ROM.

Others have mentioned number representation: ZX80-integer only, ZX81-float only, Speccy-both. The ZX80 could not display a screen and calculate at the same time. The ZX81 had fast mode which was like the ZX80, and slow mode which used the NMI to interrupt calculations and display a screen before returning to calculate during frame fly-back. The Speccy used hardware to handle the screen.

¹Database Publications. The Complete Spectrum. Stockport: Database Publications, 1984. 004.165/SIN.

²Maunder, Bob, Terry Trotter, and Ian Logan. The ZX80 Companion. Second edition. Middlesbrough, England: Linsac, 1980. 004.165/SIN.

³Logan, I., and F. O’Hara. The Complete Timex TS1000 [and] Sinclair ZX81 ROM Disassembly. P.A. Cheddington, Leighton Buzzard, Bedfordshire: Melbourne House Publ, 1982. 005.265/SIN

  • From BASIC, the Spectrum's number representation is effectively always floating point; while it may have used a special encoding internally for small integers, there's no way to force e.g. 11/2 to return anything other than 5.5. – Philip Kendall Apr 19 '22 at 11:40
  • "There is an alternative [to floating point ] way of storing whole numbers between -65535 and +65535: (i) the first byte is 0, (ii) the second byte is 0 for a positive number, FFh for a negative one, (iii) the third and fourth bytes are the less and more significant bytes of the number (or the number +131072 if it is negative), (iv) the fifth byte is 0." Quote from Vickers, Steven John. Sinclair ZX Spectrum : Basic Programming. Edited by Robin Bradbeer. Cambridge: Sinclair Research, 1982. 005.265/SIN. This means that, say, 6/3 will yield integer 2 and not 1.9999 or similar. –  Apr 19 '22 at 12:59
  • 1
    I'm well aware of the short form and how it works, I've spent far too much time in my life stepping through Z80 code :-) Do you have an actual example of an actual published BASIC program which demonstrates different behaviour on the ZX81 and the Spectrum due to the lack of the the integer forms on the ZX81? I'm sure it's possible to construct a pathological one but this feels much more like a theoretical concern than a practical one. – Philip Kendall Apr 19 '22 at 13:10
  • Sorry, I went straight from the ZX80 to the Speccy, and then into machine code. I only got my ZX81 out of interest years later. –  Apr 19 '22 at 13:21
  • 1
    My memory is that the ZX81 character codes were the same as ZX80. Definitely not the same as the ASCII-based scheme used by the Spectrum. The change was probably in anticipation of ZX Interface 1 providing serial ports, and therefore access to modems. – Toby Speight Apr 19 '22 at 16:25
  • The ZX80 and '81 have a weird character mapping to allow the compact display map used on the 1kb machines — famously the program counter is reused as a video counter, with character codes being substituted for NOP on their way to the CPU, but if bit 6 is set then no substitution occurs. That allows HALT to pass through, ending a line early, so total memory footprint per line is only one byte for an empty line, or n+1 bytes for a line with n left-aligned characters. But as a consequence there are only 64 printing characters, with codes 0–63. So ASCII wouldn't be a good fit. – Tommy Apr 19 '22 at 18:59
  • ... and per Wikipedia the character codes differ slightly — compare https://en.wikipedia.org/wiki/ZX81_character_set and https://en.wikipedia.org/wiki/ZX80_character_set for graphics characters and punctuation, e.g. '<' is at 0x17 on a ZX80 but 0x12 in the ZX81 ROM. – Tommy Apr 19 '22 at 19:17
1

Question is about translating ZX81 Basic to ZX Spectrum.

It's been about 40 years since I last did this from magazine listings, but the one thing that used to trip me up was not the bulk of the Basic listing but some POKEs and PEEKs. Or little bits of machine code to scroll screens or implement user defined characters etc, were common in ZX81 code

From memory, something like a peek of free RAM was different between the two, so that these didn't translate well without a cheatsheet of common peek/poke locations

GrantB
  • 151
  • 2
  • 3