Skip to Content
Under the HoodEmulator InternalsZX Spectrum Next: interrupts

Interrupts: Fourteen Voices, Two Wires

A Next game is a busy place. The music player wants to run fifty times a second, a raster split wants its line, a sample needs its next byte from a timer, the Wi-Fi chip has just sent a reply, and somebody pressed the DRIVE button to browse the SD card. Fourteen pieces of hardware would like the CPU’s attention, often at the same moment. The Z80 has exactly two wires to hear them with. So who decides who goes first, how does the CPU learn who called, and what happens to the ones that have to wait?

I think interrupts are where a Next program grows up. Polling works until it doesn’t: the moment your code must keep a tune steady while it loads a level, or split the screen while it runs a game, you need the hardware to tap the CPU on the shoulder at the right time. The Next’s answer is far richer than the Spectrum’s single frame interrupt, and it has a few traps that look like bugs until you know the rule behind them. Know the rules, and an interrupt becomes the most reliable clock in your program.

You should know two chapters first. Memory, Ports and NextRegs: How the Z80N Reaches the Machine explains NextRegs, the DivMMC and the Multiface, which come back here as NMI devices. The Frame and the Beam: Two Counters, Four Timings explains where in the frame the frame interrupt and the line interrupt happen, and I will not repeat it.

The road ahead

We start with the two wires into the CPU, /INT and /NMI, and the cast of fourteen maskable sources in their order of priority. Then come the two ways the Next drives /INT: pulse mode, the Spectrum’s way, with its short pulse that can be missed, and hardware IM2 mode, where every source has a vector of its own. The heart of the chapter is the daisy chain behind hardware IM2 mode, how a source waits its turn and what RETI and RETN do to the queue; if you read only one section, make it that one. After that we look at the switches and the status bits, the sources up close, how fast the CPU answers, and how the DMA steps aside for an interrupt. Last comes the NMI: its sources, its arbiter, the devices that answer at $0066, and the stackless response that keeps the stack out of it.

The facts are for core 3.02.01. Timings are in CPU T-states and, where the screen matters, for the +3 timing at 50 Hz and a CPU at 3.5 MHz. Every fact was checked against the Next’s hardware design.

Two wires into the CPU

The Z80 has two interrupt inputs. /INT, the maskable interrupt, is a level the CPU looks at once per instruction: if it is low when an instruction ends, and interrupts are enabled, the CPU answers instead of fetching the next one. I call it the doorbell: it rings, and the CPU answers when it has finished what it was doing, if it is willing to answer at all. /NMI, the non-maskable interrupt, works on an edge: the CPU remembers that it fell, and answers at the end of the instruction whatever DI said. That one is the fire alarm, and you will meet the fire brigade that answers it near the end.

line interruptUART 0 / 1 receiveCTC channels 0–3ULA frame interruptUART 0 / 1 transmitinterrupt controllerpulse or hardware IM2(NextReg $C0 bit 0)Z80NCPU/INTM1 button (F9)DRIVE button (F10)NextReg $02I/O trapNMI arbiterMultiface, DivMMCor expansion bus/NMI
The two wires. The maskable sources feed the interrupt controller, which drives /INT in pulse mode or hardware IM2 mode; the NMI sources feed the NMI arbiter, which drives /NMI.

A few rules of the CPU’s own matter for everything that follows:

  • The CPU decides at the last T-state but one of an instruction. /INT must be low at that moment.
  • Not after a prefix. The CPU never stops between a DD, FD, CB or ED prefix and its opcode, so a long chain of prefixes postpones the answer.
  • EI takes effect one instruction late. The instruction after EI always runs, which is what makes EI / RETI at the end of a handler safe; a run of EIs postpones the answer for as long as it lasts.
  • IM 1 calls $0038. IM 2 reads a byte from the data bus, the vector, and calls the address stored at I × 256 + vector. IM 0 executes the byte on the data bus as an instruction.

Fourteen voices in a fixed order

