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:
AandF, and the pairsBC,DEandHL. - The alternate set:
A',F',BC',DE'andHL', swapped in withEX AF,AF'andEXX. - The index registers
IXandIY, the stack pointerSPand the program counterPC. I, the high byte of the IM 2 table, andR, the refresh counter: its low seven bits count every opcode fetch, and bit 7 keeps whateverLD R,Alast 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
PCand 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:
- T1: the CPU puts
PCon the address bus and pulls/M1low, so the world knows this is an opcode fetch. Half a T-state later it pulls/MREQ(memory request) and/RD(read) low. - T2: the memory has until the end of this T-state to put the byte on the data bus.
- Start of T3: the CPU takes the byte, lets
/M1,/MREQand/RDgo, and starts decoding. - T3 and T4: the refresh. The CPU puts
Iin the address bus’s high half andRin its low half, pulls/RFSHlow, gives a short/MREQpulse, and adds 1 toR’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:
/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.- T1: the CPU puts the address on the bus. Half a T-state later it pulls
/MREQand/RDlow. - 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. - Middle of T3: the CPU takes the byte and lets
/MREQand/RDgo. 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:
/WR is low only from the middle of T2 to the middle of T3, while address and data are both stable.- T1: the address goes out. Half a T-state later
/MREQfalls and the byte goes on the data bus. - Middle of T2: with the data settled,
/WRfalls; this is the moment the CPU looks at/WAIT. - Middle of T3:
/WRand/MREQrise, 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:
IN takes its byte in the middle of T3.- T1: the port address goes out on all 16 lines. For an
OUT, the byte follows half a T-state later. - Start of T2:
/IORQfalls, with/RDfor anINor/WRfor anOUT, half a T-state later than/MREQwould. The port has had a whole T-state to decode its address. - TW: the wait state every I/O cycle gets for free. The CPU looks at
/WAITin its middle, so a slow port can ask for more. - Middle of T3: an
INtakes 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)andOUT (C),rputBCon it, andIN A,(n)andOUT (n),AputAin the high half andnin 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
/WAITis 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:
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:
NOPtakes 5 T-states instead of 4: one fetch.LD A,(HL)takes 9 instead of 7, andLD (HL),Atakes 8 instead of 7.PUSH BCtakes 12 instead of 11 (one fetch, two writes), andPOP BC13 instead of 10 (one fetch, two reads).OUT (n),Atakes 13 instead of 11: the opcode andnare read, the I/O cycle waits for nobody.LD (IX+d),ntakes 23 instead of 19: two opcode fetches (DDand36), the displacement andn.
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
/INTuntil 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:
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:
- T1: the CPU puts
PCon the address bus and pulls/M1low, as for an opcode fetch, but/MREQand/RDstay high: no memory is read. - 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,
/IORQfalls;/M1and/IORQlow together is the signature of an acknowledge, and the winning device puts its byte on the data bus. - Start of T3: the CPU takes the byte, the vector, and lets
/M1and/IORQgo. - 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:
- 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 moveSPand the two writes of the return address, that takes 13 T-states. The Spectrum’s ROM runs in IM 1, and its routine at$0038is the keyboard and clock handler. - IM 2 builds an address from
Ias 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:
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 ntakes Y and X from the operand, not from the subtraction it did, on both the Zilog chip and the Next. SoCP $28sets both, whateverAholds.BIT n,rtakes 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.SCFandCCFon a Zilog chip take them fromA, 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 1So 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
DDorFDprefix before an instruction that usesHorLmakes it use the high or low half ofIXorIYinstead:LD A,IXH,INC IYL,ADD A,IXL. Assemblers call themIXH,IXL,IYHandIYL(orXH,XL,YH,YL). SLL, the opcodesCB 30toCB 37: shift left and put a 1 into bit 0, whereSLAputs a 0. Some assemblers call itSL1orSLI.- The indexed bit instructions with a copy.
DD CB d xxwith a register in its low bits, such asRLC (IX+d),B, rotates the byte in memory and also copies the result intoB.RESandSETdo the same. IN (C), also writtenIN F,(C), opcodeED 70: reads the port, sets the flags asIN r,(C)would, and throws the byte away. A handy way to test bit 7 of a port inSwithout spending a register.OUT (C),0, opcodeED 71: writes a byte to portBC. On an NMOS Zilog chip that byte is 0, on a CMOS one it is$FF. On the Next it is 0.- Duplicates.
NEGhas eight opcodes (ED 44,4C, …7C),RETNseven (ED 45,55, …7D, next toRETIatED 4D).ED 4E,66and6Eset IM 0,ED 76IM 1,ED 7EIM 2.ED 63andED 6Bare long forms ofLD (nn),HLandLD HL,(nn), 4 T-states slower than the usual ones. - Everything else in the
EDpage does nothing and takes 8 T-states: two opcode fetches. - Prefix chains. A
DDorFDbefore an instruction that uses noHLcosts 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:
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)andBIT 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.SCFandCCFcopy Y and X fromA, 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,IandLD A,Rkeep Y and X as they were. A Zilog chip copies them from the byte read or loaded.CPI,CPD,CPIRandCPDRcopy Y and X from the byte they compared. A Zilog chip takes them from bits 3 and 1 ofAminus the byte, minus the half carry.INI,IND,OUTI,OUTDand their repeating forms set the flags of decrementingB:Nset,SandZfrom the newB,Hwhen the decrement borrowed from bit 4,P/VwhenBwas$80, andCwhenBwas 0. A Zilog chip computesN,H,CandP/Vfrom the byte moved and fromCorL, in a formula people found by testing the chip.- A repeating block instruction (
LDIR,CPIR,INIR,OTIRand 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,IandLD A,Rcopy IFF2 intoP/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
| Instruction | Opcode | T-states | What it does |
|---|---|---|---|
MUL D,E | ED 30 | 8 | DE = D × E, unsigned |
ADD HL,A | ED 31 | 8 | HL = HL + A, unsigned; clears carry |
ADD DE,A | ED 32 | 8 | the same for DE |
ADD BC,A | ED 33 | 8 | the same for BC |
ADD HL,nn | ED 34 lo hi | 16 | HL = HL + nn |
ADD DE,nn | ED 35 lo hi | 16 | DE = DE + nn |
ADD BC,nn | ED 36 lo hi | 16 | BC = BC + nn |
TEST n | ED 27 n | 11 | the 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
| Instruction | Opcode | T-states | What it does |
|---|---|---|---|
SWAPNIB | ED 23 | 8 | swaps the two halves of A |
MIRROR A | ED 24 | 8 | reverses the order of A’s bits |
BSLA DE,B | ED 28 | 8 | DE shifted left by B, zeros in |
BSRA DE,B | ED 29 | 8 | DE shifted right by B, copies of bit 15 in |
BSRL DE,B | ED 2A | 8 | DE shifted right by B, zeros in |
BSRF DE,B | ED 2B | 8 | DE shifted right by B, ones in |
BRLC DE,B | ED 2C | 8 | DE 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
| Instruction | Opcode | T-states | What it does |
|---|---|---|---|
PUSH nn | ED 8A hi lo | 23 | pushes the value nn |
JP (C) | ED 98 | 13 | jumps 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
| Instruction | Opcode | T-states | What it does |
|---|---|---|---|
PIXELAD | ED 94 | 8 | HL = the ULA screen address of the dot at E, D |
PIXELDN | ED 93 | 8 | HL = the address one dot row down |
SETAE | ED 95 | 8 | A = 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:
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
retHow it works:
PIXELADandSETAEturn the starting position into an address and a bit, once.- Every dot is the classic read, set, write of a byte.
PIXELDNreplaces 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
| Instruction | Opcode | T-states | What it does |
|---|---|---|---|
LDIX | ED A4 | 16 | LDI, but skips the write if the byte equals A |
LDIRX | ED B4 | 21 / 16 | repeats LDIX while BC is not 0 |
LDDX | ED AC | 16 | as LDIX, with HL going down and DE up |
LDDRX | ED BC | 21 / 16 | repeats LDDX while BC is not 0 |
LDPIRX | ED B7 | 21 / 16 | repeats a copy from an 8-byte pattern |
LDWS | ED A5 | 14 | copies (HL) to (DE), then L + 1 and D + 1 |
OUTINB | ED 90 | 16 | sends (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
| Instruction | Opcode | T-states | What it does |
|---|---|---|---|
NEXTREG r,n | ED 91 r n | 20 | writes n to NextReg r |
NEXTREG r,A | ED 92 r | 17 | writes 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 FBas 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 FBandED FEthe SamRam’s meanings: both writeAto the address inHL, one into the add-on’s RAM, the other into its ROM. - CSpect, the Next emulator many Next developers work with, reads
ED 01andED 02asSETBRKandCLRBRK, which set and clear breakpoints over an address range; each is followed by five bytes of operands. sjasmplus assembles them only with its--zxnext=cspectoption. CSpect’sBREAKandEXITlive in the other prefix pages, asFD 00andDD 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.