Z80 Assembly programming for the ZX Spectrum

When I was young, The ZX Spectrum was the cheapest of the 8 bits, and frequently looked down upon by CPC and C64 owners... Despite its more limited graphics, they do yield some interesting advantages... compared to the CPCs 4 color mode 1... the ZX Spectrum has similar resolution, and twice the onscreen colors - what's more, it uses half the screen memory which means Spectrum games are often significantly smoother than their CPC equivalents...

These days, clever developers are able to work around the spectrums color limitations, and produce impressive looking games with fast gameplay on this classic 8 bit.
Cpu 3.5mhz Z80 3.5mhz Z80
Ram 48k/128k 128k
Vram 8k 8k
Resolution 4-color 256x192 8 color / 2 brightness per 8x8 tile 256x192 8 color / 2 brightness per 8x8 tile
Sound chip Beeper AY-3-8910
ZX Spectrum +3
Chibiakumas Tutorials:

Lesson H2 - Hello World on the ZX Spectrum

Lesson A2 - Interrupt Mode 2
   Lesson P1 - Basic Firmware Text functions

Lesson P2 - More Text Functions, Improvements... and the Sam Coupe!

Lesson P4 - Bitmap graphics on the ZX Spectrum and Sam Coupe

Lesson P6 - Keyreading on the Amstrad CPC, ZX Spectrum and Sam Coupe

Lesson P18 - Making Sound with the AY-3-8910 on the Amstrad CPC, MSX,ZX Spectrum.... and NeoGeo + Atari ST!!

Lesson P23 - Sound with the 'Beeper' on the ZX Spectrum and Apple II

Lesson P26 - Bankswitching and hardware detection on the ZX Spectrum

Lesson P35 - Playing Digital Sound with WAV on the AY!

Lesson P37 - Playing Digital Sound with WAV on the Sam Coupe, Camputers Lynx and ZX Spectrum

The Keyboard

We can read in a row of the keyboard by setting BC to the correct value and using IN A,(C)... the resulting byte will contain a 0 if the key is pressed, 1 if it is not.

C= &FE
B=... 0 1 2 3 4
%11111110 SHIFT Z X C V
%11111101 A S D F G
%11111011 Q W E R T
%11110111 1 2 3 4 5
%11101111 0 9 8 7 6
%11011111 P O I U Y
%10111111 ENTR L K J H
%01111111 SPC DEL    M       N       B   

AY-3-8910 Sound Chip:

Register Meaning Bit Meaning Details
0 Tone Pitch L - Channel A LLLLLLLL Lower value = Higher pitch
1 Tone Pitch H - Channel A ----HHHH Lower value = Higher pitch
2 Tone Pitch L - Channel B LLLLLLLL Lower value = Higher pitch
3 Tone Pitch H - Channel B ----HHHH Lower value = Higher pitch
4 Tone Pitch L - Channel C LLLLLLLL Lower value = Higher pitch
5 Tone Pitch H - Channel C ----HHHH Lower value = Higher pitch
6 Noise Generator ---NNNNN Higer = Faster noise
7 Mixer  --NNNTTT   N=Noise T=Tone (Channel --CBACBA 1=mute 0=normal)
8 Amplitude - Channel A ---EVVVV E=Envelope (1=Enabled) VVVV=Volume
9 Amplitude - Channel B ---EVVVV E=Envelope (1=Enabled) VVVV=Volume
10 Amplitude - Channel C ---EVVVV E=Envelope (1=Enabled) VVVV=Volume
11 Envelope L (Volume over time)  LLLLLLLL Lower=Faster Envelope
12 Envelope H (Volume over time)  HHHHHHHH Lower=Faster Envelope
13 Envelope Selection ----EEEE Envelope number (See PDF)
For more details, please see the AY sound chip PDF

Beeper Sound Chip:
The "Beeper" sound chip is incredibly crude... it is controlled by bit 5 of the port &FE... by turning it on and off we can make simple sounds...

