Undocumented bits in the flags register

ZXNet echo conference «zx.spectrum»

From Alexey Mednonogov To All 21 July 1997


Hi Andrew!

18 Jul 97 16:13, Andrew Mikheyev wrote to All about Undocumented bits in the
flag register:

AM> I want to ask All, does anyone know by what principle the Z80
AM> changes %subj%?

A piece of Russian documentation for the emulator Z80

=== Cut ===

There is another dark area in the Z80 processor that has
its impact on some programs, such as Saber Wulf, Ghosts'n
Goblins and Speedlock. These are undocumented flags!

Bits 3 and 5 of the F flag register are not used. But they can contain
information and it can be used, since there are such
obvious commands as PUSH AF and POP AF. But, nevertheless, sometimes
the contents of these flags change. I found the following
rule of thumb:

The values ​​of bits 7,5,3 repeat the values ​​of the corresponding bits
of the last 8-bit result of an instruction that changed
normal flags.

For example, after the ADD A,B instruction, these bits will be identical to the bits in
register A. Bit 7 of register F is the sign bit and it, of course, by
definition always follows this rule. the exception is the command
CP x (where x is a register or (HL) or direct argument). In this case, the bits
are copied not from the result, but from the argument.

For instructions that operate on double-byte operands, 8 bits
fall underthe inferred rule is the most significant 8 bits of the 16-bit
result. This is to be expected, since the significant
bit is the most significant bit.

Ghosts'n Goblins use undocumented flags due to a
programmer error. In the Saber Wulf program, the rhinoceros
behaves unnaturally, sometimes running in small circles in the corners if the sign bit
of the BIT command is not calculated correctly. I'll quote:

AD86 DD CB 06 7E BIT 7,(IX+6)
AD8A F2 8F AD JP P,#AD8F

Amazing code sample! As for the “Speedlock”, it
performs so many incomprehensible and puzzling tricks that in order
for it to work, everything in the emulator must exactly
correspond to the real Spectrum.

Well, and finally, in the 128th ROM there is also an unusual use of the
AF register - there it is used to temporarily store the address of the transition to
the subroutine.

In general, in order to satisfy all possible programs and still have a fast-running emulator, I had to make a compromise. undocumented flags are not always (but only most
of the time) emulated correctly, but I won't tell you when they are not.

=== Cut ===


Cordially Yours, Alexey