Skip to Content
Under the HoodEmulator InternalsZX Spectrum Next: how the screen is drawnThe big picture

The Big Picture: Five Producers, Four Layers, One Dot

Load a modern game on a ZX Spectrum Next and look at one frame. A playfield built from tiles scrolls smoothly from right to left. Behind it a background in a few hundred colours scrolls more slowly, for depth. Dozens of characters move over both. At the bottom sits a score panel in the unmistakable Spectrum font and colours. And the same machine, switched to another mode, runs a 1982 48K game exactly as the 48K did, down to the T-state. How can one screen be all of that at once?

On the original Spectrum, one chip produced every dot. On the Next, five producers of dots run side by side, each reading its own memory. They feed four layers: two of the producers, the ULA and LoRes, share one layer and take turns at it. So for every position on the screen there are four candidate dots, and something has to pick one of them, about fourteen million times a second. No pressure. I find the whole arrangement easiest to hold in my head as a small theatre company, and by the end of the cast section you will know each member of it by name.

Why should you care how that choice is made? Because nearly every surprise a Next program gives you on screen is that choice at work. A sprite that vanishes behind the background, a score panel that shows through a hole in Layer 2, a magenta that turns out to be no colour at all: each one is a rule of the machinery you are about to meet. That is why I would read this map before any of the details: know it, and you know where to look when the picture is not the one you meant to draw.

This is the first of the screen chapters. The ones that follow take each piece apart; here you see how the pieces fit. You should know the Spectrum’s own screen first: ZX Spectrum: How the Screen Is Drawn explains the ULA, the bitmap and its attributes, and contention, and the screen chapters re-explain only what the Next changes. How a Television Draws a Picture explains the beam and the lines, and The ZX Spectrum Next Core and the FPGA explains why the Next’s hardware lives in an FPGA, and how Klive’s emulator is checked against it.

The road ahead

We start with what the Next kept from the Spectrum and what it built beside it, then meet the cast: the five producers, the four layers they make, the border and the fallback colour. Next comes the question of where each layer may draw, with the display area, the wide area, the clip windows that fence a layer in and the scroll offsets that move its picture without moving a byte. Then the one idea that carries all the screen chapters: every layer gives exactly two answers for every dot. If you read only one section, make it that one. After that we follow those answers through the video pipeline, stage by stage, and one dot all the way to the screen. We count the clocks the picture runs on and the 512 colours it ends as, look at what stays exactly as it was on the Spectrum, and finish with a walk through the chapters that follow.

The numbers in the screen chapters are for the Next’s 128K/+3 display timing at 50 Hz, with the master clock at exactly 28 MHz (VGA mode 0, or HDMI). The other timings change the size of the frame, not the way it is drawn; the frame chapter lists them. Every fact was checked against the Next’s hardware design.

From one chip to a pipeline

The ZX Spectrum’s picture has a single source. The ULA reads the screen memory, turns bytes into dots and sends them to the television; nothing else in the machine takes part. The Next keeps that ULA, and keeps it compatible to the bit: the same bitmap, the same attributes, the same order of reads, the same contention. Every 48K or 128K program that writes to $4000 sees exactly the screen it expects. I count that as the Next’s first and quietest achievement.

What the Next adds is not a better ULA but more producers beside it. Each is a separate piece of hardware with its own memory and its own idea of what a picture is: a bitmap with a byte per dot, a grid of tiles, a set of sprites. They all run at the same time, in step with the same beam. For each position the beam reaches, they offer four candidate dots, one for each layer: the ULA and LoRes share a layer, and only one of them fills it (the cast, below, says why). A compositor after them picks the winner. I call the compositor the referee: at every dot it looks at the candidates, applies rules it never bends, and names one of them the winner, without caring how any of them got there.

ZX Spectrum 48Kscreen RAM$4000–$5AFFULAtelevisionZX Spectrum Nextbanks 5 and 7block RAMinside the FPGAexternal SRAM16K pattern RAMread at 28 MHzULALoRestilemapLayer 2spritescompositorpalettes,transparency,ordereither/orone dot
One path against five producers, but only four layers: the ULA and LoRes share one input to the compositor, and only one of them fills it. LoRes is drawn dashed because, when it is on, it takes the ULA’s place.

Look at the memories on the left. The producers do not queue for one memory, as the CPU and the ULA do on the Spectrum. The FPGA keeps bank 5 (and the first 8K of bank 7, the 128K’s second screen) in its own block RAM, which has two ports: one is shared by the CPU and LoRes, the other by the ULA and the tilemap. Layer 2 reads the external SRAM, the Next’s main memory, where its bitmap can be large. The sprites keep their shapes in a 16K memory of their own, which the CPU can reach only through I/O ports. It is block RAM inside the FPGA, and a very fast one: the sprite hardware can read a new byte from it at every tick of the 28 MHz master clock, four times for every dot the beam draws. That speed is what lets it draw dozens of sprites into a line in the time the beam takes to draw one (the sprites chapter’s time budget shows how). Giving each producer its own way to its data is what lets them all run at once. If you ask me, that is the real secret of the Next’s screen: not faster memory for everyone, but no queue at all.

