A wireless keyboard and mouse combo. You could use a wired keyboard and mouse but then you not only use up 1 extra valuable USB port but also have wires all over the place. I actually use the Logitech K400 plus wireless keyboard+trackpad combo.

A nice case to hold the Raspberry Pi. There are plenty of nice looking ones. I went for a simple black one keeping in theme with the speccy.

Once you have all the items from the above shopping list. It’s time to put it all together!

First things first

Putting together the hardware is super easy – just pop the Pi into it’s case and make sure the case is fitting snugly around the Pi. The software installation is easy too:

Download the Retro-pie image from https://retropie.org.uk/download/.

Use a SD formatter like Win32DiskFormatter to format the SD card (you may need a SD card adapter to put the microSD in) with the above image.

Pop the microSD into the Pi.

Hook up the Pi to the monitor/TV via the HDMI cable. Attach the game controller or its dongle (if it’s wireless) to the Pi. You will need keyboard and mouse too to set up things, so attach them to the USB slots.

Lastly, attach the microUSB power cable to the Pi. There are a couple of ways to source the power actually. You could use a cellphone charger that come with microUSB. Or you could use a powered USB Hub. Or you could use the TV/monitor itself to power the Pi if the TV/monitor has a USB port. This is what I do. This allows the Pi to power up automatically when the TV turns on. Note that the Pi 3 officially requires a 2.5VA power source to work faultlessly. So far, I’ve used my TV to power the Pi 3 without issues so maybe that power rating is for when you’re pushing the Pi 3 to drive a lot more stuff via it’s USB ports or external devices. I just have the Steam controller dongle and the wireless keyboard dongle on the USB ports. Seem to work fine!

The Setup

If all is well, the Retro-pie setup will automatically install the OS and itself on the Pi. It will also install something called the EmulationStation (ES), which is a front-end to the emulation goodness. You will see it come up once the installation is over and the Pi boots up. I suggest you follow the steps outlined here to complete the setup and then come back once those are out of the way.

If you’ve completed the steps from that link, your Pi should be on the wifi network and your controller should be ready to go. In my case, the Steam controller wasn’t detected and so I had to perform a couple of additional steps, which I will outline here in case you have a Steam controller too:

Go the Retro-pie setup page. This can be found by selecting RetroPie in the EmulationStation window, and then selecting RetroPie Setup.

This will drop you to a basic GUI setup page from which you can install all sorts of additional goodies for your Pi. Choose the Manage Packages option.

Choose Manage driver packages and select the steamcontroller package. It will ask you to choose between Xbox360 and keyboard/mouse controls – pick the former!

Back out all the way back to EmulationStation. Your Steam Controller should work now.

If the Steam controller isn’t working (i.e you aren’t able to navigate EmulationStation by using the D-pad), you can try restarting the Pi from the EmulationStation menu (Quit > Restart).

If the controller still isn’t working, go to Configure Input in the EmulationStation menu. It will ask you if you wish to configure input – select Yes. In the next window it should say that a gamepad was detected and ask you to hold a button to configure it. Do so, and map the controls on the controller to whatever you want. That’s it!

Play-time!

Well almost. You need to put some games on the Pi in order to play them. The cool thing about RetroPie is that it already comes with a dozen or so emulators pre-installed. They will show up in EmulationStation automatically if RetroPie finds games for them in a special folder called “roms”. I use Samba (via a Windows laptop) to transfer games, but you can use SSH or USB or SFTP to transfer files.

Sticking to Samba, if your Pi and your computer are on the same WiFi network, you should be able to see a folder called retropie under your Network in Windows Explorer. Under this is a folder called roms. You should see a folder called “zxspectrum” under this. If you don’t, create one and put all your speccy games in this folder. Zipped games are fine.

Now if you check in EmulationStation, you should see Sinclair ZX Spectrum listed. Selecting this will show you a list of speccy games that can be played. Choose any game to start playing.

The immediate thing you will notice is that your controller is useless. Most games usually expect you to select a control from a menu (1 for Keyboard, 2 for Kempston etc) before you can play the game. To do so, press the Select button on your controller (or whatever it was that you mapped for Select in the controller setup phase). This brings up the Fuse keyboard overlay. From here you can use your controller to select any speccy key. Press Select again to drop back into the game. If you start the game now expecting the controller to work like a Kempston, you will be in for a surprise – it doesn’t! That’s because you still need to tell the emulator how to treat the controller before it can pass on the same info to the game.

To do so, press Select + X. This will bring up the RetroArch GUI. RetroArch is the emulation gateway which interfaces between an emulator (known as a ‘core’ in RetroArch lingo) and the Pi. This is what allows you to actually use multiple emulators on the Pi.

Anyway, there are a lot of settings to play around with if you wish. But let’s just focus on getting the controller to work as a bog standard Kempston joystick for all speccy games, by default.

Go to the Quick Menu in RGUI – you may need to press Back or B on the controller to see the option. Select the Controls option. At the very top should be the “User 1 Device Type” field set to either “RetroPad” or “None”. Whatever it is set to, press Right or Left on the controller to scroll through the various joystick options. If you see Kempston, leave it at that. Scroll down to the Save Core Remap File option and choose it to create a default remap file. Press Back to go to previous menu and then select Save Core Override option. Finally select Resume. Your controller should work like a Kempston joystick now.

