ZX Spectrum: How the Screen Is Drawn
Ask anyone who has written machine code for the ZX Spectrum about its screen, and sooner or later they will mention the strange order of its memory. The first 32 bytes are the top line of pixels, as you would expect. The next 32 are not the second line but the ninth. The second line starts 256 bytes further on. Load a picture from tape and you can watch it happen: the image arrives as a venetian blind of thin stripes, one third of the screen at a time.
This looks like a quirk, or a mistake. It is neither. It falls straight out of how the Spectrum’s custom chip, the ULA, reads the screen from memory, and how the memory chips of 1982 liked to be read. This article explains how the ULA draws the picture, and then shows step by step why that design gave the screen memory exactly this shape.
You need to know the Z80 and have some engineering basics. You do not need to know how a television works: How a Television Draws a Picture explains the beam, the scan lines, the sync pulses and how colour travels to the screen, and this article builds on it. If you have read the ZX81 display article, you will find the Spectrum the opposite of its little brother: there, the CPU draws every dot; here, the CPU hardly takes part at all.
How this article is organised
- The frame (Part 1): the timing the ULA follows, line by line and frame by frame.
- The screen in memory (Part 2): the bitmap, the attributes, and the famous order.
- How the ULA draws a line (Part 3): the reads behind every sixteen pixels, the shift register, colours, FLASH and the border.
- Why the memory looks scrambled (Part 4): dynamic RAM, rows and columns, and the one requirement that fixes the order of the address bits. If you read only one part, read this one.
- Sharing the memory (Part 5): contention, the floating bus and the snow.
- The emulator (Part 6): how Klive reproduces all of this, and where to read its code.
The numbers in this article are those of the 48K Spectrum, the original design. The 128K models and their descendants work the same way with slightly different timings; Part 6 lists them. Everything below is also how Klive emulates the machine: its Spectrum cores draw the picture T-state by T-state, as the ULA does.
Part 1: The frame
The ULA (Uncommitted Logic Array) is Sinclair’s custom chip, the Spectrum’s only “video chip”. It generates the sync pulses, reads the screen from memory, turns bytes into coloured dots and sends them to the television, all by itself. The Z80 only writes the screen memory; it does not need to know where the beam is. (Except when it wants to, as the rest of this article shows.)
Three numbers that make it fit
- The ULA runs on a 7 MHz clock and sends out one dot per clock: 2 dots per T-state of the Z80, which runs at half that speed, 3.5 MHz.
- One scan line is 224 T-states: 224 × 0.286 µs = 64 µs, exactly one PAL line.
- A character cell is 8 dots wide, so one cell takes 4 T-states: the 256 dots of a screen line take 128.
One line and one frame
Of the 224 T-states of a line, 128 draw the display area, the 256 × 192 dots that show the screen memory. 24 draw the right border, 48 are spent on the horizontal blanking and the HSYNC pulse, and the last 24 draw the left border of the next line. The border is the frame of a single colour around the display area; on the Spectrum it is wide, 48 dots on either side. (Spectrum BASIC uses the word PAPER for something else: the background colour of a character cell, which Part 2 explains. This article never uses it for the area.)
A frame is 312 lines: 16 lines of vertical sync and blanking, 48 lines of top border, the 192 lines of the display area, and 56 lines of bottom border, the last few of which fall into the blanking again. That is 312 × 224 = 69,888 T-states per frame, 19.97 ms, or 50.08 frames a second.
At the start of every frame the ULA pulls the Z80’s INT line for 32 T-states. This is the
Spectrum’s only regular interrupt, and it is a frame interrupt: it tells the program that the beam
is at the top of the frame, well above the display area. Its first line begins 14,336 T-states
(64 lines) later. A program that waits for the interrupt (HALT) and then updates the screen has
those 64 lines of head start before the beam catches up with it, which is how flicker-free games
are written.
Part 2: The screen in memory
The screen occupies 6,912 bytes at the bottom of the RAM, from $4000:
| Area | Addresses | Size | What it holds |
|---|---|---|---|
| Bitmap | $4000–$57FF | 6,144 bytes | One bit per dot, 256 × 192: 1 is ink, 0 is paper |
| Attributes | $5800–$5AFF | 768 bytes | One byte per 8 × 8 character cell, 32 × 24 cells: the colours |
Each bitmap byte holds eight dots side by side, the leftmost in bit 7. The bitmap says only whether a dot is ink or paper. Which colours ink and paper are comes from the cell’s attribute byte:
bit: 7 6 5 4 3 2 1 0
FLASH BRIGHT PAPER INKINK and PAPER are colours 0–7 (black, blue, red, magenta, green, cyan, yellow, white). BRIGHT makes both of them brighter, and FLASH swaps them every 16 frames, about three times a second.
Why cells and not a colour for every dot? Memory, and the television. A colour of its own for each of the 49,152 dots would take 24K at four bits each, more than the whole RAM of the 16K model, and the ULA could not have read it fast enough. And a PAL television blurs colour far more than brightness anyway (see Colour is blurrier than brightness): sharp dots of one colour on a ground of another were what the screen could show well. The price is the Spectrum’s famous attribute clash: two sprites of different colours in the same 8 × 8 cell have to share its two colours.
The famous order
The strange order of the bitmap is not a whim or a mistake. It is a hardware trick that keeps the ULA fast and cheap: page-mode reads of the dynamic RAM. The ULA reads each bitmap byte and the attribute of its cell as a quick pair from the same row of the memory chips, and that works only if the two addresses share their low bits. That one requirement fixes where every bit of the bitmap address goes. Part 4 explains the trick step by step; this section shows what it does to the addresses.
The attributes are laid out the way you would expect: 32 bytes for each row of cells, row after row.
The attribute of the cell in character row r and column c is at $5800 + 32 × r + c.
The bitmap is not. Number the dot lines 0 to 191 from the top, and write the line number y as eight
bits Y7…Y0 and the byte’s column (0–31) as five bits X4…X0. The bitmap byte’s address is not
$4000 + 32 × y + x. Its bits are shuffled:
Read the bitmap’s address from the left: 010 puts it at $4000; Y7 Y6 say which third of
the screen (the top, middle or bottom 64 lines); Y2 Y1 Y0 say which of the eight lines inside a
character it is; Y5 Y4 Y3 say which of the eight character rows of that third; and X4…X0
the column. So the address counts lines in an unusual order: first along a line, then down the
character rows of the third, and only then down the lines inside a character:
| Address | Line | |
|---|---|---|
$4000 | 0 | the top line of character row 0 |
$4020 | 8 | the top line of character row 1 |
$40E0 | 56 | the top line of character row 7, the last row of the top third |
$4100 | 1 | the second line of character row 0 |
$4700 | 7 | the bottom line of character row 0 |
$47E0 | 63 | the bottom line of the top third |
$4800 | 64 | the first line of the middle third |
$5000 | 128 | the first line of the bottom third |
That is what you see when LOAD "" SCREEN$ fills the bitmap in memory order:
Before Part 4 shows how the trick works, let us look at how the ULA actually reads this memory.
Part 3: How the ULA draws a line
Four reads for sixteen dots
While the beam is over the display area, the ULA needs a bitmap byte and an attribute byte for every 8 dots, which is every 4 T-states. It does not fetch them one at a time. It works in groups of 8 T-states and 16 dots, two character cells, and in each group it makes four reads in quick succession:
- the bitmap byte of the first cell,
- the attribute byte of the first cell,
- the bitmap byte of the second cell,
- the attribute byte of the second cell.
Then it leaves the memory alone until the next group. It reads each pair a little ahead of time and keeps the bytes in latches, so that they are ready when the beam reaches their cells. Across a whole line, that is 64 bytes of bitmap and attribute for 32 cells, all inside the 128 T-states of the display area.
From bytes to coloured dots
The bitmap byte goes into a shift register, which pushes its bits out one at a time at 7 MHz, leftmost first. For each bit, the ULA looks at the cell’s attribute: a 1 sends the INK colour to the video output, a 0 the PAPER colour, both made brighter if BRIGHT is set. The ULA has an internal counter of frames, and when the FLASH bit is set it swaps INK and PAPER for 16 frames out of every 32. The colour itself leaves the ULA as a brightness and two colour-difference levels, which a separate encoder chip turns into the PAL signal, as the television article describes.
Notice what this means: the ULA reads every byte at the moment the beam needs it, not once per frame. If a program changes a byte after the ULA has read it, the change appears only in the next frame; if it changes it before, it appears now. Programs exploit this: by rewriting a cell’s attribute between its scan lines, in step with the beam, a program can give a single 8 × 8 cell eight different pairs of colours (the multicolour and rainbow effects of later games).
The border
Outside the display area, the ULA reads nothing. It sends the border colour instead, which a program
sets with bits 0–2 of an OUT to port $FE. The ULA uses whatever value the port holds at the
moment, so a program that changes the border many times a frame draws stripes on it: the coloured
bars of a tape loading are the ROM’s loader flipping the border colour with every edge of the signal
it reads.
Part 4: Why the memory looks scrambled
Here is the question again: why does the bitmap address count the character rows before the lines inside a character? Part 2 named the answer, page-mode reads of the dynamic RAM. This part shows how they work and why they force the order, starting with the memory chips.
How dynamic RAM is addressed
The Spectrum’s screen lives in the lower 16K of RAM, made of eight 4116 dynamic RAM chips. Each chip holds 16,384 bits, one bit of every byte, and stores them in a grid of 128 rows by 128 columns. Its address needs 14 bits, but to keep the chip small and cheap it has only 7 address pins. So the address goes in two halves, one after the other:
- The row address (7 bits) is put on the pins and latched by a pulse on the RAS (row address strobe) pin. The chip then reads that whole row of 128 bits into an internal buffer. This is the slow part.
- The column address (7 bits) is put on the same pins and latched by a pulse on CAS (column address strobe). The chip picks one bit out of the buffered row and puts it on its output.
Once a row is open, the chip can deliver another bit from the same row with just a new column address and another CAS pulse, without repeating the slow row step. This is page mode, and it makes the second read from the same row much quicker than a fresh read.
On the Spectrum, the ULA drives these chips, and it splits the Z80’s address the simple way: the low 7 bits, A0–A6, are the row; the next seven, A7–A13, are the column. (The Z80’s memory refresh agrees: during refresh it puts the R register on A0–A6, so it steps through the rows, which is what a dynamic RAM needs to keep its contents.)
The requirement: a bitmap byte and its attribute in one row
Now recall the four reads of Part 3: a bitmap byte, then its own attribute, then the next bitmap byte and its attribute. The ULA has to make them all in one short burst and leave the memory free for the CPU the rest of the time. If each bitmap byte and its attribute sit in the same DRAM row, each pair is one row step and two quick column steps, a page-mode pair. That is what the ULA does: open a row, read the bitmap byte, read the attribute, then do the same for the next cell.
So the design has one requirement: a bitmap byte and the attribute of its cell must have the same A0–A6. Follow what that requirement does:
- The attribute’s address is fixed by the cell. The attributes are 32 per row of cells, in order: the attribute address is the character row (Y7…Y3, five bits) followed by the column (X4…X0). Its low byte, A0–A7, is therefore Y5 Y4 Y3 X4 X3 X2 X1 X0.
- The bitmap byte’s low bits must match. For the shared row, the bitmap address must also have X4…X0 in A0–A4 and Y4 Y3 in A5–A6; keeping Y5 in A7 as well makes the whole low byte the same, so the ULA can drive it once for both reads.
- What is left goes on top. The bitmap still needs the three bits the attribute does not have, Y2 Y1 Y0, the line inside the character, and the two highest, Y7 Y6. They can only go above the low byte, in A8–A12.
And there is the famous order. Because an attribute serves all eight lines of its cell, it cannot contain the line-inside-the-character bits, and so they are pushed above the character row in the bitmap address. The “natural” order, with Y2 Y1 Y0 in the low byte, would give a bitmap byte and its attribute different rows, and every pair of reads would need two slow row steps instead of one.
The other side of it is just as neat. Look at the two addresses in the figure of Part 2 again: they are made of the same counter bits, only wired to different address pins, plus a few constant bits. The ULA’s horizontal counter gives X4…X0, its line counter Y7…Y0, and the two addresses are two fixed arrangements of those wires. No adder, no multiplier: in an uncommitted logic array, where every gate was precious, that mattered.
The last free choice, the order of Y7 Y6 and Y2 Y1 Y0 in A8–A12, does not matter to the memory. Sinclair put Y2 Y1 Y0 at the bottom, in A8–A10, and that choice is a gift to programmers, as we see next. The thirds are its side effect: Y7 Y6 on top divide the screen into three blocks of 2K.
The source of this explanation. When I started designing my ZX Spectrum emulator about ten years ago, it frustrated me that I could not understand every detail of how the screen is put together. I read many articles and technical descriptions, but none of them told the whole story. The book that completed the picture and made it clear was Chris Smith’s The ZX Spectrum ULA: How to Design a Microcomputer (2010), which reconstructs the ULA from its silicon. That the ULA reads a bitmap byte and its attribute as a page-mode pair comes from it, and it remains the best reference for the timing detail of everything in this article.
What it means for programs
Sinclair’s choice of A8–A10 for the line inside the character makes the most common move of all, one
line down inside a character, a single instruction: INC H. Printing a character works like
this: point HL at the top line of the cell, then eight times copy a byte of the character’s shape
and INC H. Finding a cell’s attribute from its bitmap address is a few rotations:
; HL = the address of a bitmap byte (010 Y7Y6 Y2Y1Y0 | Y5Y4Y3 X4..X0)
; Out: DE = the address of its attribute (010110 Y7Y6 | Y5Y4Y3 X4..X0)
AttrOf:
ld a,h ; A = 010 Y7Y6 Y2Y1Y0
rrca
rrca
rrca ; A = Y2Y1Y0 010 Y7Y6
and $03 ; A = 000000 Y7Y6
or $58 ; A = 010110 Y7Y6
ld d,a
ld e,l ; the low byte is the same: that is the whole point
retMoving one dot line down across a character boundary is the price. Incrementing H moves down a line; when the line inside the character wraps from 7 back to 0, the carry has gone into Y6 instead of Y3, and the address must be corrected:
; HL = the address of a bitmap byte. Out: HL = the byte one dot line below it.
LineDown:
inc h ; the next line inside the character
ld a,h
and $07 ; did Y2Y1Y0 wrap to 0?
ret nz ; no: done, the common case (7 times out of 8)
ld a,l
add a,$20 ; yes: the next character row (Y5Y4Y3 + 1)
ld l,a
ret c ; it wrapped too: the INC H has already moved us into the next third
ld a,h
sub $08 ; still in the same third: take back the carry INC H put into Y6
ld h,a
retEvery Spectrum game that draws sprites carries a routine like this one, or a table of 192 line addresses to skip it.
Part 5: Sharing the memory
Contention
The ULA and the CPU share the lower 16K, and the ULA cannot wait: if it is late, the picture is
wrong. So when both want that memory at the same time, the ULA wins, and it makes the CPU wait by
stopping the CPU’s clock. The Z80 is frozen for a few T-states, and carries on when the
ULA is done. This is called contention, and the memory from $4000 to $7FFF is the contended
memory.
It happens only while the beam is over the display area, and only in the first 6 T-states of each 8 T-state group, the ULA’s window in the figure of Part 3. A CPU access to contended memory that starts at the first T-state of a group waits 6 T-states, one that starts at the next waits 5, and so on: the well-known sequence 6, 5, 4, 3, 2, 1, 0, 0. The first contended T-state of the frame is 14,335, one before the first line of the display area.
The ULA can stop the clock only on the first T-state of a machine cycle. An instruction fetch waits before it starts, if at all, and never during the refresh that ends it, even when the I register points into contended memory.
The consequences for programs:
- Code in the upper 32K runs at full speed, always. Timing-critical code, such as a tape loader, is kept out of the lower 16K.
- Code or data in the lower 16K slows down while the display area is being drawn, by an amount that depends on exactly when each access falls. A program in the 16K model, which has no other RAM, runs noticeably slower than the same program in the upper RAM of a 48K.
- I/O is contended too. The ULA stops the clock for accesses to its own port (any even port
address), and an I/O access whose address lies in
$4000–$7FFFlooks like a memory access to it.
The floating bus
The ULA reads the memory over the same wires the CPU’s data bus is connected to through resistors.
When the CPU reads a port that no device answers, it gets whatever is on its data bus at that moment.
In the border, that is $FF. Over the display area, it is the byte the ULA is fetching right then: a bitmap
or attribute byte of the cell the beam is about to draw. This is the floating bus, and a few games
use it to find out where the beam is without counting T-states.
The test programs measure it exactly. On the 48K, an IN that samples the bus at T-state 14,338 reads
the bitmap byte of $4000, at 14,339 its attribute, at 14,340 and 14,341 the next cell’s bitmap and
attribute, and then $FF for four T-states, before the next group. So the ULA reads its four bytes
three T-states into its contention window, not at its start. The 128K does the same from 14,364.
Snow. During a refresh cycle the Z80 puts the I and R registers on the address bus. If a program
sets I to a value from $40 to $7F (as some programs do to place an interrupt table), every refresh
cycle looks like an access to the screen memory. When one falls into the ULA’s window, the ULA’s page
mode read picks up the refresh’s row address instead of its own, and reads the wrong byte. The
picture shows it as scattered wrong dots, the Spectrum’s snow. When the refresh meets the ULA’s
first read, the cell’s bitmap and attribute come from the address with R in its low seven bits; when
it meets the second, the second cell is not read at all and shows the first one’s bytes again.
Klive emulates snow on the 16K, 48K and 128K (the +2A/+3 and the clones do not have it). It is on by default; Snow Effect in the machine’s menu, or in Settings › Machine, turns it off for a program that sets I there and should not show it.
Part 6: How Klive follows the beam
A table for every T-state
Klive’s Spectrum cores do what the ULA does, one T-state at a time. When a machine starts, the core builds tables with one entry for every T-state of the frame (69,888 on the 48K). Each entry says what the ULA is doing at that moment: drawing border, drawing dots from the first or the second latched byte, fetching a bitmap or an attribute byte (with its address, worked out by the bit arrangement of Part 2), and how long a CPU access to contended memory starting at that T-state would wait. The tables come from a handful of numbers per machine: the lines of sync, border and display area, the T-states of each part of a line, and the contention sequence.
Drawing late, but at the right moment
The core does not draw every T-state as it happens; that would be slow. It draws lazily: it remembers the last T-state it has drawn, and catches up to the current one only when something that the picture depends on is about to change:
- a write to the screen memory,
- an
OUTthat changes the border colour, - the end of the frame.
Because the catch-up uses the memory and the border as they are before the change, everything the beam has already passed is drawn with the old values and everything after with the new ones. That is what the real beam would have shown, so border stripes, multicolour attributes and beam-racing tricks come out right, at a fraction of the cost of drawing every T-state.
“The beam has already passed” has to be exact to the T-state, and three details make it so:
- The ULA reads later than the tables say. Klive’s tables fetch a byte a few T-states before the
real ULA reads it (4 on the 48K, 2 on the 128K), and they draw it from there. A write is therefore
measured against the real read: a write cycle that starts at 14,334 is in the RAM when the ULA reads
$4000at 14,338, one that asks for 14,335 is held by the contention until 14,341 and is too late. - The border changes once per 8 dots. The ULA takes the border colour once every 4 T-states, so
a change cannot start in the middle of a character’s width. An
OUTthat ends 14,339–14,342 T-states after the interrupt changes the border exactly at the position of$4000’s column (the WoS 48K FAQ; 14,365–14,368 on the 128K), and Klive draws it there. - A prefixed instruction’s accesses fall on their own T-states.
LD (nn),BC,LD A,(IX+d)and the other prefixed instructions fetch their second opcode in a 4 T-state cycle of its own; their memory accesses that follow are contended at the T-states they really happen.
Klive’s display is checked against test programs whose results come from real machines: ZX Spectrum
Timing Tests 48K and 128K, HALT2INT (whose author photographed his own early and late 48Ks),
Float48K, Float128K and FloatSpy, Border Time and Screen Time. They live in the repository
(dev/vendor/spectrum-tests/) and run in Klive’s slow test tier.
ZX Spectrum: How Klive’s Accuracy Is Tested
shows each program’s result in Klive, beside the real machines’ where there is a photo.
Contention is applied from the same tables: whenever the CPU touches contended memory, the core looks up the wait for the current T-state and adds it to the clock. The floating bus reads the byte the table says the ULA is fetching.
Early and late machines
Real Spectrums do not all agree to the T-state. On many of them everything the ULA does comes one
T-state later against the interrupt than the documented figures: contention starts at 14,336 instead
of 14,335, and the reads, the border and the floating bus move with it. These are the late (or
“alternate”) timings; a warm ULA can drift from early to late. Late ULA Timing in the machine’s
menu (or Settings › Machine) switches Klive’s 16K, 48K and 128K to them. An .szx snapshot records
which timing its machine ran, and Klive sets it when it loads one.
The other models
The 128K Spectrum and its successors use the same ULA design with a slightly faster clock and a longer line:
| 48K | 128K, +2 | |
|---|---|---|
| CPU clock | 3.5 MHz | 3.5469 MHz |
| T-states per line | 224 | 228 |
| Lines per frame | 312 | 311 |
| T-states per frame | 69,888 | 70,908 |
| First display line starts at | 14,336 | 14,364 (line 63) |
| First contended T-state | 14,335 | 14,361 |
| Interrupt length | 32 T-states | 35 T-states |
The screen memory, the attributes and the order of the bitmap are the same on every model. The 128K can show a second screen from another RAM bank, which the ULA reads in exactly the same way.
The +2A and +3 replace the ULA with an Amstrad gate array on the 128K’s frame. It reads the
bitmap of $4000 at 14,367, three T-states after the 128K’s ULA, and it stops the CPU only for a
memory access: I/O, port $FE included, is never contended, and neither are an instruction’s
internal T-states. Its contended RAM banks are 4 to 7, its pattern is 1, 0, 7, 6, 5, 4, 3, 2, and it
has no snow and no late timing.
Where to read the code
| What | Where |
|---|---|
| The timing tables, the drawing, contention and the floating bus | src/emu/machines/zxSpectrum/wasm/common/zx-spectrum-ula.c |
The 48K’s screen numbers (Sp48ScreenConfig) | src/emu/machines/zxSpectrum48/wasm/sp48/sp48.c |
| Catching up before a screen-memory write | src/emu/machines/zxSpectrum48/wasm/sp48/sp48-memory.c |
| Catching up before a border change | src/emu/machines/zxSpectrum/wasm/common/zx-spectrum-ports.c |
| The 128K timings | src/emu/machines/zxSpectrum128/sp128Timings.ts |
Summary
- The ULA draws the Spectrum’s picture by itself: 7 MHz dots, 2 per T-state, 224 T-states a line, 312 lines and 69,888 T-states a frame, with a frame interrupt at the top.
- The screen is a 6,144-byte bitmap (ink or paper for every dot) and 768 attribute bytes (the two colours of each 8 × 8 cell).
- Over the display area, the ULA reads a bitmap byte and its attribute for every cell, two cells per 8 T-states, just before the beam needs them.
- The lower 16K is dynamic RAM, addressed by row (A0–A6) and column (A7–A13). Reading a bitmap byte and its attribute as a quick page-mode pair requires them to share their low address bits.
- The attribute cannot contain the line inside the character, so the bitmap address cannot have it in
its low bits either: Y2 Y1 Y0 move up to A8–A10, and the screen memory gets its famous order,
with the thirds as a side effect.
INC Hmoving one line down is the bonus. - When the CPU wants the screen memory while the ULA is reading it, the ULA stops the CPU’s clock: contention, 6, 5, 4, 3, 2, 1, 0, 0.
- Klive follows the beam with per-T-state tables and draws lazily, catching up just before every change the picture could see, against the moment the ULA really reads. Snow and the late timings of many real machines are emulated, and the timing is checked against test programs measured on real machines.