Interrupts

See all articles from QZX v4 n2
Author(s): Thomas B. Woods
Publication: QZX
Pub Details: QZX v4 n2 (Volume 4, Issue 2)
Date: January 1986
Pages: 2-4

An interrupt is a signal sent to the computer which tells it to stop whatever it is doing and perform some special task. In other words, an interrupt signal does just what its name implies: it interrupts the computer’s normal program execution and causes the machine to execute a special interrupt handler program instead. This interrupt handler would normally be a subroutine designed to take care of some special task whenever an interrupt occurs. When this subroutine is finished, normal program execution would pick up right where it left off when the interrupt first occurred.

There are three different kinds of interrupts. They are the Bus Request, the Non-Maskable Interrupt, and the Maskable Interrupt. It is the maskable variety that interests us here. Maskable interrupts differ from the first two kinds in that they can be masked. This means that the computer can be made to ignore them if desired. Two machine code commands, EI and DI (Enable Interrupts and Disable Interrupts) serve as switches which turn the interrupt handling on or off. After the EI command is executed, any maskable interrupt signal sent to the Z80 microprocessor will cause the normal program execution to stop temporarily while the interrupt service routine is called. If a DI instruction is executed, the computer will ignore any maskable interrupts. Programs execute as if the interrupt never occurred.

Interrupts have many uses. They can warn the system of eminent power failures, trigger the computer to read an input port at a critical instant in time, form the basis of an independent real time clock, etc. The T/S2068 uses interrupts as its keyboard read routine. Internal hardware generates an interrupt signal 60 times a second. Every time a signal occurs, the 2068 stops to see if you’re pressing a key.

The question now arises, when an interrupt occurs, where exactly does the computer go to find the proper interrupt handler program? Or put a bit differently, how can you the programmer have any say in where the computer jumps when the interrupt occurs? This is where “interrupt modes” become important. The Z80 has three different ways of handling maskable interrupts. Which method (or mode) it uses is determined by the program being run. There are three machine code commands (IM0, IM1, and IM2) which can be included in a program to set the interrupt mode you wish to use. Each mode has its advantages and disadvantages.

In interrupt mode 0 (IM0) when a maskable interrupt occurs, the computer stops and waits for one instruction to be placed on the data bus by the device which caused the interrupt. A typical instruction would be a CALL or RST to a subroutine, however JP’s can also be used. This mode is not used on the T/S2068.

Interrupt mode 1 (IM1) is the normal 2068 mode and also the simplest. In this mode, and interrupt signal causes the computer to automatically perform a RST 38 instruction. In other words, call the subroutine beginning at address 0038 hex. I mentioned previously that keyboard scanning is performed via interrupts on the 2068; Therefore, if you were to look at the 2068 ROM, you would find the key scanning routine starting at address 0038.

The third interrupt mode (IM2) is the most versatile mode; but, unfortunately, also the most difficult to implement. In this mode, the device causing the interrupt places a number on the data bus just like in mode 0. Mode 2, however, uses this number as the low half of a 16 bit address. It takes the high half from the special “I” register. After resetting bit zero of this “vector address” to zero, the computer then treats this number as an entry on a jump table. Whatever value is found at the two bytes starting at the address formed by the vector will be the address the computer will call to handle the interrupt.

In this way, you could have a total of 128 different subroutines to service the interrupt. You could have just one “intelligent” device specify the proper routine by having it send the interrupt and the low half of the desired vector which points to the proper service address. Or, you could have as many as 128 different devices each interrupting the computer, and each with its own unique service routine. This is all accomplished by having the device which causes the interrupt to also supply the lower 8 bits of the desired address in the jump table. It isn’t absolutely necessary to have a hardware device supply the low half of the vector address. On the ZX Spectrum. pull-up resistors on the 8 data lines insure that there will always be the number FF hex on the data bus when a maskable interrupt causes a vector to be formed. This means that you can always count on the fact that the interrupt table will always be located at the address xxFE where “xx” is the value found in the I register. Therefore, if you preset the I register with the value of FB hex, and at address FBFE you place the address of your service routine, you could put the computer into mode 2 and have the internally generated interrupts cause the machine to jump to your own code. That works for the Spectrum.

There are problems which crop up when you try to use IM2 on the 2068. Namely, there are no pull-up resistors on the 2068 data bus. Without them, you have no way of knowing what number will be there when a vector is being formed by IM2. On the T/S2068, you can set the I register to FB, just like it was noted above for the Spectrum; but, since the data bus which forms the low half of the vector could be just about anything, you just don’t know where to put the service routine address. It could be anywhere from FB00 to FBFF. There is a sneaky way to get around this, however. That is to put the address of your interrupt handler everywhere within the range of the vector table. In other words, fill the entire space from FB00 to FBFF with the same number. When an interrupt occurs, even though you don’t know exactly where in the table the handler will be chosen, you will have covered all the bases by insuring that the same service routine address will be found everywhere.

(Publisher’s Note: This and more machine code information is contained in the Operator’s and Programmer’s Reference Manual for the Pseudoscope, a serial bit stream analyzer for the T/S2068 and the Experimentor’s Universal Input/Output Port. More information about these items can be found in the ad in this issue.)