Some games don’t support Kempston though. For these games, you will have to over-ride the default option that was configured above. To do so, first fire up the game. Then do the same steps above, except select a supported joystick in “User 1 Device Type” and then do a Save Game Remap file instead of a Save Core Remap file, and similarly do a Save Game Override.

That’s it! You can start playing your favourite speccy game from the comfort of your couch now.

Bonus stuff

Better display

If you’re playing on a HD like display you will notice that most games are quite pixelated. You can reduce the pixelation by enabling BiLinear Filtering from the RGUI > Settings > Video page.

Another emulator

Fuse isn’t the only emulator that can run speccy games on the Pi. You can also try FBZX, Zesarux and UnrealSpeccy. To do so, go to RetroArch Setup > Manage Packages > Manage Optional Packages and install the emulators you need. You can tell RetroPie which emulator to use by pressing a button when you see the config dialog box whenever you launch a game.

Interesting packages

You may also find these packages of interest (from Manage Optional Packages):

Kodi – A media library. That’s all I have to say about it. *cough*

ScummVM – Remember all those point-and-click Lucas Arts adventure games? You can play them on the Pi using this package.

Scraper – A nice package that allows you to scrape game info, art, et al so that your library looks pretty. EmulationStation has its own built in scraper that you can access via the ES menu, but this scraper does a better job than the built-in one. Instructions on using either of them can be found here.

ES Themes

EmulationStation supports themeing. The stock one isn’t bad but there are more to be found online. To access them, go to the ES Themes setting from the RetroPie page in ES. I would highly recommend the ‘zoid’ and ‘tronkyfran’ themes.

Making Games Load and Run Automatically

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

While this is simple enough to achieve for an experienced Sinclair BASIC programmer, it is an area often overlooked. In particular, programmers migrating to the Spectrum from other machines will not be familiar with the way this is done.

In order to run a machine code routine, we have to start it from BASIC. This means writing a small BASIC loader program, which clears the space for the machine code, loads that code, and then runs it. The simplest sort of loader would be along these lines:

10 CLEAR 24575: LOAD ""CODE: RANDOMIZE USR 24576

The first command, CLEAR, sets RAMTOP below the area occupied by the machine code, so BASIC doesn’t overwrite it. It also clears the screen and moves the stack out of the way. The number that follows should usually be one byte below the first byte of your game. LOAD “”CODE loads the next code file on the tape, and RANDOMIZE USR effectively calls the machine code routine at the address specified, in this case 24576. This should be the entry point for your game. On a Spectrum, The ROM sits in the first 16K, and this is followed by various other things such as screen RAM, system variables and BASIC. A safe place for your code is above this area, all the way up to the top of RAM at address 65535. With just a short BASIC loader a start address of 24576, or even 24000 will give you plenty of room for your game.

This loader program is then saved to tape using a command like this:

SAVE "name" LINE 10

LINE 10 indicates that on loading, the BASIC program is to auto-run from line 10.

After the BASIC loader comes the code file. You can save a code file like this:

SAVE "name" CODE 24576,40960

CODE tells the Spectrum to save a code file, as opposed to BASIC. The first number after this is the start address of the block of code, and the last number is its length.

That is simple enough, but what if we want to add a loading screen? Well, that is straightforward enough. We can load a screen using

LOAD ""SCREEN$

What this will do is load a block of code up to 6912 bytes long, to the start of the screen display at address 16384. Putting the screen file there is a bit trickier, because we cannot simply save out the screen as a file as the bottom two lines would be overwritten with the Start tape, then press any key message. So we load our picture into a point in RAM – say, 32768 – then use

SAVE "name" CODE 32768,6912

6912 is the size of the Spectrum’s display RAM. When we reload the block from tape using LOAD “”SCREEN$, we are specifying that we want to force the code file to be loaded into screen memory. Under these circumstances it doesn’t matter where the code file was located when it was saved.

Now we have another problem: wouldn’t the Bytes: name message that is printed up on loading the code block overwrite part of the screen? Well, yes it would. We can overcome this by poking the output stream.

Interrupts

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

Setting up your own interrupts can a nightmare the first time you try it, as it is a complicated business. With practice, it becomes a little easier. To make the Spectrum run our own interrupt routine, we have to tell it where the routine is, put the machine into interrupt mode 2, and ensure that interrupts are enabled. Sound simple enough? The tricky part is telling the Spectrum where our routine is located.

With the machine in mode 2, the Z80 uses the I register to determine the high byte of the address of the pointer to the interrupt service routine address. The low byte is supplied by the hardware. In practice, we never know what the low byte is going to be – so you see the problem? The low byte could be 0, it could be 255, or it could be anywhere in between. This means we need a whole block of 257 bytes consisting of pointers to the start address of our service routine. As the low byte supplied by the hardware could be odd or even, we have to make sure that the low byte and the high byte of the address of our service routine are identical. This seriously restricts where we can locate our routine.

We should also only locate our table of pointers and our routine in uncontended RAM. Do not place them below address 32768. Even paging in an uncontended RAM bank for the purpose, such as bank 1, will produce problems on certain models of Spectrum. Personally, I find bank 0 to be as good a place as any.

Let us say we choose address 51400 as the location of our interrupt routine. This is valid as both the high byte and low byte are 200, since 200*256+200 = 51400. We then need a table of 129 pointers all pointing to this address, or 257 instances of defb 200, located at the start of a 256-byte page boundary. Assuming we put it high up out of the way, we could start it at 254*256 = 65024.