The cast of layers

Here are the five producers and the two colours that are not producers, each with what makes it different. Five producers make four layers, because the ULA and LoRes share one. Each has a chapter of its own later; this is the introduction, so you know who is who when the action starts. I give each producer a nickname as well, because I find a character far easier to remember than a register.

sprites128, drawn line by lineLayer 2a byte for every dottilemapa grid of 8 × 8 tilesULA and borderthe Spectrum screentransparenttransparentopaque: it winsthe picture
The layers pulled apart, in their default order. For every position of the beam the compositor looks down through the planes and takes the first dot that is not transparent; here it is a tile, and the ULA below it is never seen at that spot.

The layers are a stack. At every dot, the top layer is shown if it has something there. Where it is transparent, the dot comes from the layer below it; where that one is transparent too, from the next one down, and so on to the bottom of the stack. Only where every layer is transparent does the fallback colour show. That is the referee’s whole method: look down, take the first dot that is really there. Two answers for every dot, further on, explains what makes a layer transparent at a dot.

The order of the stack is not fixed. The Sprite and Layers System register, NextReg $15, picks one of six orders for the sprites, Layer 2 and the ULA (with the tilemap), so a program can put Layer 2 in front of the sprites, or the Spectrum screen in front of everything. The tilemap can sit in front of the ULA or behind it, for the whole map or tile by tile. The figures here use the default order, the one the machine starts with: the sprites on top, then Layer 2, then the tilemap, then the ULA. I like that a single register can reshuffle the whole stage. The decision stage of the pipeline is where the order is applied, and the composition chapter’s six orders shows all of them.

  • The ULA. The Spectrum’s own screen, $4000 in bank 5 or the shadow screen in bank 7, with all of its rules. The Next lets it do more when a program asks: the Timex modes (a colour for every 8 × 1 dots, or 512 dots across), ULANext and ULA+ (more colours from the same attribute byte), hardware scrolling and a clip window. Old software never asks, so it never notices. I call the ULA the veteran: it has been on stage since 1982, and it still plays every old part to the T-state.
  • LoRes. A 128 × 96 screen with a byte, any of 256 colours, for every 2 × 2 dots. It is not a layer of its own: when it is switched on, it replaces the ULA’s picture and takes the ULA’s place in everything that follows, its priority, its clip window, its transparency. The two are never shown together: the ULA layer shows the ULA’s screen or LoRes, and only the border around it stays the ULA’s either way. That makes LoRes the understudy: it steps into the veteran’s place, takes over its part, and the two are never on stage at the same time.
  • Layer 2. A plain bitmap with one byte for every dot, in 256 × 192, 320 × 256 or 640 × 256 (half a byte a dot). No attributes, no clash, any dot any colour, at the price of memory: 48K or 80K, which is why it lives in 16K banks of the main RAM. Layer 2 is the painter, the name Memory, Ports and NextRegs gives its mapping: it paints its backdrop dot by dot, in any colour it likes.
  • The tilemap. A grid of 8 × 8 tiles, 40 or 80 across and 32 down, each tile a reference into a set of tile shapes and a byte saying how to draw it: mirrored, rotated, with which colours, in front of the ULA or behind it. A whole screen costs a few kilobytes. The tilemap is the set builder: it puts its scenery together from a box of ready-made pieces, which is why it is so cheap.
  • The sprites. 128 sprites of 16 × 16 dots, each with its own position, shape, colours, mirroring, rotation and size, which can be linked into bigger objects. The hardware never holds a picture of them: it draws each line of them just before the beam needs it. The sprites are the troupe: a company of performers, each with a costume and a place of its own, each free to wander across the stage.
  • The border. On the Next, the border is not a separate thing: it is what the ULA layer shows outside its 256 × 192 area, in the colour of port $FE. It takes part in the composition like any other ULA dot, which is why sprites and Layer 2 can be drawn over it.
  • The fallback colour. When every layer says “nothing here” for a dot, the picture still needs a colour. The Fallback Colour register, NextReg $4A, holds it.

Here is one scene, the screen of a game in progress, split into what each layer contributes:

ULA and bordertilemapLayer 2spritesno layer: the fallback colourthe picture
One frame, layer by layer, each picture with the other layers switched off. The chequerboard marks where a layer is transparent. The sky belongs to no layer: the ULA’s screen is clipped to the panel at the top, Layer 2 is transparent above its hills, and where all four are transparent the fallback colour shows.

Where each layer can draw

The Spectrum has one rectangle that matters, the 256 × 192 display area, and a border around it. The Next adds a second rectangle: the wide area, 320 × 256 dots, which is the 256 × 192 area with 32 dots added on every side. The wide area covers most of the border a television shows, and it is the canvas of the layers the Next added.

display area256 × 192wide area: 320 × 25632 lines32 dots32 dotsblankingHSYNCborderone line: 456 dots (228 T-states of a 3.5 MHz CPU)one frame:311 linestop 8 lines:verticalblanking andVSYNC
The 128K/+3 frame at 50 Hz, to scale: a 7 MHz dot across is as long as a line down. The grey parts are never seen. The dashed rectangle is the wide area; the border fills the visible frame around it.

