Z80 format

From Sinclair Wiki
Jump to navigation Jump to search


The .z80 format is used to store snapshots from a wide variety of Spectrum emulators. It was originally developed by Gerton Lunter for his emulator "Z80". Three versions of the .z80 format exist.

(There is a reference on World of Spectrum, but it's not CC-licensed so we'll have to rewrite it.)

Block compression

Repeated sequences of the same byte are stored as

ED ED xx yy

where yy is the byte and xx is the repeat count. Only sequences of length >= 5 are coded in this way, except if yy=ED in which case sequences of length >=2 are coded (otherwise they would appear to start a repeat block). A byte directly following a literal ED is not coded, for example

ED 00 00 00 00 00 00

is not encoded into

ED ED ED 06 00

instead it becomes

ED 00 ED ED 05 00

Pseudocode algorithms

For compression: (someone else should check this) (also there's currently no code to break out of the loop at EOF) (also I've written this in 'stream style' when working from an array would probably be more efficient)

int thisbyte, countbytes, nextbyte
thisbyte := read_a_byte()
do
  countbytes := 0
  nextbyte := thisbyte
  while((nextbyte == thisbyte) && (countbytes < 255))
    nextbyte := read_a_byte()
    countbytes++
  if(countbytes >= ((thisbyte==0xed) ? 2 : 5))
    write_byte(0xed)
    write_byte(0xed)
    write_byte(countbytes)
    write_byte(thisbyte)
  else
    int writecount
    for(writecount := 1 to countbytes)
      write_byte(thisbyte)
  if(thisbyte == 0xed)
    write_byte(nextbyte)
    nextbyte := read_a_byte()
  thisbyte := nextbyte
loop