Skip to Content
Under the HoodEmulator InternalsZX Spectrum Next: the Z80N CPU

The Z80N: A Familiar Heart and Twenty-Nine New Tricks

Take a Z80 program written for the Spectrum in 1983, load it on a Next, and it runs. Every instruction takes the same number of T-states, every flag lands where it did, and the music player’s carefully counted loops still keep time. Yet there is no Z80 chip anywhere on the Next’s board. The CPU is a description of a Z80, written in a hardware language and built into the FPGA beside the ULA, the sprites and everything else. So how faithful is that copy, where does it quietly differ from the original, and what did its authors add while they were at it?

I think the CPU is the one part of the Next that every program meets in every cycle. Your raster effect lives or dies by T-states, your music player by the length of a loop, your copy routine by how many bytes a cycle can move. Knowing what the CPU does on the bus explains why an instruction takes as long as it does, why the same loop is slightly slower per T-state at 28 MHz, and which of the old undocumented tricks still work. And the Z80N’s new instructions are a gift: a multiply, barrel shifts and screen arithmetic in eight T-states each, where a Z80 needs a subroutine.

You can read this chapter first among the Next’s chapters; the others build on it. Memory, Ports and NextRegs: How the Z80N Reaches the Machine takes the CPU’s addresses into the Next’s 2 MB, and Interrupts: Fourteen Voices, Two Wires takes its two interrupt inputs into the Next’s interrupt system.

The road ahead

We start with what the Next’s CPU is and why a copy in an FPGA can be a perfect one, then take a quick look at its registers, including the two you cannot see. Then we go down to the bus: how an instruction breaks into machine cycles, how the opcode fetch works and what its refresh half is for, how memory and I/O cycles look, and the one place where the Next’s timing differs from the Spectrum’s, the wait state at 28 MHz. After that come the CPU’s dealings with others: the DMA borrowing the bus, HALT, and the interrupt response. The second half is about behaviour Zilog never wrote down: the two undocumented flags, the undocumented instructions, and the places where the Next’s CPU and a Zilog chip part ways; if you read only one section, make it the one called “Where the copy departs from the original”. Last come the twenty-nine Z80N instructions, group by group, with their timings.

The facts are for core 3.02.01, and the 28 MHz section also says what core 3.03 changed. Timings are in CPU T-states, at 3.5 MHz unless a section says otherwise, and without contention. Every fact about the Next was checked against its hardware design.

A Z80 without a chip

The Z80 appeared in 1976, made by Zilog in NMOS, the transistor process of its day; a CMOS version, which uses less power, followed in the 1980s. The two behave the same in every documented way and differ in a few undocumented ones, which will matter later. The ZX Spectrum used the NMOS chip.

The Next’s CPU, the Z80N, is neither. It began life as an open-source description of a Z80 written for FPGAs, and the Next’s designers corrected its timing, completed its undocumented behaviour and added the new instructions. In the FPGA it is one more block of logic, clocked from the same 28 MHz master clock as the picture, so a CPU T-state always lasts a whole number of master-clock ticks.

I like to call the Z80N the understudy. It learned the lead’s part so well that the audience cannot tell who is on stage: the same instructions, the same T-states, the same documented flags. But an understudy also brings a few habits of its own, and the Z80N improvises a handful of lines that the original never spoke. You will meet them in the second half.

What being a description rather than a chip means in practice:

  • No analogue behaviour. An NMOS chip has electrical quirks, such as how long a floating bus keeps its last value. The Z80N has none of its own; whatever the Next shows on an undriven bus comes from the machine around the CPU, as the memory chapter’s What you read when nobody answers explains.
  • The same T-states. Every Z80 instruction takes exactly as many T-states as on a Zilog chip, including the undocumented ones and the conditional ones taken or not taken.
  • Four speeds. The CPU Speed register, NextReg $07, runs it at 3.5, 7, 14 or 28 MHz.

Registers, and one you cannot see

The Z80N has the Z80’s registers and not one more; none of its new instructions needed a new register.

  • The main set: A and F, and the pairs BC, DE and HL.
  • The alternate set: A', F', BC', DE' and HL', swapped in with EX AF,AF' and EXX.
  • The index registers IX and IY, the stack pointer SP and the program counter PC.
  • I, the high byte of the IM 2 table, and R, the refresh counter: its low seven bits count every opcode fetch, and bit 7 keeps whatever LD R,A last put there.
  • The interrupt state: the two flip-flops IFF1 and IFF2, and the interrupt mode, 0, 1 or 2.

The one you cannot see is a scratch register. WZ, also called MEMPTR, is a 16-bit register the CPU uses to build addresses: LD A,(nn), JP nn, CALL nn and many others leave a value in it. No instruction reads it, but on a Zilog chip it leaks out through the flags of BIT n,(HL). On the Next it does not, and we will come back to that.

Machine cycles and T-states

The Z80 does nothing in one go. Every instruction is a short series of machine cycles, M-cycles for short, and each M-cycle is one of a few kinds of conversation with the bus, a few T-states long. A T-state is one tick of the CPU’s clock, 286 nanoseconds at 3.5 MHz. There are four kinds of M-cycle you need to know:

  • The opcode fetch, M1: 4 T-states. It reads an opcode byte at PC and refreshes memory.
  • The memory read: 3 T-states, for operands and data.
  • The memory write: 3 T-states.
  • The I/O cycle: 4 T-states, a read or a write on a port.

