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.snaCheck 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. | Title | Folder | Download |
|---|---|---|---|
| Part I · Before the first byte | |||
| 1 | The machine with rubber keys: what the ZX Spectrum is, and why it is programmed this way | ch01/ | |
| 2 | Numbers a computer understands: bits, bytes, hexadecimal, and two's complement | ch02/ | |
| 3 | Inside the case: the Z80, the ULA, the ROM, and the memory map | ch03/ | |
| 4 | Setting up the development environment on Windows, macOS, and Linux | ch04/ | |
| 5 | Your first program: a coloured border, assembled, loaded, and photographed | ch05/ | |
| 6 | Seeing inside the machine: the Fuse debugger | ch06/ | |
| Part II · The Z80 language | |||
| 7 | Registers, flags, and the shape of a Z80 instruction | ch07/ | |
| 8 | Moving data: LD and every addressing mode | ch08/ | |
| 9 | Arithmetic: eight-bit and sixteen-bit sums, carries, and BCD | ch09/ | |
| 10 | Logic, bits, shifts, and rotates | ch10/ | |
| 11 | Making decisions: the flags, JP, JR, DJNZ, and conditional returns | ch11/ | |
| 12 | Subroutines and the stack: CALL, RET, PUSH, POP, and the shadow registers | ch12/ | |
| 13 | Blocks, tables, and the index registers: LDIR, IX, IY, and lookup tables | ch13/ | |
| 14 | Counting T-states: what an instruction costs on a 3.5 MHz Z80 | ch14/ | |
| 15 | sjasmplus: labels, macros, includes, devices, and the snapshot | ch15/ | |
| 16 | The ROM as a library: printing, the system variables, and when to leave it alone | ch16/ | |
| Part III · The screen, the attributes, and the contended bus | |||
| 17 | Taking the machine: interrupts, IM 1 and IM 2, and a frame of your own | ch17/ | |
| 18 | How a frame is built: 312 lines of 224 T-states, and 69,888 in all | ch18/ | |
| 19 | The screen memory: 6,144 bytes in an order nobody would choose | ch19/ | |
| 20 | Attributes: ink, paper, bright, flash, and the eight-by-eight cell | ch20/ | |
| 21 | Attribute clash: why it happens, and five ways to live with it | ch21/ | |
| 22 | Plotting and drawing: pixels, lines, and fast address arithmetic | ch22/ | |
| 23 | Software sprites: shifting, masking, XOR, and pre-shifted graphics | ch23/ | |
| 24 | Drawing against the beam: flicker, tearing, and when to draw | ch24/ | |
| 25 | Contended memory: who owns the bus while the ULA reads the screen | ch25/ | |
| 26 | The border: stripes, timing, and the border as a measuring instrument | ch26/ | |
| 27 | Scrolling: pixel, character, and attribute scrolling, and what each costs | ch27/ | |
| 28 | Fonts and text: a character set of your own | ch28/ | |
| 29 | Racing the beam: changing the attributes line by line | ch29/ | |
| 30 | The floating bus: reading what the ULA is reading | ch30/ | |
| 31 | Fast copies: the stack as a blitter, and a screen drawn off-screen | ch31/ | |
| Part IV · Making it a game machine | |||
| 32 | The frame loop: the interrupt as a heartbeat, fifty times a second | ch32/ | |
| 33 | Reading the keyboard: eight half-rows, ghosting, and redefinable keys | ch33/ | |
| 34 | Joysticks: Kempston, Sinclair, and cursor | ch34/ | |
| 35 | The beeper: one bit of sound, and the T-states it costs | ch35/ | |
| 36 | An engine for sound effects and music on the beeper | ch36/ | |
| 37 | Memory discipline: where things live in 48K, and why above 32768 | ch37/ | |
| 38 | Collisions: attribute tests, bounding boxes, and pixel-exact checks | ch38/ | |
| 39 | Score, text, and a status line that never flickers | ch39/ | |
| 40 | Random numbers, the R register, and game states | ch40/ | |
| 41 | The frame budget: fitting a game into 69,888 T-states | ch41/ | |
| Part V · Two complete games | |||
| 42 | The first game I: a single-screen arcade game, from specification to snapshot | ch42/ | |
| 43 | The first game II: sound, score, difficulty, and the finished tape | ch43/ | |
| 44 | The second game I: a flip-screen world, rooms in memory, and a map | ch44/ | |
| 45 | The second game II: enemies, items, music, and the finished tape | ch45/ | |
| 46 | Where to go next: three projects, sketched | ch46/ | |
| Part VI · Beyond the 48K | |||
| 47 | The 128K machines: paging, the second screen, and memory that moves | ch47/ | |
| 48 | The AY-3-8912: three channels, noise, and envelopes | ch48/ | |
| 49 | An AY music player that runs from the interrupt | ch49/ | |
| 50 | Tape: the ROM loader, headers, and a loader of your own | ch50/ | |
| 51 | One program, five machines: 48K, 128K, +2A, +3, and their timings | ch51/ | |
| 52 | Optimisation: T-states, unrolled loops, and self-modifying code | ch52/ | |
| Part VII · The rest of the craft | |||
| 53 | Compression: RLE, and a packer for screens and levels | ch53/ | |
| 54 | Title screens, loading screens, and attract mode | ch54/ | |
| 55 | Testing on real hardware: real Spectrums, real tapes, modern interfaces | ch55/ | |
| 56 | Releasing your work: TAP, TZX, inlays, and the Spectrum scene today | ch56/ | |