Which layer can draw where follows from what each was built to show:

LayerIts canvasCan it cover the border?
ULA (and the border)the whole visible frame: its 256 × 192 picture, and the border colour everywhere elseit is the border
LoRes256 × 192 (its 128 × 96 dots doubled)no
Layer 2256 × 192, or 320 × 256 and 640 × 256 in the wide areain the wide modes, up to the wide area’s edge
Tilemap320 × 256, always the wide areaup to the wide area’s edge
Sprites320 × 256 coordinate spaceonly when “sprites over border” is on (the Sprite and Layers System register, NextReg $15, bit 1); otherwise clipped to 256 × 192

So beyond the wide area, at the far edges of the screen, only the border colour is ever shown: out there the veteran plays alone. A monitor on HDMI shows a window of 360 × 288 dots of this frame: the wide area with 16 dots of border to its left, 24 to its right and 16 lines above and below. I find it worth keeping that window in mind when you place anything near the edge.

Clip windows: a rectangle for each layer

A layer does not have to cover the whole of its canvas. Each layer has a clip window, a rectangle on the screen. Inside it, the layer draws as usual. Outside it, the layer answers “transparent” for every dot, so the dot comes from the layers below it, exactly as if the layer had nothing to show there (Two answers for every dot, further on, explains the answer).

Notice what a clip window is not. It does not change the layer’s memory, and it does not move or shrink the layer’s picture: it is a mask laid over the screen, which hides part of the picture and shows what is underneath. That makes it the cheapest tool on the machine for a whole family of effects: a score panel that one layer must not draw over, a playfield framed by a border, a layer switched off for a band of the screen, a letterbox. One write of four bytes does it, where clearing the same area in memory would take thousands of bytes, and putting it back would take thousands more. I would call that a bargain.

There are four windows, one for each layer (LoRes, which takes the ULA’s place, uses the ULA’s). Each has a register of its own: the Clip Window ULA register, NextReg $1A, the Clip Window Layer 2 register, NextReg $18, the Clip Window Tilemap register, NextReg $1B, and the Clip Window Sprites register, NextReg $19.

WindowNextRegCoordinatesOne X unitAfter reset
ULA and LoResNextReg $1Athe display area1 dot0–255, 0–191
Layer 2NextReg $18the display area in 256 × 192; the wide area in 320 × 256 and 640 × 2561 dot; 2 dots in the wide modes0–255, 0–191
TilemapNextReg $1Bthe wide area2 dots0–159, 0–255
SpritesNextReg $19the display area; with “over border” (the Sprite and Layers System register, NextReg $15, bit 1), the whole wide area, or the window in wide-area units if NextReg $15 bit 5 is set1 dot; 2 dots over the border0–255, 0–191

Each window is four bytes, X1, X2, Y1, Y2, and both edges are inside it: a window of X 0–255 is 256 dots wide. Your program writes all four to the same NextReg, one after the other; a small index inside the machine says which coordinate the next write sets, steps on after every write, and wraps from Y2 back to X1. The Clip Window Control register, NextReg $1C, resets the index of any of the four windows, so you can start from X1 without counting. My advice is to reset the index before every window you write: it costs one write, and it spares you from ever wondering where the index was left.

ULA, LoRes, Layer 2 256 × 192,spritesclip window02550191the display area:1 unit = 1 dotX 0–255, Y 0–191tilemap, Layer 2 320 × 256 and640 × 256, sprites over the borderclip window01590255the wide area:1 unit = 2 dots across, 1 line downX 0–159, Y 0–255
Two coordinate systems for the same four bytes, drawn at the same scale. A clip window is always in screen coordinates: it does not move when the layer’s picture scrolls.

The right half of the figure explains the odd numbers. A coordinate is one byte, 0 to 255, but the wide area is 320 dots across. So the windows that work in the wide area count X in units of two dots: X1 = 10 means dot 20, and X2 = 159 means dots up to 319, the right edge. The Y coordinates stay in lines, and 0–255 covers the wide area’s 256 lines exactly. One thing follows from that, and let me warn you, it surprises programmers: Layer 2’s window starts as 0–255, 0–191 in every mode, which in the 320 × 256 mode covers the full width but only the top 192 of the 256 lines. If your program uses the wide modes, set the window to 0–159, 0–255 first.

Here is a window on the machine itself. The scene from the cast, once more, with Layer 2’s window narrowed:

Layer 2’s window: the whole display areaLayer 2’s window: X 64–191, Y 40–150NextReg $18 written 64, 191, 40, 150
A clip window on the real machine. Nothing in Layer 2’s memory changed: outside its window Layer 2 answers “transparent”, and the dot comes from the layers below it, here the fallback colour and the tiles.

The ULA’s window is special in one way: the border is never clipped. A window that leaves only the top sixteen lines of the ULA’s picture, as in the scene, still leaves the whole border. The veteran lets you fence in its picture, but never its border.

Scrolling without moving memory

Moving a picture on the Spectrum meant moving bytes. To scroll the screen one character to the left, a program had to copy almost all of the 6,912 bytes of the screen. I have a lot of respect for the programmers who made that look smooth.