Ugh! Still, now we come to our interrupt routine. Interrupts can occur during any period, so we have to preserve any registers we are likely to use, perform our code, optionally call the ROM service routine, restore the registers, re-enable interrupts, then return from the interrupt with a RETI. Our routine might resemble this:

If you are not reading the keyboard via the system variables you may wish to dispense with the RST 56. Doing so will free up the IY registers. However, if your game’s timing counts the frames using the method described in the timing chapter, you will need to increment the timer yourself:

ld hl,23672 ; frames counter.
inc (hl) ; move it along.

With all this in place, we are ready to set off our interrupts. We have to point the I register at the table of pointers and select interrupt mode 2. This code will do the job for us:

Music and AY Effects

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

The AY-3-8912

Introduced with the 128K models and pretty much standard since then, this is a very popular sound chip, used on various other computers, not to mention video games and pinball machines. Basically, it has 14 registers, which can be written to, or read from, via in and out instructions.

The first six registers control the tone for each of the three channels, and are paired in the little-endian way we would expect, ie register 0 is Channel A tone low, register 1 is channel A tone high, register 2 is channel B tone low, and so on. Register 6 controls the white noise period, values of 0 to 31 are valid. 0 gives the highest frequency noise, 31 the lowest. Register 7 is the mixer control. Bits d0-d5 select white noise, tone, neither, or both. To enable tone or white noise, a bit must be reset, so 0 outputs tone and noise from all three channels, 63 outputs nothing. Registers 8, 9 and 10 are envelope/amplitude controls. A value of 16 tells the chip to use the envelope generator, 0-15 will set the volume for that channel directly.

In practice, you are better off controlling the volume yourself, along with the tone. By varying these from one frame to the next, it is possible to produce a variety of very good sound effects. Should you wish to use the envelope generator, the next two registers, 11 and 12, are paired to form the 16-bit period of the envelope, and register 13 determines the pattern. The 128K manual explains the full list of patterns, but I won’t cover them here as I have never found them particularly useful myself.

To read a register, we write the number of the register to port 65533, then immediately read that port. To write to a register, we again send the number if the register to port 65533, and then the value to 49149. To the uninitiated, Z80 opcodes don’t appear to be capable of writing to 16-bit port addresses. Don’t let that confuse you, it’s just that the way they are written is misleading. out (c),a actually means out (bc),a and out (n),a actually does out (a*256+n),a.

Reading a sound chip register does have some uses. You might want to read the volume registers 8, 9 and 10 and display volume bars – I did something along those lines in Egghead 5. Also, believe it or not, the Sinclair light gun is read via sound chip register 14. Yes, really. It only actually yields two pieces of information, whether or not the trigger is pressed, and whether or not the gun is pointed at a bright part of the screen, or indeed any bright object. It is up to the programmer what he does with that information.

By calling w8912 once every iteration of the main loop, the sound is constantly updated. It is then up to you to update the buffer as each noise changes from one frame to the next. Think of it as “animating” sound. However, just because you stop updating the sound registers the sound won’t stop playing. The AY chip will keep playing your tone or noise until instructed to stop. A quick way to do this is to set the three amplitude registers to 0. In the example above, write a zero to sndv1, sndv2 and sndv3 then call w8912.

Using Music Drivers

Most 128K music drivers, and some 48K ones, have two entry points. An initialisation/termination routine which stops all sound and resets the driver to the beginning of the tune, and a service routine to be called repeatedly, usually 50 times per second. A good place to store music is usually 49152, the start of the switchable RAM bank area. If you know the start address of the driver, or are in a position to determine this yourself, the very beginning is usually the initialisation address which sets up your music at the start. More often than not, the code at this point either simply jumps to another address, or loads a register or register pair before jumping elsewhere. The service routine tends to immediately follow this jp instruction. If your driver has no other documentation, you may have to disassemble the code to find this address, usually 3-6 bytes in.

To use a music driver, call the initialisation address prior to starting, and also when you want to turn it off. Between these points, you need to call the service routine repeatedly. This can either be done manually, or by setting up an interrupt to do the job automatically. If you choose to do this manually, for example in menu code, bear in mind that clearing the screen and displaying a menu, high score table, instructions etc will take more than 1/50th of a second to do, so this will delay your routine and could sound odd. It might be better to write a routine to clear the screen over several frames with some sort of special effect, punctuated with halts and calls to the service routine every frame.

Mathematics

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

Adding and subtracting is straightforward enough on the Spectrum’s CPU, we have an abundance of instructions to perform these tasks. But unlike some later processors in the series, the programmer of the Z80A has to do his own multiplication and division. While such calculations are rare, they have their uses in certain types of game, and until you have routines to do the job, certain things are very tricky to do. For example, without Pythagoras’ theorem, it can be difficult to program an enemy sprite to shoot at the player with any degree of accuracy.

Suppose sprite A needs to fire a shot at sprite B. We need to find the angle at which sprite A is to fire, and some trigonometry is necessary to do this. We know the coordinates of the sprite, Ax, Ay, Bx and By, and the distances between these, Bx-Ax and By-Ay, will give us the opposite and adjacent line lengths. Unfortunately, the only way to calculate the angle from the opposite and adjacent is to use arctangent, and as tangents are only suitable for certain angles, we are better off using sine or cosine instead. So in order to find the angle from sprite A to sprite B, we need to find the length of the hypotenuse.

