Skip to Content

The Tilemap: A Screen of 8 × 8 Tiles

Have you ever looked closely at a scrolling game? Almost always, the scenery repeats. The same brick, the same patch of grass, the same pipe, over and over. Game consoles were built on exactly that observation: store each little graphic once, and keep the screen as a list of which one goes where. So what does the Next do with the idea, and how small can a full-width picture get?

It matters for time as much as for memory. When the scenery scrolls, the hardware moves the whole picture, and your program only adds what comes into view: a new entry in the list for each cell, instead of every dot of the new graphics. The Next’s tilemap is that idea. A 320 × 256 picture would take 80K as a Layer 2 bitmap; the tilemap draws one from 2.5K: a grid of 40 × 32 small numbers, each naming an 8 × 8 tile to draw there. Each tile can be mirrored, rotated, given its own 16 colours and put above or below the Spectrum’s screen, tile by tile, and the whole grid scrolls by the dot. Bringing a new column of scenery onto the screen means writing 32 map entries, 64 bytes; drawing the same 8-dot column into a 320 × 256 Layer 2 picture means writing 2,048 bytes, 32 times as many. I can hardly think of a better bargain on the whole machine.

The big picture calls the tilemap the set builder, and I think the name fits it perfectly: it puts its scenery together from a box of ready-made pieces, a wall here, a window there, and never paints a single dot of its own. That is exactly why it is so cheap.

Read the big picture and colours and palettes first: the tilemap uses the wide area, the clip windows and the palette offsets they explain.

The road ahead

We start with the bytes: the map that says which tile goes where, the tile definitions that say what each tile looks like, and the small piece of memory, bank 5 and half of bank 7, where both must live. That corner is cramped, so we work out how big each table is, which layouts fit, and one trap that catches a program moving its tables into bank 7. Then we take a map entry apart: its attribute byte, a tile’s 4-bit dots, and the key idea, the eight orientations that mirroring and rotation give every tile for free. After that come text mode, 80 columns, and how a tile decides whether it sits above or below the ULA. We finish with scrolling and clipping, and with how the tilemap fetches its bytes one character ahead of the beam.

The pictures show the Next with the tilemap alone on screen (the ULA switched off, the fallback colour black), at 128K/+3 timing and 50 Hz. Each picture is written by a test that first checks the dots the text describes.

The map and the tiles

The tilemap covers the 320 × 256 wide area with a grid of 40 columns by 32 rows of 8 × 8 cells. What it draws comes from two separate areas of memory, each with a start address of its own:

  1. The map says which tile goes where. It has one entry per cell, row after row, 40 entries to a row: the number of the tile to draw in the cell and, in a second byte, how to draw it (you meet that byte in the attribute byte’s section). The map holds no dots at all.
  2. The tile definitions say what each tile looks like. They hold the tiles’ dots, 32 bytes for each tile, tile 0 first, then tile 1, and so on. They say nothing about where a tile appears.
the ULA’s display areaone cell: 8 × 8 dots,4 bits eachmap entry r × 40 + c:tile number, attribute40 × 32 cells over the wide area(column 14, row 9 highlighted)
The tilemap’s grid: 40 × 32 cells over the whole wide area. Each cell is drawn from two places: its entry in the map, which names a tile, and that tile’s dots in the tile definitions.

In the set builder’s terms, the map is the floor plan and the tile definitions are the box of pieces. The map refers to the tile definitions only by number, so any number of cells can name the same tile. See what that gives you? Change one tile’s 32 bytes, and every cell that shows it changes at once; change one map entry, and only that one cell changes. A whole meadow of grass can sway in the wind from a single 32-byte write. I find that the most delightful consequence of the whole idea.

Bank 5 and bank 7: where the tables can live

Here is the catch: the map and the tile definitions cannot be anywhere in the Next’s memory. The tilemap reads its bytes through the same memory port as the ULA, and that port reaches only one kind of memory: the block RAM inside the FPGA, which holds exactly 24K: all 16K of bank 5, and the first 8K of bank 7. The rest of the Next’s RAM, the main memory outside the FPGA, is out of the tilemap’s reach; that is why its areas are so small. The set builder, in other words, works in a small back room, and I would keep that picture in mind whenever you plan where your tables go. (The ULA chapter’s The same ULA, new memory explains why the screen memory is kept inside the FPGA.)