Scrolling by one dot is harder still. Eight dots share a byte, so no byte can simply be copied: every bitmap byte has to be shifted by one bit, and the bit that falls out of it carried into its neighbour, along every line, in the screen’s scrambled address order. Even with the tightest code, an unrolled RL (HL) and DEC L for each byte, that is 19 T-states for each of the 6,144 bytes: about 117,000 T-states, more than one and a half frames of a 48K, before the contention of the screen memory slows it down further. And that moves only the dots. The colours belong to 8 × 8 cells and can move only a whole character at a time, so the picture tears into ink that has moved and colours that have not. That is why Spectrum games that scroll smoothly scroll a small window, not the whole screen.

Bigger pictures make it worse: to move a 48K Layer 2 picture by copying, a 3.5 MHz CPU would need about fourteen frames. All of this is far too slow for anything that has to move every frame.

So the Next lets the hardware do it. All the producers except the sprites have scroll offsets: registers that say where in its picture the layer starts reading. With an offset of zero, the dot at the top-left of the layer’s area is the first dot of its picture. With an X offset of 96, it is the 97th dot of the first line, and everything shifts 96 dots to the left. One register write moves the whole picture. I think hardware scrolling is the single biggest gift the Next gives a games programmer.

And the picture wraps round. What is pushed off the left edge does not disappear; it comes back in on the right, and what leaves the top comes back at the bottom. The picture behaves like a loop, or like the surface of a doughnut:

the layer’s picture in memorywhat the screen showsDCBAABCDX 96Y 64shown fromthe offset,wrapping atthe edgesboth rectangles are 256 × 192: the ULA’s picture, scrolled by NextRegs $26 and $27
Scrolling moves where the layer starts reading its picture, not where it draws. The red dot, the scroll offset, becomes the screen’s top-left corner, and the picture wraps round: the pieces A to D swap places, and no byte of memory moves.

Wrapping is what makes endless scrolling work. A game that scrolls its playfield to the left draws each new column of graphics into the column of memory that has just scrolled off the left edge, just before it comes back in on the right. The picture never needs to be bigger than the screen.

The offsets have registers of their own: the ULA X Scroll and ULA Y Scroll registers, NextRegs $26 and $27; the LoRes X Scroll and LoRes Y Scroll registers, NextRegs $32 and $33; for Layer 2 the Layer2 X Scroll LSB register, NextReg $16, with bit 0 of the Layer 2 X Scroll MSB register, NextReg $71, and the Layer2 Y Scroll register, NextReg $17; and for the tilemap the Tilemap X Scroll MSB and Tilemap X Scroll LSB registers, NextRegs $2F and $30, and the Tilemap Offset Y register, NextReg $31:

LayerX offsetY offsetThe picture wraps at
ULANextReg $26: 0–255NextReg $27: 0–191256 × 192
LoResNextReg $32: 0–255NextReg $33: 0–191256 × 192, in dots: one step is half a LoRes block
Layer 2NextRegs $16 and $71 bit 0: 0–319 (in 640 × 256, steps of two dots)NextReg $17: 0–191 or 0–255its own size: 256 × 192, or 320 × 256
TilemapNextRegs $2F and $30: 0–319, or 0–639 with 80 columnsNextReg $31: 0–255its own size: 320 or 640 × 256

The sprites have no scroll offset. They do not need one: every member of the troupe has its own position, and moving a sprite is a matter of changing it.

Because each layer has its own offsets, the layers can scroll at different speeds, which is how a game makes a picture look deep: the far-away hills move slowly, the ground in front moves fast. Here is the scene from the cast again, three frames apart:

Layer 2 X 0tilemap X 0Layer 2 X 16tilemap X 48Layer 2 X 32tilemap X 96
Scrolling on the real machine: Layer 2 and the tilemap scroll by their own offsets, so the platforms move three times as fast as the hills, the old trick of parallax. The ULA’s panel and the sprites stay put. In the last frame the left platform has run off the left edge and comes back in on the right.

Scrolling and clip windows work together. The clip window belongs to the screen, the scroll offset to the layer’s picture, so a layer can scroll inside a window that stays put, and a status panel on another layer can stay exactly where it is. The ULA chapter’s scrolling by the dot and the chapters on the other layers give the details of each layer’s offsets; changing them in the middle of a frame, for a screen that is split into bands that scroll separately, is the subject of the raster effects chapter’s split scroll.

Two answers for every dot

Here is the idea all the screen chapters are built on, and I think it is the most elegant one in the Next’s design. For every position of the beam, every layer gives the compositor exactly two answers:

  1. Which colour? As an 8-bit index into a palette, not as a colour. The palette turns it into a colour later.
  2. Am I transparent here? Whether this layer has anything to show at this dot at all.

That is all the compositor, our referee, ever learns about a layer. It does not know about tiles, sprites, attributes or bitmaps. Every rule the later chapters describe, from the Timex modes to sprite anchors, is a way of arriving at these two answers. A few layers add a flag to them: the border says “I am border”, the tilemap says “I am behind the ULA”, and Layer 2’s palette can say “draw me in front of everything”.