The hypotenuse is calculated by squaring the x distance, adding it to the square of the y distance, then finding the square root. There are routines in the Sinclair ROM to do all of this, but there is one serious drawback: as anyone who has ever used Sinclair BASIC will tell you, the maths routines are incredibly slow for writing games. So we have to knuckle down and write our own.

Squaring our x and y distances means using a multiplication routine and multiplying the numbers by themselves. Thankfully, this part is relatively painless. Multiplication is achieved in the same way as you would perform long multiplication on paper, although this time we are working in binary. All that is required is shifting, testing bits, and adding. Where a bit exists in our first factor, we add the second factor to the total. Then we shift the second factor left, and test the next bit along in our first factor. The routine below, taken from Kuiper Pursuit, demonstrates the technique by multiplying H by D and returning the result in HL.

Now we need a square root, which is where our problems begin. Square roots are a lot more complicated. This means doing a lot of divisions, so first we need a division routine. This can be seen to work in the opposite way to multiplication, by shifting and subtracting. The next routine, also from Kuiper Pursuit, divides HL by D and returns the result in HL.

In the same way that multiplication is made up of shifting and adding, and division is done via shifting and subtracting, so square roots can be calculated by shifting and dividing. We’re simply trying to find the “best fit” number which, when multiplied by itself, gives us the number with which we started. I won’t go into detailed explanation as to how the following routine works – if you really are that interested, follow my comments and step it through a debugger. Taken from Blizzard’s Rift, it returns the square root of HL in the accumulator.

With the length of the hypotenuse calculated, we can simply divide the opposite line by the hypotenuse to find the cosine of the angle. A quick search of our sine table will then tell us what that angle is. Phew!

This is the entire calculation taken from Blizzard’s Rift. Note that it uses the adjacent line length rather than the opposite, so finds the arccosine instead of the arcsine. It is also only used when the ship is above the gun turret, giving the player the opportunity to sneak up and attack from underneath. Nevertheless, it demonstrates how a sprite can fire at another with deadly accuracy. If you have ever played Blizzard’s Rift, you will know exactly how lethal those gun turrets can be.

; Ship is above the gun so we can employ some basic trigonometry to aim it.
; We need to find the angle and to do this we divide the adjacent by
; the hypotenuse and find the arccosine.
; First of all we put the length of the opposite on the stack:
mgunx ld a,(nshipy) ; ship y coordinate.
ld hl,guny ; gun y coord.
sub (hl) ; find difference.
jr nc,mgun0 ; result was positive.
neg ; negative, make it positive.
mgun0 cp 5 ; y difference less than 5?
jr c,mgunu ; yes, point straight up.
push af ; place length of opposite on stack.
; Next we require the length of the hypotenuse and we can use good
; old Pythagoras' theorem for this.
ld h,a ; copy a to h.
ld d,h ; copy h to d.
call imul ; multiply integer parts to get 16-bit result.
push hl ; remember squared value.
ld hl,nshipx ; gun x coordinate.
ld a,(gunx) ; ship x coordinate.
sub (hl) ; find difference, will always be positive.
ld h,a ; put x difference in h.
ld d,h ; copy h to d.
call imul ; multiply h by d to get square.
pop de ; get last squared result.
add hl,de ; want the sum of the two.
call isqr ; find the square root, hypotenuse in a.
pop de ; opposite line now in d register.
ld h,a ; length of hypotenuse.
ld l,0 ; no fraction or sign.
ex de,hl ; switch 'em.
; Opposite and hypotenuse are now in de and hl.
; We now divide the first by the second and find the arcsine.
; Remember - sine = opposite over hypotenuse.
call div ; division will give us the sine.
ex de,hl ; want result in de.
call asn ; get arcsine to find the angle.
push af
; Okay, we have the angle but it's only 0 to half-pi radians (64 angles)
; so we need to make an adjustment based upon the quarter of the circle.
; We can establish which quarter of the circle our angle lies in by
; examining the differences between the ship and gun coordinates.
ld a,(guny) ; gun y position.
ld hl,shipy ; ship y.
cp (hl) ; is ship to the right?
jr nc,mgun2 ; player to the left, angle in second quarter.
; Angle to play is in first quarter, so it needs subtracting from 64.
ld a,64 ; pi/2 radians = 64 angles.
pop bc ; angle in b.
sub b ; do the subtraction.
ld (ix+1),a ; new angle.
ret ; we have our angle.
; Second quarter - add literal 64 to our angle.
mgun2 pop af ; original angle.
add a,192 ; add pi/2 radians.
ld (ix+1),a ; new angle.
ret ; job's a good 'un!

Sophisticated Movement

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

So far, we have moved sprites up, down, left and right by whole pixels. However, many games require more sophisticated sprite manipulation. Platform games require gravity, top-down racing games use rotational movement and others use inertia.

Jump and Inertia Tables

The simplest way of achieving gravity or inertia is to have a table of values. For example, the Egghead games make use of a jump table and maintain a pointer to the current position. Such a table might look like the one below.

To initiate a jump, we would set jptr to jtabu. To start falling, we would set it to jtabd.

Okay, so it’s a bit simplistic. In practice, we would usually use the value from the jump table as a loop counter, moving the player up or down one pixel at a time, checking for collisions with platforms, walls, deadly items etc as we go. We might also use the end marker (128) to signify that the player had fallen too far, and set a flag so that the next time the player hits something solid, he loses a life. That said, you get the picture.

Fractional Coordinates

