The Frame and the Beam: Two Counters, Four Timings
A 48K game that changes the border colour at an exact T-state, to draw stripes that line up with the picture, works on the Next. So does a 128K demo that does the same on a frame of a different shape, and a Pentagon demo on a third. And the very same machine sends a sharp picture to a modern monitor over HDMI, a standard none of those programs had ever heard of. One machine, four old frames and a new cable. How?
Well, all of it rests on two counters. One counts the dots of a line, the other the lines of a frame. Every event of the picture, the blanking, the sync pulses, the display area, the interrupts, is a moment at which these counters reach a certain value. Change a handful of those values, and the Next becomes a different machine. Why should you care? Because every effect that touches the screen at the right moment, a split border, a raster bar, a status panel that stays still while the playfield scrolls, is timed against these counters. Know them, and you know exactly when your program’s writes reach the screen. I find that the most reassuring thing about the Next’s screen: nothing about its timing is vague.
Everything here builds on the previous chapter, The Big Picture: Five Producers, Four Layers, One Dot, and on ZX Spectrum: How the Screen Is Drawn, whose section on the frame explains the original 48K one. If the beam, blanking and sync are new to you, How a Television Draws a Picture explains them first.
The road ahead
We start with the two counters themselves: what they count, and how every part of the frame is nothing more than a range of their values. That is the key idea, and if you read only one section, make it that one. Then come the four shapes of frame the Next can count, copied from the 48K, the 128K, the +3 and the Pentagon, and what 60 Hz does to them. After that, the interrupts: the frame interrupt the Spectrum always had, the line interrupt the Next adds, and the curious way its lines are numbered. Next, the one clock behind the picture and the CPU, at every CPU speed. Last, we follow the picture out of the machine, as RGB, through the scan doubler as VGA, or as HDMI.
The figures use the 128K/+3 timing at 50 Hz unless they say otherwise, and the frame rates are those of the master clock at exactly 28 MHz (VGA mode 0, or HDMI).
Two counters
The beam’s position is held in two counters. The horizontal counter counts the dots of a line at 7 MHz, one step per dot, from 0 at the start of the line’s blanking to the last dot of the line, 455 in 128K timing, and then starts again. Each time it starts again, the vertical counter counts one more line, from 0 to the last line of the frame, 310, and then the frame starts again. I call the pair the timekeepers: they never do anything but count, and every other part of the screen asks them what time it is.
That is all the timing there is. Really. Everything else is a comparison with these two numbers:
- Blanking: dots 0 to 95 of every line, and lines 0 to 7 of every frame. Nothing is shown there; the beam returns.
- Sync: the horizontal sync pulse on dots 16 to 47, the vertical one on lines 1 to 4, inside the blanking, where a monitor looks for them.
- The display area: dots 136 to 391, lines 64 to 255, the Spectrum’s 256 × 192.
- The wide area: dots 104 to 423, lines 32 to 287, the 320 × 256 of Layer 2’s wide modes, the tilemap and the sprites.
- The border: everything else that is not blanked.
Here is the whole frame drawn to scale, so you can see how much of it is never shown at all:
Look how much room the blanking takes on the left: 96 dots of every line, more than a fifth, go by while the beam flies back. Your program’s time keeps running there all the same, and I think of those dots as free time: the beam shows nothing, so your program can work undisturbed.
Three numbers that make it fit
Three numbers hold the frame together, and if you ask me, they are worth knowing by heart:
- 7 MHz dots. One step of the horizontal counter is one dot of the 256-wide picture, and half a T-state of a 3.5 MHz CPU, exactly as on the Spectrum.
- 456 dots a line, 228 T-states: the 128K’s line. A 48K line is 448 dots, 224 T-states.
- 311 lines a frame: 456 × 311 = 141,816 dots, 70,908 T-states. The 48K’s frame is 312 lines, 69,888 T-states.
Unroll the frame, and the two counters become two strips:
Counters that start early
The producers of the picture do not simply wait for the beam. A dot has to be fetched from memory and looked up in a palette before it can be shown, so each producer works a little ahead of the beam, with counters of its own that start before the area they draw. The ULA’s counter starts 12 dots before the display area, the counter of the wide area 16 dots before it and 2 lines above it. The picture still appears exactly where the main counters say: the head start is used up by the work. I think of a kitchen that starts cooking before you sit down, so the plate arrives exactly when you do.
Your program can change the timing at any moment, but the counters take a new timing only at the end of a frame, when both of them go back to 0. A frame is never half one shape and half another. I like that rule: the timekeepers finish the sentence they started before they change their language.
Four timings, two frame rates
The Next can count four shapes of frame. Bits 6–4 of the Machine Type register, NextReg $03, choose one
(with bit 7 set to allow the change), and the machine type the Next starts with chooses it at power-on:
| Timing | Dots a line | T-states a line at 3.5 MHz | Lines (50 Hz) | T-states a frame | Display area from line | Frame rate at 28 MHz |
|---|---|---|---|---|---|---|
| 48K | 448 | 224 | 312 | 69,888 | 64 | 50.08 Hz |
| 128K/+2 | 456 | 228 | 311 | 70,908 | 64 | 49.36 Hz |
| +2A/+2B/+3 | 456 | 228 | 311 | 70,908 | 64 | 49.36 Hz |
| Pentagon | 448 | 224 | 320 | 71,680 | 80 | 48.83 Hz |
Why copy them so exactly? Because the shape of the frame is a contract with the programs. A program that changes the border at a counted T-state, or rewrites an attribute just before the beam reaches it, is written for one frame. Give it a frame one line longer, and its stripes drift. So the Next does not have one frame that is “close enough”: it has the frame of each machine, to the dot. This is the part of the Next I respect most: compatibility taken literally.
The 128K and the +3 share a frame. What differs is small but real: the +3’s frame interrupt comes one T-state earlier, and the two machines hold up the CPU in different memory banks and in a different pattern (more on that in Dots, ticks and T-states below). The Pentagon, a Russian clone, has a taller frame, wider vertical blanking and no contention at all. Here are all four side by side:
One column of the table deserves a second look: the frame rate. The Next’s dots are exactly 7 MHz, a quarter of its 28 MHz clock. The original 128K ran its dots slightly faster, at about 7.09 MHz, so its frame took 50.02 per second. The same frame on the Next runs at 49.36 Hz, 1.3% slower. Can your program see the difference? No. Every frame has the same number of T-states, every line the same number of dots. Only a stopwatch can tell, and a program carries no stopwatch.
The 60 Hz frame
Bit 2 of the Peripheral 1 Setting register, NextReg $05, switches any timing but the Pentagon to 60 Hz.
Nothing changes along the line. The frame loses lines: 264 instead of 311 or 312, and the display area starts
on line 40 instead of 64. Above and below it, 32 lines of border are left, exactly the extra lines of the wide
area, so at 60 Hz the wide area fills every visible line and there is no border above or below it at all.
The frame rate becomes 58.15 Hz (128K) or 59.19 Hz (48K). My advice: if your program uses the wide area,
look at it at 60 Hz too, because the border it may have counted on above and below is gone.
What does that look like on the screen? Here is the scene of the previous chapter, the same program, at both rates:
The frame interrupt
Once in every frame, at a fixed value of the two counters, the ULA, the veteran of the cast, raises the
frame interrupt, the Spectrum’s 50 times a second. It comes near the top of the frame, well before the display area, so that a
program that waits for it with HALT has time to update the screen before the beam gets there:
- 48K: on line 0, 64 lines before the display area.
- 128K/+2 and +3: on line 1, 63 lines before it (the +3’s one T-state earlier on the line).
- 60 Hz: on line 0, only 40 lines before it. A program that needs all of the 50 Hz head start does not get it at 60 Hz.
- Pentagon: on the very last line of the frame, so its 80 lines of head start begin at the bottom.
These are the moments the original machines used, so the T-states between the interrupt and any dot of the
screen are those of the original; the Spectrum screen chapter lists them. The interrupt line is held for 32
CPU cycles in 48K and +3 timing and 36 in 128K and Pentagon timing. Bit 2 of the Line Interrupt Control
register, NextReg $22, switches the frame interrupt off. I would leave it on unless you have a reason
to switch it off.
The line interrupt
The Next adds a second one: the line interrupt, raised before a line your program chooses. Bit 1 of the
Line Interrupt Control register, NextReg $22, switches it on, and the line number is nine bits, bit 0 of
NextReg $22 and the eight bits of the Line Interrupt Value register, NextReg $23. I call it the alarm
clock: you set it for a line, and it rings a little before that line arrives.
Now, here comes the curious part. The line numbers are counted from the display area, not from the top of the frame: line 0 is the first line of the display area, 191 the last, and the numbers go on through the bottom border to the end of the frame and then continue at the top of the next one. So the lines of the top border have the highest numbers: in 128K timing at 50 Hz, the first line of the frame is 247 and the line just above the display area is 310.
The Active Video Line registers, NextRegs $1E and $1F, read the line the beam is on, in the same numbers:
bit 8 in NextReg $1E, the low eight bits in NextReg $1F. They are two reads, and the beam can move on
between them. Read NextReg $1E at the end of line 255 and NextReg $1F just after the line has become 256,
and the two halves say 0; the other way round they can say 511. So let me give you the safe way: read NextReg
$1E, then $1F, then $1E again, and try again if the two NextReg $1E reads differ. I find this the
one habit worth forming in this whole section.
“Before the line” is meant literally. The interrupt for line n is raised on line n − 1, near the right-hand end of its display area, at its dot 243. From there to the start of line n’s display area the beam still has the right border, the blanking and the left border to draw: a little over 100 T-states at 3.5 MHz, enough for your program to set up a change that must be in place when line n begins. That is the alarm clock’s courtesy: it wakes you early enough to get dressed.
Line numbers for the wide area
Line 0 is the first line of the display area, the ULA’s 256 × 192. That suits the ULA and Layer 2 at 256 × 192, whose rows count from the same line. It does not suit the layers of the wide area. The tilemap, the sprites and Layer 2 in its 320 × 256 and 640 × 256 modes count their rows from the top of the wide area, 32 lines higher. Their row 0 is line 279 in 128K timing at 50 Hz, their row 32 is line 0, and their last 32 rows are lines 192 to 223. A program that wants an interrupt before row r of the wide area has to ask for line r − 32, wrapped round the frame: a different number in every timing, because the frames have different numbers of lines (row 0 is line 280 in 48K timing, 288 in Pentagon timing, and 232 at 60 Hz). Uhm, nobody wants to keep that table in their head.
The Vertical Line Count Offset register, NextReg $64, removes the problem. It is an offset added to the
line numbers: the first line of the display area gets the number the offset says, instead of 0. Set it to
32, and line 0 becomes the first line of the wide area, so the line numbers are exactly the rows of the
320 × 256 layers, in every timing. The offset applies to everything that uses the numbers: the line
interrupt, the active line read through the Active Video Line registers, NextRegs $1E and $1F, and the
Copper. A new offset takes effect when the beam next reaches the first line of the display area, so it can
take up to a frame. If your program works mostly with the wide layers, I would set the offset to 32 once,
early, and forget the other numbering exists.
Splitting the screen with the line interrupt has one weakness, though: the CPU reacts only when its current instruction ends, a few T-states later or earlier from frame to frame. The Copper, which the chapter on changing the picture mid-frame explains, does the same job without that jitter, with the same line numbers. For anything that must line up to the dot, I would reach for the Copper first.
Dots, ticks and T-states
The picture and the CPU run on one clock. The 28 MHz master clock ticks four times for every dot; the
14 MHz half dots of the previous chapter’s pipeline take two ticks each. The CPU’s clock is made from the same
clock, at the speed set by the CPU Speed register, NextReg $07: 3.5, 7, 14 or 28 MHz. So a T-state always
lasts a whole number of ticks, and the CPU and the beam can never drift apart. I think this single
decision saves more trouble than any other in the design.
What changes with the CPU speed is only how much your program can do in a line: 228 T-states in a 128K line at 3.5 MHz, 456 at 7 MHz, 912 at 14 MHz and 1,824 at 28 MHz. The beam does not care. It counts its 7 MHz dots whatever the CPU does, like a metronome that has never heard of the band.
Contention, the ULA holding up the CPU when both want the screen memory, exists only at 3.5 MHz, the
speed the old programs were written for. It follows the pattern of the chosen timing, the 48K’s, the 128K’s
or the +3’s, and the Pentagon has none. At 7 MHz and above the CPU is never held up, and bit 6 of the
Peripheral 3 Setting register, NextReg $08, switches contention off at 3.5 MHz too. How contention works is
explained in the Spectrum screen chapter’s
Sharing the memory; the
Next reproduces it exactly. I like to think of contention as the price of nostalgia, charged only to the
programs that ask for the old speed.
From counters to cables
The video pipeline of the previous chapter puts out a 9-bit colour at 14 MHz, together with the blanking and the two sync signals. Its lines come at about 15.5 kHz (7 MHz divided by 448 or 456 dots), the line rate of a European television. That is fine for a television, and much too slow for a computer monitor. So the Next has three ways out, and I find it remarkable that one machine speaks to all three generations of screen:
RGB at 15 kHz
The first way out is the simplest. With the scan doubler off, that is with bit 0 of the Peripheral 1
Setting register, NextReg $05, clear, the picture leaves exactly as the counters time it: one line per line,
15.5 kHz, 50 frames a second. A television or an old monitor with an RGB (SCART) input shows it the way it
showed a Spectrum. If you still have such a screen, I think this is the most authentic picture the Next
can give you.
The scan doubler and VGA
A VGA monitor wants lines at least twice as fast, about 31 kHz. The scan doubler gets there without changing the frame. It writes each line into a line buffer as the pipeline makes it, at 14 MHz, and reads it out twice, each time at 28 MHz, double speed. Two output lines fit into the time of one input line, so the monitor gets a 31 kHz picture with every line shown twice, one line behind the original. I call the scan doubler the photocopier: it takes each line once and hands out two copies, faster than the original came in.
Twice as many lines, each repeated, look smoother than a television ever did. Do you miss the dark gaps
between the lines of a CRT? Bits 1–0 of the Peripheral 4 Setting register, NextReg $09, darken one of each
pair of lines by 50%, 25% or 12.5%: scanlines. The photocopier, in other words, can be told to print
its second copy a little faint, and I confess I like the 25% setting best.
Some monitors refuse the Next’s 50 Hz frame. For them there are seven VGA modes, chosen in the Video
Timing register, NextReg $11: modes 0 to 6 set the master clock from 28 MHz in steps up to 33 MHz.
Everything is timed by that clock, so everything speeds up together, the CPU, the beam, the frame rate, by up
to 18%, until the monitor accepts it. I find that a pragmatic answer: rather than argue with the monitor,
the whole machine runs a little faster.
HDMI
HDMI is the strictest guest at the party. It does not accept a television’s timing at all: it carries fixed television standards, and the Next uses two, 720 × 576 at 50 Hz and 720 × 480 at 60 Hz. The Next fits its picture into them without changing the frame. It takes a window of 360 × 288 dots around the wide area, 720 pixels of 14 MHz across, and sends every line twice: 576 lines. (At 60 Hz the window is 360 × 240 and the lines become 480.)
The pixels pass through a small buffer of 8,192 pixels, about eleven lines, which is written at 14 MHz and read at the HDMI pixel clock. And here is the trick that keeps it small, and I think it is a lovely one: the HDMI clock is not a free-running 27 MHz. It is made from the Next’s own 28 MHz clock, with a ratio chosen for each timing and frame rate, so that one HDMI frame lasts exactly as long as one Next frame. The two never drift apart, and the buffer never fills or runs dry. The price is a frame rate a little away from the standard one, the Next’s own 49.36 Hz in 128K timing, which monitors accept. HDMI works in VGA modes 0 and 1 only; the higher modes switch it off.
HDMI also carries 8 bits per colour channel, not 3. The Next makes 8 from 3 by repeating them: level 5,
101, becomes 10110110. The palettes chapter’s
From palette to screen
comes back to this.
What to take home
Let me put it all back together. Two counters, the timekeepers, time the whole picture: one counts the 7 MHz dots of a line, the other the lines of a frame. Blanking, sync, the display area, the wide area and the interrupts are all nothing more than ranges or moments of their values.
The Next has the frames of four machines: the 48K (448 dots × 312 lines), the 128K/+2 and +3
(456 × 311) and the Pentagon (448 × 320), chosen by the Machine Type register, NextReg $03, and taken
only at the end of a frame, so
a program that counts T-states finds exactly the frame it was written for. 60 Hz, NextReg $05 bit 2,
shortens the frame to 264 lines, and the wide area then fills every visible line. Because the dots are exactly
7 MHz, the frames run at 50.08, 49.36 and 48.83 Hz: the 128K’s frame is 1.3% slower than on the original,
with the same T-states.
The frame interrupt comes where the originals had it. The line interrupt, the alarm clock,
NextRegs $22 and $23,
comes just before a chosen line, numbered from the first line of the display area, and an offset of 32 in
NextReg $64 numbers the lines from the top of the wide area instead, the rows of the 320 × 256 layers.
Behind it all, one 28 MHz clock ticks four times per dot and drives the CPU at 3.5, 7, 14 or 28 MHz, and
contention exists only at 3.5 MHz.
The picture leaves as RGB at 15 kHz, through the scan doubler, the photocopier, as VGA at 31 kHz, or as HDMI 720 × 576, a 360 × 288 window with every line twice, on a clock made from the Next’s own so that the frames never drift. Two counters, and the whole picture dances to them.