ULA / LoResindex: 8 bits, into the ULA palettetransparent if its colour = the value in NextReg $14,or clipped, or the ULA switched offflag: bordertilemapindex: 8 bits, into the tilemap palettetransparent if its dot = the value in NextReg $4C,or clipped, or the tilemap switched offflag: below the ULALayer 2index: 8 bits, into the Layer 2 palettetransparent if its colour = the value in NextReg $14,or clipped, or Layer 2 switched offflag: priority (from its palette entry)spritesindex: 8 bits, into the sprite palettetransparent if its dot = the value in NextReg $4B,or clipped, or no sprite here, or sprites offcompositorsees onlythese answers
What each layer tells the compositor about one dot. The colour tests, against the value in NextReg $14, are made after the palette lookup; the dot tests, against the values in NextRegs $4B and $4C, before it.

Look at the red lines in the figure: there are two kinds of transparency, and which a layer uses says something about it.

  • The ULA, LoRes and Layer 2 are transparent where their dot’s colour, after the palette lookup, equals the global transparency colour, held in the Global Transparency Colour register, NextReg $14. These layers draw whole screens of colours, and “this colour means see-through” is the natural rule for a bitmap.
  • The sprites and the tilemap are transparent where their index, before the lookup, equals the value in the Sprite Transparency Index register, NextReg $4B (sprites), or in the Tilemap Transparency Index register, NextReg $4C (tilemap). These layers draw shapes out of small 4- or 8-bit patterns, and “this value in the pattern is the hole” is the natural rule for a shape. (The tilemap’s text mode, whose tiles have one bit per dot, uses the colour rule instead.)

Both registers start at the same value, $E3, and so does the fallback colour. That is why $E3, a bright magenta, is the colour of “nothing” on the Next, its way of saying “nobody home”: in the default Layer 2 palette, index $E3 is colour $E3, so a Layer 2 bitmap filled with $E3 is invisible from the start.

A layer that is switched off, or clipped by its clip window at this dot, answers “transparent” too. To the compositor there is no difference between “off”, “outside my window” and “a hole in my picture”. I find that a relief: three ways to say “not me”, and the referee treats them all alike.

The video pipeline

The compositor is not one step but a short pipeline, the video pipeline. A dot enters it when the beam’s counters reach its position and leaves it, as a colour, a few clock ticks later. While one dot is in the last stage, the next ones are already in the stages before it, so a new dot comes out at every tick. I like to picture it as an assembly line that never stops. Here is the road from the beam’s counters to the colours, in four stages:

stage 0: gather+0stage 1: look up+1stage 2: decide+1.5stage 3: out+4ULALoRestilemapLayer 2spriteslatched for the dot:NextRegs $15, $68,$14, $4A, $43, $6B,$42, clip windowsULA + tilemappalettes1,024 × 16 bitsLayer 2 + spritepalettes1,024 × 16 bitsread at 28 MHz,the two layersin turntransparent?NextRegs $14, $4B, $4CULA + tilemap → Ubelow flag; NextReg $68order of S, L, UNextReg $15 bits 4–2priority bit, blendingNextReg $15 = 110, 111nothing opaque?the fallback, NextReg $4Adelay,blanking9-bit RGBtimes in 7 MHz periods after the beam’s counters reach the dot
The video pipeline. Stage 1 has two boxes because the Next has two palette memories, read side by side; inside each, two layers take turns. Stage 3 changes nothing: it delays the colour by three more ticks, over which the logic of stage 2 can be spread so that it keeps up with the clock. The colour leaves four 7 MHz periods after it was gathered.

Before we walk through the stages, though, there is one fact about what comes out of the end that changes how you read everything else. I find it the least intuitive fact about the Next’s screen.

Two dots for every position of the beam

So far, a dot has been one step of the beam’s counters, one 7 MHz dot. The pipeline does not produce one colour for it but two: a left half and a right half, each one 14 MHz dot wide, side by side on the screen. Its last stages run fast enough for that: the decision (stage 2) is made at 14 MHz, and the palette lookups before it (stage 1) at 28 MHz, so both halves get their own.

For most layers the two halves are the same. A layer that draws 256 dots across, the ULA’s standard screen, LoRes, Layer 2 in 256 × 192 and 320 × 256, and every sprite, offers the same answers for both halves, and the screen shows its dot twice as wide as a 14 MHz dot. The layers that draw 512 or 640 dots across, the ULA’s Timex hi-res mode, the tilemap’s 80 columns and Layer 2’s 640 × 256, offer a different answer for each half, and then the two halves really differ.

The compositor decides each half on its own, with that half’s answers: the referee holds two contests for every position, not one. So the layers can mix inside one position of the beam: a hi-res tile dot that is opaque in the left half and transparent in the right shows the tile in one half and whatever lies below it in the other.

one position of the beam: one 7 MHz dotleft halfright halfsprites1 dot at 7 MHztransparenttilemap, 80 columns2 dots at 14 MHztile: opaquetile: transparentULA, standard screen1 dot, offered twicepaperpaperon the screenthe tilethe ULA’s papertwo 14 MHz dots,each decidedon its own
One position of the beam, two dots on the screen. A layer of 256 dots across offers the same answers for both halves; a hi-res layer, here the tilemap in 80-column mode, offers two. In the default order the transparent sprite lets the tilemap decide: its left half is opaque and wins, its right half is transparent, so the ULA’s paper shows there.