If we want more sophisticated gravity, inertia, or rotational movement we need fractional coordinates. Up until now, with the Spectrum’s resolution at 256×192 pixels, we have only needed to use one byte per coordinate. If instead we use a two-byte register pair, the high byte for the integer and low byte for the fraction, we open up a whole new world of possibilities. This gives us 8 binary decimal places, allowing very precise and subtle movements. With a coordinate in the HL pair, we can set up the displacement in DE, and add the two together. When plotting our sprites, we simply use the high bytes as our x and y coordinates for our screen address calculation, and discard the low bytes which hold the fractions. The effect of adding a fraction to a coordinate will not be visible every frame, but even the smallest fraction, 1/256, will slowly move a sprite over time.

Now we can take a look at gravity. This is a constant force, in practice it accelerates an object towards the ground at 9.8m/s^2. To simulate it in a Spectrum game, we set up our vertical coordinate as a 16-bit word. We then set up a second 16-bit word for our momentum. Each frame, we add a tiny fraction to the momentum, then add the momentum to the vertical position. For example:

Then, to plot our sprites, we simply take the high byte of our vertical position, verpos+1, to give us the number of pixels from the top of the screen. Different values of DE will vary the strength of the gravity, indeed we can even swap the direction by subtracting DE from HL, or by adding a negative distance (65536-distance). We can apply the same to the y coordinate too, and have the sprite subject to momentum in all directions. This is how we would go about writing a Thrust-style game.

Rotational Movement

The other thing we might need for a Thrust game, top-down racers, or anything where circles or basic trigonometry is involved is a sine/cosine table. Mathematics isn’t everybody’s cup of tea, and if your trigonometry is a little rusty I suggest you read up on sines and cosines before continuing with the remainder of this chapter.

In mathematics, we can find the x and y distance from the centre of a circle given the radius and the angle by using sines and cosines. However, whereas in maths a circle is made up of either 360 degrees or 2 PI radians, it is more convenient for the Spectrum programmer to represent his angle as, say, an 8-bit value from 0 to 255, or even use fewer bits, depending on the number of positions the player sprite can take. He can then use this value to look up his 16-bit fractional values for the sine and cosine in a table. Assuming we have an 8-bit angle set up in the accumulator, and we wish to find the sine, we simply access the table in a manner similar to this:

Sinclair BASIC actually provides us with the values we require, with its SIN and COS functions. Using this, we can POKE the values returned into RAM and either save to tape, or save out the binary using an emulator such as SPIN. Alternatively, you may prefer to use another programming language on the PC to generate a table of formatted sine values. to import into your source file, or include as a binary. For a sine table with 256 equally-spaced angles, we would need a total of 512 bytes, but we would need to be careful to convert the number returned by SIN into one our game will recognise. Multiplying the sine by 256 will give us our positive values, but where SIN returns a negative result, we might need to multiply the ABS value of the sine by 256, then either subtract that from 65536 or set bit d7 of the high byte to indicate that the number must be subtracted rather than added to our coordinate. With a sine table constructed in this manner, we don’t need a separate table for cosines, as we just add or subtract 64, or a quarter-turn, to the angle before looking up the value in our table. To move a sprite at an angle of A, we add the sine of A to one coordinate, and the cosine of A to the other coordinate. By changing whether we add or subtract a quarter turn to obtain the cosine, and which plane uses sines and which uses cosines, we can start our circle at any of the 4 main compass points, and make it go in a clockwise or anti-clockwise direction.

Double Buffering

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

Until now we have drawn all our graphics directly onto the screen, for reasons of speed and simplicity. However, there is one major disadvantage to this method: if the television scan line happens to be covering the particular screen area where we are deleting or redrawing our image then our graphics will appear to flicker. Unfortunately, on the Spectrum there is no easy way to tell where the scan line is at any given point so we have to find a way around this.

One method which works well is to delete and redraw all sprites immediately following a halt instruction, before the scan has a chance to catch up with the image being drawn. The disadvantage to this method is that our sprite code has to be pretty fast, and even then it is not advisable to delete and re-draw more than two sprites per frame because by then the scan will be over the top border and into the screen area. Of course, locating the status panel at the top of the screen might give a little more time to draw our graphics, and if the game is to run at 25 frames per second we could employ a second halt instruction and manoeuvre another couple of sprites immediately afterwards.

Ultimately, there comes a point where this breaks down. If our graphics are going to take a little longer to draw we need another way to hide the process from the player and we need to employ the use of a second buffer screen. This means that all the work involved in drawing and undrawing graphics is hidden from the player and all that is visible is each finished frame once it has been drawn.

There are two ways of doing this on a Spectrum. One method will only work on a 128K machine, so we will put that to one side for the time being. The other method actually tends to be more complicated in practice but will work on any Spectrum.

Creating a Screen Buffer

The simplest way to implement double buffering on a 48K Spectrum is to set up a dummy screen elsewhere in RAM, and draw all our background graphics and sprites there. As soon as our screen is complete we copy this dummy screen to the physical screen at address 16384 thus:

While in theory this is perfect, in practice copying 6912 bytes of RAM (or 6144 bytes if we ignore the colour attributes) to the screen display every frame it is too slow for arcade games. The secret is to reduce the amount of screen RAM we need to copy each frame, and to find a faster way than by transferring it with the LDIR instruction.

The first way is to decide how big our screen is going to be. Most games separate the screen into 2 areas: a status panel to display score, lives and other bits of information, and a window where all the action takes place. As we don’t need to update the status panel every frame our dummy screen only needs to be as big as the action window.

