INTRODUCTION
The purpose of this paper is to bring together many of the details on the Interrupt Mode 2 (IM2) that I have gathered over the past several years. References are included in brackets, eg [1], to indicate source material detailed at the end of the text.
BACKGROUND
An interrupt is a signal sent to the microprocessor which generally suspends the execution of the current program, without the program being aware of it. The processor resumes its original work when the interrupt service routine finishes. These interrupts take place at the rate of 60 per second (50 per second for Spectrum).
Interrupts are used for two main purposes: [1]
1. To deal with hardware events such as a key being pressed or a character arriving through a serial port. These events cannot be ignored; the incoming data must be either processed immediately or stored in a buffer. 2. To call subroutines that are provided by the hardware or operating system.
SPECTRUM
In the Sinclair Spectrum and indeed in the TS2068, such an interrupt forces it to jump to 38 hex or 56 decimal where it increments the system variable FRAMES and scans the keyboard to determine what is being keyed in, if anything. If this interrupt were diverted elsewhere to a user routine it would provide a very powerful facility with unlimited potential uses. This is precisely what the use of IM2 is all about.
There are actually three interrupt modes in the Z80 processor but only two of them apply here: IM1 and IM2. 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 interrupting device is the ULA (Uncommitted Logic Array) which implements necessary timing and hardware control. It is used to select chips and decode addresses for RAM and ROM; for timing and control of the Z80A microprocessor; for keyboard operation sensing; to control signal conditioning and transfer of input and output information. [2]
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. In the Spectrum, no such low order byte is provided and by default the Z80 thinks it sees FFh or 255d. Apparently this does not always work properly ; Toni Baker warns against using IM2 on page 102 of her book on machine code in that after some time a system hangup appears to occur. In any case, it certainly does not work with the TS2068, at all.
Still, it is worth examining the Spectrum method more closely, the better to understand what has to be done with the TS2068.
The I register needs to be changed to a value so that the pair of addresses <I*256+255> and <I*256+256> point to a user defined routine. But be warned: the I register cannot be altered to hold a value between 64 (40h) and 127 (7Fh). If the I register is loaded with addresses within the forbidden area, there will be some screen interference. [3]
An inspection of the addresses so formed is made and a jump is made to the address that it holds; the latter is known as a VECTORed address. Therefore a vector must be found somewhere in RAM and above 32768 (8000h).
A search of the Spectrum ROM turns up a suitable value for I which will cause a jump to 65129. This value is 09h. Putting this into the address formulae in the previous paragraph gives the pair of addresses 09FF and 0A00 (2599 and 2600 dec); examining the ROM disassembly for Spectrum shows these two addresses contain 69FE or FE69 taking the bytes in the proper order which equates to 65129 dec. This is the start address of the user routine that IM2 will activate.
Two added notes: [3]
1. If Interface 1 is fitted to the Spectrum, the vectored address may be taken from the Shadow ROM causing a crash. 2. Prior to using IM2, all registers should be saved and then restored on returning to the normal IM1 status.
TS2068
The TS2068 does not supply a fixed low order byte but rather one that may vary from 0 to 255. This could be a problem but there is a way out. One technique to overcome this problem is to create a block of 257 addresses each filled with the same value. The I register is altered to point to this block and IM2 is called. No matter what the lower order byte turns out to be, the block will produce the same address which will vector control to the start of the user-generated routine. Why 257 addresses? Remember that two addresses are inspected and if the low order byte were 255 (ie, the last of 256 bytes), there would have to be one more address with the same value as all the rest.
The vectored address can be a value which is a multiple of 256; or it can be any address, if a specific one is defined in the set-up routine. If the vectored address finds an instruction to jump to a specific address, then that is where it will go.
Examine listing 1 (a MOTHER PROGRAM used to generate the block of bytes and set up the vectoring to a specified address).
LISTING 1
FE00 C5 PUSH BC :Save all
FE01 D5 PUSH DE :the
FE02 E5 PUSH HL :registers
FE03 F5 PUSH AF
FE04 2100FB LD HL,FB00 :point to the block start.
FE07 0600 LD B,00 :set reg B for loop;becomes
FF when loop starts.
FE09 36FC LD (HL),FC :put FC into HL
FE0B 23 INC HL :point to next byte.
FE0C 10FB DJNZ FE09 :repeat until B=00 again
FE0E 36FC LD (HL),FC :put FC into 257th address
FE10 3EC3 LD A,C3 :put C3 into A register
FE12 32FCFC LD (FCFC),A :load FCFC with C3 (JP)
FE15 2169FE LD HL,FE69 :load HL with addr 65129
FE18 22FDFC LD (FCFD),HL :poke this into next two addrs
FE1B 3EFB LD A,FB :load A register with FB
FE1D ED47 LD I,A :load I register with FB
FE1F F1 POP AF :restore all registers
FE20 E1 POP HL
FE21 D1 POP DE
FE22 C1 POP BC
FE23 ED5E IM2 :turn on IM2 & jump to 65129
FE25 C9 RET :return
However, this complexity is not needed if a jump is to an address which is an exact multiple of 256. Listing 2 below gives an example of this version. [4]
LISTING 2
FE00 C5 PUSH BC
FE01 D5 PUSH DE
FE02 E5 PUSH HL
FE03 F5 PUSH AF
FE04 2100FE LD HL,FE00
FE07 0600 LD B,00
FE09 36FD LD (HL),FD
FE0B 23 INC HL
FE0C 10FB DJNZ FE09
FE0E 36FD LD (HL),FD
FE10 3EFE LD A,FE :put FE into A register
FE1D ED47 LD I,A :load I register with FE (65024)
FE1F F1 POP AF
FE20 E1 POP HL
FE21 D1 POP DE
FE22 C1 POP BC
FE23 ED5E IM2 :turn on IM2
FE25 C9 RET :return
In this version the value loaded into the A register at address FE10 determines where the user defined routine should start.
Some final thoughts:
When this technique (the 257 byte table) was installed in the TOOLKIT utility TS2068 version (tstk.C1 on the Omnibus Disk), it of course made all the options using IM2 operational. Later when the TS2068 version was converted back to Spectrum, the technique still did its job, at least with the Spectrum Emulator. Whether it works on a Spectrum machine has not been confirmed.
A concerted effort has been made here to collect accurate information as a ready reference next time use of IM2 is to be attempted. There may be errors and I would be grateful if any reader would let me know of any that are found.
Footnotes (References used in the text).
[1] Barron’s Dictionary of Computer Terms
[2] ZX Spectrum User’s Handbook; by Simpson and Terrell
[3] Howard Allen: Popular Computing Weekly 850704
[4] Floyd Chrysler: Time Designs Sep/Oct 87.