Some instructions add a few T-states of internal work to one of their cycles, while nothing happens on the bus. Take LD A,(nn): an opcode fetch (4), two operand reads for the address (3 + 3), and a data read (3) make 13 T-states. LD (nn),A is the same, with a write in place of the last read, and also takes 13.

Prefixes are opcodes too. CB, ED, DD and FD each take an opcode fetch of their own, so an ED instruction is at least 8 T-states long and counts R up by two. The DD CB and FD CB instructions are the exception: after the two prefixes, the displacement and the final opcode are read as plain memory reads, so R counts only two.

The opcode fetch

The first M-cycle of every instruction does two jobs, one after the other. Here it is, signal by signal:

T1T2T3T4addressPCI (high) : R (low)/M1/MREQ/RD/RFSHdataopcodetaken here1234fetchrefresh, R + 1
The opcode fetch, M1, to scale in T-states. T1 and T2 read the opcode at PC; T3 and T4 put I and R on the address bus for the refresh. The CPU takes the byte at the start of T3.
  1. T1: the CPU puts PC on the address bus and pulls /M1 low, so the world knows this is an opcode fetch. Half a T-state later it pulls /MREQ (memory request) and /RD (read) low.
  2. T2: the memory has until the end of this T-state to put the byte on the data bus.
  3. Start of T3: the CPU takes the byte, lets /M1, /MREQ and /RD go, and starts decoding.
  4. T3 and T4: the refresh. The CPU puts I in the address bus’s high half and R in its low half, pulls /RFSH low, gives a short /MREQ pulse, and adds 1 to R’s low seven bits.

Why refresh? The Spectrum’s RAM was dynamic: each bit is a tiny capacitor that forgets in a few milliseconds unless something reads its row. The Z80 does that for free in the half of the M1 cycle where it is busy decoding anyway. I find it one of the cleverest ideas in the chip: a chore done in time nobody else wanted.

The Next’s memory is static RAM, which never forgets, so it needs no refresh. The Z80N still runs the refresh half, because it is part of the cycle’s timing, and R still counts. The Next keeps its memory out of the refresh cycle and passes /RFSH to the expansion bus, for a peripheral that might want it.

Reading, writing and talking to ports

Every M-cycle after the opcode fetch is one of four kinds: a memory read, a memory write, an I/O read or an I/O write. A memory read is the opcode fetch’s first half with one T-state more:

T1T2T3addressmemory address/MREQ/RD/WAITsampleddata inbytetaken here123
A memory read, to scale in T-states. /MREQ and /RD are low from the middle of T1 to the middle of T3, and the CPU takes the byte at that last edge, half a T-state later than in an opcode fetch.
  1. T1: the CPU puts the address on the bus. Half a T-state later it pulls /MREQ and /RD low.
  2. Middle of T2: the CPU looks at /WAIT. If a device holds it low, a wait state, TW, comes between T2 and T3, and the CPU looks again in the middle of each one.
  3. Middle of T3: the CPU takes the byte and lets /MREQ and /RD go. With no refresh to do, the cycle ends here: three T-states, where the fetch took four.

A memory write has the same shape, but the CPU drives the data bus and the strobe is /WR:

T1T2T3addressmemory address/MREQ/WR/WAITsampleddata outbyte123
A memory write, to scale in T-states. The byte is on the data bus from the middle of T1, and /WR is low only from the middle of T2 to the middle of T3, while address and data are both stable.
  1. T1: the address goes out. Half a T-state later /MREQ falls and the byte goes on the data bus.
  2. Middle of T2: with the data settled, /WR falls; this is the moment the CPU looks at /WAIT.
  3. Middle of T3: /WR and /MREQ rise, and the memory keeps the byte. The address and the data stay half a T-state longer, so the memory never sees them change under a live /WR.

An I/O cycle uses /IORQ instead of /MREQ, and it has one T-state more, an automatic wait state, because the peripherals of 1976 needed the time. IN and OUT share the cycle and differ in who drives the data bus:

T1T2TWT3addressport address: BC, or A : n/IORQ/RD (IN)/WR (OUT)/WAITsampleddata, OUTbytedata, INbytetaken1234
An I/O cycle, to scale in T-states, with the automatic wait state TW between T2 and T3. The strobes fall a whole T-state into the cycle, at the start of T2, and an IN takes its byte in the middle of T3.
  1. T1: the port address goes out on all 16 lines. For an OUT, the byte follows half a T-state later.
  2. Start of T2: /IORQ falls, with /RD for an IN or /WR for an OUT, half a T-state later than /MREQ would. The port has had a whole T-state to decode its address.
  3. TW: the wait state every I/O cycle gets for free. The CPU looks at /WAIT in its middle, so a slow port can ask for more.
  4. Middle of T3: an IN takes the byte, and all the strobes rise.