So if we were to have a status panel as an 80 x 192 pixel at the right edge of the screen that would leave us a 176×192 pixel window, meaning our dummy screen would only need to be 22 chars wide by 192 pixels high, or 22×192=4224 bytes. Manually moving 4224 bytes from one part of RAM to another is far less painful than manipulating 6114 bytes. The trick is to find a size which is large enough not to restrict gameplay while being small enough to be manipulated quickly. Of course, we may also want to make our buffer a little larger around the edges. While these edges are not displayed on the screen they are useful if we wish to clip sprites as they move into the action window from the sides.

Once we have set our buffer size in stone we need to write a routine to transfer it to the physical display file one or two bytes at a time. While we are at it, we can also re-order our buffer screen to use a more logical display method than the one used by the physical screen. We can make allowances for the peculiar ordering of the Spectrum’s display file in our transfer rountine, meaning any graphics routines which make use of our dummy screen buffer can be simplified.

There are two really quick ways of moving a dummy screen to the display screen. The first, and most simple method, is to use lots of unrolled LDI instructions. The second, and more complicated method, makes use of PUSH and POP to transfer the data.

Let us start with LDI. If our buffer is 22 chars wide we might transfer a single line from the buffer to the screen display with 22 consecutive LDI instructions – it is much quicker to use lots of LDI instructions than to use a single LDIR. We could write a routine to transfer our data across a single line at a time, pointing HL to the start of each line of the buffer, DE to the line on the screen where it is needed, and then 22 LDI instructions to move the data across. However, as each LDI instruction takes two bytes of code, it stands to reason that such a routine would be at least twice the size of the buffer it moved. A considerable hit when dealing with a little over 40K of useful RAM. You may instead wish to move the LDI instructions to a subroutine which copies a pixel line, or perhaps a group of 8 pixel lines, at a time. This routine could then be called from within a loop – unrolled or not – which could take care of the HL and DE registers.

The second method is to transfer the buffer to the screen using PUSH and POP instructions. While this does have the advantage of being the fastest way there is, there are drawbacks. You do need complete control of the stack pointer so you can’t have any interrupts occurring mid-way through the routine. The stack pointer must be stored away somewhere first, and restored immediately afterwards.

The Spectrum’s stack is usually located below your program code, but this method involves setting the stack to point to each part of the buffer in turn, and then using POP to copy the contents of the dummy screen buffer into each of the register pairs in turn. The stack pointer is then moved to the relevant point in the screen display RAM, before the registers are PUSHed into memory in the reverse order to that in which they were POPped. Ie, values are POPped from the buffer going from the start of each line, and PUSHed to the screen in the reverse order, going from the end of the line to the beginning.

Below is the gist of the screen transfer routine from Rallybug. This used a buffer 30 characters wide, with 28 characters visible on screen. The remaining 2 characters were not displayed so that sprites moved onto the screen slowly from the edge, rather than suddenly appearing from nowhere. As the visible screen width is 28 characters wide, this requires 14 16-bit registers per line. Obviously, the Z80A doesn’t have this many, even counting the alternate registers and IX and IY. As such, the Rallybug routine splits the display into two halves of 14 bytes each, requiring just 7 register pairs. The routine sets the stack pointer to the beginning of each buffer line in turn, then POPs the data into AF, BC, DE and HL. It then swaps these registers into the alternate register set with EXX, and POPs 6 more bytes into BC, DE and HL. These registers now need to be unloaded into the screen area, so the stack pointer is set to point to the end of the relevant screen line, and HL, DE and BC are PUSHed into position. The alternate registers are then restored, and HL, DE, BC and AF are respectively copied into position. This is repeated over and over again for each half of each screen line, before the stack pointer is restored to its original position.

Now we have our dummy screen, we can do anything we like to it without the risk of flicker or other graphical anomalies, because we only transfer the buffer to the physical screen when we have finished building the picture. We can place sprites, masked or otherwise, anywhere we like and in any order we like. We can move the screen around, and animate the background graphics, and most importantly, we can now scroll in any direction.

Different techniques are required for different types of scrolling, although they all have one thing in common: as scrolling is a processor-intensive task, unrolled loops are the order of the day. The simplest type of scroll is a left/right single pixel scroll. A right single pixel scroll requires us to set the HL register pair to the start of the buffer, then execute the following two operands over and over again until we reach the end of the buffer:

For most of the time, however, we can get away with only incrementing or decrementing the l register, instead of the HL pair, speeding up the routine even more. This does have the drawback of having to know exactly when the high order byte of the address changes. For this reason, I usually set my buffer address in stone right at the beginning of the project, often at the very top of RAM, so I don’t have to rewrite the scrolling routines when things get shifted around during the course of a project. As with the routine to transfer the buffer to the physical screen, a massive unrolled loop is very expensive in terms of RAM, so it is a good idea to write a smaller unrolled loop which scrolls, say, 256 bytes at a time, then call it 20 or so times, depending upon the chosen buffer size.

In addition to scrolling one pixel at a time, we can scroll four pixels fairly quickly too. By replacing rl (hl) with rld in the left scroll, and rr (hl) with rrd in the right scroll, we can move 4-pixels.

