Skip to Content

Putting It Together: Four Layers, One Colour

Four candidates for every dot, and one winner. The rules that pick it fit on a page. Yet they give the Next layers that can swap places on every line, stencils that cut one picture to the shape of another, colours that add up across layers, and a single Layer 2 colour that can climb above everything else on the screen. Not bad for a page of rules, is it? I think this is the page of the Next’s screen that pays back the most for the least reading.

Why should you care who wins? Because that is what your player sees. Whether your hero walks in front of the hills or behind them, whether the score panel stays readable over a busy backdrop, whether a sprite disappears behind the border: every one of those is decided here, dot by dot. Get the rules right, and the layers work for you. Get them wrong, and you spend an evening wondering where your spaceship went.

Read the big picture chapter first: it introduces the two answers every layer gives for every dot, and the pipeline that carries them. The palettes chapter explains how each layer’s colour and transparency are made. Here we take those answers as given and follow what stage 2 of the pipeline does with them: the step that turns four answers into one colour. The big picture chapter calls stage 2 the referee, and I like the name more each time I use it: four players claim every dot, and it blows the whistle for exactly one of them, by rules it never bends unless the rule book itself says so.

The road ahead

We start with a look at stage 2 as a whole: what goes in, and what comes out. Then come the four reasons a layer can have nothing to show at a dot. Next, the ULA and the tilemap join forces and become one layer, either one above the other or as a stencil that cuts each to the other’s shape. After that comes the heart of the matter, the six orders of the three remaining layers, seen on the machine; if you read only one section, make it that one. Two exceptions follow, the Layer 2 priority bit and sprites over the border. Then blending, where the Next adds colours instead of choosing one, and what that is good for in a real game, which is my favourite part. We finish with what happens when nothing at all is opaque, and gather the referee’s rules up at the end.

The pictures are the Next running the scene of the big picture chapter, with Layer 2 and the tilemap scrolled so that every pair of layers overlaps somewhere. They come from checked tests: the colour of each marked dot is the one the text predicts.

Four answers, one colour

For one dot, stage 2, the referee, gets four colours, one from each layer: the ULA (or LoRes), the tilemap, Layer 2 and the sprites. With each comes the layer’s answer to “am I transparent here?” and a few flags. The ULA says whether its dot is border, the tilemap whether the tile is below the ULA, and Layer 2 whether its palette entry carries the priority bit.

From these it makes one 9-bit colour in two steps. First the ULA and the tilemap are mixed into one layer, called U. Then the three remaining layers, S (sprites), L (Layer 2) and U, are put in order, and the first one that is not transparent wins. Two settings replace the ordering with blending, and when nothing at all is opaque, the fallback colour fills the dot.

ULAtilemapLayer 2spritesmix into Ubelow flag: tile attributebit 0, NextReg $6B bit 0stencil: NextReg $68 bit 0order and winnersix orders of S, L, UNextReg $15 bits 4–2Layer 2 priority bitfrom its palette entryor: blend L with UNextReg $15 = 110, 111source: NextReg $68bits 6–5the border exceptionnothing opaque?the fallback colour,NextReg $4AULSone9-bitcoloureach input: a 9-bit colour, “transparent?” and its flags (border, below, priority)
Stage 2: four layers in, one colour out. The NextRegs in the boxes are the ones each step reads.

One more thing worth seeing, and I find it the quiet strength of the whole design. All the settings stage 2 uses, the order, the stencil and blend bits, the transparency and fallback colours, are taken once for each dot as it enters the pipeline, and they travel with it. So your program, or the Copper, can change them between two dots of the same line. The change takes effect from the next dot on, never half-way through one.

When a layer is transparent

A layer is transparent at a dot for one of four reasons, and the referee does not care which: a player who is not on the pitch is not on the pitch, whatever kept him off it.

LayerSwitched off byIts clip windowIts own test: transparent when
ULA (and LoRes)the ULA Control register, NextReg $68 bit 7the Clip Window ULA register, NextReg $1Aits colour equals the value in the Global Transparency Colour register, NextReg $14
Tilemapthe Tilemap Control register, NextReg $6B bit 7 clearthe Clip Window Tilemap register, NextReg $1Bits dot equals the value in the Tilemap Transparency Index register, NextReg $4C (text mode: its colour equals the value in NextReg $14)
Layer 2port $123B bit 1 clearthe Clip Window Layer 2 register, NextReg $18its colour equals the value in NextReg $14
Spritesthe Sprite and Layers System register, NextReg $15 bit 0 clearthe Clip Window Sprites register, NextReg $19no sprite covers the dot, or its dot equals the value in the Sprite Transparency Index register, NextReg $4B