Stage 0: gather

The beam’s counters reach a position, and each producer offers its two answers for it. In the same tick, the pipeline latches the registers that steer everything after it: the layer order, the transparency and fallback colours, which palettes are shown, which layers are on. They are held for this dot while it travels through the stages. That is how a write to one of these registers in the middle of a line takes effect: from the next dot the beam reaches, and never half-way through one. If you plan raster effects, I would keep this rule closer than any other. A few settings are taken less often, the ULA’s scroll per 8-dot cell, the sprites a line ahead; the raster effects chapter’s when a write shows lists them, and that chapter is all about using this.

Stage 1: look up

The indices go through the palettes. There are eight: a first and a second palette for each of the ULA, the tilemap, Layer 2 and the sprites, 256 entries each. But there are only two memories, each holding four of them: one for the ULA and the tilemap, one for Layer 2 and the sprites. Two numbers describe each memory: 1,024 entries of 16 bits.

Why 1,024 entries? Look at what picks an entry. Its address has ten bits. One says which of the memory’s two layers is looking up: the ULA or the tilemap in one memory, Layer 2 or the sprites in the other. One says which of that layer’s two palettes is shown, the first or the second (the Palette Control register, NextReg $43, chooses for three of the layers, and the Tilemap Control register, NextReg $6B, for the tilemap). The other eight are the layer’s own index. Two layers, two palettes, 256 indices: 2 × 2 × 256 = 1,024, four palettes one after the other in one memory. (Entries, not bytes: an entry is two bytes, so each memory holds 2K.)

Why 16 bits? An entry needs only ten: the 9-bit colour, and a priority bit written with it. Only Layer 2’s entries use that bit: it can lift a Layer 2 dot above everything, and it travels with the colour from here on. Sixteen is simply what the memory is made of. One block of the FPGA’s RAM holds 16 Kbit, and laid out as 1,024 words each word is 16 bits wide: 1,024 × 16 is one whole block, used as it comes, with six bits of every entry spare. Packed four to a block, the eight palettes take two blocks of RAM; eight memories of their own would take far more of a chip that has few to spare.

address10 bitsL9P8I77I66I55I44I33I22I11I00layerpalettethe layer’s 8-bit index2 layers × 2 palettes × 256= 1,024 entriesentry16 bitsP15–1401301201101009R28R17R06G25G14G03B22B11B00priorityLayer 2sparethe colour: RGB3331,024 entries × 16 bits = 16 Kbit: one block of the FPGA’s RAM
What one palette memory holds. The address picks the layer, the palette and the index; the entry holds the colour and, for Layer 2, the priority bit. Ten of the sixteen bits carry meaning; the width is that of the block of RAM the memory is made of.

Sharing has a price in time, and the pipeline pays it with speed. Each memory is read at every tick of the 28 MHz clock, its two layers taking turns: the layer bit of the address says which tick it is. That is two lookups for each 14 MHz dot, one for each layer, so all four layers get their colours for every dot, even in the modes that draw 512 or 640 dots across.

This is why stage 1 is drawn as two boxes in the pipeline figure, not four. The two boxes are not two steps one after the other: they are the two memories, and they work at the same time. The taking of turns happens inside each box, here over the four 28 MHz ticks of one 7 MHz period:

28 MHz tick: 0 1 2 3 memory 1: ULA tilemap ULA tilemap memory 2: Layer 2 sprites Layer 2 sprites

Four boxes would show four memories, each with a lookup of its own. That is a natural design, but not the Next’s: its four lookups are made by two memories, two at a time. Neat, isn’t it? I admire how little hardware this takes.

Stage 2: decide

Now the compositor, the referee, has four colours with their flags, and it applies the rules:

  • Is each layer transparent here? The colour rule (against the value in the Global Transparency Colour register, NextReg $14) is checked now; the index rules were already applied by the sprites and the tilemap themselves.
  • The ULA and the tilemap become one layer, U. Before the main order is applied, these two are mixed: the tilemap goes in front of the ULA, or behind it where a tile asks for it. In stencil mode a dot is shown only where both are opaque.
  • The order. Three layers are left, S (sprites), L (Layer 2) and U, and the Sprite and Layers System register, NextReg $15, picks one of six orders, from SLU (sprites in front, the default) to ULS. The first opaque one, in that order, wins.
  • The exceptions. A Layer 2 dot with the priority bit wins whatever the order. Two further settings of NextReg $15 do not order Layer 2 and U at all but add their colours, channel by channel.
  • Nothing opaque? Then the fallback colour, from the Fallback Colour register, NextReg $4A.

The composition chapter follows each of these rules in full, with the machine’s own pictures. My advice: learn the default order first, and treat everything else as an exception to it.

One dot through the pipeline

Let’s take the dot marked in the exploded figure of the cast, in the default settings, and follow it. I find one worked dot teaches more than any list of rules. Here is the figure again, with what each layer answers at that dot:

spritesstage 0: no sprite here→ transparentLayer 2stage 0: index $E3 (below the hills)stage 1: colour $E3stage 2: matches NextReg $14: transparenttilemapstage 0: a tile dot, index $13stage 1: a greenstage 2: opaque, in front of the ULA→ U is the tile: it winsULA and borderstage 0: paperstage 1: a bluestage 2: behind the tile, not shownorder SLU: the top firsttransparenttransparentopaque: it wins
The dot marked in the exploded figure, followed through the stages. Stage 0 gathers each layer’s answer, stage 1 looks it up in the layer’s palette, stage 2 decides. The ray stops at the first plane that is not transparent.

Step by step:

  1. Stage 0. The sprites have nothing at this position: transparent. Layer 2’s bitmap holds $E3 here, below its band of hills. The tilemap offers a dot of a platform tile, index $13. The ULA offers its paper colour.
  2. Stage 1. Layer 2’s $E3 becomes the colour $E3 (and a ninth bit). The tile’s $13 becomes whatever the tilemap palette holds at $13, a green.
  3. Stage 2. Layer 2’s colour equals the value in the Global Transparency Colour register, NextReg $14: transparent. The tile is not behind the ULA, so U is the tile’s green. The order is SLU: the sprites are transparent, Layer 2 is transparent, U is not. The dot is green.

Everything in the later chapters happens inside one of these three steps.

Three clocks

Three numbers make it all fit, and I would learn these three before any register:

  • 28 MHz is the master clock. Everything in the video pipeline counts in its ticks.
  • 4 ticks per dot. The beam’s counters step at 7 MHz, a quarter of the master clock: one step is one dot of a 256-wide line, the same dot the Spectrum’s ULA sends at 7 MHz. Inside each step a sub-pixel counter counts the four master ticks, and the memories hand out those ticks in turn: the ULA and the tilemap take turns at bank 5, and each palette memory serves its two layers.
  • 2 dots per step for the fine layers. Layers that draw 512 or 640 dots across, the ULA’s Timex hi-res mode, the tilemap’s 80 columns and Layer 2’s 640 mode, run at 14 MHz, two dots per 7 MHz step. Stage 2 decides at 14 MHz too, so the final picture has 14 MHz resolution across, and a 256-wide layer offers each of its dots twice.
28 MHz master clock28.0 MHz (VGA 0, HDMI) … 33 MHz (VGA 6)÷ 2÷ 428 MHzsub-pixel counter:4 slots per dot, for turnsat memories and palettes14 MHzULA dots (512 across in hi-res)tilemap (80 columns)Layer 2 640 × 256stage 27 MHzthe beam’s counters, stage 0spritesLoResLayer 2 256 × 192, 320 × 256CPU clock, picked by NextReg $073.5 MHz (from 7 MHz, held back for contention) · 7 · 14 · 28 MHz
Which part of the picture runs on which clock. The CPU’s clock is not a fourth clock but one of the same three, or the 7 MHz clock with every other tick taken out to make 3.5 MHz.

The VGA modes 1 to 6 (the Video Timing register, NextReg $11) raise the master clock in steps up to 33 MHz, for monitors that do not accept the standard rate. Everything above scales with it: the frame keeps its shape and only gets faster.

The CPU deserves a sentence, and I find this one of the tidiest decisions in the design. At the top level of the design, the CPU’s clock is selected from the video clocks: 28, 14 or 7 MHz as they are, or 3.5 MHz made from the 7 MHz clock by letting only every other tick through. Contention, at 3.5 MHz, is that clock holding back a tick. So the CPU speed changes how much a program can do in a frame, never the picture: the beam’s counters run at 7 MHz whatever the CPU does.

512 colours

Every path through the pipeline ends in the same form: a 9-bit colour, 3 bits each of red, green and blue, RGB333, 512 colours. Why 9? Three bits a channel is what the Next’s video connector puts out: a 3-bit digital-to-analogue converter per channel for VGA and RGB. HDMI carries 8 bits a channel, and the 3 bits are stretched to fill them.

blue 0blue 1blue 2blue 3blue 4blue 5blue 6blue 7In each slice: red 0–7 downward, green 0–7 to the right.one of the ZX Spectrum’s 15 colours, as the Next’s default ULA palette holds them
The Next’s 512 colours, one square each, as eight slices by blue level. The ringed squares are the ZX Spectrum’s 15 colours as the Next’s default ULA palette makes them: each channel off, at level 5, or at level 7 with BRIGHT. Bright magenta is the one exception, a step of green away from its corner.

Fifteen, not sixteen: the Spectrum has 16 colour codes, eight colours with and without BRIGHT, but BRIGHT black is the same black. And one of the fifteen has moved, which I think is the best-hidden detail on the whole screen. Pure bright magenta, red 7, green 0, blue 7, is $E3 in the 8-bit form, the global transparency colour from Two answers for every dot: on the ULA layer every bright magenta dot would be a hole. So the Next’s default palette gives bright magenta one step of green, a shade the eye hardly tells apart and the compositor never mistakes for “nothing”. Sneaky, and rather clever.

