IM
There are some limitations when using the IM instruction on a Sinclair ZX Spectrum 48k. The ZX Spectrum 48k uses the Z80 CPU, but it has a custom ULA (Uncommitted Logic Array) that handles some of the system's functions, including interrupts. This means that the IM instruction might not work as expected in certain situations.
Specifically, the ZX Spectrum 48k has a fixed interrupt vector table, and the IM instruction might not properly interact with the Spectrum's custom hardware. Therefore, it's generally recommended to use the Spectrum's built-in interrupt handling routines instead of directly using the IM instruction.
Interrupt Mode 0 (IM 0): expects the external device to supply the first byte of the opcode during the interrupt acknowledge cycle. This byte is then executed by the Z80 CPU as a standard opcode. This requires the peripheral to have a way to place a specific instruction on the data bus when the interrupt occurs.
I was unable to find any examples of peripherals that would utilise IM 0 interrupt. it seems to be quite rare that we would use this case.
Interrupt Mode 1 (IM 1): This is the default mode set by the ROM during bootup. In this mode, the interrupt jumps to address $0038, which is used for the ROM keyboard scanning routine. This mode does not preserve the IY register, so if you need to use it in your code, you'll need to disable interrupts or switch to IM 2.
Interrupt Mode 2 (IM 2): This mode is often used by game programmers for frame-locked games. In IM 2, an interrupt vector table of 128 words is created, starting at a 256-byte boundary (e.g., $FE00). When an interrupt occurs, the CPU uses the value on the data bus as an index into this table to fetch an address. This allows multiple peripherals to trigger interrupts and call the relevant handling routine.
Programmers often use a block of memory at $3900 in the 48k ROM as the vector table. This space was reserved for functionality that didn't make it into the 48k Spectrum. By placing a jump instruction at $FFFF and knowing that the program counter (PC) wraps to $0000 during execution, programmers can jump to their desired interrupt routine.
| Instruction | Description | Opcode |
|---|---|---|
| IM 0 | External device interrupt. The device should supply the first byte of the opcode during the interrupt acknowledge cycle. | ED46 |
| IM 1 | ROM keyboard scanning routine | ED56 |
| IM 2 | Custom Interrupt Handling | ED5E |