Two things in this table are easy to miss, and I would underline both. First, the ULA’s colour test applies to its border as much as to its picture: a border whose colour equals the value in NextReg $14 is transparent too. Second, switching the ULA off with bit 7 of NextReg $68 removes it as a layer, but not as a source for blending. Your program can hide the ULA and still blend Layer 2 with it, as you will see in the blending section.

The ULA and the tilemap become one

The ULA and the tilemap are mixed first, into the single layer U that takes part in the ordering. They are a pair in the hardware too: they read bank 5 through the same memory port, and their palettes share one palette memory. The veteran and the set builder, as the big picture chapter calls them, share a house and a wardrobe, so it is no surprise that the referee treats them as one team. A program often uses the tilemap as a text or tile screen in front of the Spectrum picture, or behind it.

Which of the two is in front is decided tile by tile. Bit 0 of a tile’s attribute byte puts the tile below the ULA; bit 0 of the Tilemap Control register, NextReg $6B, overrides every tile and puts the whole tilemap on top. (In 512-tile mode bit 0 of the attribute is the ninth bit of the tile number, so there every tile is below the ULA unless NextReg $6B bit 0 says otherwise.)

The rule is then the same as for any two layers: the upper one shows where it is opaque, the lower one where the upper is transparent, and U is transparent only where both are. I like that the tile decides for itself: one map can put its platforms in front of the Spectrum picture and its sky behind it.

a tile above the ULAtileULAattribute bit 0 = 0a tile below the ULAULAtileattribute bit 0 = 1,or 512-tile modethe tilemap forced on topall tilesULANextReg $6B bit 0 = 1U = the upper of the two where it is opaque, otherwise the lower one;transparent only where both are
The ULA and the tilemap, mixed into U. The tile’s attribute or NextReg $6B decides which is on top.

Stencil mode: each layer cuts the other

Bit 0 of the ULA Control register, NextReg $68, turns the mix into something else: a stencil. With both the ULA and the tilemap switched on, U is opaque only where both are opaque, and its colour is their two colours combined bit by bit with AND. Each layer cuts the other to its own shape: a tilemap of solid colour shows only through the ULA’s ink, or the ULA’s picture only inside the tiles.

ULA101 101 000ANDtile000 111 111=U000 101 000both opaque:the colours, bit by biteither transparent:transparenta stencil: each of the two layers cuts the other to its own shape
Stencil mode: both layers opaque gives their colours ANDed, bit by bit; either transparent gives a transparent U.

The AND also changes the colours. A white dot (all ones) lets the other layer’s colour through unchanged, a black one (all zeros) gives black, and anything between keeps only the bits both share. So a stencil can tint as well as cut. Two jobs for the price of one bit, and if you ask me, the most underused bit of NextReg $68.

Six orders

Three layers are left, S, L and U, and bits 4–2 of the Sprite and Layers System register, NextReg $15, choose how they are stacked. The numbers fit neatly. Three bits make 8 combinations. Three layers can be stacked in 3 × 2 × 1 = 6 orders: any of the three on top, either of the other two in the middle, and the last one at the bottom. So six of the eight values, 000 to 101, are the six orders, and the two left over, 110 and 111, are not orders at all. They are the two blend modes, which get a section of their own below. I always enjoy that bit of arithmetic: the hardware had two values to spare, and it did not waste them.

In each order, stage 2 looks at the layers top first and takes the first that is not transparent:

000: SLU (default)spritesLayer 2ULA + tilemap001: LSULayer 2spritesULA + tilemap010: SULspritesULA + tilemapLayer 2011: LUSLayer 2ULA + tilemapsprites100: USLULA + tilemapspritesLayer 2101: ULSULA + tilemapLayer 2sprites
The six orders of NextReg $15 bits 4–2, top layer first. The machine starts with SLU.

Why six, and not one fixed order? Because games want different things. A game with a Layer 2 backdrop and a Spectrum-style status bar wants the ULA in front of Layer 2 (SUL, USL). A game with a tile playfield and Layer 2 effects on top of it wants Layer 2 in front of the tilemap (SLU, LSU). A game whose sprites walk behind scenery wants the sprites below (LSU, LUS, ULS). And because the order is taken for every dot, your program can change it at a chosen line, with the line interrupt or the Copper, and have different orders in different bands of the screen. My rule of thumb: pick the order by asking which layer must never be hidden, and put it on top.

Let’s see all six on the machine. Four points are marked where two layers overlap: A a sprite on a hill, B the ULA’s score panel on the edge of a hill, C a platform tile on a hill, and D a sprite standing on the ground of tiles.

ABCD000: SLUABCD001: LSUABCD010: SULABCD011: LUSABCD100: USLABCD101: ULS
The same frame in the six orders. Watch the four marked dots: each order picks a different layer at some of them.

Here are the winners at the four dots:

OrderA: sprite / Layer 2B: ULA / Layer 2C: tile / Layer 2D: sprite / tile
SLUspriteLayer 2Layer 2sprite
LSULayer 2Layer 2Layer 2sprite
SULspriteULAtilesprite
LUSLayer 2Layer 2Layer 2tile
USLspriteULAtiletile
ULSLayer 2ULAtiletile

Read a column and you read one pair of layers: at A the sprite wins exactly where S is above L, at D exactly where S is above U. Read a row and you see that an order is nothing more than three such decisions. That’s all there is to it, really. When I cannot remember what an order does, I read its three letters as three duels, and the table writes itself.

The Layer 2 priority bit

The order has two exceptions, and here is the first, the one place where the referee lets a player jump the queue. A Layer 2 palette entry can carry a priority bit (bit 7 of the second write to the 9-bit Palette Value register, NextReg $44, as the palettes chapter explains). A Layer 2 dot whose entry has it, and which is not transparent, goes above every other layer, whatever the order says. It works in every order where Layer 2 is not on top already: SLU, SUL, USL and ULS, and in the blend modes. In LSU and LUS it changes nothing, because Layer 2 is on top anyway.

USLUSL, entry $A8 with the priority bit
The priority bit on the machine, in USL, where Layer 2 is at the bottom. Only the entry with the bit, the hills’ body, rises; the hills’ edge, another entry, stays below.

The bit belongs to the palette entry, not to the dots, and that is what makes it so precise. One colour of a Layer 2 picture can be put in front of the sprites, a window frame or a foreground column, while the rest of the picture stays behind them, without splitting the picture into two layers. I find this one of the neatest tricks in the whole video system: one bit, and the painter, Layer 2, can put part of its picture in front of the troupe without asking anyone.

Sprites over the border

The second exception is about the border, and I am glad it exists. In the three orders where U is above the sprites, LUS, USL and ULS, the ULA would hide every sprite behind its border, because the border is an opaque ULA dot. Stage 2 makes one exception: where U’s dot is the border, the tilemap is transparent and a sprite is opaque, the sprite wins. So sprites that are allowed over the border (bit 1 of the Sprite and Layers System register, NextReg $15) stay visible there in every order. Without it, a game that puts the ULA in front would lose every sprite the moment it reached the frame round the picture, which is exactly where a ship flies in from.

borderULA pictureU above S:over the picture, the ULAcovers the spriteover the border, the spritecovers the border all the same(unless a tile is there)
The border exception: with U above S, the ULA’s picture covers a sprite, but the border does not.

Blending: adding instead of choosing

Two more values of bits 4–2 of the Sprite and Layers System register, NextReg $15, 110 and 111, do not order Layer 2 and U at all: they add their colours. Each channel of the two colours, red, green and blue, levels 0 to 7, is added, and the sum, 0 to 14, is brought back into 0 to 7. Here the referee stops choosing a winner and mixes the two players’ colours instead, which is the most surprising thing it does:

  • 110: add and clamp. A sum above 7 becomes 7. Adding brightens: a light source makes Layer 2 lighter, a black one leaves it as it is.
  • 111: add, then subtract 5. A sum up to 4 becomes 0, a sum of 12 or more becomes 7, and anything between loses 5. With a source at level 5, Layer 2 is unchanged; a lighter source lightens it, a darker one darkens it. One layer can shade another both ways.

Let’s blend one dot, channel by channel: Layer 2 at levels 5, 2 and 0, with a source of 5, 5 and 0.

Layer 25R2G0Bblend source5R5G0B110: add, clamp7R10→77G7→70B0→0111: add, − 55R10→52G7→20B0→0+=
One dot blended, channel by channel: Layer 2 5, 2, 0 with a source of 5, 5, 0. Under each result, the sum and what it becomes. The dashed line is level 7.

What Layer 2 is added to, the blend source, is chosen by bits 6–5 of the ULA Control register, NextReg $68: the ULA, the tilemap, or the two already mixed. Whichever of the ULA and the tilemap is not the source is still drawn on its own, above or below the sprites by the tiles’ below flag. So in the blend modes the order is a fixed one of its own:

Layer 2 + source,if the priority bit is setthe tilemap or the ULA,the upper onespritesthe tilemap or the ULA,the lower oneLayer 2 + source,where Layer 2 is opaqueNextReg $68bits 6–5blendsourcedrawnseparately00the ULAthe tilemap10the ULA/tilemap mixnothing11the tilemapthe ULA01nothing(Layer 2 alone)boththe upper / lower one: by the tiles’below flag
The order of the blend modes, top first, and what NextReg $68 bits 6–5 choose.

