ZX Spectrum Assembly · the code

Every program in the book

The sources and the programs, in folders named exactly as the book names them. Free to use in your own work, commercial or not, under the MIT licence.

Assemble and run a chapter

You need sjasmplus (assembler), Fuse (emulator and debugger) and Python 3 (verifier and generators). Open a terminal in the folder and type:

cd ch05
sjasmplus border.asm
fuse border.sna

Check everything

From the top folder of the full download, this assembles every source again and compares the result with the reference bytes:

python3 verify.py
  • Every program writes its own snapshot. The SAVESNA line at the end of a source names the .sna file that Fuse opens; build.sh in each folder assembles every program of the chapter.
  • A chapter folder holds everything the chapter prints: the sources, the snapshots and tapes that were photographed, the generator that wrote the sources, the meas script and its results, the figures.
  • verify.py assembles every program again and compares it with the bytes the book was made from. It needs Python 3 and sjasmplus 1.24.0, and uses zmakebas and snapconv where it finds them. Chapter 4 installs the tools on Linux, Windows and macOS.
  • appI holds the answers to the workshops that change a program, one folder per chapter. No ROM image is included: the Spectrum's ROM comes with the emulator.

Chapter by chapter

Ch.TitleFolderDownload
Part I · Before the first byte
1The machine with rubber keys: what the ZX Spectrum is, and why it is programmed this waych01/
2Numbers a computer understands: bits, bytes, hexadecimal, and two's complementch02/
3Inside the case: the Z80, the ULA, the ROM, and the memory mapch03/
4Setting up the development environment on Windows, macOS, and Linuxch04/
5Your first program: a coloured border, assembled, loaded, and photographedch05/
6Seeing inside the machine: the Fuse debuggerch06/
Part II · The Z80 language
7Registers, flags, and the shape of a Z80 instructionch07/
8Moving data: LD and every addressing modech08/
9Arithmetic: eight-bit and sixteen-bit sums, carries, and BCDch09/
10Logic, bits, shifts, and rotatesch10/
11Making decisions: the flags, JP, JR, DJNZ, and conditional returnsch11/
12Subroutines and the stack: CALL, RET, PUSH, POP, and the shadow registersch12/
13Blocks, tables, and the index registers: LDIR, IX, IY, and lookup tablesch13/
14Counting T-states: what an instruction costs on a 3.5 MHz Z80ch14/
15sjasmplus: labels, macros, includes, devices, and the snapshotch15/
16The ROM as a library: printing, the system variables, and when to leave it alonech16/
Part III · The screen, the attributes, and the contended bus
17Taking the machine: interrupts, IM 1 and IM 2, and a frame of your ownch17/
18How a frame is built: 312 lines of 224 T-states, and 69,888 in allch18/
19The screen memory: 6,144 bytes in an order nobody would choosech19/
20Attributes: ink, paper, bright, flash, and the eight-by-eight cellch20/
21Attribute clash: why it happens, and five ways to live with itch21/
22Plotting and drawing: pixels, lines, and fast address arithmeticch22/
23Software sprites: shifting, masking, XOR, and pre-shifted graphicsch23/
24Drawing against the beam: flicker, tearing, and when to drawch24/
25Contended memory: who owns the bus while the ULA reads the screench25/
26The border: stripes, timing, and the border as a measuring instrumentch26/
27Scrolling: pixel, character, and attribute scrolling, and what each costsch27/
28Fonts and text: a character set of your ownch28/
29Racing the beam: changing the attributes line by linech29/
30The floating bus: reading what the ULA is readingch30/
31Fast copies: the stack as a blitter, and a screen drawn off-screench31/
Part IV · Making it a game machine
32The frame loop: the interrupt as a heartbeat, fifty times a secondch32/
33Reading the keyboard: eight half-rows, ghosting, and redefinable keysch33/
34Joysticks: Kempston, Sinclair, and cursorch34/
35The beeper: one bit of sound, and the T-states it costsch35/
36An engine for sound effects and music on the beeperch36/
37Memory discipline: where things live in 48K, and why above 32768ch37/
38Collisions: attribute tests, bounding boxes, and pixel-exact checksch38/
39Score, text, and a status line that never flickersch39/
40Random numbers, the R register, and game statesch40/
41The frame budget: fitting a game into 69,888 T-statesch41/
Part V · Two complete games
42The first game I: a single-screen arcade game, from specification to snapshotch42/
43The first game II: sound, score, difficulty, and the finished tapech43/
44The second game I: a flip-screen world, rooms in memory, and a mapch44/
45The second game II: enemies, items, music, and the finished tapech45/
46Where to go next: three projects, sketchedch46/
Part VI · Beyond the 48K
47The 128K machines: paging, the second screen, and memory that movesch47/
48The AY-3-8912: three channels, noise, and envelopesch48/
49An AY music player that runs from the interruptch49/
50Tape: the ROM loader, headers, and a loader of your ownch50/
51One program, five machines: 48K, 128K, +2A, +3, and their timingsch51/
52Optimisation: T-states, unrolled loops, and self-modifying codech52/
Part VII · The rest of the craft
53Compression: RLE, and a packer for screens and levelsch53/
54Title screens, loading screens, and attract modech54/
55Testing on real hardware: real Spectrums, real tapes, modern interfacesch55/
56Releasing your work: TAP, TZX, inlays, and the Spectrum scene todaych56/