Raster Effects: Changing the Picture Mid-Frame
Every screen chapter so far made one quiet assumption: the registers stay still while a frame is drawn. Well, they do not have to. The machine draws the picture one dot at a time, and every dot uses the registers as they are at that very moment. Change a palette entry, a scroll offset or the layer order at the right line, and one screen becomes several. So what happens when you refuse to sit still? I have kept this chapter for last on purpose, because I think it is where the Next’s screen stops being a set of modes and becomes an instrument.
You get the effects that make a game look like more than its hardware. A sky with more colours than the palette holds. A playfield that scrolls under a status bar that does not budge. More sprites than the machine has. The CPU can do all of this, if somebody tells it when the beam reaches a line. But the Next also has a small processor whose only job is to do it on time: the Copper, which waits for a position of the beam and then writes a register, as exactly on every frame as the clock allows. The big picture chapter treats the screen as a small theatre company; in that company, I would make the Copper the stage manager. It never appears on stage itself, it does not paint a single dot, but it calls every cue at exactly the same moment, night after night.
This chapter builds on the line numbers and the line interrupt of the frame-and-beam chapter, The Frame and the Beam: Two Counters, Four Timings, and on the pipeline of the overview, The Big Picture: Five Producers, Four Layers, One Dot. Keep them at hand; I will point back at them more than once.
The road ahead
We start with the one idea that the rest grows from: the picture is made at the moment the beam gets there, so the beam is a clock. Then we look at how exactly a write shows, because not every part of the picture takes its settings at every dot. Next comes the CPU’s way of splitting the screen, the line interrupt, and the shimmer that it cannot shake off. That brings us to the Copper, the heart of the chapter: its two instructions, where seven of its WAITs stop on the screen, its memory, how a list ends, its four modes, and why it lands on the same dot every frame. Three effects then put it to work: a sky gradient, a split scroll and thirty-two ships made from eight sprites. Then we close the series by following one dot through the whole machine, and I gather the whole screen up one last time.
The numbers are for 128K/+3 timing at 50 Hz: 311 lines of 456 dots, the CPU at 3.5 MHz. The pictures are the Next running a real Copper program each, checked to show the dots the text describes.
The beam as a clock
The picture is not stored anywhere. Every dot is made at the moment the beam reaches it, from the layers’ memory and from the registers as they are then. So a register written in the middle of a frame changes the picture from where the beam is: the part of the frame drawn before the write keeps the old value, the rest gets the new one. I find it the simplest idea in the whole series, and the one with the longest reach.
That makes the beam a clock, and every position on the screen a time: 456 dots a line, 311 lines a frame, the same every frame. Write a register at the same position in every frame, and you get a steady picture with a band in it. Write it at a slightly different position each time, and the band’s edge flickers. Believe it or not, that is the whole game in two sentences; everything that follows is about hitting the same position every time.
When a write shows
“From where the beam is” means slightly different things for different parts of the picture, because not every setting is read at every dot. Each of the earlier chapters found when its layer takes its settings, and I have collected them here, because this is the table I would pin above my desk:
| Settings | Taken | So a write shows |
|---|---|---|
| Palettes, layer order, the transparency and fallback colours, the ULA off switch, all of Layer 2’s settings, LoRes, the ULA’s clip window | at every dot | from the next dot |
| The ULA’s scroll, its Timex mode and the border colour | at every 8-dot cell (the ULA chapter) | from the next cell |
| The tilemap’s settings | at every tile fetched, a character ahead (the tilemap chapter) | from a cell or two later |
| The sprites’ attributes | as each line is drawn, a line ahead (the sprites chapter) | from a later line |
The line offset, the Vertical Line Count Offset register, NextReg $64 | at line 0 of the display area (the frame and the beam) | at the next frame |
For most effects the first row is all that matters: a palette entry or a scroll value written at the start of a line shows on that whole line. The others matter when a write lands near the edge of what it changes, and they are the reason the sprite effect in Thirty-two sprites from eight waits a few lines before it moves its sprites.
Screen memory is different again, and worth a separate warning: a byte written to the ULA’s screen or to Layer 2 shows when the layer next reads it, which for a byte the beam has already passed is the next frame.
The line interrupt and its jitter
The Next can interrupt the CPU before a line of your program’s choosing: the line interrupt, switched
on by bit 1 of the Line Interrupt Control register, NextReg $22, with the line in bit 0 of
NextReg $22 and the Line Interrupt Value LSB register, NextReg $23. It is raised on the line
before, near the end of its display area, in the line numbers that the Vertical Line Count Offset
register, NextReg $64, sets (the frame-and-beam
chapter’s line interrupt).
Your handler can then write the registers for the next band. That works, but it has a weakness: the CPU answers an interrupt only when the instruction it is running has finished. At 3.5 MHz an instruction takes up to 23 T-states, so the handler starts anywhere from 0 to 22 T-states later than it could, and one T-state is two dots. A split made this way lands up to 44 dots later in one frame than in another (more, if contention or a wait state stretches the instruction), and its edge shimmers like a road in summer. Think of it as an actor who waits for the director to shout his cue, and then first has to finish his sentence:
You can remove most of the jitter by waiting in HALT: a halted CPU runs NOP cycles of 4 T-states and
answers at the end of one, so only 0 to 3 T-states, up to 6 dots, are left. But then your program can do
nothing else while it waits. At 28 MHz the jitter is smaller, but it is still there, and
every split costs the CPU an interrupt and a handler. I am not a fan of making the CPU do this.
Wouldn’t it be nice to hand the whole job to someone who has nothing else to do?
The Copper’s two instructions
The Copper is a co-processor with exactly one job: wait for a position of the beam, then write a NextReg. It runs a list of instructions from its own memory, alongside the CPU, and costs the CPU nothing once it is started. If you read nothing else here, read this section and the four after it. The stage manager knows just two instructions:
- WAIT (bit 15 = 1) waits until the beam is on line n, bits 8–0, at or past the horizontal
position h, bits 14–9. The line is counted in the numbers that the Vertical Line Count Offset
register, NextReg
$64, sets, the same as the line interrupt’s: line 0 is the first line of the display area, or of the wide area with an offset of 32. The position h is matched at dot 8 × h of the display area, so a WAIT can stop at any 8-dot boundary; h = 0 is the first dot of the line’s display area. - MOVE (bit 15 = 0) writes the value in bits 7–0 to the NextReg in bits 14–8. Seven bits
reach NextRegs
$00–$7F: the palettes, the layer settings, the scroll registers, the sprite attributes and the MMU are all there. A MOVE to NextReg$00writes nothing; it is the Copper’s NOP.
That’s it. Two instructions, and no jumps, no loops, no arithmetic. I find it charming how much a machine can do with so little.
Where seven WAITs stop
Let’s put the WAIT to work. Here are seven of them, with the Vertical Line Count Offset register,
NextReg $64, at 0:
| Instruction | Encoded | The Copper goes on when the beam reaches | |
|---|---|---|---|
| 1 | WAIT line 0, h 0 | $8000 | dot 0 of line 0: the display area’s top-left corner |
| 2 | WAIT line 96, h 16 | $A060 | dot 128 of line 96: the middle of the display area |
| 3 | WAIT line 191, h 31 | $BEBF | dot 248 of line 191: the last cell of the last line |
| 4 | WAIT line 200, h 0 | $80C8 | dot 0 of line 200: the bottom border, 9 lines below the display area |
| 5 | WAIT line 300, h 34 | $C52C | dot 272 of line 300: the top border, right of the display area |
| 6 | WAIT line 99, h 51 | $E663 | dot 408 of line 99: the left edge of line 100’s border |
| 7 | WAIT line 278, h 53 | $EB16 | dot 424 of line 278: the top-left corner of the 320 × 256 wide area |
Example 6 needs a word, because it catches almost everyone out, and I would rather you met it here than in your own list. The Copper’s horizontal count starts 12 dots before the display area, and its line number moves on there, at dot −12. So the border to the left of line 100, dots −48 to −13, is still part of line 99 for the Copper: dot 408 of line 99 and dot −48 of line 100 are the same place.
Example 7 reaches the top-left corner of the wide area, the 320 × 256 picture of the tilemap, the sprites and the wide Layer 2 modes. It falls in the same left border, and its WAIT takes three steps to find, and I suggest you follow them once with a pencil:
- Where the corner is. The wide area begins 32 dots left of the display area and 32 lines above it. Line 0 is the display area’s first line, and in a 311-line frame the line 32 before it is line 279. So the corner is dot −32 of line 279.
- Which line the Copper counts it in. Dot −32 is left of dot −12, where the Copper’s line number moves on, so for the Copper it is part of the line above: line 278, at dot −32 + 456 = 424.
- Which h. A WAIT stops at dot 8 × h, and 424 = 8 × 53, so h is 53. The WAIT is line 278,
h 53:
$EB16.
Both numbers depend on the timing. The line follows from the length of the frame, h from the length of
a line: on a 448-dot line, dot −32 is dot 416 of the line above, and h is 52. NextReg $64 at 32 makes
the wide area’s first line the Copper’s line 0, so the corner is then on the last line of the frame,
whatever the timing:
| Timing | Frame | The corner | WAIT, NextReg $64 = 0 | WAIT, NextReg $64 = 32 |
|---|---|---|---|---|
| 128K/+3 | 456 × 311 | dot −32, line 279 | line 278, h 53: $EB16 | line 310, h 53: $EB36 |
| 48K | 448 × 312 | dot −32, line 280 | line 279, h 52: $E917 | line 311, h 52: $E937 |
| Pentagon | 448 × 320 | dot −32, line 288 | line 287, h 52: $E91F | line 319, h 52: $E93F |
And since a 128K line has 456 dots, h = 55, dot 440, is the last position a WAIT can reach; a WAIT with h of 56 or more never matches. On the 448-dot lines of 48K and Pentagon timing the last one is 54.
Uhm, one more thing to keep in mind: a WAIT compares the line only for equality. If the Copper reaches a
WAIT for a line the beam has already passed, it does not shrug and go on; it waits for that line in the
next frame. A WAIT for a line that never comes, such as $FFFF, stops the list, and that is how a list
ends, as Where a list ends explains below.
The Copper’s memory
The Copper has 2K of its own memory, 1,024 instructions, with each instruction’s high byte at the even address. Your program writes it through NextRegs:
- The Copper Address LSB register, NextReg
$61, and bits 2–0 of the Copper Control register, NextReg$62, set the byte address, 0–$7FF. - The Copper Data 16-bit Write register, NextReg
$63, takes an instruction in two writes, the high byte first, and writes both bytes at once on the second. A list can be rewritten while it runs without the Copper ever reading half an instruction. - The Copper Data 8-bit Write register, NextReg
$60, writes one byte at a time, each at once.
Both move the address on after every write. Notice how neat the 16-bit one is: you never have to stop the
Copper just to change a value in its list. My advice is to use NextReg $63 for every list you write,
and keep NextReg $60 for the rare byte you really mean to write alone.
Where a list ends
Here is a surprise, and not one I would have designed: the Copper has no end instruction, and it does not know how long a list is. It
goes on to the next instruction in its memory, and after instruction 1,023 it goes on with
instruction 0. What lies beyond the last instruction your program wrote is whatever the memory held
before: an older list, or zeros, which are MOVE $00, $00, the NOP. A list that just stops being written
would run on through them, wrap round, and start again from instruction 0 at once, wherever the beam
happens to be, instead of at the start of the next frame.
So a list ends itself the only way the Copper allows: with a WAIT that never comes true. A WAIT for
line 511 never matches, because no frame has that many lines, and neither does one with h of 56 or more,
which no line is long enough to reach. $FFFF, WAIT line 511, h 63, is the usual choice. The Copper
then sits on that WAIT, patient as a cat at a mouse hole, until something else moves it on:
- in mode 11, the start of the next frame, which sends it back to instruction 0 (see the four modes below);
- a write to the Copper Control register, NextReg
$62, that changes the mode, from the CPU; - in modes 01 and 10, nothing: the list ran once and the Copper waits for good.
Stopping the Copper itself is a write of 00 to NextReg $62 bits 7–6.
The Copper’s four modes
Bits 7–6 of the Copper Control register, NextReg $62, choose what the Copper does:
- 00, stopped. The Copper does nothing, and keeps its place in the list.
- 01, run from the start. The Copper starts at instruction 0 and runs on through its memory, wrapping from instruction 1,023 back to 0. It never restarts by itself: a list that ends with a WAIT that never comes runs once.
- 10, carry on. The Copper runs on from the instruction where it stopped, and wraps the same way.
- 11, run from the start, every frame. Like 01, but the list also starts again from instruction 0 at
line 0 of every frame, in the line numbers the Vertical Line Count Offset register,
NextReg
$64, sets. It runs once a frame, in step with the beam, whatever the CPU does.
Writing the mode the Copper already has changes nothing. Changing to 01 or 11 always starts from instruction 0; changing to 10 does not.
So which one do you want? My rule of thumb is simple:
- Mode 11 is the one for effects that repeat every frame: the gradient, the split scroll and the sprites below all use it. The CPU starts the list once, and from then on only changes the values in it, for example the scroll value a split’s first MOVE writes.
- Mode 01 suits work that has to happen once. A list without WAITs writes its registers as fast as the Copper can, two clocks a MOVE: 256 palette entries in 128 dots, far faster than the CPU could. A list with WAITs and a final WAIT that never comes makes one change at one place on the screen, for example to switch a layer on at a given line in the frame after a game has loaded its graphics.
- Mode 00 is for the times the list must not run: before a new list is written, or when a game turns its effects off.
- Mode 10 resumes a list that was stopped, from where it stopped: a program that stops the Copper with 00 for a moment, to rewrite instructions further down the list, can let it carry on with the same frame.
Why the Copper is exact
The Copper runs at 28 MHz, four clocks for every dot, and checks its WAIT at every clock. The clock it sees the beam reach the position, it moves on, and a MOVE takes two clocks: half a dot. Its writes go through the same path as the CPU’s NextReg writes, but ahead of them, so the CPU can never delay a Copper write. Nothing in it depends on what the CPU is doing, so a list does the same thing at the same dot in every frame: the Copper’s strips in the figure of The line interrupt and its jitter. No shimmer, no handler, no interrupt. The CPU is free to run the game. That is why I trust the stage manager with every split I care about.
A sky gradient from one palette entry
A palette entry is one colour, but nothing says it has to be the same colour on every line. Here the Copper rewrites ULA palette entry 16, which is both the border colour 0 and paper 0, before every one of the frame’s 311 lines:
The list is 625 instructions: two to point the palette at entry 16, then a WAIT and a MOVE for every line:
MOVE $43, $80 ; index stays put after a write
MOVE $40, 16 ; ULA palette entry 16
WAIT 0, 0 ; the first line of the display area
MOVE $41, colour(0)
WAIT 1, 0
MOVE $41, colour(1)
… ; to line 310, the last line above line 0
WAIT 511, 63 ; stop until the next frame's line 0The ULA’s picture, a skyline in ink colour 0, never changes; only the colour of its paper does. One palette entry, a sunset’s worth of colours, and the CPU did not lift a finger. I think this is the prettiest effect for the least work on the whole machine.
A status bar that stands still
A status bar that stays still while the playfield scrolls is two values of the same register in one
frame: the ULA X Scroll register, NextReg $26, holds the playfield’s scroll for lines 0–151 and 0
for the bar, lines 152–191:
The list is five instructions, run in mode 11:
WAIT 151, 33 ; line 151, dot 264: right border
MOVE $26, 0 ; no scroll: the bar, lines 152-191
WAIT 191, 33 ; line 191, dot 264: after the bar
MOVE $26, 40 ; playfield scroll, for next line 0
WAIT 511, 63 ; stop until the next frameHow it works, one frame at a time:
- At line 0 the scroll register already holds 40, written at the end of the previous frame, so the playfield is drawn scrolled 40 dots to the left.
- At dot 264 of line 151, in the right border just after the playfield’s last dot, the Copper writes 0. Line 152, the bar’s first line, starts with the new value, and so does every line of the bar.
- At dot 264 of line 191, after the bar, it writes 40 again. Nothing more is drawn with it in this frame, but it is there when line 0 of the next one begins.
- The final WAIT never comes true, so the Copper stops there; mode 11 starts the list again at line 0.
Why not simply WAIT for line 0 and line 152 themselves, at h 0? Because the ULA takes its scroll per 8-dot cell, and reads each cell’s scroll a little before it shows the cell (When a write shows). A MOVE made at dot 0 comes too late for the line’s first cell, which would keep the old scroll: an 8-dot notch at the left edge of the bar. Writing in the border of the line before has the whole width of the border to spare. Here is my advice: whenever a split has to be clean from the very first dot, make the write in the border of the line before.
To scroll the playfield, the CPU changes only one byte: the value of the second MOVE, the instruction at
address 3, which it rewrites through the Copper Address LSB, Copper Control and Copper Data 16-bit Write
registers, NextRegs $61, $62 and $63, once a frame. The bar is never
touched. I like how little the CPU has to know: one byte in someone else’s list.
Thirty-two sprites from eight
The sprite engine draws each line from the attribute table as it is when the engine reaches it, a line
ahead of the beam (the sprites chapter’s
two lines, not a screen).
So a member of the troupe can be shown in one band, then moved below the beam and shown again further
down, like an actor who leaves by one door and comes back in another costume. Here the
Copper moves sprites 0–7 three times a frame, through the Sprite Number register, NextReg $34, and
the Sprite Attribute 0, 1 and 2 registers, NextRegs $35–$37:
The list, run in mode 11 with the Vertical Line Count Offset register, NextReg $64, set to 32:
WAIT 0, 0 ; the top of the wide area
MOVE $34, 0 ; select sprite 0
MOVE $35, 8 ; X = 8
MOVE $36, 24 ; Y = 24: band 0
MOVE $37, $00 ; palette offset 0: the grey hull
MOVE $34, 1 ; sprite 1 ...
MOVE $35, 38
MOVE $36, 24
MOVE $37, $00
… ; the same for sprites 2-7
WAIT 44, 0 ; band 0 ended at line 39: free
MOVE $34, 0
MOVE $35, 12 ; X = 12: a little further right
MOVE $36, 80 ; Y = 80: band 1
MOVE $37, $10 ; palette offset 1: the blue hull
… ; sprites 1-7
WAIT 100, 0 ; band 2: Y = 136, palette offset 2
…
WAIT 156, 0 ; band 3: Y = 192, palette offset 3
…
WAIT 511, 63 ; stop until the next frameHow it works:
- One line numbering for both. NextReg
$64= 32 makes the Copper’s line 0 the first line of the wide area, so a Copper line number is a wide-area row, and a wide-area row is a sprite Y. You can then write the WAITs in the sprites’ own coordinates. - Four MOVEs a sprite. NextReg
$34selects the sprite; NextRegs$35,$36and$37then write its X, its Y and its third attribute byte, whose bits 7–4 are the palette offset. The pattern and the “visible” bit, in the fourth byte, were set once by the CPU and never change. Eight sprites take 32 MOVEs, 64 clocks: 16 dots, a small part of one line. - When to move a band. The sprite engine draws each line during the line before it. Band 0 covers lines 24–39, so the engine has finished with its sprites once line 39 has been drawn, and band 1, from line 80, needs them from line 79. Any WAIT from line 40 to line 78 works; this list uses line 44.
- Each frame from the top. Mode 11 restarts the list at line 0, where its first block puts the sprites back into band 0 before the engine reaches line 24.
The table has 128 sprites, so 32 ships do not need this; they are here only to show the trick. You need it when a game wants more sprites on the screen than the table holds, or more on one line than the time budget allows. My advice: reach for it only then, because a list that moves sprites is one more thing to keep in step with your game.
One dot’s way through the machine
This is the last chapter of the screen, and I would like to end it by stepping back. Together, the ten chapters follow one dot’s way through the machine:
| Chapter | What it adds |
|---|---|
| The Big Picture: Five Producers, Four Layers, One Dot | five producers, four layers, one compositor, the pipeline |
| The Frame and the Beam: Two Counters, Four Timings | the counters, the timings, the interrupts, the cables |
| Colours and Palettes: From an 8-Bit Index to a 9-Bit Colour | nine bits of colour, eight palettes, two kinds of transparency |
| The ULA Layer: The Spectrum’s Screen, Extended | the Spectrum’s screen with Timex modes, ULANext, ULA+ and scrolling |
| LoRes and Radastan: Big Pixels in the ULA’s Place | 128 × 96 pixels in the ULA’s place |
| Layer 2: A Byte for Every Dot | a byte per dot, in three resolutions |
| The Tilemap: A Screen of 8 × 8 Tiles | a grid of tiles, mirrored and rotated |
| Sprites: 128 Pictures and Two Line Buffers | 128 sprites, two line buffers, a time budget |
| Putting It Together: Four Layers, One Colour | the six orders, stencils and blending |
| Changing the picture mid-frame | the one you are reading: the beam as a clock, and the Copper |
Each producer reads its own memory and makes a dot for every position of the beam; the compositor, the referee, picks one of the four layers’ dots by the layer order and the transparency rules; the palettes turn indices into colours; and the registers that steer all of it can change between one dot and the next. That last fact is what turns a set of fixed modes into a machine that can show more than any one of them. In the theatre’s terms: the veteran, the understudy, the painter, the set builder and the troupe make the dots, the referee picks one at every position, and the stage manager changes the scenery between the lines. I find it a lovely company to spend ten chapters with.
What to take home
Well, that is the whole screen, end to end, and I hope it feels like one machine now rather than ten. The picture is made dot by dot as the beam reaches it, from the registers as they
are then, so a write in the middle of a frame changes the picture from where the beam is. Most
settings are taken at every dot. The ULA’s scroll, Timex mode and border wait for the next 8-dot cell, the
tilemap’s settings for the next tile it fetches, the sprites’ attributes for a later line, and NextReg
$64 for the next frame.
The line interrupt, NextRegs $22 and $23, lets the CPU split the screen, but the CPU answers only
when its current instruction ends, and that leaves up to 44 dots of jitter at 3.5 MHz.
The Copper has none of it. It runs a list of up to 1,024 instructions from its own 2K: WAIT
for a line and an 8-dot position, MOVE a value to NextReg $00–$7F. It checks its WAIT at every
28 MHz clock and goes ahead of the CPU, so it writes at the same dot every frame. A list ends with a WAIT
that never comes, usually $FFFF; NextReg $63 writes an instruction whole, and mode 11 of NextReg
$62 restarts the list at line 0 of every frame. With it, one palette entry becomes 311 colours, a
status bar stands still under a scrolling playfield, and eight sprites become 32. My parting advice
for your own programs: give every split to the stage manager,
and write in the border of the line before.
And that is the screen of the Next: ten chapters, one dot. Every effect you will ever see on it is that dot, made at the moment the beam gets there, from whatever the registers hold right then. The rest is up to your timing.