Emulate the ZX Spectrum with Raspberry Pi


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 unauthorized 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

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.

Source link


About Author

Leave A Reply