For the CPU, this block RAM simply is banks 5 and 7. Counted in the 8K pages of the Next’s MMU, bank 5 is pages 10 and 11, and bank 7’s first half is page 14. Whenever the CPU reads or writes one of those three pages, wherever it has paged them in, it uses the block RAM’s other port; there is no copy of these pages in the main memory. Bank 7’s second half, page 15, is ordinary main memory, and the tilemap cannot use it.

block RAM inside the FPGA: 24Kpage 10page 11bank 5: 16K (normally at $4000–$7FFF)page 14bank 7, first 8KCPUpages 10,11, 14port 1ULA+ tilemapport 2outside the FPGA:page 15bank 7, second 8K:main memory,out of reach
The memory the tilemap can use: 24K of block RAM, bank 5 and the first half of bank 7. The CPU reaches the same bytes through the other port.

Each area is placed on its own. The Tilemap Base Address register, NextReg $6E, says where the map starts, and the Tile Definitions Base Address register, NextReg $6F, where the tile definitions start. Both have the same layout:

Bits of NextReg $6E / $6FMeaning
70: the area is in bank 5; 1: in bank 7 (its first 8K)
6not used (0)
5–0where in the bank the area starts, in steps of 256 bytes: 0–63 in bank 5, 0–31 in bank 7

So an area always starts at a multiple of 256 bytes. As the CPU normally sees bank 5 at $4000, a value of $20 puts an area at $6000; with bit 7 set, $80 puts it at the start of bank 7, which is $C000 when port $7FFD pages bank 7 in at the top of memory, or wherever an MMU slot shows page 14. The two areas choose their bank independently: the map can be in bank 7 and the tiles in bank 5, or the other way round. An area that runs past the end of its bank continues from the bank’s start: past 16K in bank 5, past 8K in bank 7.

How big the two tables are

The size of each area is not fixed: it follows from four choices your program makes in the Tilemap Control register, NextReg $6B. Each gets its own section further on; here is only what each does to the memory. I always do this sum before anything else, and I suggest you do the same.

  • A map entry is 2 bytes or 1. Normally an entry is two bytes: the tile number, and an attribute byte that says how to draw the tile (its colours, mirroring, rotation). When every tile can be drawn the same way, a program can drop the attribute byte and use one shared attribute for all tiles; each entry is then 1 byte, the tile number alone (see the attribute byte’s section).
  • The grid is 40 or 80 columns. 40 × 32 is 1,280 cells. In 80-column mode the cells are half as wide and there are 80 × 32 = 2,560 of them, so the map holds twice as many entries (see 40 or 80 columns).
  • A tile is 32 bytes or 8. A normal tile has 8 × 8 dots of 4 bits each, two dots in a byte: 8 rows of 4 bytes, 32 bytes. In text mode a dot is a single bit, as in the Spectrum’s font: 8 rows of 1 byte, 8 bytes a tile (see Text mode).
  • There are 256 tiles or 512. A tile number is a byte, 0–255; in 512-tile mode the attribute lends a ninth bit, and the tile definitions can hold twice as many tiles (see the attribute byte’s section).

The size of each area is then a product:

AreaOne itemHow manyTotal
The mapan entry: 2 bytes, or 1a cell: 1,280, or 2,560 in 80 columns1,280 to 5,120 bytes
The tile definitionsa tile: 32 bytes, or 8 in text mode256 tiles, or 5122,048 to 16,384 bytes

That has to fit in bank 5, which is only 16K, and the ULA’s screen already uses 6.75K of it:

the ULA’sscreen6,912map: 40 × 32,1-byte entries1,280map: 40 × 32,2-byte entries2,560map: 80 × 32,2-byte entries5,120256 text-modetiles, 8 bytes2,048256 tiles,32 bytes each8,192512 tiles,32 bytes each16,384each bar to scale, in bytes16K: all of bank 5
The tilemap’s tables against bank 5. A full map and 256 tiles do not fit in bank 5 beside the ULA’s screen.