Two of the I/O cycle’s details shape the Next:

  • The port address is 16 bits. Zilog’s manual talks about 256 ports, but the CPU always drives the whole address bus: IN r,(C) and OUT (C),r put BC on it, and IN A,(n) and OUT (n),A put A in the high half and n in the low half. The Next decodes most of its ports with all 16 bits, which the memory chapter’s Ports: which bits count goes through.
  • A device can ask for more time. Any cycle stretches while /WAIT is low, a T-state at a time. On the Next, four things hold it: contention at 3.5 MHz, the memory at 28 MHz, a palette read that must wait for the Copper, and a peripheral on the expansion bus.

Contention, the ULA holding the CPU back while both want the screen memory, exists only at 3.5 MHz, in the 48K, 128K and +3 timings; the memory chapter’s Contention has the rules. The 28 MHz wait state is the Next’s own, and it deserves a section.

Four speeds and one wait state

At 3.5, 7 and 14 MHz, an instruction takes exactly the T-states of the Zilog tables; a T-state only gets shorter. At 28 MHz a T-state lasts 36 nanoseconds, and the Next adds one wait state to every memory read: every opcode fetch, every operand byte and every byte of data read. Memory writes and I/O cycles get none, and neither do reads from the first half of bank 7, page $0E, which lives in the FPGA’s own block RAM rather than in the static RAM chips. (Bank 5 lives there too, but the ULA shares it, so its reads wait like the rest.)

The reason is the memory. The CPU and Layer 2 share the Next’s static RAM chips, and the core runs one memory cycle on them per 28 MHz clock, the CPU first and Layer 2 in the cycles it leaves. So that the chips and the board’s wires can always keep up, a read gets the whole of its clock period, 36 nanoseconds, and the byte is stored at the edge that ends it. At 14 MHz and below the CPU’s T-state spans two or more of those clocks, so the byte is ready in time. At 28 MHz the CPU’s T-state is the memory’s clock, the byte arrives one clock after the CPU wants it, and the core holds /WAIT low for one T-state on every read. A write needs no answer back: the core times its write pulse inside the same period with a clock five times as fast, and it is over before the period ends. I/O cycles do not use the RAM at all.

Here are two instructions that look alike, at both ends of the speed range:

01234567891011121314151617T-statesLD A,(nn)3.5 MHzM1readreadread13 TLD A,(nn)28 MHzM1WreadWreadWreadW17 TLD (nn),A3.5 MHzM1readreadwrite13 TLD (nn),A28 MHzM1WreadWreadWwrite16 T
LD A,(nn) and LD (nn),A at 3.5 MHz and 28 MHz, to scale in T-states. At 28 MHz each read cycle gets one wait state (W); the write does not, so the store ends up one T-state faster than the load.

So the rule is easy to apply: at 28 MHz, add one T-state for every byte the instruction reads, its opcode bytes included. A few examples:

  • NOP takes 5 T-states instead of 4: one fetch.
  • LD A,(HL) takes 9 instead of 7, and LD (HL),A takes 8 instead of 7.
  • PUSH BC takes 12 instead of 11 (one fetch, two writes), and POP BC 13 instead of 10 (one fetch, two reads).
  • OUT (n),A takes 13 instead of 11: the opcode and n are read, the I/O cycle waits for nobody.
  • LD (IX+d),n takes 23 instead of 19: two opcode fetches (DD and 36), the displacement and n.

The wait state is the price of giving the memory a whole clock to answer. I think it is a fair price: 28 MHz is still roughly six to seven times as fast as 3.5 MHz, not eight. My advice is to count the T-states of a timed loop at the speed it will run at, and never to scale a 3.5 MHz count by eight.

Core 3.03 takes the wait state away. From core 3.03 on, the Next runs at 28 MHz with zero wait states, on every board: the Issue 2, Issue 4 and Issue 5 cores all have it. Every instruction is back to its Zilog T-states at every speed, NOP takes 4 at 28 MHz, LD A,(nn) 13 and POP BC 10, and the DMA’s reads lose their wait as well. So the same program runs a little faster on a 3.03 core than on a 3.02 one, and a delay loop counted for 3.02 at 28 MHz comes out short. If a loop’s exact length matters to you, read the core version from NextReg $01 and NextReg $0E and count for the core you are on; I would rather count than guess.

Sharing the bus

The CPU is not the only master of the Next’s bus. The DMA can move memory and ports on its own, and to do that it asks the CPU for the bus with /BUSRQ. The CPU lets go at the end of the M-cycle it is in, not at the end of the instruction, and says so with /BUSAK. While the DMA holds the bus, the CPU stands still in the middle of its instruction, does no refresh, and cannot take an interrupt. When the DMA lets go, the instruction carries on as if nothing had happened, only later.

That last point is why the DMA can make an interrupt pulse vanish, and why the Next has a way to make the DMA step aside. Both are in The DMA: A Second Master on the Bus and in the interrupts chapter’s The DMA steps aside.

HALT is the other way the CPU stops, but it stops working, not fetching. A halted CPU runs opcode fetches of 4 T-states each and executes them as NOPs, so R keeps counting and the refresh keeps going, while PC already points past the HALT. When an interrupt comes, the CPU leaves the halt and the interrupt returns to the instruction after it.

