ZX Spectrum
Contents
ZX Spectrum
The ZX Spectrum consists of a Z80A @ 3.5 MHz CPU with either 16k, 48k or 128K of RAM. Most demos are targeted at the Spectrum 128 because it includes more memory, a shadow screen buffer and an AY soundchip. The different models have slightly different timings - this will cause issues if you are doing cycle-exact effects like multi-color.
Setting up
Setting up your development platform for the ZX Spectrum is quite easy, first get the following tools:
- SjASMPlus (z00m's fork) - cross-platform open source assembler with nice macros for creating Binaries, TAP, TRD and SNA snapshot files out of the box, embedded Lua scripting and support for similar CPUs (Z80/R800/Z80N/i8080/LR35902).
- Rasm - extremely fast cross-platform Z80 assembler with many features
- Pasmo - for the .TAP create. Available at : http://pasmo.speccy.org
- Emulator(s): I Found ZX Sping, FUSE, UnrealSpeccy and EightyOne to work best for my usecase. Most emulators can read TAP, SNA and TRD files out of the box.
Alternatively, you can use the online Bazematic tool to do a lot of the early development at bazematic.demozoo.org
Start values
Upon startup (when called from basic), the following values can assumed:
- The alternate HL register is set to 0x2758
- IY = 0x5c3a
- BC = start address
- A = C
Video display
Video display on the ZX Spectrum is mostly CPU based with little hardware features. No hardware sprites, no specific text or video modes, only a 256x192 byte screenbuffer with 1bit pixeldata located at $4000 in memory. It is ordened a bit strange in 3 sections of 256x64 pixels, then character rows, then subrows.
Address = 010RRLLL RRRCCCCC
- where RRRRR is the row number (0..23)
- CCCCC is the column number (0..31)
- LLL is the line number within the cell (0..7)
Calculating a screen address from XY coordinates is complicated due to the weird screen layout. In a larger demo you would generate a lookup table - it's usually best to avoid such calculations in small demos, but it can be done in under 30 bytes, eg: http://www.breakintoprogram.co.uk/computers/zx-spectrum/screen-memory-layout
Adding Color
The ZX Spectrum has a 32x24 colormap located at $5800 where you can write color information for each 8x8 tile. It has has 8 colors (INK and PAPER) with 2 brightness settings that can be set like this.
color = brightness(64) | (PAPER<<3) | INK
Because updating pixel memory can be slow, especially when you are grasping for bytes, some of the tiny intros on the zx spectrum prefer to use the colorram for the effect, sometimes in combination with an overlaying pattern.
Border Color
You can set the border color to any of the 8 colors with:
ld a,0 ; bottom three bits of a contain the color
out (254),a
Memory Configuration
128K separated to 8 pages (16384 bytes size) Default configuration is:
- page 5: $4000-$7fff
- page 2: $8000-$Bfff
- page 0: $C000-$Ffff
There are two screens - page 7 and page 5. port $7FFD allow to control memory and screens:
- bits 0-2 - page number mapped to memory at $C0000
- bit 3 - Select page 5(0) or page 7(1) to be displayed.
- bit 4 - ROM Select. 0-128K,1-48K
Example of use
loop: ei
halt
pg: ld a,$17
ld bc,$7ffd
out (c),a
xor $0A
ld (pg+1),a
... do something with $C000-$DB00
jp loop
$17 is use 48K, map page 7 After xor $0A we'll get value $1D(use 48K ROM, display screen at page 7 and map page5 at $C000).This is so-called "double buffering". See also : https://worldofspectrum.org/faq/reference/128kreference.htm
Getting something on screen
Now to get something on screen, lets fill our colorram with a simple AND pattern, like so:
ld de,5800h
ld b,24
yloop:
ld c,32
xloop:
ld a,c
and b
and 7 ; make sure range is 0..7
ld (de),a
inc de
dec c
jr nz,xloop
djnz yloop
Using a Backbuffer
While the above code will run fine, you might want to consider using a backbuffer for more complex stuff. You can then simply write to another adress define by BACKBUFFER (for example A000) and copy the buffer to colorram like so:
halt ; synchronize
ld hl,BACKBUFFER
ld de,5800h
ld bc,768
ldir
Another alternative method is to use the 128's memory paging, which provides a second screen buffer. This buffer is located at a different memeory location, but otherwise it is the same:
; main loop starts here
ld a,00010111b ; set up memory banks and screen here
mainloop
halt ; sync
xor 00001010b ; flip screens
out ($fd),a
push af
; render code goes here
; screen buffer is location at 0xC000 instead of 0x4000
pop af
jr mainloop
Overlaying simple graphics
If you don't want to use a solid color/tile for. You could copy a single tile across the screen at startup for some flair.
ld a,0x55 ; 01010101 pattern
ld bc,0x1800
copyloop:
ld (de),a
inc de
dec c
jr nz,copyloop
djnz copyloop
Alternatively you could generate a pattern using predefined data, logic operations or random noise/data.
Useful routines
;calculate address of next line,HL=address
down_hl:
INC h
LD A,h
AND 7
RET NZ
LD A,L
ADD A,#20
LD L,A
RET C
LD A,H
SUB 8
LD H,A
RET
;move up at screen, HL=address
up_hl
LD A,H
DEC H
AND 7
ret nz
LD A,L
SUB 32
LD L,A
ret c
LD A,H
ADD A,8
LD H,A
ret
;write pixel
CALL 8933 ; C=X(0..255),B=Y(0..175),5C7D=COORDS
;Calculate vertical line screen Address, IN : A=Y coordinate, OUT : HL=address
py2saddr:
ld l,a
; ld a,l
and $07
ld h,a
ld a,l
and $c0
rra
inc a
rrca
rrca
or h
ld h,a
ld a,l
add a
add a
and $e0
ld l,a
;Calculate Attribute address, IB: reg D=Y,reg E=X, OUT: HL=address, destroys DE
ld l,d
add hl,hl
add hl,hl
ld h,$11
add hl,hl
add hl,hl
add hl,hl
ld d,0
add hl,de
or alter version
;H=Y coordinate,L=X coordinate
ld a,h
rrca
rrca
rrca
push af
and 3
add a,$58
ld h,a
pop af
and %11100000
add a,l
ld l,a
calculate Pixel coordinate
;IN: A=X coordinate,C=L=Y coordinate
;OUT: HL=screen address,A=bit value
call #22B0
Sound
The original Spectrum has only a 1 bit sound capability (BEEP) through its internal speaker. Later models included the AY-3-8910 Soundchip which provides 3 channels of PSG sound.
Make some noise - Beeper
Setting/toggling bit 4 of port 254 will enable the beeper speaker.
ld a,$10 ;Bit 4 set
beeploop: xor $10 ;toggle Bit 4
out (254),a
ld b,90 ;wait
djnz $
jp beeploop
Note that the bottom three bits of port 254 also set the border color (see above).
Make some noise - AY
You can access the AY soundchip by outputting to the following ports:
ld bc,0xfffd
ld a, ay register number
out (c),a
ld b,0xbf
ld a, data byte
out (c),a
For more information about the soundchip, check out: https://www.atarimagazines.com/v4n7/stsound.html
Additional Resources
I found resources on ZX Spectrum sizecoding to be sparse.
- All kinds of Z80 Information http://www.z80.info/index.htm
- Another source for good Z80 Information http://z80-heaven.wikidot.com
- Assembler subforum on world of spectrum https://worldofspectrum.org/forums/categories/assembler
- Development subforum on Spectrum Computing https://spectrumcomputing.co.uk/forums/viewforum.php?f=6
- Blogpost on ZX Spectrum coding from a X86 coder's perspective on superogue's sizecdoing blog http://marquee.revival-studios.com/blog/blog_fluxus.html
- Easy to read list of all Z80 instructions (including undocumented ones) with size and timing information https://clrhome.org/table/
- Detailed information on the screen layout http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout/ http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-two/ and http://www.overtakenbyevents.com/lets-talk-about-the-zx-specrum-screen-layout-part-three/
Small demos with documented source code
- Ceci N'est Pas Un Cube by Ate Bit https://www.pouet.net/prod.php?which=29691
- Zxwister by Ate Bit https://www.pouet.net/prod.php?which=44123
- Muse by Ate Bit https://www.pouet.net/prod.php?which=62880
- Starlet Guitarlet by HOOY-PROGRAM https://www.pouet.net/prod.php?which=52553
- Clangers On The Dancefloor by HOOY-PROGRAM https://www.pouet.net/prod.php?which=29696
- Chessington World Of Adventure by HOOY-PROGRAM https://www.pouet.net/prod.php?which=76074
- Roll Me Gently by Joker https://www.pouet.net/prod.php?which=86338