The sums are tight. A map of 2,560 bytes and 256 tiles of 8,192 come to 10,752 bytes, and with the ULA’s 6,912 that is 17,664: more than bank 5 holds. This is where bank 7 earns its place: its 8K is exactly the size of 256 tiles. Coincidence? I doubt it.

Layouts that fit

Here are four common ways to share the 24K, each with the values for the Tilemap Base Address register, NextReg $6E, and the Tile Definitions Base Address register, NextReg $6F. Bank 5’s addresses are those the CPU normally sees; bank 7’s assume it is paged in at $C000. The bytes right after the ULA’s screen, from $5B00, are where 48K BASIC keeps its system variables, so these layouts suit a program that does not return to BASIC.

LayoutThe mapThe tile definitionsNextRegs $6E, $6FThe ULA’s screen
256 tiles beside the ULAbank 5, $5B00–$64FF (40 × 32, 2-byte entries)bank 7, $C000–$DFFF (256 tiles)$1B, $80kept
All in bank 5, fewer tilesbank 5, $5B00–$64FFbank 5, $6500–$7FFF (216 tiles)$1B, $25kept
512 tilesbank 7, $C000–$C9FFall of bank 5, $4000–$7FFF (512 tiles)$80, $00gone: switch the ULA off
80 columns of textbank 5, $5B00–$6EFF (80 × 32, 2-byte entries)bank 5, base $6F00: characters 32–127 at $7000–$72FF$1B, $2Fkept

Two of these give something up. The first uses bank 7’s first half, which is also where a 128K program keeps its shadow screen (port $7FFD bit 3): the two cannot be used at once. The third fills bank 5 with tiles, so the ULA, which reads bank 5 for its own screen, would show tile bytes as a scrambled picture; your program switches it off with bit 7 of the ULA Control register, NextReg $68. The veteran, as the big picture calls the ULA, has to leave the stage so the set builder can use its room. The text layout’s tile definitions start at $6F00 but hold only characters 32–127: a character’s tile number is its code, so tiles 0–31 are never used, and their 256 bytes may hold anything. My favourite of the four is the first: it keeps the ULA’s screen and leaves the set builder a full box of 256 pieces.

A trap: moving a setup from bank 5 to bank 7

Let me warn you about this one. The 8K limit of bank 7 is easy to forget, because to the CPU bank 7 looks like any other 16K bank. Here is how it catches a program out.

Suppose your program has the map at $4000 and the tile definitions at $6000, both in bank 5: the Tilemap Base Address register, NextReg $6E, is $00 (bank 5, offset 0) and the Tile Definitions Base Address register, NextReg $6F, is $20 (bank 5, offset 32 × 256 = 8K). It works. Now the program copies all of bank 5 into bank 7, byte for byte, to free bank 5 for something else, and switches both areas to bank 7 with the same offsets: $6E = $80 and $6F = $A0. What could possibly go wrong?

bank 5block RAM$6E = $00 ✓$6F = $20 ✓the map$4000–$5FFFthe tiles$6000–$7FFFcopied byte for bytebank 7page 14: the map’s copyblock RAMpage 15: the tiles’ copymain memory: out of reach$6E = $80 ✓finds the map$6F = $A0 ✗8K into bank 7wraps to page 14
Copying bank 5 into bank 7 moves the tiles into page 15, where the tilemap cannot read. Asking for them 8K into bank 7 wraps round to the start of page 14, the map.

The map still works: offset 0 in bank 7 is the start of page 14, where the map’s copy now is. The tiles do not. The copy put bank 5’s first 8K, the map, into page 14, the block RAM, but bank 5’s second 8K, the tiles, into page 15, the main memory the tilemap cannot see. And asking for offset 32, 8K into bank 7, does not reach page 15 anyway: only the low 13 bits of the address are used in bank 7, so 8K wraps round to 0, the start of page 14. The tilemap reads its tile definitions from the map’s bytes: tiles 0–79 are made of the 2,560 bytes of the map, and tiles 80–255 of whatever else was copied from bank 5’s $4A00–$5FFF, often the ULA’s screen. The screen shows the right arrangement of cells, each drawn as noise. Every tile is in the right place, and every one of them is garbage: the set builder follows the floor plan faithfully, with pieces cut from the floor plan itself.