The Next has fourteen maskable sources, and they stand in a fixed order of priority, the same in every mode: the line interrupt first, then the two UARTs’ receivers, then the CTC’s channels, then the ULA’s frame interrupt, and last the two UARTs’ transmitters. Behind them all stands the expansion bus, for a peripheral plugged into the edge connector.

highest priorityeach one passes its turn to the nextline0 · +$00UART 0receive1 · +$02UART 1receive2 · +$04CTC 03 · +$06CTC 14 · +$08CTC 25 · +$0ACTC 36 · +$0CCTC 4reserved7 · +$0ECTC 5reserved8 · +$10CTC 6reserved9 · +$12CTC 7reserved10 · +$14ULAframe11 · +$16UART 0transmit12 · +$18UART 1transmit13 · +$1Aindex · vector offset; dashed: no such channel in this core; then the expansion bus
The fourteen maskable sources in priority order, highest first, with each one’s index and the offset it adds to the vector. CTC channels 4 to 7 have places but do not exist in this core.

Two things in that list surprise people, and I like both of them. First, the alarm clock beats the veteran: the line interrupt, index 0, outranks the ULA’s frame interrupt, index 11. That is the right way round. A raster split must land on its line or it is visibly wrong, while a once-a-frame job can start a few microseconds late and nobody notices. Second, the CTC has eight places but only four channels: channels 4 to 7 are reserved, never fire, and their enable and status bits read 0.

Pulse mode: one pulse for everybody

After a reset the Next drives /INT in pulse mode, the way the Spectrum always did: when a source asks for attention, /INT goes low for a fixed time and then lets go, whatever the CPU did meanwhile. The time is counted in CPU clocks: 32 T-states in 48K and +3 timing, 36 in 128K and Pentagon timing, at every CPU speed. So at 28 MHz the pulse is eight times shorter in real time than at 3.5 MHz.

081624324048566472T-statesEI← /INT low for 32 Ttaken at the first instruction end inside the pulseDI← /INT low for 32 TEI takes effect at 50: the pulse is over, the interrupt is lostdashed: instruction ends
Pulse mode, to scale in T-states. With interrupts on, the CPU answers at the first instruction end inside the 32-T pulse. With interrupts off past the pulse’s end, the interrupt is lost.

The pulse is a moment, not a promise, and here is what follows from that:

  • The pulse is shared. Every enabled source in pulse mode starts the same pulse. A second request while the pulse is low starts nothing new, so two sources that fire close together give the CPU one interrupt.
  • A pulse can be missed. If interrupts stay disabled for longer than the pulse, or the DMA holds the bus across it, the CPU never sees it. Nothing holds the request; only its status bit remembers that it happened.
  • The CPU cannot tell who rang. In IM 1 it calls $0038 whatever the source. In IM 2 the data bus carries $FF, so the CPU reads its handler’s address from I × 256 + $FF. And IM 0, which so much folklore warns about, is perfectly predictable on the Next: the FPGA drives $FF itself, and $FF is RST $38, so IM 0 behaves exactly like IM 1. (A device on an enabled expansion bus may put its own byte there instead.)

Since the vector says nothing, a pulse-mode handler that serves several sources reads the status bits to find out who rang. I find this the most practical trick of pulse mode:

Isr: push af push bc ld bc,$243b ; select INT Status 0 ld a,$c8 out (c),a inc b in a,(c) ; bit 1: line, bit 0: ULA nextreg $c8,a ; write the 1s back: clears them ; ... test bits 1 and 0, do the work ... pop bc pop af ei reti

If that handler can ever run late, it can still tell a missed pulse from no pulse at all: the status bit was set even though the doorbell rang while nobody was listening.

Hardware IM2 mode: a vector for every voice

Set bit 0 of the Interrupt Control register, NextReg $C0, and the Next drives /INT in hardware IM2 mode. Now a source holds /INT low until the CPU acknowledges it, so nothing is missed, and during the acknowledge the Next puts a vector on the data bus that says exactly who called:

76543210vectorbyteNextReg $C0bits 7–5source index0–130exampleCTC 010100110$C0 = $A1 gives 101; CTC 0 is index 3, 0011; vector $A6I = $BE: the handler’s address is read from $BEA6 and $BEA7the highest vector is $C0’s top bits + $1A, so a table needs only 28 bytes
The hardware IM2 vector: three bits from NextReg $C0, the source’s index, and a 0. With $C0 = $A1, CTC channel 0 gives $A6, and with I = $BE its handler’s address is read from $BEA6.

Bits 7 to 5 come from NextReg $C0, bits 4 to 1 are the source’s index from the priority list, and bit 0 is always 0, so every source gets a two-byte slot in a table of 28 bytes. Here is a setup with the table at $BE00, the vectors from $A0, and handlers for the ULA and CTC channel 0:

di ld a,$be ; vector table page: $BE00 ld i,a im 2 ld hl,UlaIsr ld ($beb6),hl ; ULA: $A0 + 11 x 2 = $B6 ld hl,CtcIsr ld ($bea6),hl ; CTC 0: $A0 + 3 x 2 = $A6 nextreg $c0,$a1 ; vectors from $A0, hardware IM2 ei

My advice: once a program has more than one source, use hardware IM2 mode. No 257-byte tables of identical bytes, no guessing who rang, and no interrupt lost because a long LDIR happened to run under DI.

The daisy chain

Behind hardware IM2 mode sits a daisy chain, the scheme Zilog designed for its own peripherals: the fourteen sources stand in a row in priority order, and each one passes a permission to the next. A source may interrupt only while the permission reaches it, and it withholds the permission from everybody behind it while it is busy. I call it the queue, and it is a very polite one. Each source goes through four states:

  1. Idle. The source passes the permission on unchanged.
  2. Requesting. Something happened; the source pulls /INT low if the permission reaches it and the CPU is in IM 2. From now on it withholds the permission, so the sources behind it cannot jump the queue.
  3. Acknowledged. The CPU’s acknowledge cycle reached it; it puts its vector on the bus.
  4. In service. The CPU runs its handler. The source still withholds the permission, until a RETI releases it.
line1UART 0Rx1UART 1Rx1CTC 0in service0CTC 10CTC 20CTC 30ULApermission passed along the chain:may still interrupt:a nested handlermust wait for CTC 0’s RETI
The chain with CTC channel 0 in service. The sources before it still have the permission and may interrupt its handler; the ones after it wait for its RETI.

The rules are short, but they only come alive in real situations, so let me walk you through a few that happen on a Next all the time. In all of them NextReg $C0 holds $A1, so the vectors are $A0 for the line interrupt, $A6 for CTC channel 0, $B6 for the ULA and $B8 for UART 0’s transmitter.

A raster split during the music, with the serial port waiting. This is the busiest everyday case, and the one I would draw first if I had to explain the chain on a napkin. Your game plays its music from the ULA’s frame interrupt, splits the screen with the line interrupt, and sends bytes to the Wi-Fi chip through UART 0:

mainULA (11)line (0)UART 0 Tx (12)123456runsruns onhandlerpausedhandlerhandlerwaitinghandler
A nested handler and a waiting one, in order of events (not to scale). The line interrupt stands before the ULA and interrupts its handler; UART 0’s transmitter stands behind it and waits for the ULA’s RETI.
  1. The ULA rings at the frame’s start. The CPU acknowledges it with vector $B6, and the ULA is in service. Its handler, the music player, executes EI at once, because it knows it will run for a while.
  2. The alarm clock rings for the split. The line interrupt stands before the ULA, so the permission still reaches it, and the music player’s EI from step 1 means the CPU is willing to answer: it interrupts the music player with vector $A0. Both conditions are needed. Without that EI, the CPU would ignore the request until the music player ended, however high the line interrupt stands in the chain. The ULA’s handler is paused, but the ULA is still in service.
  3. UART 0’s transmit buffer empties. Its transmitter stands behind the ULA, so the permission does not reach it: it waits, requesting, however long that takes.
  4. The line handler’s RETI releases the line interrupt, the highest source in service. The CPU is back in the music player, and the transmitter still waits, because the ULA is still in service.
  5. The music player’s RETI releases the ULA. The permission flows down the chain again, reaches the transmitter, and it rings at once: vector $B8.
  6. Its RETI releases it, and your main program carries on.

