Making Use Of Spectrum Interrupts



Everything from software clocks to automatic

screen scrolling - Nigel Dore shows you how

to divert the interrupt to user routines to

create a very powerful facility.



[ This article included one "mother" program for the 16k

  and one for the 48k, plus six example routines. The user

  was supposed to enter a mother program, and then enter

  an interrupt routine over the top of it. Since I have

  provided a TZX with pre-entered listings instead, I've

  changed references to "entering" below to "loading" or

  "merging" as appropriate. The article ends with the

  descriptions of the individual sample routines, to which

  I've added a note of their program names on the TZX. ]



An interrupt is a signal sent to the microprocessor which

generally suspends the execution of the current program -

without the program knowing it.

  Every 20 ms the Z80 processor in the Spectrum receives an

interrupt which forces it to suspend what it is currently

doing and to jump to 38 hexadecimal or 56 decimal where it

increments the ssytem variable FRAMES and scans the key-

board so that the Spectrum knows what you are keying in, if

anything. If this interrupt could be diverted elsewhere to

a user routine it would provide a very powerful facility

which would have almost limitless uses. The way in which

the Z80 responds to an interrupt is governed by which

interrupt mode it is operating in; the Z80 has three modes.

  Interrupt mode 0 allows the interrupting device to place

one instruction on to the databus after the interrupt for

execution by the Z80. Interrupt mode 1 makes a jump to

38 hexadecimal or 56 decimal and this is the mode that the

Spectrum uses. Interrupt mode 2 is more complex. When the

Z80 receives an interrupt, it expects the interrupting

device to place one byte of data on the data bus which

acts as the low order of an address. The high order of

the address comes from the I register. The Z80 looks at

this address which gives it a second address that it then

jumps to.

  The last mode provides a method of diverting the Z80 to

a user routine for, although the Spectrum does not provide

the byte fo data required, this means the Z80 thinks it

sees FFH or 255 decimal. Thus we need only change that 

I register so that the address



        I*256 + 255



and the following one point to our user routine. However,

the I register cannot be moved so that



           I*256



is in RAM without causing screen interference.

  Therefore we must find our vector address somewhere in

ROM. A quick search finds suitable vectors for both 16k

and 48k machines:



           I = 40



will produce a jump to 32348 for 16k machines,



           I = 9



will produce a jump to 65129 for 48k machines. In order to

see what can be done with these interrupts first type in

the mother program for your particular machine which allows

all the following programs to be compatible between both

machines. [ These are the first two programs on the TZX. ]

Here are the commands for turning the routine on or off:



    ON

    16k  LET a=USR 32330

    48k  LET a=USR 65180

    OFF

    16k  LET a=USR 32340

    48k  LET a=USR 65190



  Turn the routine on. [Since the programs on TZX have (of

course!) been checked,] the routine should be working. The

mother program on its own adds one useful feature: the

depression of symbol shift and space together now acts as

Break but with these added advantages. The new Break works

in machine-code loops, it clears the screen and returns the

colours to the normal black on white. The use of this break

when not running a program causes a buzz with Colour Reset

and Clear Screen.

  To see what the routine can do, turn it off and [merge

"border" from the TZX]. Make sure you have turned the

routine off and Run the new program. Turn the routine back

on and you will be greeted by a pulsating border display

and a clicking sound which continues while your own

programs are running.

  You might like to try loading a Basic program off a

cassette. The border will start pulsating again as soon as

the program has loaded. This program achieves its effect by

outputting the contents of the frame counter to the port

which controls the border 50 times a second.

  The other five examples can be [merged] in the same way

but remember to take great care that the old routine is

turned off before running the mother program to enter the

new routine.



Figure 1.

[This is described in the text. On TZX: "border".]



Figure 2 This shows you how your Spectrum can generate

sounds while you enter or run a program. [On TZX: "sound".]



Figure 3 This makes the screen scroll automatically unless

the n key is pressed. [On TZX: "scroll".]



Figure 4 This provides an On Error Beep function. It causes

the computer to beep when an error occurs until Enter is

pressed. [On TZX: "errorbleep".]



Figure 5 This generates a moving landscape at the bottom of

the screen which can be used by one of your own games. The

speed at which the ground moves can be changed by Poking

23681 which should be followed by Poke 23672,0. This

program should be saved before being run. [Of course, the

version on the TZX ("landscape") was. But re-save it if

you want to use it in another program.]



Figure 6 This last routine generates a display of the

24-hour clock at the top right of the screen. The computer

will even beep on the hour, all while your own program

runs. Save this program before running it. [Same note as

for Figure 5. On TZX: "clock".]