Interrupts, from the CPU’s side

A program that waits for a device can ask it, again and again, whether anything has happened. That is polling, and it spends the CPU’s time on asking. An interrupt turns the question around: the device signals when it needs attention, the CPU finishes the instruction it is running, saves PC on the stack and jumps to a handler, a routine written for that device. The handler does its work and returns, and the interrupted program carries on where it stopped, none the wiser, as long as the handler leaves the registers as it found them.

The Z80 has two interrupt inputs, for two kinds of interrupt.

Maskable interrupts

The everyday kind arrives on /INT. It is maskable: the program can switch it off with DI and on again with EI, for example while it changes the handler or runs code whose timing must not be disturbed. The switch is a flip-flop in the CPU, IFF1. Accepting an interrupt clears it too, so a handler starts with interrupts off and is not interrupted by the next request until it executes EI itself.

/INT is a level. A device holds it low until it is served, and the CPU answers only while IFF1 is set. The line has no memory: if it goes high again before the CPU answers, the request is gone. Where the CPU goes is set by the interrupt mode, which comes below.

On the Next, /INT serves the regular events: the frame interrupt, 50 or 60 times a second at the start of every picture, which the Spectrum’s ROM uses to read the keyboard and count time; the line interrupt, at a raster line the program chooses; the CTC’s four timers; and the two UARTs, when a byte arrives or the transmitter wants the next one. The Next joins them onto the one wire in one of two ways:

  • Pulse mode, the Spectrum’s way and the default: any source starts the same short pulse on /INT, and a handler that serves several sources has to find out which one rang.
  • Hardware IM2 mode: a source holds /INT until it is answered, so no request is lost, and each source hands the CPU a byte of its own, so in IM 2 each gets its own handler. When two sources ask at once, the one with the higher priority goes first.

The non-maskable interrupt

The other input, /NMI, is non-maskable: DI cannot stop it. It is meant for the events that must be served whatever the program is doing; Zilog’s own example is an impending power failure. On the Spectrum it became the freeze button. An add-on such as the Multiface presses /NMI, the CPU jumps to $0066, and the add-on pages its own ROM in at that address, so it can stop any game at any moment to save a snapshot, change a value or poke a cheat. The Next has two such buttons: M1, for its built-in Multiface, and DRIVE, for the DivMMC’s NMI handler, and a program can raise the same NMIs from software.

/NMI is an edge: the CPU remembers that it fell until it can answer, and the NMI wins when both kinds are waiting. Since it can arrive in the middle of anything, a maskable handler included, the CPU keeps a second flip-flop, IFF2, to remember whether interrupts were on. EI and DI set and clear both flip-flops together; an NMI clears IFF1 and leaves the old value in IFF2, and the NMI handler’s RETN puts it back, so the interrupted program gets its interrupts exactly as they were.

When the CPU looks

The CPU does not look at /INT all the time. It samples it once per instruction, at the start of the instruction’s last T-state, and answers when the instruction ends. Two rules make exceptions: it never answers between a prefix (CB, DD, ED, FD) and the rest of its instruction, and it never answers right after EI. Here is the end of a handler, with a new request already waiting:

048121620242832364044T-states/INTinstructionsPOP HLEIRETIIFF1 is 0: ignoredjust after EI: ignoredtaken: answered as soon as RETI ends
The instruction after EI, to scale in T-states. /INT is low from T-state 1, but the CPU ignores it at the end of POP HL, because IFF1 is 0, and at the end of EI; it takes it only after RETI, and answers it as soon as RETI ends.

The gap after EI is what makes EI followed by RETI safe: the return always happens before the next interrupt, so a stream of interrupts can never pile return addresses up on the stack. A halted CPU keeps looking too: HALT executes NOP cycles of 4 T-states until an interrupt comes, and the CPU samples /INT in each one.

The acknowledge cycle

When the CPU accepts an /INT, it clears IFF1 and IFF2 and runs a special M1 cycle, the interrupt acknowledge, to ask the interrupting device for a byte:

T1T2TWTWT3T4addressPCI : R/M1/MREQ/IORQ/RFSHdata invectortaken1234
The interrupt acknowledge, to scale in T-states. /IORQ takes the place of /MREQ and /RD, two wait states come for free, and the CPU takes the vector byte at the start of T3, as it takes an opcode.
  1. T1: the CPU puts PC on the address bus and pulls /M1 low, as for an opcode fetch, but /MREQ and /RD stay high: no memory is read.
  2. The two wait states: Zilog added them so that a chain of devices has time to agree which of them answers. Half way through the first, /IORQ falls; /M1 and /IORQ low together is the signature of an acknowledge, and the winning device puts its byte on the data bus.
  3. Start of T3: the CPU takes the byte, the vector, and lets /M1 and /IORQ go.
  4. T3 and T4: the refresh, as in every M1 cycle.

The three interrupt modes

The interrupt mode, set by IM 0, IM 1 or IM 2, decides what happens next:

02468101214161820T-statesIM 0, IM 1then $0038acknowledgepushpush13 TIM 2then (I : vector)acknowledgepushpushreadread19 TNMIthen $0066fetchpushpush11 Tpush: a memory write of the return address; read: the handler’s address from the table
The three responses, to scale in T-states. Each pushes the return address in two writes; IM 2 then reads the handler’s address from a table, and the NMI starts with an opcode fetch whose byte it ignores.
  • IM 0 executes the byte as an instruction, usually an RST. It is the 8080’s way, kept for its old peripherals; Spectrum software does not use it.
  • IM 1 ignores the byte and calls $0038: one handler for everything. With the acknowledge, one extra T-state to move SP and the two writes of the return address, that takes 13 T-states. The Spectrum’s ROM runs in IM 1, and its routine at $0038 is the keyboard and clock handler.
  • IM 2 builds an address from I as the high byte and the vector as the low byte, reads the handler’s address from there, low byte first, and calls it: 19 T-states. The table can sit anywhere in memory, and with 128 entries it can tell 128 devices apart. Games use IM 2 to put their own handler in place of the ROM’s, and the Next’s hardware IM2 mode uses it to give every source a handler of its own.

At 28 MHz the reads get their wait states and nothing else does: the acknowledge reads no memory and the pushes are writes, so IM 1 still takes 13 T-states and IM 2 takes 21 (19 on core 3.03, which has no wait states).

The NMI’s response

An NMI needs no device to name it. The CPU clears IFF1, copies the old IFF1 into IFF2, runs an opcode fetch at PC whose byte it ignores, and pushes the return address, then continues at $0066: 11 T-states. Because IFF2 remembers whether interrupts were on, the handler can find out: LD A,I and LD A,R copy IFF2 into the P/V flag.

RETN and RETI both pop the return address and copy IFF2 back into IFF1. The two differ only in what devices see: Zilog’s peripherals watch the bus for RETI’s opcode to learn that their handler has finished, and so does the Next’s interrupt controller, which in hardware IM2 mode keeps a source in service until its handler returns.

One Next detail fits here. In pulse mode no device drives the data bus during the acknowledge, and the Next answers with $FF. In IM 0 the CPU executes $FF, which is RST $38, so IM 0 behaves exactly like IM 1; in IM 2 the CPU reads its handler’s address from I:$FF and the byte after it.

The Next’s sources, their priorities, the vectors and the NMI’s devices have a chapter of their own, Interrupts: Fourteen Voices, Two Wires, and its How fast the CPU answers adds the wait for the running instruction to the costs above.

The flags, documented and not

The flag register F has eight bits, and Zilog documented six of them. The other two were marked “not used”, and they are used all the time:

bit 7Ssignbit 6Zzerobit 5Ybit 5 ofthe resultbit 4Hhalfcarrybit 3Xbit 3 ofthe resultbit 2P/Vparity oroverflowbit 1Nsubtractbit 0Ccarrydashed: undocumented by Zilog
The flag register. Zilog documented six flags; bits 5 and 3, here Y and X, are undocumented, and most instructions copy bits 5 and 3 of their result into them.

I will call bit 5 Y and bit 3 X; you will also meet them as F5 and F3. The general rule is simple: an instruction that changes the flags copies bits 5 and 3 of its result into Y and X. ADD A,B that gives $28 sets Y (bit 5 of $28 is 1) and X (bit 3 is 1). The exceptions are where it gets interesting:

  • CP n takes Y and X from the operand, not from the subtraction it did, on both the Zilog chip and the Next. So CP $28 sets both, whatever A holds.
  • BIT n,r takes them from the register it tested.
  • BIT n,(HL) on a Zilog chip takes them from the high byte of WZ, the hidden register. This is how WZ was discovered, and the only way a program can see it.
  • SCF and CCF on a Zilog chip take them from A, mixed with the old flags in a way that depends on whether the instruction before changed the flags.

Undocumented does not mean invisible. No instruction tests Y or X directly: there is no jump, call or return on them, and they take no part in any calculation. But F is a register like any other the moment it leaves the CPU. PUSH AF writes it to the stack next to A, and any POP brings it back into a register a program can test:

; Was Y, bit 5 of F, set by the last instruction? push af ; F goes to the stack, below A pop bc ; and comes back in C bit 5,c ; Z is 0 if Y was 1

So Y and X are part of every instruction’s visible result, and they matter. A program that keeps F as data, a checksum or a copy protection that mixes the pushed flags into its sums, gets a different answer when they differ. The exerciser programs that test a Z80, or an emulator, compare the whole of F after every instruction, Y and X included. And a program can tell one Z80 from another by how SCF and CCF set them. An emulator that is faithful in the documented flags only is not faithful.

The Next agrees with a Zilog chip on the documented flags of every instruction, and on Y and X of most of them. Where it does not agree is the subject of the next section but one.

Undocumented instructions

