Emulate the ZX Spectrum with Raspberry Pi

ZX Spectrum Raspberry Pi
(Image credit: Tom's Hardware)

Sir Clive Sinclair, born in 1940 is an entrepreneur and inventor that many will know. You may know him for his quirky Sinclair C5 battery powered “velomobile” which was set to solve commuters problems in 1985. But many will know him for a range of electronic products that bear his name. Sinclair Radionics was formed in 1961 and it created cost effective electronic products such as pocket calculators and portable televisions.

But, in 1980, Sinclair's company released the ZX80 computer for £99.95 and it was marketed as the “first personal computer for under £100”. The ZX80 proved a cost effective means to get your first computer and learn how to code but it did have a few quirks, famously causing the screen to flicker as each key was pressed. The ZX81 released in 1981 and retailing for half the price of the ZX80 addressed this and other issues while providing a solid base for learners eager to code. An agreement with Timex saw the ZX81 hit the US market as the Timex Sinclair 1000, and an unauthorised clone, the TK85 was released in Brazil via Microdigital Eletronica.

The ZX81 was a success and from that initial success more “ZX” machines were released with better graphics, and infamous rubber keys. The first ZX spectrum, released in 1982 introduced the rubber keys and diminutive size of the iconic classic computer. It is here where we start our journey into emulating the ZX Spectrum machines, chiefly the 48K and 128K aka “The Toastrack”. Can emulation give us the same nostalgic glow, can we create BASIC code projects and what is the best way to emulate a ZX Spectrum? Below, we show you two methods: one which turns your Raspberry Pi into a Spectrum and another which gives you a simple emulator that should work in Ubuntu and other flavors of Linux.

Emulating the Spectrum with Raspberry Pi

We don’t need much computing power to emulate a Spectrum and so, even your oldest Linux machine ,should be up to the job. There are emulators available for Windows, macOS, smartphones and even Nokia’s range of Linux Internet Tablets from the late 2000s.

Installing ZXBaremulator

ZX Spectrum Raspberry Pi

(Image credit: Tom's Hardware)

ZXBaremulator is a bare metal emulator which runs directly on the Raspberry Pi. It is compatible with all models of Pi excluding the Raspberry Pi 4, which makes ZXBaremulator an ideal candidate to reuse an old Raspberry Pi. The bare metal emulation runs directly on the Pi hardware and provides precise emulation of the Z80 processor.  It can also be used with real hardware thanks to the Raspberry Pi GPIO. This means that we can embed a Pi inside the shell of a broken “Speccy” and reuse the original keyboard.

To install ZXBaremulator you will need a spare micro SD card. Used used a 128MB card.

  1. Format the SD card as a FAT device.
  2. Download the All Files Needed archive from the website.
  3. Extract the contents of the download to the SD card.
  4. Eject the SD card and insert it into the Raspberry Pi.
  5. Connect your Pi accessories and power on.

ZX Spectrum Raspberry Pi

(Image credit: Tom's Hardware)

In just a few seconds we will see the ZXBaremulator boot into a Spectrum 48K. If you would like to emulate a 128K, then press ALT + F3, a Spectrum +2A is ALT + F4, returning to a 48K Speccy via ALT + F2.

Writing a few lines of BASIC

The BASIC (Beginners All-Purpose Symbolic Instruction Code) language was the Python of the 1980s. It typically came pre-installed as a ROM chip that the machine booted directly from. Writing BASIC is relatively simple as it is a human-readable language. The classic and easiest BASIC project is “Hello World” and it looks like this.

10 PRINT “Hello World”
20 GO TO 10
RUN

Line 10 will write “Hello World” to the screen and line 20 instructs the code to go back to line 10 in an infinite loop. Typing RUN will start the code.

ZX Spectrum Raspberry Pi

(Image credit: Tom's Hardware)

To write this for ourselves we need to remember that the keyboard of a Spectrum is very different than what we use now. The keys of a Spectrum have multiple uses and it can be easy to get confused. Pressing ALT + K will open the keyboard help menu, showing the Spectrum keyboard layout and color coded keywords.

  1. Press SHIFT + P to insert PRINT.
  2. Press CTRL + P to insert “ then type your message, in this case “Hello World”. Don’t forget to add another “ at the end of the message.
  3. Press Enter for a new line. Type 20.
  4. To insert GO TO press SHIFT + G.
  5. To type RUN, press R and the word will appear.
  6. Press Enter to run the code.

The code will print the message on to the screen and once the text fills the screen we are asked if we would like to scroll the text. Press Y or N as you see fit.

Emulating a Spectrum on Any Linux Computer

ZX Spectrum Raspberry Pi

(Image credit: Tom's Hardware)

We installed Fuse (Free Unix Spectrum Emulator) on Ubuntu 18.04. Fuse is considered the de facto emulator for Sinclair machines. Open a terminal and type the following to install the Fuse app and Spectrum machine ROMs.

$ sudo apt update
$ sudo apt-get install fuse-emulator-gtk
$ sudo apt install spectrum-roms

Once installed, launch the application by typing this command.

$ fuse

Getting to Grips With BASIC and Fuse

When Fuse starts we are presented with the 48K BASIC interpreter. So let's fire up a little BASIC code and learn a few of its quirks. Firstly it shares the same multiple function per key keyboard layout that all Sinclair machines have. So to write the simple Hello World script.

The BASIC code for this project is a little more complicated than before. The code will  loop round five times, asking for a number each time. These numbers are added together to produce a sum at the end. It demonstrates how to create a for loop, and create variables in BASIC.

1. Type 10 to start the line, then press SHIFT + L to insert LET. This is used to create a variable, total, and store the integer value of zero.

10 LET total=0

2. Type 20 and then press SHIFT + F to insert FOR, then type c=1 and press CTRL + F to insert TO. Type the integer 5. This creates a for loop that will iterate five times.

20 FOR c=1 TO 5

3. Type 30 and press SHIFT + I to insert the INPUT keyword, this will capture the numbers from the user and save them to a variable called “a”. The INPUT keyword takes an extra argument that enables the prompt to ask a question / give an instruction to the user.

30 INPUT “Number”,a

4. Type 40 and using another LET (SHIFT+L) we update the value stored in the total variable, adding the next integer to the sum.

40 LET total=total+a

5. Type 50, then press SHIFT + N to insert NEXT, then add the variable c. This will update the value stored in c, used to control our FOR loop.

50 NEXT c

6. Type 60 and insert PRINT (SHIFT + P) to print the value stored in the total variable.

60 PRINT total

Press R to trigger RUN to appear on the screen. Press Enter and provide five numbers. The code will add each number together and once all five are entered it will show the final amount.

Complete Code Listing

10 LET total=0
20 FOR c=1 TO 5
30 INPUT “Number”,a
40 LET total=total+a
50 NEXT c
60 PRINT total

If you would like to add a running total to the code, add a new line, 45 which prints the total each time the for loop iterates.

45 PRINT total

A version of this article originally appeared in an issue of Linux Format Magazine.

Les Pounder

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

  • Co BIY
    My first computer exposure was playing on my dad's Timex Sinclair 1000. He had quickly tired of it and I had free use of it. It had no storage (a cassette tape system was available I think) so it limited me to programs within my short attention span in a single session.

    Even then the horrible membrane keypad seemed unnecessarily cheap and nonfunctional. The pain involved in retyping every session made it essentially non functional.

    I was introduced to coding but it seemed tedious and unrewarding.

    But soon I would get the chance to use an Apple II at school. Color screen, good keyboard and 5 1/4 floppy disks!
    Reply
  • Yebemather
    It is great that RISC is being pushed so hard in the last decade. We can finaly do things that we did on CISC 40 years ago!
    Reply
  • nofanneeded
    ZX Spectrum was a failure ... bad keyboard . and bad way of shortcuts . Apple 2/ee/2c was around at the same time and was far superior.

    Also Commodore came and destroyed Spectrum to pieces in Europe back in the day.
    Reply
  • Neutrinophyre
    nofanneeded said:
    ZX Spectrum was a failure ... bad keyboard . and bad way of shortcuts . Apple 2/ee/2c was around at the same time and was far superior.

    Also Commodore came and destroyed Spectrum to pieces in Europe back in the day.
    Dont know where you are getting your information from but the spectrum wsd one of the biggestbselling home computers in the uk and parts of mainland europe. With new games being released regularly. Plus still has a big following.
    Reply
  • BogdanH
    ZX Spectrum was my second comp (after ZX81) and even games were a selling point, it was an excellent tool for learning programming (and I don't mean BASIC). Talking about which of them (Sinclair, Commodore, Apple) was better, is just silly. Importance of Sinclair is, that he made a first usable computer that anyone could afford! It was the first comp that created the term "hobby programmer".
    But reason for my writing is actually Raspberry Pi emulating Spectrum. Seriously, what's the point to "downgrade" Raspberry Pi to an 40 years old budget comp? What a waste of time.
    Reply
  • DZIrl
    BogdanH said:
    ZX Spectrum was my second comp (after ZX81)

    Same situation here. It was introduction to computers for many kids in Europe cause Apple II was too expensive. And big.
    Later I, my parents, bought Apple II replica for, think, some 1500 DEM while Spectrum vas like 200 DEM.

    I just googled and this is what they say on Wiki:
    It was originally released on 23 April 1982 with 16 KB of RAM for £125 (equivalent to £348.92 in 2019) or with 48 KB for £175 (equivalent to £488.48 in 2019);
    Oh my God! 3 times in 40 years!
    Reply
  • allstarspfunk
    nofanneeded said:
    ZX Spectrum was a failure ... bad keyboard . and bad way of shortcuts . Apple 2/ee/2c was around at the same time and was far superior.

    Also Commodore came and destroyed Spectrum to pieces in Europe back in the day.
    Not in my house, it was a sucess, played it every day for several years, tons of games most of them were great... and I don't think it's fair to compare to another computer that was like 10 times more expensive...
    Reply
  • nofanneeded
    allstarspfunk said:
    Not in my house, it was a sucess, played it every day for several years, tons of games most of them were great... and I don't think it's fair to compare to another computer that was like 10 times more expensive...

    Commodore 64 was ~$550 not ten times more expensive . and it destroyed all home pc market in Europe lol .

    and Atari 400 in USA at the time was about the same price as well .

    I also remember MSX from Japan was competing at $499

    MSX was also a hit ,in 1983 , and Konami made the first Metal Gear game on it.

    ZX spectrum had no hope compared to these PCs back in the day.

    by the way , my father owned all of them :p , including Apple //e as well , and ZX Spectrum .
    Reply
  • BogdanH
    nofanneeded said:
    Commodore 64 ... destroyed all home pc market in Europe lol...
    -at that time, home PC market didn't exist at all in Europe.
    ..my father owned all of them ...
    In short, you have no idea how it was back then.
    Reply
  • nofanneeded
    BogdanH said:
    -at that time, home PC market didn't exist at all in Europe.

    In short, you have no idea how it was back then.

    you are simply wrong.
    Reply