ZX Spectrum Contended Memory
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)(Learn how and when to remove this template message)
Some of the ZX Spectrum's 64 KB addressable memory space is referred to as contended memory. With the rest of the memory, the Z80 microprocessor is the exclusive bus master, so it reads and writes operate at its full bus speed but contended memory space is shared between the ULA and the Z80, with the ULA having higher priority. Contended memory occupies addresses 0x4000..0x7FFF of the Z80 memory map. This is the first 16 KB of RAM in the 48 KB machine and the entire RAM of the 16 KB machine.
Contention occurs as a result of the ULA reading the display and attributes data in this RAM to produce the TV video display. Contention therefore does not occur during horizontal flyback, vertical flyback or during the screen border.
Accesses to I/O also contend if the ULA must read contended memory while the Z80 accesses a ULA I/O port. ULA I/O ports are those with an even address (address bit 0 is a 0).
The Z80 must wait whenever the ULA needs to access contended memory before proceeding with its own contended memory read or write. The effect to the user is that programs that make use of contended memory or contended I/O run slower than those that avoid it.
The ULA must wait for a Z80 read or write to complete before it can stop the Z80's clock and access the memory; it will stop the Z80's clock only during the first clock cycle of a machine cycle.
Early Spectrums make the assumption that at most one contended memory access will occur in a machine cycle: this isn't true in instruction fetch cycles if the programmer has maliciously configured the refresh address to fall within the contended area. In that case the ULA will decline to load its video address and the video byte displayed will be whatever the processor was reading. The consequence of this is random noise on the display, or 'snow'.
Since this effect did not occur under normal operation — it required a programmer's deliberate intervention — it was very seldom seen, being used only in some games as a special graphical effect.
Later versions of the Spectrum, including all 128kb versions, correct the bug.
The ULA does not use the Z80's bus request/grant mechanism to become bus master during its contended memory accesses. Instead, it extends the high period of the Z80 clock input to stop all microprocessor activity. While this is less elegant than performing a bus request, it does gain contended memory access to fewer Z80 clocks. This removes any need for the ULA to read display data in advance, a benefit with the crowded circuitry in the full ULA.
The ULA connects directly to the memory system ICs while the Z80 connects to them through isolating series current-limit resistors. This allows the ULA and memory to drive each other while the Z80 is also driving the memory. Using resistors instead of tristate bus driver ICs reduces the expense of the circuit but does take a higher average I/O pin current from the Z80, ULA and memory ICs.