Originally published in SINCUS NEWS, we are bringing you this series with the permission of the author.
“All right”, comes the chorus; “what’s an Interrupt and why should I care?” I’ll admit, it’s possible to lead a normal happy life even if you’ve never heard of an Interrupt. But in that case, you’ll have missed something that’s at least lots of fun and, at most very useful.
This series of articles will try to give something to everyone. Those who despise technical details will be able to pick out some programs that can be entered and immediately used, to give new power to their computers. Beginning machine code programmers will learn of a hidden bug in the system that can do weird things to their software. Advanced machine code programmers will find a versatile tool that will allow them to do things they may not have suspected possible. Those who like to build hardware will also find a few interesting tricks. By the end of this first article, we’ll understand what Interrupts are and have a small program that demonstrates Interrupts in action and which may be of use once incorporated into a BASIC program. We’ll build on this demonstrator program in the future. In order to accommodate the many levels of experience of various members, this article is laid out in topics. Each starts with simple explanations and progresses into technical detail. If you find yourself in too deep, the water becomes shallow again at the start of the next topic!
1 What is an Interrupt?
Perhaps an analogy would be the best way to begin. Suppose, while you are reading this article, the telephone rings. You’ll probably set the newsletter downs mentally remembering where you were and go answer the phone when you’re done, you’ll come back and resume where you left off, you’ve just serviced an Interrupt. Let’s try another analogy. Suppose you find my articles so interesting that you absolutely can’t be disturbed while reading them. Because of this, you unplug your phone before you start reading and plug it back when you’re done. If the world outside tries to Interrupt you, you won’t know and won’t respond. During that time, you’ve disabled the Interrupt. Now for one more analogy. Your neighbor knows you have a habit of unplugging your phone, so he comes to your house and rings your doorbell. He can see you through the window so you can’t ignore him. You set down the newsletter, and open the door. You are servicing a Non-Maskable Interrupt (NMI).
The TS-2068 has both maskable and non-maskable Interrupts activated by pulling one of two pins on the expansion connector to ground. When this happens, the present value of the program counter goes on the stack, and the machine starts executing at some new location, where the Interrupt handler software is. If it will make it easier to picture, it acts as though a CALL (machine code, but very much like a GOSUB) instruction has been added right where the computer happens to be running code. In fact, the Interrupt handler is written as a subroutine that actually can be CALLed. Exactly where in memory the Interrupt handler may be located will be dealt with later.
The TS-2068 generates its own maskable Interrupt every 1/60 second. This causes the keyboard to be scanned and the 3 byte system variable FRAMES to be increased by one count. This variable can be used as a clock or timer and, in fact is what the PAUSE instruction uses to determine whether it has waited long enough. (Have you noticed that the number that follows PAUSE is a count, also in sixtieths a second?). This 60 Hertz Interrupt is also synchronized to the beginning of each video frame on your TV or monitor, which can be useful. It’s not hard to divert this interrupt so it can do some work for us on top of its normal duties. We’ll demonstrate this in a moment.
2 Can’t an Interrupt disrupt a program that is running?
Absolutely. One place where our phone answering analogy breaks down is in the fact that you remember having answered the phone, but the routine being Interrupted has no knowledge that it’s been temporarily set aside. This means that the Interrupt handler software has to be carefully written so as not to change anything unexpectedly. For example the first thing usually done is to PUSH all registers onto the stack. The last thing it does is to POP them all back into place before it Returns to the program that was Interrupted. Therefore, even though the Interrupt handler may have temporarily changed the registers, it leaves them exactly as it found them.
M1 Code FETCH cycle operation MI Maskable Interrupt M3 Memory WRITE cycle M2 Memory READ cycle NMI Non-Maskable Interrupt WR WRITE RAM Random Access Memory RD READ ROM Read Only Memory R/W READ/WRITE IORQ Input/Output Request EI Enable Instruction AROS Application ROM-Oriented Software DI Disable Instruction LROS Language ROM-Oriented Software JP Jump EPROM Electrically Programmable Read Only Memory JR Jump Relative EEPROM Electrically Erasable Prog. Read Only Memory Z Zero EMU Emulator NZ Non Zero 000H(000) Hexadecimal (decimal)
3 What about programs where the exact time required to execute a loop is critical? Won’t an Interrupt change that timing?
Yes it would, in such circumstances, an Interrupt could be disastrous. When such things are expected ( LOADing, SAVEing, BEEPing, LPRINTing are all examples) the maskable Interrupt is disabled with the DI machine code instruction. The non-maskable Interrupt cannot be disabled, and could be quite disruptive if misused. It is normally not used with the TS-2068, and a ROM bug generated by Sinclair and faithfully copied by TIMEX, makes it nearly impossible to use any way. Next time, we’ll investigate some hardware methods that get around this bug.
The following experiments show how things can go when unexpected Interrupts appear, or when necessary Interrupts fail to materialize. I’ve mentioned that the TS-2068 generates its own maskable Interrupt (from now on, we’ll just call it the Interrupt) every sixtieth of a second. This can be turned off in hardware by setting bit 6 or I/O port FFH (255). It’s not quite the same as executing a DI, but it has the same effect, and can be done from BASIC.
TYPE IN:
10 OUT 255,64
20 PAUSE 5
If you RUN 20, the program runs in a flash; PAUSE 5 doesn’t take very Long after all. However, if you just RUN, the computer is locked up until you shut off the machine. Line 10 shut off the Interrupts. (The analogy now is not so much like unplugging your phone as it is shutting down the phone company! Fortunately, recent actions by the US Justice Department have prevented the analogy from seeming overly bizarre.) Remember the systems variable FRAMES is incremented every time an Interrupt occurs. PAUSE 5 waits for it to get incremented 5 times. Unfortunately, with no Interrupts, FRAMES doesn’t change and the computer sets out to prove that it’s more patient than its owner!
For the case where we don’t want Interrupts, those who own a TS-2040 PRINTER may type in the following:
10 PRINT AT 10,10; "WES"
20 RANDOMIZE USR 2562
30 STOP
40 PRINT AT 10,10; "WES"
50 RANDOMIZE USR 2563
The ROM routine at 2563 contains the COPY command. If you RUN this, you’ll get a piece of paper with my name on it. However, the TS-2040 printer is controlled by a precisely timed set of pulses. An Interrupt would cause some of these pulses to be lost. For this reason, the first instruction in the COPY command is DI which disables the Interrupt. If we instead RUN 40, we will have skipped around the DI instruction and the print sequence is disrupted 60 times a second by unwanted Interrupts. This times my name comes out as a meaningless blur. I liked the first way better!
The moral to machine code programmers is, no matter how tight the little loops in your programs, the computer is sneaking in 60 times a second unless you DI first. Do that DI before entering any critical timing loops and restore things later with EI. Don’t forget that the keyboard won’t be scanned and FRAMES won’t be updated while your DI is active.
4 Where does the Interrupt handler have to be placed in memory? Can I put it where I want it, or add my own handler?
You have a Little control, in some cases. The non-maskable Interrupt always starts at location 0066H. In the TS-2068, this is in the ROM. I mentioned a bug there that keeps us from normally using this feature. We’ll discuss this next time along with a different hardware method to correct the bad byte using the TIMEX ROM.
The maskable Interrupt operates in 3 software selectable modes. MODE 0 causes the Interrupt to start executing at a location defined solely by external hardware. We won’t use it here but it is mentioned for completeness. MODE 1 causes the Interrupt to start executing at location 0038H. This is how the TS-2068 normally operates, and the Interrupt handler is located there. The TS-2068 Technical manual Section 5.3.1 suggests a totally worthless method of intercepting the MODE 1 Interrupt; I consider it worthless because it can’t be used along with BASIC. Let’s be greedy and demand it all. Once again, we can use the AROS/LROS Board with a change to the Interrupt handler, but this still requires one to build the board. let’s demand a software only BASIC comparable technique. It turns out that one exists!
Our ability to easily use the Interrupt lies in Interrupt MODE 2. In it the most significant byte of an address is kept in the Z-80’s “I” register. The least significant byte is read from the data bus. IMPORTANT: users of Spectrum Emulators should note that Real Spectrums put a different value on the data bus FFH (255) than do TS-2068’s (I’ve detected 0F, 2F, 3F and 0E so far, with evidence that there may be others), For this reason, certain Spectrum software that uses Interrupt MODE 2 won’t work on a TS-2068, even with an emulator. It appears that putting pull-up resistors in the data bus fixes this problem. (Only bit 2 already has a pull-up resistor; probably used by code at location 0BFFH (3071) in the EXROM, for detecting whether additional memory expansion banks are present. This will be the subject of another article, but it’s worth pointing out here to explain why only 7, not 8, resistors are needed to enhance Spectrum emulators.)
In the spirit of true greediness, wanting our own Interrupt handler to work even without pull-up resistors, we will want to tolerate any value on the data bus. We even want to tolerate variable values on the data bus. Fortunately, there’s a renegade Spectrum add-on joystick that does just such a thing during Interrupts. This is fortunate for us, because it caused our British friends to solve the problem for us.
One thing I haven’t mentioned is that the address assembled from the “I” register and the data bus is NOT the address of the Interrupt handler! It is the ADDRESS OF THE ADDRESS of the Interrupt handler. Although this makes it a bit more difficult to understand MODE 2, it lets us put the handler wherever we want; we can even change it easily while a program is running. In designing our Interrupt code, we’ll borrow rather heavily from the solution proposed by Tom Webb, in Advanced Spectrum Machine Languages, Melbourne House. If we put FEH (254) in the “I” register but don’t know what will appear on the data bus the machine will get the address of the Interrupt handler from somewhere between locations FE00H (6524) and FF00H (65280). If we fill this 257 byte block with FDH’s (253), then the address of the Interrupt handler will I always be FDH FDH! This is 3 bytes before the block of FD’s, and is just long enough for a JP instruction to the real Interrupt handler. Doing this, our software only fix for the hardware problem takes up only 260 bytes of memory, and it’s all in one continuous block! We have 255 bytes of memory available above the FD block and it would be most convenient to locate our Interrupt handler there. We’ll end the handler with a JP to the ROM Interrupt handler, so that the keyboard will still be scanned, as usual. (Being lazy as well as greedy, we’d rather not do that ourselves!)
5 Can we do something useful with this handler?
There’s nothing wrong with being practical, so why not? There are a number of Spectrum programs that use MODE 2 to actually add new commands to BASIC. The following program will give a much simpler, but distinctly related example by adding a new function to the TS-2068. As long as the Interrupt is enabled, you can immediately COPY the screen to the printer by simultaneously pressing SYMBOL SHIFT and BREAK. This can even be done while a program is running, and even in the middle of a PRINT statement. When the copy is done, the program will continue, completely oblivious to the fact that it’s been Interrupted. The printout will include the edit line.
10 REM IM2 Demonstration Program
20 REM Causes a Copy-Screen WhenBREAK and SYMBOL-SHIFT are pressed together.
30 CLEAR 65020
40 FOR j=65024 TO 65280: POKE j,253: NEXT j
50 POKE 65021,195: POKE 65022,8: POKE 65023,255
60 FOR j=65281 TO 65314: READ k: POKE j,k: NEXT j
70 DATA 62,254,237,71,237,94,201,245,197,213,229,62,127,219,254,246,224,254,252,32,6,243,6,192,205,5,10,225,209,193,241,195,56,0
80 RANDOMIZE USR 65281
Certain BASIC commands disable the Interrupt. During such intervals, this copy-screen function won’t work. These commands are (LOAD, SAVE, VERIFY, MERGE, COPY, LLIST, LPRINT and BEEP). Some commercial machine code programs also disable the Interrupt. Add the following to your own BASIC program (exact line numbers aren’t important, as long as you get the lines in the right order.) Make sure your program executes it once; more times won’t hurt, but they won’t help, and take a few seconds to run. Once this is done, the copy-screen command is active, and will remain so, even if you STOP the program and LOAD in a new one.
The NEW command shuts off the Interrupt mode, but leaves the code intact, so that it can be reactivated with only the RANDOMIZE USR 65281.
We’ll save a discussion of the program for next time, and we’ll discuss the problems of relocating it, and how to modify it to print only part of the screen. Until then, you might get some enlightenment amusement / frustration by taking it apart, to see how it works. The correct answer will be printed here next time!