Vertical scrolling is done by shifting bytes around in RAM, in much the same way as the routine to transfer the dummy screen to the physical one. To scroll up one pixel, we set our FROM address to be the start of the second pixel line, the TO address to the address of the start of the buffer, then copy the data from the FROM address to the TO address until we reach the end of the buffer. To scroll down, we have to work in the opposite direction, so we set our FROM address to the end of the penultimate line of the buffer, our TO address to the end of the last line, and work backwards until we reach the start of the buffer. The added advantage of vertical scrolling is that we can scroll up or down by more than one line, simply by altering the addresses, and the routine will run just as quickly. Generally speaking, it isn’t a good idea to scroll by more than one pixel if your frame rate is lower than 25 frames per second, because the screen will appear to judder.

There is one other technique that can be employed with vertical scrolling, and it is one I employed when writing Megablast for Your Sinclair. This involves treating the dummy screen as wrap-around. In other words, you still use the same amount of RAM for the dummy buffer, but the part of the buffer from which you start copying to the top of the screen can change from one frame to the next. When you reach the end of the buffer, you skip back to the beginning. With this system, the routine to copy the buffer takes the address of the start of the buffer from a 16-bit pointer which could point to any line in the buffer, and copies the data to the physical screen line by line until it reaches the end of the buffer. At this point, the routine copies the data from the start of the buffer to the remainder of the physical screen. This makes the transfer routine a little slower, and complicates any other graphics routines – which also have to go back to the first line whenever they go beyond the last line in the buffer. It does, on the other hand, mean that no data needs to be shifted in order to scroll the screen. By changing the 16-bit pointer to the line which is first copied to the physical screen, scrolling is done automatically when the buffer is transferred.

Timing

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

The Halt Instruction

We measure the speed of a Spectrum game by the amount of time it takes for a complete pass of the main loop, including all the jobs done by routines called within that loop. The simplest way to introduce a delay is to insert halt instructions to wait for an interrupt at certain points in the main loop to wait for an interrupt. As the Spectrum generates 50 interrupts per second, this means that main loops which have 1, 2 or 3 such pauses will run at 50, 25 or 17 frames per second respectively, so long as the other processing does not take up more than a frame to complete. Generally speaking, it is not a good idea to have the player sprite moving more slowly than 17 frames per second.

Actually, the halt instruction can be quite handy. In effect, it waits for the television scan line to reach the end of the screen. This means that a good time to delete, move then re-display a sprite is immediately after a halt, because the scan line won’t catch up with the image, and there is no chance of flicker. If you have your game’s status panel at the top of the screen, this means there is even further for the scan line to travel before it reaches the sprite area, and you can often squeeze in a couple of sprites after a halt without much danger of flickering.

The halt instruction can also be used in a loop to pause for longer periods. The following code will pause for 100 fiftieths of a second – or two seconds:

Unfortunately, halt is a blunt instrument. It always waits for the next interrupt, regardless of how long is left before the next one. Imagine a situation where your main loop takes 3/4 of a frame to do its processing most of the time, but every so often has periods where extra processing is involved, taking up an extra 1/2 a frame. Under these circumstances, a halt will keep the game at a constant 50 frames per second for the majority of the time, but as soon as the extra processing kicks in, the first interrupt has passed, and halt will wait for the next one, meaning that the game slows down to 25 frames per second periodically.

There is a way around this problem, and that is to count the number of frames that have elapsed since the last iteration of the main loop. On the Spectrum, the interrupt service routine in the ROM updates the Spectrum’s 24-bit frames counter 50 times per second, as well as doing other things. This counter is stored in the system variables at address 23672, so by checking this location once every iteration of the loop, we can tell how many interrupts have occurred since the last time we were at the same point. Naturally, if you want to write your own interrupt routines you will either have to use rst 56 to update the clock, or increment a frame counter yourself if you wish to use this method.

This routine is designed to stabilise a game to run at a more-or-less constant 25 frames per second:

Instead of simply sitting in a loop, you could perform some additional non-essential processing. For example, I tend to cycle my sprites around the table I hold them in, changing the order in which they are displayed each loop to help prevent flickering.

Seeding Random Numbers

The Spectrum’s frame counter is useful for something else: it can be used to initialise the seed for random numbers. Using the random number generator in the random numbers chapter, we can do this:

This is fine if we’re working on genuine hardware, and will ensure a game does not start with the same sequence of random numbers every time it is played. Unfortunately, emulator authors have a nasty habit of automatically loading tape files once opened – a practice which not only makes development difficult, it results in the machine always being in the same state every time a particular game is loaded, meaning random numbers can follow the same sequence every time that game is played. The solution for the games programmer is to wait for a debounced keypress as soon as our game has loaded, after which we can set our seed. This introduces a human element and ensures the random number generator is different every time.

Enemy Movement

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

So we have our playfield, and can allow the player to manipulate a sprite around it, but what we now need are some enemy sprites for the player to avoid. A new programmer could struggle here, but it really is far simpler than it first appears.

Patrolling Enemies

The easiest type of enemy to program is that with a fixed algorithm to follow, or a predetermined patrol route. We covered one such technique in the Centipede game earlier. Another very simple example is that found in games such as JetSet Willy, where a sprite travels in a single direction until it reaches the end of its patrol, then switches direction and heads back to its starting point, before changing direction again and starting the cycle again. As you might imagine, these routines are incredibly easy to write.

Firstly we set up our alien structure table with minimum and maximum coordinate positions and the present direction. It’s generally a good idea to comment these tables so we’ll do that too.