Two things follow from this order, and both surprise programmers who expect a blend mode to be “SLU with adding”. Let me warn you about them before they surprise you too:

  • The blend source shows only through Layer 2. It is at the bottom, added to Layer 2, and it has no place in the order of its own. Where Layer 2 is transparent, the source is not shown at all: the dot comes from the layers above or from the fallback colour. With the ULA as the source, the Spectrum screen and even its border disappear wherever Layer 2 has nothing.
  • The tilemap can rise above the sprites while Layer 2 stays below them. With the ULA as the source, a tile in front of the ULA is drawn above the sprites, and the blended Layer 2 underneath both: a split the six orders cannot make, since there the tilemap and the ULA move together.

Where the source itself is transparent, Layer 2 is shown as it is, in both modes. And a Layer 2 dot with the priority bit shows its blended colour on top of everything.

Here is mode 110 on the machine, with the ULA as the source and its screen opened to a yellow paper. Look at the border first; I find it the most instructive part of the picture:

SLU110: Layer 2 + the ULA, clamped
Blend mode 110 on the machine, the ULA opened to a yellow paper over the whole display area. The hills add up to bright yellow; where Layer 2 is transparent the yellow does not show and the fallback blue does, even over the border; the tiles cover the sprites.

What blending is good for

What do you do with it, then? If you ask me, blending is the Next’s way of lighting a picture. Layer 2 holds the detailed picture, 8 bits a dot, and the blend source holds the light: something coarse and cheap to change, laid over it.

  • Light and shadow. Make the ULA the source, with an empty bitmap, and give its attribute cells paper colours. In mode 111 a cell at level 5 in all three channels, the Spectrum’s normal white, leaves the picture under it as it is; BRIGHT white lights it up, and darker colours shade it: a blue cell, levels 0, 0 and 5, darkens red and green by five levels and leaves blue as it is. A torch in a dark cave, a spotlight following the player, the shadow of a cloud: each is a handful of attribute bytes, 768 for the whole screen, instead of a redrawn 48K picture. The tilemap works the same way, with a light level for each 8 × 8 tile.
  • Fades and tints of the whole screen. If every cell of the source shows the same palette entry, changing that one entry recolours everything at once. Raise it step by step in mode 110 and the picture fades to white; lower it in mode 111 and it darkens, until at black every level has dropped by five and only the brightest parts keep a glimmer; give it a little red and the whole picture flushes red, the classic flash when the player is hit. One palette write per frame.
  • Bands of colour. Change the source’s colour between lines, with the line interrupt or, better, the Copper, and the picture gets a sunset tint at the top and deep-water blue at the bottom, while Layer 2 itself is untouched.
  • Glow and haze. Adding light to a picture is what glow, fog and mist look like: a pale source over part of the screen in mode 110 makes it look seen through haze.

The limits come with the format. Each channel has only eight levels, so steps are coarse, and the clamps at 0 and 7 lose detail in the darkest and brightest parts. But for the cost of a few register or attribute writes, the CPU never touching the 48K or 80K of Layer 2, nothing else on the Next comes close. Here is my advice: whenever you are about to redraw a whole picture just to make it darker, reach for blending first.

When nothing is opaque

When every layer is transparent at a dot, in any mode, stage 2 shows the fallback colour, held in the Fallback Colour register, NextReg $4A: an 8-bit colour made into nine bits like a write to the 8-bit Palette Value register, NextReg $41. The machine starts with $E3 there. Programs usually set it to black, or, as the pictures here do, to a sky blue that then needs no layer at all. Free sky, no memory required. I call it the referee’s spare ball: when nobody claims the dot, play goes on with this one, and I would always set it on purpose rather than leave $E3 to chance.

What to take home

Here is the referee’s rule book on one page. Stage 2 takes four colours, one from each layer, with their transparency answers and flags, and makes one 9-bit colour. A layer is transparent at a dot when it is switched off, clipped, or fails its own test: the colour against the value in NextReg $14 for the ULA and Layer 2, the dot against the values in NextRegs $4B and $4C for the sprites and the tilemap.

First the ULA and the tilemap become one layer, U: the tile on top, unless its attribute or 512-tile mode puts it below, and NextReg $6B bit 0 can force every tile on top. In stencil mode, NextReg $68 bit 0, U is opaque only where both are, and its colour is the AND of the two.

Then NextReg $15 bits 4–2 put S, L and U in one of six orders, values 000 to 101, and the first opaque layer wins. The setting is taken for every dot, so it can change in the middle of a frame. Two exceptions bend the order: a Layer 2 palette entry with the priority bit lifts its dots above everything, and in the orders with U above S, sprites still show over the border.

The two values left over, 110 and 111, blend Layer 2 with a source chosen by NextReg $68 bits 6–5, clamped at 7 or minus 5. The source shows only through Layer 2, and the tilemap can rise above the sprites. When nothing at all is opaque, the fallback colour in NextReg $4A fills the dot.

If I had to keep one idea from all this, it is that the settings travel with each dot, so every rule above can change between two dots of a line. Four candidates, one winner, and now you know how the referee thinks.

Last updated on