The palettes are what connect the 8-bit indices of the two answers to these 9-bit colours, and the 8-bit indices are what keep the layers cheap: a Layer 2 dot is one byte, not two. An 8-bit palette entry can also be written in the short form RRRGGGBB, which leaves out the lowest bit of blue; the palettes chapter explains how that bit is made, and where the eight palettes are selected and written.

What stays the same

All this is added beside the Spectrum, not on top of it. Switch the Next to 48K or 128K timing and leave the new layers off, as an old program does, and what is left is the Spectrum, with the veteran on stage alone:

  • The frame keeps the Spectrum’s shape: 448 dots by 312 lines for the 48K, 456 by 311 for the 128K and +3 (the frame drawn in Where each layer can draw), and 448 by 320 for a Pentagon. A dot is still half a T-state of a 3.5 MHz CPU, so a line still lasts 224 or 228 T-states.
  • The ULA reads the screen the same way: a bitmap byte and an attribute for every 8 dots, in the same order and at the same moments relative to the interrupt.
  • Contention stops the CPU at the same T-states, in the same pattern. It exists only while the CPU runs at 3.5 MHz: at 7, 14 or 28 MHz the CPU is never held back (and bit 6 of the Peripheral 3 Setting register, NextReg $08, switches it off at 3.5 MHz too). That is the price of speed being paid by compatibility: a program that counts T-states against the beam works only at the speed it was written for. I think that is a fair price.
  • The border is still port $FE, changed at the same moments.

The Spectrum screen chapter’s explanation of all this, from the frame to the floating bus, holds for the Next’s ULA. What the Next changes about the ULA is the subject of the ULA chapter.

The screen chapters

Where to from here? A chapter for each plane of the stack, and a few about the picture as a whole:

spritesLayer 2the tilemapthe ULA and LoResthe big picture (this chapter)the frame and the beamcolours and palettesputting the layers togetherchanging the picture mid-framea chapter for each layerthe chapters about the whole picture
The screen chapters as a map: a chapter for each plane of the stack (the ULA’s plane has two, the ULA and LoRes), and five about the picture as a whole, this one among them.

Two chapters lay the ground, and I would read them before all the others. The frame chapter covers the beam’s counters, the timing modes, 50 and 60 Hz, the interrupts, and the road from the counters to VGA and HDMI. The palettes chapter covers RGB333, the eight palettes and how they are written, both kinds of transparency, and the fallback colour.

Then come the layers, one chapter each, in any order you like. The ULA chapter extends the Spectrum’s screen with the Timex modes, ULANext, ULA+, scrolling and clipping. The LoRes chapter explains LoRes and Radastan, the 128 × 96 modes that take the ULA’s place. The Layer 2 chapter explains its three resolutions, and why two of them store their bytes down the screen. The tilemap chapter explains maps, tiles, mirroring and rotation, in front of the ULA or behind it. And the sprites chapter explains their attributes, anchors, the line buffer and its time budget.

After the layer chapters, the composition chapter puts them together: stage 2 in full, with the six orders, stencils and blending. Last comes the raster effects chapter, on changing the picture mid-frame: the Copper and line interrupts. It comes last because it changes the settings of all the others while a frame is drawn, and, I admit, it is my favourite.

What to take home

Here is the company, then, in one place. The Next keeps the Spectrum’s ULA, the veteran, bit-compatible, and adds four producers beside it: LoRes, Layer 2, the tilemap and the sprites. Each reads its own memory, and all of them run at once. Five producers make four layers, because the ULA and LoRes share one: LoRes, the understudy, when it is on, replaces the ULA’s picture, and the border stays the ULA’s either way. The painter (Layer 2), the set builder (the tilemap) and the troupe (the sprites) complete the cast.

The ULA draws the 256 × 192 display area and the border around it; Layer 2’s wide modes, the tilemap and the sprites can draw over the wide area, 320 × 256. A clip window fences each layer in, and scroll offsets move its picture without moving a byte. Where no layer has a dot, the fallback colour from NextReg $4A shows.

For every dot, every layer gives two answers, a colour index and “am I transparent?”, sometimes with a flag, and the compositor sees nothing else. Transparency comes in two kinds: by colour after the palette (NextReg $14, for the ULA, LoRes and Layer 2) and by index before it (NextReg $4B for the sprites, NextReg $4C for the tilemap). All three, and the fallback, start at $E3.

The pipeline gathers the answers and latches the registers (stage 0), looks the indices up in eight palettes held in two time-shared memories of 1,024 entries of 16 bits (stage 1), and decides the winner (stage 2): the ULA and tilemap mixed into one layer, then one of six orders, the priority bit, blending, the fallback. It runs on a 28 MHz master clock; the beam steps at 7 MHz, the fine layers and the decision at 14 MHz, so every 7 MHz dot comes out as two. Every colour ends as RGB333, 512 colours, and the CPU’s clock is picked from the same clocks and never changes the picture.

That is the whole map. The chapters that follow zoom in on one corner of it at a time, and if you keep only one picture from all this, keep the referee: four candidates, two answers each, and one winner, about fourteen million times a second.

Last updated on