If we wanted to go further we might introduce an extra flag to our table, ix+6, to control the speed of the sprite, and only move it, say, every other frame if the flag is set. While simple to write and easy on memory usage, this sort of movement is rather basic and predictable and of limited use. For more complicated patrolling enemies, for example the alien attack waves in a shoot-em-up, we need tables of coordinates and while the code is again easy to write, coordinate tables quickly chew up memory especially if both x and y coordinates are stored. To access such a table we need two bytes per sprite which act as a pointer to the coordinate table.

The slightly more complicated example below demonstrates an 8-ship attack wave using a table of vertical coordinates. The horizontal position of each sprite moves left at a constant rate of 2 pixels per frame so there’s no need to bother storing it. It uses the shifter sprite routine from chapter 8 so the sprites are a little flickery, but that’s not important here.

So far we have dealt with predictable drones, but what if we want to give the player the illusion that enemy sprites are thinking for themselves? One way we could start to do this would be to give them an entirely random decision making process.

Here is the source code for Turbomania, a game originally written for the 1K coding competition in 2005. It’s very simple, but incorporates purely random movement. Enemy cars travel in a direction until they can no longer move, then select another direction at random. Additionally, a car may change direction at random even if it can continue in its present direction. It’s very primitive of course, just take a look at the mcar routine and you’ll see exactly what I mean.

If you have assembled this game and tried it out you will realise that it quickly becomes boring. It is very easy to stay out of the reach of enemy cars to cover one side of the track, then wait until the cars move and cover the other side. There is no hunter-killer aspect in this algorithm so the player is never chased down. What’s more, this routine is so simple cars will reverse direction without warning. In most games this is only acceptable if a sprite reaches a dead end and cannot move in any other direction.

Perhaps we should instead be writing routines where aliens interact with the player, and home in on him. Well, the most basic algorithm would be something along the lines of a basic x/y coordinate check, moving an alien sprite towards the player. The routine below shows how this might be achieved, the homing routine almov is the one which moves the chasing sprite around. Try guiding the number 1 block around the screen with keys ASD and F, and the number 2 block will follow you around the screen. However, in doing this we soon discover the basic flaw with this type of chase – it is very easy to trap the enemy sprite in a corner because the routine isn’t intelligent enough to move backwards in order to get around obstacles.

The best alien movement routines use a combination of random elements and hunter-killer algorithms. In order to overcome the problem in the listing above we need an extra flag to indicate the enemy’s present state or in this case its direction. We can move the sprite along in a certain direction until it becomes possible to switch course vertically or horizontally, whereupon a new direction is selected depending upon the player’s position. However, should it not be possible to move in the desired direction we go in the opposite direction instead. Using this method a sprite can find its own way around most mazes without getting stuck too often. In fact, to absolutely guarantee that the sprite will not get stuck we can add a random element so that every so often the new direction is chosen on a random basis rather than the difference in x and y coordinates.

Cranking up the Difficulty Levels

The weighting applied to the direction-changing decision will determine the sprite’s intelligence levels. If the new direction has a 90% chance of being chosen on a random basis and a 10% chance based on coordinates the alien will wander around aimlessly for a while and only home in on the player slowly. That said, a random decision can sometimes be the right one when chasing the player. An alien on a more difficult screen might have a 60% chance of choosing a new direction randomly, and a 40% chance of choosing the direction based on the player’s relative position. This alien will track the player a little more closely. By tweaking these percentage levels it is possible to determine difficulty levels throughout a game and ensure a smooth transition from the simplest of starting screens to fiendishly difficult final levels.

Scores and High Scores

Note: This article was originally written by Jonathan Cauldwell and is reproduced here with permission.

More Scoring Routines

Up until now we have gotten away with an unsophisticated scoring routine. Our score is held as a 16-bit number stored in a register pair, and to display it we have made use of the Sinclair ROM’s line number print/display routine. There are two main drawbacks to this method, firstly we are limited to numbers 0-9999, and secondly it looks awful.

This method works well, though we’re still limited to a five-digit score of no more than 65535. For a more professional-looking affair complete with any number of leading zeroes we need to hold the score as a string of ASCII digits.

I have used the same scoring technique for something like 15 years now, it isn’t terribly sophisticated but it’s good enough to do what we need. This method uses one ASCII character per digit, which makes it easy to display. Incidentally, this routine is taken from the shoot-em-up More Tea, Vicar?

Simple, but it does the job. Pedants would no doubt point out that this could be done using BCD, and that all the opcodes for this are in the Z80 instruction set.

High Score Tables

High Score routines are not especially easy to write for a beginner, but once you have written one it can be re-used again and again. The basic principle is that we start at the bottom of the table and work our way up until we find a score that is greater than, or equal to, the player’s score. We then shift all the data in the table below that point down by one position, and copy our player’s name and score into the table at that point.

We can set the hl or ix register pair to the first digit of the bottom score in the table and work our way comparing each digit to the corresponding one in the player’s score. If the digit in the player’s score is higher we move up a position, if it is lower we stop there and copy the player’s score into the table one place below. If both digits are the same we move to the next digit and repeat the check until the digits are different or we have checked all the digits in the score. If the scores are identical we place the player’s entry in the table one place below. This is repeated until a score in the table is higher than the player’s score, or we reach the top of the table.

When first initialising a high score table it may be tempting to place your own name at the top with a score that is very difficult to beat. Try to resist this temptation. High score tables are for the player to judge his own performance, and there is no point in frustrating the player by making it difficult to reach the top position.