The Z80’s opcode tables have holes, and most of the holes do something. Programs found them, used them, and shipped with them, so the Next runs every one of them as a Zilog chip does:

  • The index registers’ halves. A DD or FD prefix before an instruction that uses H or L makes it use the high or low half of IX or IY instead: LD A,IXH, INC IYL, ADD A,IXL. Assemblers call them IXH, IXL, IYH and IYL (or XH, XL, YH, YL).
  • SLL, the opcodes CB 30 to CB 37: shift left and put a 1 into bit 0, where SLA puts a 0. Some assemblers call it SL1 or SLI.
  • The indexed bit instructions with a copy. DD CB d xx with a register in its low bits, such as RLC (IX+d),B, rotates the byte in memory and also copies the result into B. RES and SET do the same.
  • IN (C), also written IN F,(C), opcode ED 70: reads the port, sets the flags as IN r,(C) would, and throws the byte away. A handy way to test bit 7 of a port in S without spending a register.
  • OUT (C),0, opcode ED 71: writes a byte to port BC. On an NMOS Zilog chip that byte is 0, on a CMOS one it is $FF. On the Next it is 0.
  • Duplicates. NEG has eight opcodes (ED 44, 4C, … 7C), RETN seven (ED 45, 55, … 7D, next to RETI at ED 4D). ED 4E, 66 and 6E set IM 0, ED 76 IM 1, ED 7E IM 2. ED 63 and ED 6B are long forms of LD (nn),HL and LD HL,(nn), 4 T-states slower than the usual ones.
  • Everything else in the ED page does nothing and takes 8 T-states: two opcode fetches.
  • Prefix chains. A DD or FD before an instruction that uses no HL costs 4 T-states and changes nothing, and in a chain of them the last one wins.

The ED page is also where the Z80N put its new instructions, in holes no Zilog program ever used:

x0x1x2x3x4x5x6x7x8x9xAxBxCxDxExF0x1x2x23242728292A2B2C3x303132333435364x404142434445464748494A4B4C4D4E4F5x505152535455565758595A5B5C5D5E5F6x606162636465666768696A6B6C6D6E6F7x7071727374757678797A7B7C7D7E8x8A9x90919293949598AxA0A1A2A3A4A5A8A9AAABACBxB0B1B2B3B4B6B7B8B9BABBBCCxDxExFxZilogZilog, undocumentedZ80NED B6: acts as LDIRXempty: 8 T NOP
The ED page of the Next’s CPU, opcodes ED 00 to ED FF. Zilog’s instructions sit in $40–$7F and in four columns of block instructions; the Z80N’s fill holes around them. Every empty cell is an 8-T-state NOP.

Where the copy departs from the original

Here are the understudy’s own lines. Every one of them is about the two undocumented flags, the hidden register or a byte the CPU puts on the bus; none changes a documented flag, a register or a T-state.

  • BIT n,(HL) and BIT n,(IX+d) clear Y and X on the Next. A Zilog chip fills them from WZ’s high byte. The consequence is bigger than it looks: WZ is invisible on the Next. The register exists, the CPU uses it to build addresses, but no program can ever observe it.
  • SCF and CCF copy Y and X from A, always. A Zilog chip does so only after an instruction that changed the flags; otherwise it mixes in the old Y and X.
  • IN r,(C), IN (C), LD A,I and LD A,R keep Y and X as they were. A Zilog chip copies them from the byte read or loaded.
  • CPI, CPD, CPIR and CPDR copy Y and X from the byte they compared. A Zilog chip takes them from bits 3 and 1 of A minus the byte, minus the half carry.
  • INI, IND, OUTI, OUTD and their repeating forms set the flags of decrementing B: N set, S and Z from the new B, H when the decrement borrowed from bit 4, P/V when B was $80, and C when B was 0. A Zilog chip computes N, H, C and P/V from the byte moved and from C or L, in a formula people found by testing the chip.
  • A repeating block instruction (LDIR, CPIR, INIR, OTIR and the others) sets the same flags on every repetition as on the last one. A Zilog chip, while it repeats, takes Y and X from the program counter.
  • LD A,I and LD A,R copy IFF2 into P/V, correctly, even when an interrupt is taken right after. An NMOS Zilog chip reports 0 there, a known fault that the CMOS chip fixed.

So which chip is the Next, NMOS or CMOS? Neither, and that is the honest answer. It writes 0 with OUT (C),0, like the NMOS chip; it gets LD A,I right, like the CMOS one; and its Y and X follow rules of their own. If you ask me, none of this harms a real program: I have never met a game that depended on Y after CPIR. But a test program that checks the undocumented flags will fail on a real Next, and a copy protection that hides a key in WZ will not find it. My rule of thumb: never let a Next program depend on Y, X or WZ.

Twenty-nine new instructions

The Z80N’s additions all live in the ED page, behind the prefix, so each costs at least 8 T-states. A Zilog chip runs every one of them as an 8-T-state NOP, so a program that uses them does nothing useful on a Spectrum, and does it quietly. They come in groups, and I will take them one group at a time. Unless a group says otherwise, an instruction leaves the flags alone.

Arithmetic

InstructionOpcodeT-statesWhat it does
MUL D,EED 308DE = D × E, unsigned
ADD HL,AED 318HL = HL + A, unsigned; clears carry
ADD DE,AED 328the same for DE
ADD BC,AED 338the same for BC
ADD HL,nnED 34 lo hi16HL = HL + nn
ADD DE,nnED 35 lo hi16DE = DE + nn
ADD BC,nnED 36 lo hi16BC = BC + nn
TEST nED 27 n11the flags of AND n, and A stays