What works instead is to put everything the tilemap reads into bank 5 or page 14, never page 15:

  • The tiles in bank 7, the map in bank 5. Copy the 8K of tile definitions to the start of bank 7 and set NextReg $6F to $80; leave the map in bank 5 with NextReg $6E at $00. This is the “256 tiles beside the ULA” layout of Layouts that fit.
  • Both in bank 7, if they fit in 8K together. A map of 2,560 bytes and 176 tiles of 32 bytes come to exactly 8,192: for instance the map at offset 0 ($6E = $80) and the tiles at offset 10, right after the map’s 2,560 bytes ($6F = $8A).

After a reset, NextReg $6E is $2C and NextReg $6F is $0C: the map at $6C00 and the tiles at $4C00, inside the ULA’s bitmap. So set both yourself before you switch the tilemap on. I would not trust a reset to have left anything sensible there.

The attribute byte

A map entry is two bytes: the tile number, 0–255, and an attribute byte that says how to draw it. Think of the attribute as the set builder’s note on the floor plan: which way round, and in which paint:

byte 0: the tile numbertile 0–255byte 1: the attributepalette offsetXYRUbits 7–43210X: mirror acrossY: mirror downR: rotate 90° clockwiseU: the ULA over this tile,or tile bit 8 in 512-tile modewith NextReg $6B bit 5, an entry is the tile number only, and NextReg $6Cis the attribute of every tile: half the map, the same look everywhere
A map entry: the tile number and its attribute byte, or the tile number alone with one attribute for all.

When every tile can be drawn the same way, the attribute is wasted space. Bit 5 of the Tilemap Control register, NextReg $6B, removes it: each entry is then one byte, the map halves to 1,280 bytes, and every tile uses the Default Tilemap Attribute register, NextReg $6C.

Need more than 256 tiles? Bit 1 of NextReg $6B switches on 512-tile mode: bit 0 of the attribute becomes the ninth bit of the tile number, 0–511, and 512 tiles take the whole of bank 5. The bit no longer says “ULA over this tile”, so in 512-tile mode every tile is below the ULA unless bit 0 of NextReg $6B puts the whole tilemap on top (see Above or below the ULA). It is a fair swap, if you ask me: twice the pieces, at the price of one choice per tile.

A tile’s dots

A tile is 8 × 8 dots of 4 bits: 32 bytes, 4 bytes for each row, two dots in a byte, the high nibble on the left. The dot at column x of row y of tile t is in byte t × 32 + y × 4 + x ÷ 2 of the tile definitions.

A 4-bit dot is a number 0–15, and the attribute’s palette offset becomes the high four bits of the index into the tilemap palette, as the palettes chapter’s A dot and an offset explains. So one tile can be drawn in any of 16 sets of 16 colours: a red and a blue enemy, or a lit and an unlit window, are the same 32 bytes with a different offset. Bit 4 of the Tilemap Control register, NextReg $6B, chooses which of the two tilemap palettes the tilemap uses.

The tilemap’s transparency is decided on the 4-bit dot before the palette: a dot equal to bits 3–0 of the Tilemap Transparency Index register, NextReg $4C, $F after a reset, is transparent, whatever the offset. So the empty parts of a tile let the layers below it show. I like this rule a lot: it does not care which colours a tile is painted in, only which of its 16 dot values means “nothing here”.

Eight orientations

If you remember one thing about the tilemap, make it this. Bits 3–1 of the attribute turn a tile into any of eight versions of itself: the eight ways an 8 × 8 square can be laid down, four turns and their mirror images. Here is one tile drawn eight times, with attributes $00 to $0E; the red dot is the tile’s top-left dot:

$00: as stored$02: rotate$04: Y mirror$06: rotate, Y$08: X mirror$0A: rotate, X$0C: X and Y$0E: all three
The eight orientations of one tile on the machine. Follow the red dot: each one lands where the rule below says it must.