Notice the two RETIs in a row in steps 4 and 5: each one releases exactly one source, the highest in service, and the chain unwinds in the order it was built. I find that very tidy.

Two sources at the same instant. Say a CTC channel that plays samples and the ULA’s frame interrupt fire at the same moment. Both move to “requesting”, but CTC channel 0 stands before the ULA, so it withholds the permission even before the CPU has answered anyone. The CPU takes vector $A6 first; the ULA waits until the CTC handler’s RETI, and its handler starts that much later. If your frame interrupt must be punctual, keep the handlers of everything before it short.

A handler that forgot EI. The CPU clears its interrupt enable when it acknowledges an interrupt, so a handler that leaves EI to its very end cannot be interrupted at all, not even by the alarm clock that stands before it. The line interrupt waits, requesting, until the music player’s closing EI / RETI, and your raster split lands wherever the music player happens to finish: a few lines too low, and a different line every frame. My rule of thumb: a handler that runs longer than a few dozen T-states executes EI early.

A timer faster than its handler. CTC channel 0 with a prescaler of 16 and a time constant of 100 ticks 28,000,000 ÷ 16 ÷ 100 = 17,500 times a second, every 200 T-states at 3.5 MHz. If its handler takes 250 T-states, the next tick comes while the channel is still in service. The chain does not queue it: one outstanding request per source, so the repeat is folded into the one being served, and only the status bit records it. The sample player now runs at half its speed, and nothing crashes to tell you. Keep the handler shorter than the timer’s period, or slow the timer down.

A handler that ends with RET. This one is my favourite bug, because it looks so innocent. CTC channel 0’s handler ends with RET instead of RETI. The chain never sees a RETI, so the channel stays in service forever, and it withholds the permission from everybody behind it: CTC channels 1 to 3, the ULA and both transmitters never interrupt again. The line interrupt and the UART receivers, which stand before it, still work, which makes the symptom baffling: “the frame interrupt stopped after the first sample”. The status bit gives it away. In hardware IM2 mode a source’s status bit reads 1 while it is in service, so NextReg $C9 bit 0 stuck at 1 tells you exactly which handler forgot its RETI.

RETI releases, RETN does not

The chain learns that a handler has finished by watching the instructions the CPU fetches: when it sees RETI, the opcode pair ED 4D, the highest-priority source in service goes back to idle and the permission flows again. The rules are strict, and I would learn them by heart:

  • Only RETI releases. RETN (ED 45 and its aliases) releases nothing. A maskable handler that ends with RETN leaves its source in service, and that source and everything behind it are blocked for good.
  • Only in IM 2. A RETI executed while the CPU is not in IM 2 releases nothing either. The chain knows the CPU’s mode because it watches the IM instructions go by, and NextReg $C0 bits 2–1 read back what it saw.

What if the CPU is in IM 1 while hardware IM2 mode is on? Then the chain cannot interrupt at all, with one exception: the veteran still rings in the old way. The ULA’s frame interrupt alone falls back to a pulse, so a program that switches hardware IM2 mode on but stays in IM 1 still gets its fifty interrupts a second. The other sources move to “requesting” and wait there. The moment the CPU executes IM 2, they all ring at once, in priority order. I find that a lovely piece of backward compatibility, and a great way to be very surprised.

Switches and status bits

Every source has an enable, and the enables sit in three registers, the INT EN 0, INT EN 1 and INT EN 2 registers, NextRegs $C4, $C5 and $C6:

  • NextReg $C4: bit 0 the ULA, bit 1 the line interrupt, bit 7 the expansion bus. It reads $81 after a reset: the ULA and the bus on, the line interrupt off.
  • NextReg $C5: bits 3–0 the CTC channels. A channel’s enable is the same flip-flop as bit 7 of its own control word, so writing either one overwrites the other.
  • NextReg $C6: for each UART, receive (bit 0 for UART 0, bit 4 for UART 1), receive “near full only” (bits 1 and 5) and transmit (bits 2 and 6).