MUL D,E is the one I would miss most if it went away: an 8 × 8 multiply in 8 T-states, where a Z80 loop needs well over a hundred. Its result fits DE exactly, so it never overflows.

ADD rr,A adds A as an unsigned byte, so it moves a pointer forward by up to 255, and it wraps from $FFFF to 0. Let me warn you about its carry: it is always cleared, even when the sum wraps. Do not test the carry after it to detect a wrap; compare the result instead. The other flags stay. ADD rr,nn leaves every flag alone, carry included, and wraps the same way.

TEST n is to AND n what CP n is to SUB n: the flags without the result, so you can test bits of A without a copy.

Bits and shifts

InstructionOpcodeT-statesWhat it does
SWAPNIBED 238swaps the two halves of A
MIRROR AED 248reverses the order of A’s bits
BSLA DE,BED 288DE shifted left by B, zeros in
BSRA DE,BED 298DE shifted right by B, copies of bit 15 in
BSRL DE,BED 2A8DE shifted right by B, zeros in
BSRF DE,BED 2B8DE shifted right by B, ones in
BRLC DE,BED 2C8DE rotated left by B

A barrel shift moves a value any number of places in one step, where SLA moves it one. The shifts use bits 4 to 0 of B, so a count from 0 to 31: 16 or more shifts every bit out, and BSRA DE,B then leaves $0000 or $FFFF by the sign. BRLC uses bits 3 to 0, because rotating by 16 changes nothing. MIRROR A is a gift for sprites: flip a row of 1-bit graphics horizontally in one instruction.

The stack and a jump

InstructionOpcodeT-statesWhat it does
PUSH nnED 8A hi lo23pushes the value nn
JP (C)ED 9813jumps by a byte read from port BC

PUSH nn saves loading a register to push a constant, for a return address or a parameter. Notice its operand: unlike every other 16-bit operand of the Z80, it is stored high byte first. Your assembler takes care of that, but a disassembler that does not know the Z80N will show nonsense.

JP (C) reads a byte from port BC and jumps to it, times 64, inside the 16K block the next instruction is in. If JP (C) sits at $8000 and the port answers $10, it jumps to $8000 + $10 × 64 = $8400. It was made for jump tables driven by hardware, such as a byte read from the expansion bus.

The screen

InstructionOpcodeT-statesWhat it does
PIXELADED 948HL = the ULA screen address of the dot at E, D
PIXELDNED 938HL = the address one dot row down
SETAEED 958A = the bit of the dot in its byte

The Spectrum’s screen layout, with its thirds and its interleaved rows, takes a Z80 a dozen instructions to turn a dot’s position into an address. PIXELAD does it in one: E is the dot’s column (x, 0 to 255), D its row (y, 0 to 191), and here is where their bits go:

D: the row (y)E: the column (x)y7y6y5y4y3y2y1y0x7x6x5x4x3x2x1x0HL010y7y6y2y1y0y5y4y3x7x6x5x4x3H starts with 010: the screen begins at $4000. The row’s bitsare split into its third, its character row and its dot row.x2–x0 (red) are not in the address: SETAE makes them A = $80 >> (x & 7).
PIXELAD builds the screen address from D (the row, y) and E (the column, x). The row’s bits are split between H and L the way the Spectrum’s screen interleaves them; SETAE turns the column’s low three bits into the dot’s bit in the byte.

SETAE gives the dot’s bit: $80 for column 0, $40 for column 1, down to $01 for column 7, from E’s low three bits. PIXELDN moves HL one dot row down, across the character rows and the thirds, without you counting them. Together they draw a vertical line:

; Draws a vertical line of B dots down from column E, row D VLine: pixelad ; HL = address of the dot (E, D) setae ; A = the dot's bit in its byte ld c,a ; keep it in C Dot: ld a,c or (hl) ; set the dot ld (hl),a pixeldn ; HL = same column, one row down djnz Dot ret

How it works:

  • PIXELAD and SETAE turn the starting position into an address and a bit, once.
  • Every dot is the classic read, set, write of a byte.
  • PIXELDN replaces the twelve or so instructions a Z80 needs to step down a row, which is where the time went in the Spectrum’s line-drawing routines.

A small warning: below row 191, PIXELDN goes on past the screen, into the attributes. Count your rows.

Block copies and an output

InstructionOpcodeT-statesWhat it does
LDIXED A416LDI, but skips the write if the byte equals A
LDIRXED B421 / 16repeats LDIX while BC is not 0
LDDXED AC16as LDIX, with HL going down and DE up
LDDRXED BC21 / 16repeats LDDX while BC is not 0
LDPIRXED B721 / 16repeats a copy from an 8-byte pattern
LDWSED A514copies (HL) to (DE), then L + 1 and D + 1
OUTINBED 9016sends (HL) to port BC, then HL + 1

The repeating forms take 21 T-states for a byte after which they repeat and 16 for the last, like LDIR.

The X in the names means exclude: the byte equal to A is not written. That is a transparent colour, in hardware. Set A to the colour your sprite uses for “nothing here”, and LDIRX copies the sprite over a background without wiping it. HL, DE and BC move on for a skipped byte as for a written one.