See the example to the right... by changing the pause (caused by BC) we can change the pitch of the sound... 3000 will be a relatively low pitch... 500 will be higher...

Some clever programs even manage to "Fake" multiple sound channels!

The big disadvantage to all this is that the CPU will be busy during the whole time, so the Beeper chip isn't very helpful, and we'll want to use the AY sound chip on the 128k systems... but on the 48k machines, it's all we've got!
    xor a
    xor %00010000    ;---S-BBB        S=Sound B=Border
    out (&fe),a
    ld bc,3000    ;Lower number=higher pitch
    dec c
    jr nz,pausey
    dec b
    jr nz,pausey       
    jr loopy

Ram Banking
Ram banking is controlled by port &7FFD and &1FFD - they can be written, but not read, therefore, you should keep a backup of the value last sent to this port... by default the firmware keeps one at &5B5C and &5B67

Port Backup Bits Details
&7FFD   &5B5C    - - IRSMMM   MMM= ram bank at C000 (0-7)   S=Screen page bit    R=Rom Low bit    I=I/O Disabling 
&1FFD &5B67  - - - SDR - P P = paging mode (0=normal 1=+3)    R=Rom high bit    D = Disk Motor    S=Printer strobe

The ZX Spectrum 128 has 4 banks of 16k, the first is always rom on the 128k... the +3 CAN have ram in this bank, but this will mean you cannot support the 128k system (only about 15% of spectrums on the market are +3's) 
Note... the Black +2 has the same hardware as the +3... the Grey +2 has the same hardware as the spectrum128 system
ZX 128K
&0000  ROM
&4000 Screen 1 (5)*
&8000 Ram (2)
&C000 Screen 2 (7)
* ZX Firmware uses &5B00-&6000

Ram Contention
'Contention' is banks of memory which are slower due to sharing with the screen memory, unfortunately, the banks that are contended are different on the 128k machines and the +3
128K +3
Ram 0 Ram 0
Ram 1 Ram 1
Ram 3 Ram 3
Ram 4 Ram 4
Ram 6 Ram 6

Spectrum +3 Ram Options
As Mentioned, the spectrum +3 has some special banking options, which were used to allow CPM to work on the Spectrum - they are enabled by setting bit 0 of &1FFD to '1' to turn on this special mode

&1FFD  Bits 2,1

00 01 10 11
&C000 Bank 3 Bank 7 Bank 3 Bank 3
&8000 Bank 2 Bank 6 Bank 6 Bank 6
&4000 Bank 1 Bank 5 (S) Bank 5 (S) Bank 7 (S)
&0000 Bank 0 Bank 4 Bank 4 Bank 4

S=Screen Bank

Memory Map
0000 3FFF ROM
4000 57FF Screen Ram
5800 5AFF Screen Ram Color Data
5B00 5BFF Printer Buffer (Sysvars on +3)
5C00 5CBF System Vars
5CC0 5CCA Reserved
5D3B FF57 Available Memory (Between Prog and Ramtop
FF58 FFFF Reserved (User defined characters)

Screen Map

Calculating screen addresses can be performed with the following 'formula'

7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
0 1 0 Y7 Y6 Y2 Y1 Y0
Y5 Y4 Y3 X4 X3 X2 X1 X0

Each Color byte is defined in the following format:

% 7 6 5 4 3 2 1 0

Flash Bright Back Back Back Fore Fore Fore

FOREground and BACKground colors are defined by 3 bits each... values from 0-7:
7 6 5 4 3 2 1 0
White Yellow Cyan Green Magenta Red Blue Black

Spectrum +3 Disk File Header

Position Bytes Content Details Example
&0000 8 PLUS3DOS Text Header PLUS3DOS
&0009 1 EOF byte EOF Character 26
&000A 1 Issue Num Issue Num 1
&000B 1 Version Num Version Num 0
&000F 4 Size+128 Size INC Header &1080
&0010 2 Size Size of file &1000
&0012 5 Basic Header Basic Header &03,&00,&80,&00,&80
&0017 104 Unused Unused 0 0 0 0
&007F 1 Checksum Checksum of Header bytes 0-126 (MOD 256) ?
&0080 Program Code

Spectrum Links
Fuse - My Spectrum emulator of choice!
Spectrum 128k and Spectrum 48K reference - Great summary of the hardware - provides much of the info you'll want for ZX dev
Basic Manual - You'll want to know at least enough basic to do calls and operate the computer
Spectrum Computing Forum - Web community full of helpful people!

General Z80 Assembly Tutorials:
B. Beginner series - Learn the basics
A. Advanced series - In more detail
M. Multiplatform series - programming methods that work on all systems


View Options
Default Dark
Simple (Hide this menu)
Print Mode (white background)

Top Menu
***Main Menu***
Youtube channel
Email Newsletter
Merch Store
Amazon Affiliate Link
AkuSprite Editor
Dec/Bin/Hex/Oct/Ascii Table

Alt Tech
Please note: I wlll upload more content to these alt platforms based on the views they bring in

Z80 Content
***Z80 Tutorial List***
Learn Z80 Assembly (2021)
Learn Z80 Assembly (old)
Hello World
Advanced Series
Multiplatform Series
Platform Specific Series
ChibiAkumas Series
Grime Z80
Z80 Downloads
Z80 Cheatsheet
DevTools kit
Z80 Platforms
Amstrad CPC
Elan Enterprise
Gameboy & Gameboy Color
Master System & GameGear
Sam Coupe
ZX Spectrum
Spectrum NEXT
Camputers Lynx

6502 Content
***6502 Tutorial List***
Learn 6502 Assembly
Advanced Series
Platform Specific Series
Hello World Series
Grime 6502
6502 Cheatsheet
DevTools kit
6502 Platforms
Apple IIe
Atari 800 and 5200
Atari Lynx
BBC Micro
Commodore 64
Commodore PET
Commander x16
Super Nintendo (SNES)
Nintendo NES / Famicom
PC Engine (Turbografx-16)
Vic 20

68000 Content
***68000 Tutorial List***
Learn 68000 Assembly
Hello World Series
Platform Specific Series
Grime 68000
68000 Cheatsheet
DevTools kit
68000 Platforms
Amiga 500
Atari ST
Neo Geo
Sega Genesis / Mega Drive
Sinclair QL
X68000 (Sharp x68k)

8086 Content
Learn 8086 Assembly
Platform Specific Series
Hello World Series
8086 Cheatsheet
DevTools kit
8086 Platforms

ARM Content
Learn ARM Assembly
Learn ARM Thumb Assembly
Platform Specific Series
ARM Downloads
ARM Cheatsheet
DevTools kit
ARM Platforms
Gameboy Advance
Nintendo DS
Risc Os

Risc-V Content
Learn Risc-V Assembly
Risc-V Downloads
Risc-V Cheatsheet
DevTools kit

PDP-11 Content
Learn PDP-11 Assembly
PDP-11 Downloads
PDP-11 Cheatsheet
DevTools kit

TMS9900 Content
Learn TMS9900 Assembly
TMS9900 Downloads
TMS9900 Cheatsheet
DevTools kit
TMS9900 Platforms
Ti 99

6809 Content
Learn 6809 Assembly
6809/6309 Cheatsheet
DevTools kit
6809 Platforms
Dragon 32/Tandy Coco
Fujitsu FM7
TRS-80 Coco 3

65816 Content
Learn 65816 Assembly
65816 Cheatsheet
DevTools kit
65816 Platforms

eZ80 Content
Learn eZ80 Assembly
eZ80 Downloads
eZ80 Cheatsheet
DevTools kit
eZ80 Platforms

Work in Progress

Misc bits
Ruby programming

Buy my Assembly programming book
on Amazon in Print or Kindle!

Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!

Buy my Assembly programming book
on Amazon in Print or Kindle!

Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!

Buy my Assembly programming book
on Amazon in Print or Kindle!

Buy my Assembly programming book

Available worldwide!
Search 'ChibiAkumas' on
your local Amazon website!
Click here for more info!