Layer 2: A Byte for Every Dot
Layer 2 is the simplest idea on the Next: one byte, one dot, any colour. No attributes, no colour clash, no scrambled addresses. And yet, uhm, it has the most surprising memory layout of all the layers. In two of its three resolutions the bytes do not run across the screen, line after line, as in every other bitmap you have met. They run down it, column after column. Why on earth would anyone build it that way?
Why should you care? Because Layer 2 is where a Next program goes when it wants a picture without compromises: a backdrop painted in many colours, a photo, a drawing program, a screen the CPU can fill dot by dot without ever thinking about cells. If you have ever fought the ULA’s two colours per cell, you know how freeing that is. The big picture calls Layer 2 the painter, and I like the name: it paints its backdrop dot by dot, in any colour it likes, with no grid telling it where one colour must stop. But the freedom costs memory, lots of it, and knowing where those bytes are, and in which order, is what lets your code draw fast.
Read the big picture first (the layers, the wide area, clip windows and scrolling in general), and colours and palettes (how a byte becomes a colour, and when it is transparent). If those are fresh in your mind, you are ready.
The road ahead
We start with the idea itself, one byte for every dot, and what it costs, and then find where those
bytes live: in ordinary 16K banks, with a second, hidden picture beside the one on screen. Then comes
the heart of the matter. Layer 2 has three resolutions, and two of them store their bytes down the
screen; once you see how the hardware turns a position into an address, the odd order turns out to be
the obvious one, so if you read only one section, read that one. After that we squeeze two dots into a
byte at 640 × 256, and look at how your program reaches the picture, through port $123B with four
practical recipes, or through the MMU. Scrolling, clipping and colours follow, and we end with the
question of how Layer 2 reads a byte for every dot without slowing the CPU down.
The pictures show the Next with Layer 2 alone on screen (the ULA switched off, the fallback colour black), at 128K/+3 timing and 50 Hz. Each picture comes from a program checked to draw exactly the dots the text describes.
One byte, one dot
The ULA spends one bit on each dot and one attribute byte on every 8 × 8 cell, so 6,912 bytes paint the whole Spectrum screen, at the price of two colours per cell. Layer 2 spends a whole byte on each dot. The byte is a palette index, so every dot can have any of the 256 colours of the Layer 2 palette, which are any 256 of the Next’s 512. Here is the difference, eight dots at a time:
The price is seven times the memory, and seven times the work for a CPU that redraws the picture. That is why Layer 2 could not simply be one more ULA mode. The ULA’s memory is bank 5, 16K kept inside the FPGA, as the ULA chapter explains; a 48K or 80K picture does not fit there. So Layer 2 is a layer of its own, with its own way into the Next’s main memory. I think that was the right call: the veteran keeps its small, fast room, and the painter gets a studio big enough for its canvases.
Where Layer 2 lives
Layer 2 lives in the Next’s ordinary RAM, counted in 16K banks: bank 5 is the ULA’s screen, bank 0
the Spectrum’s usual top 16K, and a 2 MB Next has 112 of them. Bits 6–0 of the Layer 2 Active RAM
bank register, NextReg $12, name the bank where the picture starts; it continues in the banks
after it. A 256 × 192 picture takes 3 banks (48K), the 320 × 256 and 640 × 256 pictures 5 banks
(80K).
Beside it, the Layer 2 Shadow RAM bank register, NextReg $13, names a second picture: the
shadow. Layer 2 always shows the picture of NextReg $12; the shadow is never shown, and exists
only for the CPU, which can map either one into memory, as you will see in a moment. Your program
draws the next frame into the shadow while the active picture is on screen, then swaps the two
numbers: double buffering, with not a single byte copied. The painter works on the canvas behind
the easel while the audience admires the one in front. I find this one of the friendliest features
of the layer. Here is where the two pictures sit after a reset:
Two details, and the first one is a trap. The defaults, banks 8 and 11, suit the 256 × 192 mode: in
the wide modes the active picture runs into the shadow’s first two banks, so if you want double
buffering there, set NextReg $13 to 13 or beyond. My rule of thumb: the moment you pick a wide mode,
move the shadow. And a picture placed so that it runs past the end of the memory the Next can address
has no dots there: those parts of Layer 2 are transparent.
Three resolutions
Bits 5–4 of the Layer 2 Control register, NextReg $70, choose the resolution:
NextReg $70 bits 5–4 | Resolution | Bits a dot | Where | Memory | Byte order |
|---|---|---|---|---|---|
00 | 256 × 192 | 8 | the display area | 48K, 3 banks | across: line after line |
01 | 320 × 256 | 8 | the wide area | 80K, 5 banks | down: column after column |
10 | 640 × 256 | 4 | the wide area | 80K, 5 banks | down: column after column |
The 256 × 192 mode covers the ULA’s display area exactly. The two wide modes cover the 320 × 256 wide area, the ULA’s display area with 32 dots of border on every side. Look at the last column of the table: two orders. That is the surprise from the opening, and I think it deserves a section of its own.
Why the bytes run down the screen
Watch how the hardware turns a position into an address. In 256 × 192, the line y and the dot x are each a byte, 0–255 (the line only up to 191), and the address of the dot within the picture is the two bytes side by side: y as the high byte, x as the low byte. No arithmetic at all, and the dots run across the screen, because x is the part that counts up first.
$12.Now try the same with 320 dots across. The dot no longer fits in a byte: it needs 9 bits, 0–319. So “line high, dot low” would leave a gap, and the address of line 1 would have to start at 320, which takes a multiplication, y × 320 + x. Hardware hates multiplying in a hurry. So turn the picture on its side, and the problem disappears. I find this the most elegant dodge in the whole screen design. The wide modes have exactly 256 lines, so the line is the coordinate that fills a byte, and the address is again two coordinates side by side: the column x as the high part, the line y as the low byte. The dots now run down the screen, and each column of the picture is one 256-byte page of memory.
So the rule is the same in every mode, and I find it easiest to remember it like this: whichever coordinate is 256 long goes in the low byte. Here it is on the machine, with the same 80K of bytes in both modes, eight colours, each painted over 2,048 bytes in a row of memory:
For your program, the column order has advantages of its own. Moving down a column is INC L, and
moving across is INC H; an 8K memory page holds exactly 32 whole columns. Vertical lines, sprites and
scrolling text that walk down the screen become the cheap direction. On the ULA screen, with its
scrambled lines, moving down was the expensive one, so the tables have turned rather nicely. My advice:
when you design a routine for a wide mode, let it walk down the columns, not across the lines.
640 × 256: two dots in a byte
The third mode doubles the dots across, to 640, each one a 14 MHz dot, half the width of a standard dot: the painter switches to a finer brush. The memory stays at 80K and the order stays column after column: what changes is the byte. It now holds two dots, four bits each, the high nibble on the left:
Why four bits, and not a byte for each of the 640 dots? Because Layer 2 gets one byte for every 7 MHz dot from memory, and that is the same in all three modes (how it fetches them is the subject of the section on fetching, near the end). At 320 across, a byte is a dot. At 640 across, the same byte has to serve two dots, so each gets half of it. The 640 mode is the 320 mode’s memory, read at the same pace, split differently. I like how little this mode costs the hardware: nothing new to fetch, only a byte cut in two.
Four bits give each dot 16 colours, and the palette offset, bits 3–0 of the Layer 2 Control
register, NextReg $70, picks
which 16: it becomes the high four bits of every index, as the
palettes chapter
explains. To my mind, this is the 640 mode’s real job: text. Here it is on the machine: three lines of
an 80-column text, ink nibble $F and paper nibble $0, with two offsets:
The CPU’s view through port $123B
Layer 2 reads its picture from banks the CPU does not see unless it asks to. There are two ways to
ask: port $123B, which this section and the next one are about, and the MMU.
The Layer 2 control port, port $123B, can lay the picture over the bottom of the CPU’s memory.
Memory, Ports and NextRegs pictures this as
the painter’s scaffolding, put up over the bottom of the address space so the CPU can reach the
canvas:
Bits of port $123B | Meaning |
|---|---|
| 7–6 | which part: 00, 01, 10 the first, second or third 16K at $0000–$3FFF; 11 the first 48K at $0000–$BFFF |
| 5 | not used (0) |
| 4 | 0 for this layout; 1 for the bank offset below |
| 3 | 0 maps the active picture (the Layer 2 Active RAM bank register, NextReg $12), 1 the shadow (the Layer 2 Shadow RAM bank register, NextReg $13) |
| 2 | 1: CPU reads come from Layer 2 |
| 1 | 1: Layer 2 is shown (also bit 7 of the Display Control 1 register, NextReg $69) |
| 0 | 1: CPU writes go to Layer 2 |
A write with bit 4 set stores a bank offset in bits 2–0 instead, 0–7 banks, added to the part chosen in bits 7–6. The 48K window shows only three of a wide picture’s five banks; with an offset, any of them can be reached.
$123B: one bank at $0000–$3FFF, or three at $0000–$BFFF. Either can be switched on for reads, for writes, or for both.Four ways to use port $123B
If you ask me, the separate switches for reads and writes are what make the port useful. With
writes only, the CPU still reads, and runs, whatever is normally at those addresses, the ROM at
$0000–$3FFF for a start, while every write there lands in Layer 2. Here are four common uses, each
with the port value that sets it up.
1. Plot a dot, one third at a time (layout 1, writes only, 256 × 192). The three thirds of a 256 × 192 picture are 64 lines each, and bits 7–6 of the line number are the third’s number. So the line’s top two bits go straight into the port, and the rest of the line and the dot become the address, H the line within the third, L the dot:
; Plot x = E, y = D (0-191) in the colour at L2Ink
Plot: ld a,d
and $c0 ; y's bits 7-6: the third
or $03 ; bit 1: shown; bit 0: writes mapped
ld bc,$123b
out (c),a
ld a,d
and $3f ; the line within the third
ld h,a
ld l,e ; the dot
ld a,(L2Ink)
ld (hl),a ; to Layer 2; reads still see ROM
retThe routine can live anywhere, even in the area it draws through, because only its writes are
redirected, and only those below $4000: its stack can be anywhere from $4000 up. I find it
charming that the painter can stand on the very scaffolding it is painting under.
2. Work on the whole picture at once (layout 2, reads and writes, 256 × 192). With port value $C7
the whole 48K picture sits at $0000–$BFFF with no gaps: H is the line and L the dot, and your
program can read a dot, change it and write it back. This replaces one colour with another over the
whole screen:
di ; no ROM handler at $0038 now
ld bc,$123b
ld a,$c7 ; layout 2, reads + writes, shown
out (c),a
ld hl,$0000
Next: ld a,(hl)
cp $e0 ; red...
jr nz,Skip
ld (hl),$1c ; ...becomes green
Skip: inc hl
ld a,h
cp $c0
jr nz,Next
ld a,$02 ; unmap, Layer 2 still shown
out (c),aLet me warn you about this one: with reads mapped, everything the CPU fetches below $C000 comes from
Layer 2, its own instructions included. So the code, its stack and its data must be at $C000 or
above, and interrupts off. Forget that, and the CPU cheerfully runs your picture as a program. I have
nothing against modern art, but not as machine code.
3. Reach all 80K of a wide picture (layout 2 with a bank offset, 320 × 256). In the wide modes the
48K window holds columns 0–191, again with no gaps, but now H is the column and L the line: moving
down a column is INC L. For the columns from 192 on, a write with bit 4 set moves the window: with
bank offset 2 it shows banks 2–4, columns 128–319, and column x is at H = x − 128. This draws the
whole of column 300 in green:
ld bc,$123b
ld a,$c3 ; layout 2, writes mapped, shown
out (c),a
ld a,$12 ; bit 4 set: bank offset 2
out (c),a
ld h,300-128 ; column 300
ld l,0 ; from line 0...
ld b,0
Col: ld (hl),$1c
inc l ; ...down all 256 lines
djnz Col
ld bc,$123b
ld a,$10 ; the offset back to 0
out (c),a
ld a,$02 ; unmap
out (c),aPort value $C3 maps writes only, so this code can run anywhere, below $C000 too: the
CPU’s instruction fetches and other reads are not redirected and still see the normal memory. What
moves is every write below $C000: while the port is set, those go to Layer 2, so the stack, which
CALL, PUSH and an interrupt write to, must be at $C000 or above, and so must any variable the
code updates.
4. Draw off screen, then show (bit 3, writes only). Port value $CB maps the shadow picture,
the Layer 2 Shadow RAM bank register, NextReg $13, names, while the picture of the Layer 2 Active RAM
bank register, NextReg $12, stays on screen, untouched. Your program draws the whole next
frame into the shadow, then swaps the two bank numbers at the frame interrupt, and the new frame
appears at once, never half drawn:
ld bc,$123b
ld a,$cb ; layout 2 + shadow + writes, shown
out (c),a
; ... draw the frame with plain writes to $0000-$BFFF
ld a,$02
out (c),a ; unmap
halt ; frame interrupt: beam past picture
nextreg $12,11 ; the shadow is now shown...
nextreg $13,8 ; ...the old picture the next shadowWith writes mapped, every write below $C000 goes to Layer 2, the CPU’s own pushes included, so the
stack must be at $C000 or above while the port is set. That is the one rule all four recipes share,
and my advice is to make it a habit: whenever port $123B maps anything, keep your stack in the top
16K.
Through the MMU
The other way in is the Next’s MMU, the eight registers from the MMU slot 0 register, NextReg $50,
to the MMU slot 7 register, NextReg $57, which page any 8K page of RAM into any 8K slot of the
CPU’s 64K. A 16K bank b is 8K pages 2b and 2b + 1,
so the default picture, banks 8–12, is pages 16–25. In the wide modes an 8K page is exactly 32 columns,
so your program can page in the columns it is drawing and leave the rest of memory alone. Which way
do I prefer? Port $123B for a quick job at the bottom of memory; the MMU when the painting takes a
while and the rest of the program has to keep running around it.
Scrolling, clipping and colours
Layer 2 scrolls by its own offsets: the Layer2 X Scroll LSB register, NextReg $16, with bit 0 of
the Layer 2 X Scroll MSB register, NextReg $71, as a ninth bit, and the Layer2 Y Scroll register,
NextReg $17. The picture wraps round at its own size:
| Mode | X offset | Wraps across at | Y offset | Wraps down at |
|---|---|---|---|---|
| 256 × 192 | 0–255 dots (NextReg $71 is not used) | 256 | 0–191 | 192 |
| 320 × 256 | 0–319 dots | 320 | 0–255 | 256 |
| 640 × 256 | 0–319, in steps of two dots | 640 | 0–255 | 256 |
Its clip window, the Clip Window Layer 2 register, NextReg $18, counts in the area the mode
covers: in 256 × 192, the display area, one unit a dot; in the wide modes, the wide area, X in units of
two 7 MHz dots (four dots in 640 × 256) so that 0–159 spans the 320 positions, and Y one unit a
line. The clip window starts as 0–255, 0–191, which in a wide mode leaves the bottom 64 lines clipped
until your program writes 0–159, 0–255. Let me warn you, this one catches almost everyone: switch to
320 × 256, forget the clip window, and the bottom of your picture is missing. The
big picture
has the details of both.
Its colours come through the two Layer 2 palettes; the palette offset of the Layer 2 Control register,
NextReg $70, is added to the
high four bits of an 8-bit dot and becomes the high four bits of a 4-bit one. Layer 2 is
transparent where its colour, after the palette, equals the value in the Global Transparency
Colour register, NextReg $14, $E3 after a reset. And a palette entry can carry the priority
bit that lifts its dots above every other layer, as the
composition chapter
shows.
Every one of these settings, and the bank and the resolution, is taken again for every dot, so a change made in the middle of a line, by the CPU or the Copper, shows from the next dot. I love that: the painter will happily change brushes in the middle of a stroke if you ask.
Fetching a byte for every dot
The ULA reads block RAM inside the FPGA. Layer 2 reads the Next’s main memory, an external static RAM shared with the CPU and the DMA. It needs one byte for every 7 MHz dot while it draws, a read about every 140 nanoseconds, and the CPU at 28 MHz wants the same memory too. So how do they not get in each other’s way? I think the answer is one of the quiet triumphs of the design.
The memory is fast enough to be shared by time. It completes one access in every tick of the 28 MHz clock, four accesses per dot, and each tick goes to whoever asks, in a fixed order of priority: the CPU first and Layer 2 second; the DMA, while it holds the bus, takes the CPU’s place (the DMA chapter explains how). I picture it as a revolving door that turns four times for every dot, with the CPU always at the front of the queue:
Layer 2 can afford second place because it is never in a hurry: it asks for each byte a dot ahead of the dot it draws, so a request that waits a tick still arrives in time. The CPU never waits for Layer 2 at all, at any speed; unlike the ULA’s memory on a Spectrum, Layer 2’s has no contention. And the one byte per dot is also what sets the 640 mode’s four bits: that is the answer the section on two dots in a byte promised.
What to take home
Here is the painter’s whole kit, then. Layer 2 is a bitmap with one byte for every dot, a
palette index, kept in
the Next’s main memory: 48K or 80K in 16K banks, starting at the bank in NextReg $12. NextReg $13
names a shadow picture for double buffering, and only NextReg $12’s is ever shown. After a reset
they point at banks 8 and 11, which suits 256 × 192; move the shadow to 13 or beyond for the wide modes.
Bits 5–4 of NextReg $70 choose 256 × 192 over the display area, or 320 × 256 and 640 × 256
over the wide area. The address of a dot is its two coordinates side by side, with the one that
is 256 long in the low byte. That is why the bytes run across in 256 × 192 and down in the wide
modes, one column per 256-byte page. In 640 × 256 a byte holds two 4-bit dots, high nibble
first, because Layer 2 gets one byte per 7 MHz dot in every mode, and the palette offset in bits 3–0 of
NextReg $70 gives those dots their high four bits.
Your program reaches the picture through port $123B, one 16K bank at $0000 or 48K at
$0000–$BFFF, for reads, writes or both, with a bank offset to reach the rest; or through the
MMU, NextRegs $50–$57, 8K at a time. Layer 2 has its own scroll (NextRegs $16, $71 and
$17), wrapping at its own size, and its own clip window (NextReg $18) in the units of its area;
it is transparent where its colour equals the value in NextReg $14.
Its bytes come from the main memory, which makes four accesses in every 7 MHz dot. The CPU gets first pick, Layer 2 needs only one of the four, and so Layer 2 never slows the CPU down. If you keep one thing from all this, keep the rule of the low byte: whichever coordinate is 256 long goes there, and the odd order of the wide modes stops being odd. One byte, one dot, any colour: the rest is up to your brush.