The ULA’s switch is shared by three doors, all leading to one flip-flop: port $FF bit 6 (1 = off), bit 2 of the Line Interrupt Control register, NextReg $22 (1 = off), and bit 0 of NextReg $C4 (1 = on, the other way round). The line interrupt’s enable is bit 1 of both NextReg $22 and NextReg $C4. Whichever you wrote last wins, so pick one register and stick to it.

Every source also has a status bit, in the INT Status 0, 1 and 2 registers, NextRegs $C8, $C9 and $CA, laid out like the enables. A status bit is set by the source’s event, and writing a 1 clears it; writing a 0 leaves it alone, so a handler clears exactly what it served and nothing else. Three details matter:

  • The CTC and the UARTs set their status even when their interrupt is disabled. That is “polled mode”: leave the interrupt off and watch the bit. The ULA and the line interrupt are different: switched off, they raise nothing, not even a status bit.
  • In hardware IM2 mode a status bit reads 1 while its source is requesting or in service, and writing a 1 cannot clear it until the RETI.
  • NextReg $22 bit 7 reads the pulse. It is 1 while the pulse-mode /INT is low, from any source, not only the ULA. Writing it does nothing.

And a program can ring the doorbell itself. The Generate Maskable Interrupt register, NextReg $20, raises a request when you write a 1: bit 7 the line interrupt, bit 6 the ULA, bits 3–0 the CTC channels. These requests ignore the enables, set the status bit and behave like the real thing in either mode. I use it to test a handler without waiting for the hardware.

The sources up close

The ULA’s frame interrupt comes once a frame at a fixed point of the beam: on line 0 in 48K timing and at 60 Hz, on line 1 in 128K and +3 timing at 50 Hz, and on the last line in Pentagon timing, which has no 60 Hz frame. The frame-and-beam chapter’s frame interrupt has the details.

The line interrupt, the alarm clock, rings for line n near the end of line n − 1’s display area, in the line numbers the frame-and-beam chapter’s line interrupt explains. A line number one past the last line of the frame rings like line 0; anything higher never rings.

The CTC, the metronome of the DAC chapter, raises a channel’s interrupt when the channel’s count reaches zero. Its timer counts the 28 MHz master clock through a prescaler of 16 or 256, so its rate does not change with the CPU’s speed. On a Zilog CTC a program writes an interrupt vector into the chip; the Next decodes that write and throws it away, because the vector comes from the chain. CTC: Four Timers in a Ring opens the channels up.

The UARTs have two receive conditions and one transmit condition:

  • Receive, data available: the receive buffer holds at least one byte.
  • Receive, near full: the 512-byte buffer holds 384 bytes, three quarters. With the “near full only” bit set in NextReg $C6, only this one interrupts.
  • Transmit: the transmit buffer has become empty. That is about one character’s time before the line falls silent, because the last byte is still being sent.

All requests are edges: a source asks once when its condition becomes true, not for as long as it stays true. For the UART that means a byte arriving into a buffer that is not empty raises nothing new, so my advice is to drain the whole buffer in the handler, not to take one byte per interrupt.

How fast the CPU answers

Between a source’s request and its handler’s first instruction there are two delays: the instruction that is running must end, and then the acknowledge itself takes its time.

