Contended memory

Contended memory is a quirk of the ZX Spectrum's hardware design which means that it is on average slower to access those memory areas which are shared with the ULA than it is to access other memory areas. This occurs because the RAM cannot be read by two devices (the ULA and the processor) at once, and the ULA is given higher priority so it can drawn the screen correctly. Therefore, programs which access this "contended memory" (which is from 0x4000 to 0x7fff on the 48K machine, which is the actual 16k version) or try to read from an IO port where the result is provided by the ULA (any port with the low bit reset on the 48K machine) will be slowed if the ULA is reading the screen. This effect occurs only when the actual screen is being drawn; when the border is being drawn or the TV is in either horizontal or vertical refresh, the ULA does not need to access memory and therefore no delays occur.

General principles
In order for the ULA to be able to access the memory it needs without problems being caused by the Z80 attempting to access the memory at the same time, the ULA arranges for the Z80 to be temporarily paused if the Z80 attempts to access the appropriate memory or IO ports; the exact details of which memory is affected and at which times is given in the Details section below. For memory access, this happens on the first tstate (T1) of any instruction fetch, memory read or memory write operation. For IO operations, this can happen on all tstates; see the Contended IO article for the specifics. The table below gives the pattern of contention that is applied for each opcode, which is essentially equivalent to when T1 operations happen in each instruction.

48K
On the 48K machine, the memory from 0x4000 to 0x7fff is contended. If the contended memory is accessed 14335 or 14336 tstates after an interrupt (see the timing differences section below for information on the 14335/14336 issue), the Z80 will be delayed for 6 tstates. After 14336 tstates, the delay is 5 tstates. The pattern continues as follows:

This pattern (6,5,4,3,2,1,0,0) continues until 14463 tstates after interrupt, at which point there is no delay for 96 tstates while the border and horizontal refresh are drawn. The pattern starts again at 14559 tstates and continues for all 192 lines of screen data. After this, there is no delay until the end of the frame as the bottom border and vertical refresh happen, and no delay until 14335 tstates after the start of the next frame as the top border is drawn.

128K / +2
On the 128K and +2 Spectrums, memory pages 1, 3, 5 and 7 are contended. This means that RAM from 0x4000 to 0x7fff is always contended (as memory page 5 is always mapped in there) and RAM from 0xc000 to 0xffff can be contended if page 1, 3, 5 or 7 is paged in there. The 128K and +2 Spectrums also have a different timing pattern from the 48K machine due to their different line and frame lengths: the 6,5,4,3,2,1,0,0 pattern starts 14361 tstates after interrupt, and repeats every 228 tstates rather than 224.

+2A / +3
The +2A / +3 ULA differs more significantly in that it applies less contention than the 48K or 128K ULAs. Specifically, it applies contention only if the MREQ line is active, whereas the 48K ULA applies it under all circumstances. In the table below, contention patterns which differ on the +3 are shown in italics. The timing pattern also differs significantly:

The pattern repeats until 14990 tstates, when the first scanline has been finished, after which no delays are inserted until 14589 tstates when the pattern begins again.

NTSC Spectrum
The NTSC Spectrum has the same 6,5,4,3,2,1,0,0 contention pattern as the 48K machine, but starting at tstate 8959 rather than 14335.

Examples
Both these refer to a 48K Spectrum.

Example 1: if PC = 25000, HL = 26000, the instruction at address 25000 is LD (HL),A and we're at tstate 14335:


 * Delay for 6 tstates (the contention delay for tstate 14335); now at tstate 14341.
 * 4 tstates fetching the opcode; now at tstate 14345.
 * Delay for 4 tstates (delay for tstate 14345); now at tstate 14349.
 * 3 tstates storing the byte; now at tstate 14352.

The next opcode will then be read at tstate 14352.

Example 2: the same setup as example 1, except with PC=40000 (not contended):


 * No delay because PC is not contended.
 * 4 tstates fetching the opcode; now at cycle 14339.
 * Delay for 2 tstates (for tstate 14339); now at tstate 14341;
 * 3 tstates storing the byte; now at tstate 14344.

Timing differences
It has been observed that on some machines, the timings have been observed to be consistently one tstate later than on other machines. All timings given in this document are for "early timing" machines; for late timing machines, simply add one to add tstate counts given.

The physical reason for this difference is not well understood; in some emulators, the option for changing this behaviour refers to Zilog or clone CPUs, but both behaviours have been seen with both genuine Zilog and clone CPUs.

Instruction breakdown
In this below:


 * dd is any of the registers BC,DE,HL,SP
 * qq is any of the registers BC,DE,HL,AF
 * ss is any of the registers BC,DE,HL
 * ii is either of the index registers IX or IY.
 * ir is the IR (Interrupt and Refresh) register pair
 * cc is any (applicable) condition NZ,Z,NC,C,PO,PE,P,M
 * nn is a 16-bit number
 * n is an 8-bit number
 * b is a number from 0 to 7 (BIT/SET/RES instructions)
 * r and r' are any of the registers A,B,C,D,E,H,L
 * alo is an arithmetic or logical operation: ADD/ADC/SUB/SBC/AND/XOR/OR and CP
 * sro is a shift/rotate operation: RLC/RRC/RL/RR/SLA/SRA/SRL and SLL (undocumented)

Further notes:


 * The values for the registers listed in the table below are relative to the starting value of the register when the instruction is about to be executed.
 * For conditional instructions, entries in [square brackets] are applied only if the condition is met. If the instruction is not conditional (e.g. CALL nn) the entries in [] always apply.
 * The replacement of HL by either IX or IY does not affect the timings, except for the addition of an initial pc:4 for the DD or FD prefix; similarly, a DD or FD prefix on an instruction which does not involve HL just adds an initial pc:4.
 * The undocumented variants of the doubly shifted DDCB and FDCB opcodes have the same timings as the documented versions.
 * In some read-modify-write operations (like INC (HL)), the write operation is always the last one. That may be important to know the exact point in which video is updated, for example. In such instructions that point is annotated for clarity as "(write)" after the address.
 * Access to I/O ports is treated differently to access to memory; full details are given Contended IO. The delays specified there should be applied when an I/O port is accessed; this is designated by "IO" in the table below.