Look closely at LDDX: HL goes down but DE goes up. That is not a slip; it copies a row of bytes in reverse order, which mirrors a row of a 256-colour picture horizontally.

LDPIRX fills memory from an 8-byte pattern. It reads its byte from HL with the low three bits replaced by those of E, writes it to DE unless it equals A, and moves DE on, leaving HL alone. As DE walks forward, the pattern repeats every 8 bytes, lined up with the destination: a dithered fill, a striped background, a texture.

LDWS copies a byte and moves to the next column of the source and the next row of the destination, as INC L and INC D would; the flags are those of INC D. It was made for the Layer 2 screen, where D is a row and E a column. OUTINB is OUTI without touching B, so B keeps the port’s high byte and HL streams a table to it.

The flags after the block copies follow LDI’s rules, more or less, and I would not rely on them: test BC yourself.

NextRegs

InstructionOpcodeT-statesWhat it does
NEXTREG r,nED 91 r n20writes n to NextReg r
NEXTREG r,AED 92 r17writes A to NextReg r

These write a NextReg without the two ports. The usual way, through port $243B to select a register and port $253B to write it, takes two OUTs and a register to hold the port. More important to me than the speed: NEXTREG leaves port $243B’s selection alone. An interrupt handler that writes NextRegs with NEXTREG can never confuse a main program that selected a register and is about to write it. The memory chapter’s Two ways into the NextRegs compares the two.

One instruction off the list, and the empty cells

ED B6 is in no list of the Z80N’s instructions, yet it is not a NOP: it behaves exactly like LDIRX, timing included. It is what is left of an instruction that was planned and never finished, and it is the same on cores 3.02.01 and 3.02.04 and on every board, the KS3 included. I mention it so it does not surprise you in a disassembly, not so you use it: a later core may finish it or take it away.

Every other empty cell of the ED page is a plain 8-T-state NOP on the Next. That is what the hardware does, but it is not what every tool makes of those cells. Because no real program should contain them, emulators have long borrowed empty ED cells for private instructions of their own:

  • Fuse treats ED FB as the trap of the SLT snapshot format: a multi-level game packed into one SLT file executes it to load its next level from that file.
  • ZEsarUX, while it emulates the SamRam add-on, gives ED FB and ED FE the SamRam’s meanings: both write A to the address in HL, one into the add-on’s RAM, the other into its ROM.
  • CSpect, the Next emulator many Next developers work with, reads ED 01 and ED 02 as SETBRK and CLRBRK, which set and clear breakpoints over an address range; each is followed by five bytes of operands. sjasmplus assembles them only with its --zxnext=cspect option. CSpect’s BREAK and EXIT live in the other prefix pages, as FD 00 and DD 00.

Other Z80 descendants fill the same holes with real instructions: on the Zilog Z180, ED 00 and ED 01 are IN0 and OUT0. So treat the empty cells with care. An emulator’s private instruction does something else on a real Next: CSpect’s SETBRK runs there as an 8-T-state NOP, and then the CPU executes its five operand bytes as code. Keep such instructions out of anything that will run on a real machine, and do not count on an empty cell being a NOP in every emulator either. When an unexpected ED byte turns up in a disassembly, read it as a sign that the code was built for a particular emulator, or that it is data.

What to take home

The Z80N is a Z80 rebuilt in logic, and a faithful one: the same registers, the same instructions, documented and undocumented, and the same T-states, down to the opcode fetch’s refresh half that the Next’s static memory does not need. Every instruction is a string of machine cycles, an opcode fetch of 4 T-states, memory reads and writes of 3 and I/O cycles of 4, and the clock behind them runs at 3.5, 7, 14 or 28 MHz as NextReg $07 says.

The timing has one Next rule of its own: at 28 MHz every memory read gets one wait state, writes and I/O none, because a read gets a whole 28 MHz clock of the memory and its byte comes one clock late. Count one extra T-state per byte read, opcodes included, except from page $0E. Core 3.03 drops that wait on every board, so there the Zilog counts hold at all four speeds. Contention exists only at 3.5 MHz. The DMA takes the bus between M-cycles, HALT keeps fetching NOPs, and an interrupt costs 13 T-states in IM 1 and 19 in IM 2 once the running instruction ends.

The understudy’s own lines are all in the corners Zilog never documented. Y and X, bits 5 and 3 of F, follow the Zilog rules for most instructions but not for BIT n,(HL), SCF, CCF, IN r,(C), LD A,I, the block compares and block I/O, or repeating block instructions; WZ never shows; OUT (C),0 writes 0, and LD A,I reports IFF2 correctly. None of it touches a documented flag.

And the twenty-nine new instructions sit in the holes of the ED page: MUL D,E and the 16-bit additions, TEST, SWAPNIB, MIRROR and the barrel shifts, PUSH nn with its high byte first and JP (C), the three screen helpers, the block copies that skip a transparent byte, OUTINB, and the two NEXTREGs that leave port $243B alone. A Spectrum runs them all as NOPs; a Next runs them in 8 to 23 T-states. Old heart, new tricks, and not a single old program broken.

Last updated on