0612182430364248T-statesan instruction of 23 TCPUackpush PCvectorhandlerrequestdecision, 1 T before the endIM 2 acknowledge: 19 T (IM 1: 13 T)waiting for the instruction to end: 0 to 22 T, 0 to 44 dots at 3.5 MHz (without waits)
How fast the CPU answers, to scale in T-states: up to 22 T for the running instruction to end, then 19 T of IM 2 acknowledge before the handler starts.
  • The running instruction ends. The longest Z80N instruction takes 23 T-states, and the CPU decides one T-state before an instruction ends, so the wait is anything from 0 to 22 T. That is the jitter of the raster effects chapter’s line interrupt: up to 44 dots at 3.5 MHz, as long as no contention or wait state stretches the instruction.
  • The acknowledge. In IM 1 it takes 13 T-states: an acknowledge cycle of 7 and the two writes that push the return address. In IM 2 it takes 19: the same, and two reads that fetch the handler’s address.
  • HALT helps, but not completely. A halted CPU executes NOP cycles of 4 T-states each and can answer only at the end of one, so a program waiting in HALT still sees 0 to 3 T of jitter.

The DMA steps aside

The DMA can hold the bus for a long transfer, and in pulse mode an interrupt that comes and goes during it is simply lost. In hardware IM2 mode the Next can do better: the DMA INT EN 0, 1 and 2 registers, NextRegs $CC, $CD and $CE, laid out like the enables, choose which sources may interrupt the DMA. While such a source is requesting or in service, the DMA stops asking for the bus between bytes, the CPU takes the interrupt and runs the handler, and the DMA carries on after the RETI. Bit 7 of NextReg $CC does the same for the NMI. This works only in hardware IM2 mode, and the DMA itself raises no interrupt when it finishes. I like this arrangement: the transfer gives way to the doorbell instead of making it wait in the rain.

The fire alarm

The NMI cannot be switched off with DI, and on the Next it belongs to two devices you met in the memory chapter: the fire brigade, the Multiface, and the butler, the DivMMC. Its sources are:

  • The M1 button (F9 on a PS/2 keyboard) asks for a Multiface NMI, allowed by bit 3 of the Peripheral 2 Setting register, NextReg $06.
  • The DRIVE button (F10) asks for a DivMMC NMI, allowed by bit 4 of NextReg $06. It works only while the DivMMC’s port is switched on.
  • The Reset register, NextReg $02: writing bit 3 asks for a Multiface NMI, bit 2 for a DivMMC NMI, under the same enables. The Copper can write it too.
  • An I/O trap: with the I/O Traps register, NextReg $D8, bit 0 set, touching the +3’s disk ports $2FFD and $3FFD asks for a Multiface NMI, so a program can emulate the +3’s disk drive in software.
  • The expansion bus, for a device on the edge connector.

Only one NMI can be under way at a time, and an arbiter picks the source:

M1 button (F9)NextReg $02 bit 3I/O trapDRIVE button (F10)NextReg $02 bit 2expansion bus$06 bit 3$06 bit 4arbiter, in this order:1. Multiface, unless theDivMMC is mapped in orport $E3 bit 7 is set2. DivMMC, unless theMultiface is active3. the expansion busidlefetchholdend/NMI is low from the moment a source is taken until the fetch at $0066
The NMI arbiter: the Multiface first, then the DivMMC, then the expansion bus, each with the conditions that refuse it; then /NMI runs through idle, fetch, hold and end.
  1. The Multiface first, unless the DivMMC is mapped in for any reason (an esxDOS call is enough) or port $E3 bit 7 holds the DivMMC in by hand.
  2. Then the DivMMC, unless the Multiface is active.
  3. Then the expansion bus.

A request that loses is gone; it does not wait. Once a source is taken, /NMI goes low at once and the arbiter walks four states: idle, fetch until the CPU fetches the opcode at $0066, hold while the device that answered says it is busy, and end, before it takes the next request. While the arbiter is past fetch, a new request changes nothing, not even the flags of NextReg $02.

Who answers at $0066

The NMI sends the CPU to $0066, and whoever the arbiter chose appears there to answer:

  • The fire brigade pages itself in during that very fetch: the Multiface’s ROM at $0000, its RAM at $2000. It leaves on a RETN or when the program touches its ports, which depend on the Multiface type chosen in the Peripheral 5 Setting register, NextReg $0A.
  • The butler comes in through his $0066 entry point, instant or delayed as bits 1 and 0 of the Divmmc Entry Points 1 register, NextReg $BB, say. It needs no ROM 3; it needs only a DivMMC NMI. He leaves on a RETN or through $1FF8–$1FFF.