The rotate bit always turns the tile 90 degrees clockwise: there is no setting for the other direction or for half a turn, because the mirrors provide those. The rule is: rotate 90 degrees clockwise first, then mirror. Rotation moves the tile’s dot (u, v) to (7 − v, u); then X mirror takes the column x to 7 − x, and Y mirror the row y to 7 − y. The two mirrors together are a half turn, 180 degrees, so rotate plus X and Y ($0E) is 90 + 180 = 270 degrees clockwise: 90 degrees anticlockwise. I always remember it as “turn, then flip”.

And look how neat the hardware is about it: it does all this without moving a single dot. To draw the dot at column x, row y of the cell, it reads a different dot of the tile: it turns x into 7 − x for X mirror (and, because a rotation also flips across, for rotate too, the two cancelling when both are set), turns y into 7 − y for Y mirror, and for rotate swaps the two before it forms the address. Three bits of the attribute change three bits of the address: the transformation costs nothing. I think this is the cleverest trick in the tilemap.

Why eight? Because game graphics are full of symmetry. A pipe, a wall, a corner, a ladder, a character facing left or right: one tile per shape covers every direction, and the tile set, which is the expensive part of bank 5, shrinks by up to eight times. A corner piece, after all, does not care which corner it is in.

Text mode

Bit 3 of the Tilemap Control register, NextReg $6B, switches the tilemap to text mode. A tile becomes a character in the way the Spectrum’s font is: 1 bit a dot, 8 bytes a tile, one byte a row, bit 7 on the left. Two hundred and fifty-six characters take 2K instead of 8K, and a copy of the Spectrum’s font fits in 768 bytes.

A dot of one bit cannot pick a colour by itself, so the attribute does more of the work: its bits 7–1 become a 7-bit palette offset, and the dot is bit 0 of the index. So every character picks its own pair of palette entries, even for paper and odd for ink, from 128 pairs. The price is the other attribute bits: in text mode there is no mirroring and no rotation, and bit 0 of the attribute is colour too. Fair enough: few people want their letters upside down.

Transparency changes with it: a text-mode dot is transparent when its colour, after the palette, equals the value in the Global Transparency Colour register, NextReg $14, the rule the ULA and Layer 2 use, and not when its index matches the Tilemap Transparency Index register, NextReg $4C. Here it is on the machine, 80 columns of text mode, with four colour pairs:

attributes $00, $02, $04, $06: entry pairs 0–1, 2–3, 4–5, 6–7
Text mode at 80 columns: the left half of the screen, four rows. A whole 80 × 32 screen of it is 5,120 bytes of map and 768 bytes of font.

40 or 80 columns

Bit 6 of the Tilemap Control register, NextReg $6B, doubles the columns to 80 × 32. The cells keep their 8 dots, but the dots are 14 MHz dots, half as wide, so 80 cells fill the same 320-dot width as 40 did, 640 dots across, like Layer 2’s 640 mode. The map doubles to 80 entries a row, 5,120 bytes with attributes.

40 columns: a cell is 8 dots of 7 MHz8 dots80 columns: a cell is 8 dots of 14 MHz, half as widethe same width: 8 of 40 cells above, 16 of 80 below
40 and 80 columns over the same width of screen. In 80 columns, each cell is half as wide.

Eighty columns are for text, in my view: with text mode, you get an 80 × 32 screen of characters, the size of a terminal or a word processor, from 5K of map and a font. With 4-bit tiles they work as well, at half the width per dot.

Above or below the ULA

The tilemap and the ULA are mixed into one layer before the layer order is applied, and the attribute’s bit 0 decides, tile by tile, which of the two is in front: 0 puts the tile above the ULA, 1 puts the ULA above the tile. Bit 0 of the Tilemap Control register, NextReg $6B, overrides every tile and puts the whole tilemap on top. The composition chapter’s The ULA and the tilemap become one explains the mix, and its stencil mode.

This is how a game puts a tile-based playfield behind a Spectrum-style status panel, or a text window in front of the Spectrum’s screen. It is also why the tilemap and the ULA share one memory: they are, in the end, one layer. The veteran and the set builder share a stage, and each tile decides which of them stands in front.

Scrolling and clipping

The tilemap has its own scroll: an X offset of 10 bits, bits 1–0 of the Tilemap X Scroll MSB register, NextReg $2F, above the Tilemap X Scroll LSB register, NextReg $30, and a Y offset in the Tilemap Offset Y register, NextReg $31. The picture wraps round at its own size:

ModeX offsetWraps across atY offsetWraps down at
40 columns0–319 dots3200–255256
80 columns0–639 dots of 14 MHz6400–255256

The grid covers exactly the wide area, so a scroll never shows anything but the map: what leaves on one side comes back on the other. A game that scrolls endlessly writes the next column of tiles into the map column that has just left the screen, one byte or two per row, 32 rows: 64 bytes for a whole new column. The landscape is a treadmill, and the player never sees the belt. If anyone asks me why tiles, this is my answer.

The tilemap’s clip window, the Clip Window Tilemap register, NextReg $1B, works in the wide area: X in units of two 7 MHz dots (four dots in 80 columns), so 0–159 covers the width, and Y one unit a line. It starts as 0–159, 0–255, the whole grid, as the big picture describes.

Fetching a character ahead

The tilemap reads bank 5 through the same port of the block RAM as the ULA. The ULA, which needs its bytes at exact moments, goes first; the tilemap takes every cycle the ULA does not use. The veteran has seniority here, and rightly so. The block RAM runs at 28 MHz, and the ULA uses only a few of its cycles, so there is plenty left.

To be ready in time, the tilemap works one character ahead. While the dots of one cell are being drawn from a small buffer, it fetches the next cell into the buffer’s other half: the map entry, one or two bytes, then the tile’s row, a read for each dot. When the beam reaches the next cell, its dots are waiting, and the halves swap. A good set builder always has the next piece in hand before it is needed:

drawncell n, from half Acell n+1, from half Bfetchedtileattr8 dots of n+1 → Btileattr8 dots of n+2 → Athe halves swap at each cell edgereads go through bank 5’s second port, after the ULA’s; the settings(bases, text mode, 512 tiles, on top) are taken as each cell is fetched
The tilemap fetches each cell while the one before it is drawn. A change to a tilemap setting shows from the next cell fetched.

A scrolled screen does not line its cells up with the fetch: then a stretch of 8 dots on screen is the end of one tile and the start of the next, and the tilemap reads the second map entry part-way. Its settings, the base addresses, the attribute mode, text mode, 512 tiles and the on-top bit, are taken again for every cell it fetches, so your program or the Copper can change them between two cells of the same line. My advice: if you change a tilemap setting in the middle of a line, think in whole cells, not in dots.

What to take home

Let me put the set builder’s work in one place. The tilemap draws the 320 × 256 wide area from a grid of 40 × 32 cells of 8 × 8 dots, or 80 × 32 cells of half the width. It needs two tables: a map, one entry per cell, and the tile definitions, both in the 24K of block RAM, bank 5 or the first 8K of bank 7, at the multiples of 256 bytes set in NextRegs $6E and $6F. Bank 7’s second half is out of reach, and an offset past 8K wraps back to its start, which is the trap waiting for anyone who copies bank 5 into bank 7 wholesale.

A map entry is a tile number and an attribute: a 4-bit palette offset, X mirror, Y mirror, rotate, and the ULA-over-tile bit, which becomes the ninth tile bit in 512-tile mode. Bit 5 of NextReg $6B drops the attribute and uses the one in NextReg $6C for every tile, halving the map. A tile is 32 bytes, 4 bits a dot; the palette offset adds the high four bits, and a dot equal to NextReg $4C is transparent. Rotate 90 degrees clockwise, then mirror, and every tile has eight orientations, at no cost at all: the hardware simply changes which dot it reads.

Text mode makes tiles 1 bit a dot, 8 bytes each, with a 7-bit palette offset that gives every character its own colour pair and no mirroring; 80 columns halves the width of a cell. The tilemap scrolls by its own offsets in NextRegs $2F, $30 and $31, wrapping at its own size, and clips in wide-area units with NextReg $1B. It reads bank 5 after the ULA, one character ahead, and takes its settings afresh for every cell, so the Copper can change them in the middle of a line. If you remember one picture, make it the floor plan and the box of pieces: a few kilobytes of each, and the whole wide area is dressed. The rest is your level design.

Last updated on