Decoding Instructions
This section lists how the instructions are laid out at the bit level.
Normally if you are manually disassembling code you just need to use the list by Opcodes, however this section will be useful if you are implementing a Z80 emulator as you can see how the instruction decoding works including how the undocumented instructions work due to how the bits are organised.
How to use these decoding tables
To decode an opcode, convert it to binary then run through it from left to right, e.g. start at Bit 7 and move towards Bit 0.
As you run through the bits, start on the table from the top-left and go down then right as you find each bit. Bits are ordered with 0 first, then 1 & finally x which indicates that bit can be either 0 or 1.
When you find a match then go with that. If more than one entry matches then go for the one higher in the table as that will have higher precedence.
Z80 Instruction Decode table
To decode an instruction:
- If the opcode is CB, DD, ED or FD then go to that prefix page and decode the next byte.
-
Using the table below, locate the pattern that matches the opcode.
It's usually best to start from the left (bit 7) and go right until you find the opcode.
An
X
means a bit that can be either 0 or 1, however check adjacent rows first and always take an entry that has a 0 or 1 as higher precedence to the X.
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ||
---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Nop | |
0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | EX | |
0 | 0 | 0 | 1 | x | 0 | 0 | 0 | Flow | |
0 | 0 | 0 | x | x | 1 | 1 | 1 | Rotate | |
0 | 0 | 1 | x | x | 1 | 1 | 1 | Misc | |
0 | 0 | x | x | 0 | 0 | 0 | 1 | LD Instructions | |
0 | 0 | x | x | 0 | 0 | 1 | 1 | Arithmetic Instructions | |
0 | 0 | x | x | 0 | 1 | 0 | 0 | Arithmetic Instructions | |
0 | 0 | x | x | 0 | 1 | 0 | 1 | Arithmetic Instructions | |
0 | 0 | x | x | 0 | 1 | 1 | 0 | LD Instructions | |
0 | 0 | x | x | 1 | 0 | 0 | 1 | Arithmetic Instructions | |
0 | 0 | x | x | x | 0 | 0 | 0 | Flow | |
0 | 0 | x | x | x | 0 | 1 | 0 | LD Instructions | |
0 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | Halt | |
0 | 1 | x | x | x | x | x | x | LD Instructions | |
1 | 0 | x | x | x | x | x | x | Arithmetic Instructions | |
1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | Flow | |
1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | CB Prefix | |
1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | Flow | |
1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | EXX | |
1 | 1 | 0 | 1 | x | 0 | 1 | 1 | I/O | |
1 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | ED Prefix | |
1 | 1 | 1 | 0 | x | 0 | 1 | 1 | EX | |
1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | LD Instructions | |
1 | 1 | 1 | 1 | x | 0 | 1 | 1 | Interrupts | |
1 | 1 | x | 0 | 1 | 0 | 0 | 1 | Flow | |
1 | 1 | x | 1 | 1 | 1 | 0 | 1 | DD FD Prefix | |
1 | 1 | x | x | 0 | x | 0 | 1 | Stack Instructions | |
1 | 1 | x | x | x | 0 | 0 | 0 | Flow | |
1 | 1 | x | x | x | 0 | 1 | 0 | Flow | |
1 | 1 | x | x | x | 1 | 0 | 0 | Flow | |
1 | 1 | x | x | x | 1 | 1 | 0 | Arithmetic Instructions | |
1 | 1 | x | x | x | 1 | 1 | 1 | Flow |
Notes:
- Halt 0x76 is where the invalid LD (HL),(HL) instruction would have been.