For both, only the plain RETN, ED 45, counts as “the NMI is over”; its aliases and RETI do not.

The stackless NMI

An ordinary NMI pushes its return address onto the stack, wherever the stack happens to be. That is awkward when the interrupted program has its stack in a page the NMI handler is about to swap out, or no sensible stack at all. Set bit 3 of the Interrupt Control register, NextReg $C0, and the NMI becomes stackless:

ordinary NMIPC = $8123stack:$23, $81$C2 = $23$C3 = $81SP − 2; the address is pushed,and $C2/$C3 record it too;RETN pops the stackstackless NMIPC = $8123memoryuntouched$C2 = $23$C3 = $81SP − 2 as well, but the pushwrites no memory; the firstRETN reads $C2/$C3
An ordinary NMI pushes the return address and records it in NextRegs $C2/$C3; a stackless one runs the same push cycles without writing memory, and the first RETN reads the address back from $C2/$C3.
  • The CPU still runs both push cycles and SP still goes down by 2, but the memory request is held off, so nothing is written.
  • The return address goes to the NMI Return Address LSB and MSB registers, NextRegs $C2 and $C3. In fact they record it on every NMI, stackless or not, and a handler may read or rewrite them.
  • The first RETN after the NMI takes its return address from NextRegs $C2/$C3 instead of the stack, and SP goes up by 2 as usual. A RETI in the handler does not: it pops the real stack, like any return. Clear bit 3 inside the handler, and its RETN pops the stack after all.

I find this one of the Next’s quietly brilliant additions. A debugger or a snapshot tool can stop any program at any moment without leaving a single footprint in its memory.

Resets

A reset puts the interrupt system back into the Spectrum’s shape: NextReg $C0 becomes 0 (pulse mode, the stackless NMI off), NextReg $C4 becomes $81 (the ULA and the expansion bus on, the line interrupt off), NextRegs $C5, $C6 and $CC–$CE become 0, NextReg $22 and $23 become 0, and every status bit and every pending request is cleared. Leaving hardware IM2 mode does the same to the chain: every request and every handler in service is forgotten. So here is my last piece of advice: set the interrupt registers yourself at the start of your program, and do not inherit them from whoever ran before you.

What to take home

Two wires, then, and a well-organised crowd behind them. /INT, the doorbell, is shared by fourteen sources in a fixed order: the line interrupt, the UART receivers, CTC channels 0 to 3 (4 to 7 have places but no hardware), the ULA’s frame interrupt and the UART transmitters, with the expansion bus behind them. /NMI, the fire alarm, belongs to the Multiface and the DivMMC.

In pulse mode, the default, /INT is a pulse of 32 or 36 T-states that anybody can start and nobody can hold: miss it under DI and it is gone, and the vector is always $FF, so IM 0 is just IM 1 in disguise. In hardware IM2 mode, NextReg $C0 bit 0, every source holds /INT until it is served and supplies its vector: NextReg $C0 bits 7–5, its index, and a 0. The daisy chain makes each source wait behind the ones before it, lets higher ones nest, and releases a source only on a real RETI executed in IM 2; RETN releases nothing, and in IM 1 only the ULA still rings.

Enables live in NextRegs $C4–$C6, status in $C8–$CA (write 1 to clear, set even when disabled for the CTC and the UARTs), and NextReg $20 rings the doorbell by software. The CPU answers within 0 to 22 T plus 13 or 19 T of acknowledge, and the DMA can be told to step aside.

The NMI’s arbiter takes the Multiface before the DivMMC before the bus, its devices answer at $0066, and the stackless NMI keeps the return address in NextRegs $C2/$C3 instead of the stack. Every one of these rules has a reason, and once you know them, the crowd behind the two wires turns into a queue that never pushes.

Last updated on