MAKING USE OF SPECTRUM INTERRUPTS
from Your Computer, May 1983

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.


AN INTERRUPT is a signal sent to the microprocessor which generally suspends
the execution of the current program  without the program knowing it.

Every 20ms the Z-80 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 system variable FRAMES and scans the
keyboard 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 Z-80 responds to an interrupt is governed by which interrupt mode it
is operating in; the Z-80 has three modes.

Interrupt mode 0 allows the interrupting device to place one instruction on
to the data-bus after the interrupt for execution by the Z-80. 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 Z-80 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 Z-80 looks at this address which gives
it a second address that it then jumps to.

The last mode provides a method of diverting the Z-80 to a user routine for,
although the Spectrum does not provide the byte of data required, this means
the Z-80 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. After typing in the program,
RUN it. If after a few seconds, an "Error in program" message appears, then
carefully check the DATA statements and try again. If "OK" appears then SAVE
the program as the checksum does not detect all possible errors. 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. If the computer crashes, then re-load the program and
check it carefully. If "OK" appears on the screen then 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 add the lines in figure 1,
erasing the original lines of the same line number. 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
display will stop during the load, as it will during BEEP, for the computer
disables the interrupts because timing is important. 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 entered 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.
