SZX snapshots¶
This page describes how Xpeccy+ reads and writes zx-state (.szx) files: what it takes from
each standard block, what it adds, and where. The format comes from Spectaculator. It is at
version 1.5 and is maintained by Jonathan Needle
(the specification). This page covers
only what the spec leaves open and what Xpeccy+ keeps beyond it. To learn how to save and
open a snapshot, see Snapshots. The code is in
src/libxpeccy/filetypes/szx.c (the standard blocks) and szx_ext.c (our own data). Where
this page and the code disagree, the code is right.
This page is for the authors of other programs. The format has no version newer than 1.5, and machines it never named - TSConf, TurboSound FM, the ZX Spectrum Next - are now common. If each program keeps them its own way, a snapshot can be opened only in the program that made it. So this page describes everything Xpeccy+ writes, field by field, and another program can read our files and write files we read. For TSConf and TurboSound FM, the last section proposes blocks to share, so that each program does not have to invent its own.
Two kinds of file¶
A machine the spec names - 16K, 48K, 128K, +2, +2A, +3, Pentagon 128/512/1024, Scorpion - is written with its own machine id and the standard blocks, and any reader that knows SZX loads it. What the standard blocks have no room for (a second and third sound chip, the FM half, the exact raster phase, the disk controller in mid-command) goes into the creator block, where other readers skip it.
A machine it cannot name - ZX Evolution (BaseConf, TSConf), ATM Turbo 2+, Profi, Phoenix, ALF - is written with machine id
0x80. The standard blocks are still there (CPU, memory, AY, disks, tape), but only Xpeccy+ knows which machine to build for it, so other readers refuse the file. Ids 0..16 belong to the spec, and0x80was picked well clear of them.
Both kinds of file are loaded on the model they name. If you open a Pentagon file while a 128K is running, the machine first switches to a Pentagon, because the frame position in the file means nothing on a machine with another frame. Only the user’s own machine (one saved under its own id) on the same core is left as it is. If the file records settings that the machine does not match - ULA timings, board issue, ULA+, an AY, a Beta 128, a Covox, a device not emulated - they are not changed. The user is told, and the load goes on. A joystick, a mouse and a General Sound are only logged, because writers record whatever they were set to, used or not.
Standard blocks¶
In the table, Read is what Xpeccy+ takes from a block, and Written is what it puts in one.
Block |
Read |
Written |
|---|---|---|
|
name and version; |
|
|
all of it, see below |
all of it |
|
border, |
the same |
|
every page, compressed or not |
every page, compressed |
|
chip 0’s sixteen registers and the selected register |
the same |
|
ULA+ state, when the machine has ULA+ |
when it has ULA+ |
|
the WD1793 registers and the system register, see below |
the same |
|
the image, linked or embedded |
a link, or the disk as a UDI when it is carried |
|
the image, linked or embedded (see below) |
a link, or the |
|
nothing: the drives are the machine’s |
the drive count |
|
the image, linked or embedded, and the block it stood at |
a link and the block |
|
the level |
the level |
|
a custom ROM, put in place of the machine’s |
the custom ROM, when one was put in |
|
the General Sound’s CPU, ports and RAM |
the same |
|
compared with the machine, never taken: they are settings |
what the machine has |
|
skipped: not emulated; the user is told |
- |
any other |
skipped |
- |
What the spec leaves open, and how it is read here:
Z80R.dwCyclesStartis the machine’s position in its frame, in T from the start of the frame interrupt. The raster, the contention phase and the floating bus are set to that position. A.z80or.snacannot store this, which is why SZX is the default format. T is counted at the speed the CPU runs when the snapshot is taken, turbo included. Our ownRAYrecord (below) carries the exact dot as well.Z80R.chHoldIntReqCyclesis what is left of the INT pulse, in T. A pulse not yet taken can still be taken after the load.Z80R.chFlags:SUPPRESS_INTSis “the last instruction was EI”,HALTEDthe HALT state,FSET“the last instruction changed the flags” (for SCF/CCF’s undocumented bits).SUPPRESS_INTSandHALTEDexclude each other.IFF1 and IFF2: inside an NMI handler IFF1 is 0 and IFF2 1, and the RETN at its end enables interrupts with no EI. A reader that gates INT on IFF1 alone handles this anyway. A reader that keeps a separate “EI was last” flag has to set it from IFF1 or IFF2.
Z80R.wMemPtris the Z80’s internal WZ register; it decides bits 3 and 5 of the flags afterBIT n,(HL).AY.chCurrentRegisteris the byte last written toFFFD, all eight bits. An AY or YM answers only when the upper four bits are 0000. Any other number deselects the chip until the next number is written, so writes go nowhere and a read sees the floating bus. A reader should keep the byte, not mask it to four bits.B128: the system register (FF) is set, not written. If it were written, its MR bit going high would start the RESTORE that a real WD1793 runs when it comes out of reset, and the saved machine had finished that long ago. TheCONNECTEDflag is ignored on reading, because some writers leave it clear on a Pentagon.RAMPon a Pentagon 512/1024 numbers pages 0..31 and 0..63 the way7FFDandEFF7page them.Links in
TAPE,BDSKandDSKare written as full paths in the host’s own form. On reading, if a link does not resolve, the image is looked for beside the snapshot by its file name. An image already in the drive is left there, so a disk written to since then is not lost.An embedded
DSK: the spec namesZXSTDSKF_EMBEDDEDandZXSTDSKF_COMPRESSEDbut marks them not implemented, and says only thatchDatais a file name. Xpeccy+ reads them the wayTAPEuses them. WithEMBEDDEDset,chDatais the.dskfile’s own bytes anddwUncompressedSizeits length. WithCOMPRESSEDset too, the bytes are a zlib stream that unpacks to that length. Xpeccy+ writes an extended.dsk(EXTENDED CPC DSK File), packed if that makes it smaller.An embedded
BDSKis read as the type itschDiskTypenames - TRD, SCL, FDI or UDI - and written as a UDI, whatever file the disk came from: a UDI keeps the whole track, sector layout and protection included.Carried media: an image written into the file (the tape as its file is, a Beta disk as a UDI, a +3 disk as a
.dsk) still goes back in under its old name, which ourLINKrecord keeps. A disk that was unsaved when the file was written, or whose file is gone, comes back unsaved, so Xpeccy+ asks you to save it.Empty drives: if the file has no
BDSKorDSKblock for a drive, or noTAPEblock, the drive or the tape keeps what is in it on loading, because the file says nothing about it. The user can keep the file’s media out altogether (Load media on the Saves page), but a quick load always puts them in.
Our data: the creator block’s chData¶
Some readers report every block they do not know to the user as an error, even though the
load succeeds. So Xpeccy+ adds no blocks of its own. Its data goes in CRTR.chData, which
the spec gives to “the program that created the file”, and which every reader skips.
CRTR block
char szCreator[32] "Xpeccy+"
word chMajorVersion
word chMinorVersion
chData:
char tag[8] "Xpeccy+\0"
record... to the end of the block
A record is shaped like a block:
dword id four characters, like a block id
dword len
byte data[len]
Most records are a list of named fields:
byte keylen
char key[keylen] not terminated
word len
byte data[len]
A reader takes a field only if it has one of that name and that size. Everything else stays as the standard blocks set it. This lets a build read a file written by an older or newer build: a field that changed size is skipped, not misread. An unknown record is skipped whole.
What a field holds is Xpeccy+’s own memory layout - the C structure the emulator runs on,
not a description of the hardware. That is fine for a snapshot that has to put the machine
back exactly, and useless to anyone else. Only MACH, MAP, RAY, TS, FDCP, FM n
and SDCD have a layout of their own, which is written down here (TS is fields with one
byte after them). The rest are opaque.
Which machine: MACH and id 0x80¶
MACH is always the first record:
char core[] the core's name, zero terminated ("ZX48", "TSLab", "PentEvo"...)
char machine[] the machine's id, zero terminated ("zx48", "evo-tsconf"...)
For a file with machine id 0x80, Xpeccy+ builds the machine that MACH names. The
other records are taken only if the file’s core is the running core. On another core only
the sound chips’ records (TS, CHPn, FM n) are taken, because the ports, flags and
video mean something different there.
The records¶
Record |
What it holds |
|---|---|
|
core and machine id, above |
|
the Z80 beyond |
|
the machine: turbo, the ear input’s RC state, the floating bus’s last byte, frame and line counters, the core’s ports and registers, the CMOS clock, TSConf’s own state |
|
the pager’s map, 256 entries of 256 bytes each: |
|
the video: mode, border, flash, INT pulse, TSConf’s video state and line buffer |
|
the palette, on a machine whose palette is its own (ATM, Evo, TSConf, Profi) |
|
|
|
TurboSound: mutes, status-read mode, then |
|
each sound chip’s counters, envelope, selected register and all 256 register bytes |
|
a YM2203’s FM engine, |
|
the beeper’s level and filter |
|
SAA1099, Soundrive/Covox |
|
the disk interface and its controller |
|
the controller’s command in progress: |
|
each drive: motor, head, track, position on the track, whether its disk was unsaved |
|
the IDE interface and its two devices |
|
SMUC’s NVRAM |
|
the SD card |
|
a Z-Controller’s SD port: |
|
General Sound beyond |
|
the tape’s position inside the block, the level, the pause |
|
a carried image’s old name: |
|
the ZX Spectrum Next: see below |
|
the Next’s SD card, named, not carried: see below |
|
the Next’s ESP-01 module: see below; a ZX Evolution’s with ZiFi fitted too |
|
the ZX Evolution’s ZiFi: see TSConf |
The order of reading matters in one place: the drives and the tape are mounted after the
snapshot is in, so FLPn, DIF and TAPE wait until their images are back.
TSConf¶
TSConf is a ZX Evolution configuration, so its files carry machine id 0x80 and MACH names
TSLab / evo-tsconf. Its state is in three records, all opaque:
COMP- the#xxAFports as the core keeps them (most in its register array, the DMA addresses in its 32-bit registers), the cache configuration and the cache’s tag RAM, a DMA transfer in progress (source, destination, length, burst count, the word in flight and the DRAM cycles banked for it), the power-up flag, and the Evo’s 256-byte clock RAM.VID- the video side: graphics and tile offsets, tile and sprite pages, palette select, the INT line and position and the lines it moves on each frame, the values written but not yet taken at the start of the blanking, CRAM (512 bytes), SFILE (512 bytes), the tile map rows read ahead, and the line buffer half drawn when the snapshot was taken.MAP- windows 1-3 exist only in the pager’s map (#11AF..#13AFwrite it directly), so the map is carried as it is rather than rebuilt from ports.
With ZiFi fitted (see ZiFi), two more records follow:
ZIFI- the board controller’s side: the API mode, which device the data register serves, the interrupt mask and sources, the thresholds, both pairs of FIFOs with what is in them, the bytes on their way from the module, and the clock its times count by.ESP- the module itself, as for the ZX Spectrum Next below.
If a file without them is loaded with ZiFi fitted, both start as at power-on. If ZiFi is not fitted, these records are skipped.
RAY puts the raster back to the dot, which matters here: TSConf moves its INT anywhere in
the frame, and a T is a different number of dots at each of its three speeds.
ZX Spectrum Next¶
A Next file carries machine id 0x80 and MACH names ZXNext / zxnext. The memory is the
board’s SRAM in RAMP blocks, pages 0 to 127 on 2 MB and 0 to 63 on 1 MB. It holds the
ROMs the firmware loaded from the card, the divMMC and Multiface memory and the RAM banks,
all where the board keeps them. A file of the other size runs the Next with the file’s memory
size. The rest is in records of our own, all opaque:
NEXT- the board, part by part: the nextregs, the memory map and the ports, the copper and its program, the palettes, the sprites and their patterns, the layers and the picture the mixer is drawing, the DMA, the interrupt controller, the CTC, the AY and the DACs, the divMMC paging, the keyboard maps, both UARTs, and the SRAM behind the three banks the CPU sees as fast memory.NCLK- the Next’s times are counted from when the machine started, in its 28 MHz clock, so the clock comes back with them. The record also says whether the firmware’s start is still to come (a recording begun at power-on).RTC- the DS1307 clock chip, its battery side too.VCLK- the video clock’s fraction of a dot.SDCD- the SD card. A card is far too big to carry, but NextZXOS keeps in memory what it read from the card. The record names the card (an image or a folder) and the folder on the card where a program opened from elsewhere was put. It also holds a print of what the card holds, as the Next reads it. A load puts the card back in and that folder back on it, and says so if the card’s content has changed since. A program’s own card (b2) is made again from that System/Next, and the user’s card is set aside, as it is when the program is opened. If an older build put a whole collection on the card (see Starting programs), the collection goes back empty, because the program is in memory already.
byte flags b0: a folder, not an image; b1: the print at the end follows;
b2: a program's own card - the path is System/Next, never written
qword old print 0 with b1; without it, a print of every directory entry, not compared
char path[] zero terminated
byte folders how many follow (one at a time is put on a card)
for each:
char name[] its name in the card's root, UTF-8, zero terminated
char host[] where it is on the computer, UTF-8, zero terminated; the
program's file when it went alone (an older reader finds no
folder there)
qword print with b1: every file and folder by its name (the long one, without
case), a file's size and write time - not where they lie, the order
or the folders' times; 0 none
ESP- the ESP-01 module, which runs outside the machine: its UART, boot, the command being received, the replies waiting to go out, the Wi-Fi and TCP settings and what its links hold. The record is empty if no module is fitted. The network side is not in it: after a load the connections are closed, and the module goes on as it does when the far end drops them.
RAY puts the raster back to the dot, as for TSConf: the Next runs at four speeds.
TurboSound FM¶
A TurboSound FM is two YM2203s behind one pair of ports. If %11111xxx is written to
FFFD, it is the board’s own command, not a register number, and neither chip sees it.
Bit 0 picks the chip, bit 1 clear means that a read of FFFD returns the status, and bit 2
set turns the FM halves off. Any other byte goes to the selected chip.
How its state is kept:
The standard
AYblock holds chip 0’s SSG half, so any SZX reader gets the first chip’s music, without FM.TSholds the command’s state: which chip is selected, the status-read mode, and the mutes.CHPnholds each chip’s SSG counters and its 256 register bytes:0x00..0x0Fthe SSG,0x10..0xFEthe FM registers as last written,0xFFthe status. The register number incurRegis all eight bits.FM nis the FM engine’s own state - operator phases, envelope stages and levels, the LFO, both timers, the busy flag, the prescaler - in the order of the FM core Xpeccy+ uses. It is size-checked like a field, so a build whose engine changed skips it and keeps the registers.
The FM registers alone do not put a note back: they say what was last written, not how far each
envelope has gone. A reader that has only them can restart the notes from the registers, which
is what Xpeccy+ does if FM n does not fit.