gbz80 —
CPU
opcode reference
This is the list of opcodes supported by
rgbasm(1),
including a short description, the number of bytes needed to encode them and
the number of CPU cycles at 1MHz (or 2MHz in GBC dual speed mode) needed to
complete them.
Note: All arithmetic/logic operations that use register
A as destination can omit the destination as it
is assumed it's register
A. The following two
lines have the same effect:
List of abbreviations used in this document.
-
-
- r8
- Any of the 8-bit registers (A,
B, C,
D, E,
H, L).
-
-
- r16
- Any of the general-purpose 16-bit registers
(BC, DE,
HL).
-
-
- n8
- 8-bit integer constant.
-
-
- n16
- 16-bit integer constant.
-
-
- e8
- 8-bit offset
(-128 to
127).
-
-
- u3
- 3-bit unsigned integer constant
(0 to 7).
-
-
- cc
- Condition codes:
- Z:
- Execute if Z is set.
- NZ:
- Execute if Z is not set.
- C:
- Execute if C is set.
- NC:
- Execute if C is not set.
-
-
- vec
- One of the RST vectors
(0x00, 0x08,
0x10, 0x18,
0x20, 0x28,
0x30 and
0x38).
- ADC
A,r8
- ADC
A,[HL]
- ADC
A,n8
- ADD
A,r8
- ADD
A,[HL]
- ADD
A,n8
- AND
A,r8
- AND
A,[HL]
- AND
A,n8
- CP A,r8
- CP
A,[HL]
- CP A,n8
- DEC r8
- DEC
[HL]
- INC r8
- INC
[HL]
- OR A,r8
- OR
A,[HL]
- OR A,n8
- SBC
A,r8
- SBC
A,[HL]
- SBC
A,n8
- SUB
A,r8
- SUB
A,[HL]
- SUB
A,n8
- XOR
A,r8
- XOR
A,[HL]
- XOR
A,n8
- ADD
HL,r16
- DEC r16
- INC r16
- BIT
u3,r8
- BIT
u3,[HL]
- RES
u3,r8
- RES
u3,[HL]
- SET
u3,r8
- SET
u3,[HL]
- SWAP r8
- SWAP
[HL]
- RL r8
- RL [HL]
- RLA
- RLC r8
- RLC
[HL]
- RLCA
- RR r8
- RR [HL]
- RRA
- RRC r8
- RRC
[HL]
- RRCA
- SLA r8
- SLA
[HL]
- SRA r8
- SRA
[HL]
- SRL r8
- SRL
[HL]
- LD
r8,r8
- LD
r8,n8
- LD
r16,n16
- LD
[HL],r8
- LD
[HL],n8
- LD
r8,[HL]
- LD
[r16],A
- LD
[n16],A
- LD
[$FF00+n8],A
- LD
[$FF00+C],A
- LD
A,[r16]
- LD
A,[n16]
- LD
A,[$FF00+n8]
- LD
A,[$FF00+C]
- LD
[HL+],A
- LD
[HL-],A
- LD
A,[HL+]
- LD
A,[HL-]
- CALL
n16
- CALL
cc,n16
- JP HL
- JP n16
- JP
cc,n16
- JR e8
- JR
cc,e8
- RET cc
- RET
- RETI
- RST vec
- ADD
HL,SP
- ADD
SP,e8
- DEC SP
- INC SP
- LD
SP,n16
- LD
[n16],SP
- LD
HL,SP+e8
- LD
SP,HL
- POP AF
- POP r16
- PUSH AF
- PUSH
r16
- CCF
- CPL
- DAA
- DI
- EI
- HALT
- NOP
- SCF
- STOP
Add the value in
r8 plus the carry flag to
A.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 0
- H: Set if overflow from bit
3.
- C: Set if overflow from bit
7.
Add the value pointed by
HL plus the carry flag to
A.
Cycles: 2
Bytes: 1
Flags: See
ADC A,r8
Add the value
n8 plus the carry flag to
A.
Cycles: 2
Bytes: 2
Flags: See
ADC A,r8
Add the value in
r8 to
A.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 0
- H: Set if overflow from bit
3.
- C: Set if overflow from bit
7.
Add the value pointed by
HL to
A.
Cycles: 2
Bytes: 1
Flags: See
ADD A,r8
Add the value
n8 to
A.
Cycles: 2
Bytes: 2
Flags: See
ADD A,r8
Add the value in
r16 to
HL.
Cycles: 2
Bytes: 1
Flags:
- N: 0
- H: Set if overflow from bit
11.
- C: Set if overflow from bit
15.
Add the value in
SP to
HL.
Cycles: 2
Bytes: 1
Flags: See
ADD HL,r16
Add the signed value
e8 to
SP.
Cycles: 4
Bytes: 2
Flags:
- Z: 0
- N: 0
- H: Set if overflow from bit
3.
- C: Set if overflow from bit
7.
Bitwise AND between the value in
r8 and
A.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 0
- H: 1
- C: 0
Bitwise AND between the value pointed by
HL and
A.
Cycles: 2
Bytes: 1
Flags: See
AND A,r8
Bitwise AND between the value in
n8 and
A.
Cycles: 2
Bytes: 2
Flags: See
AND A,r8
Test bit
u3 in register
r8, set the zero flag if bit not set.
Cycles: 2
Bytes: 2
Flags:
- Z: Set if the selected bit
is 0.
- N: 0
- H: 1
Test bit
u3 in the byte pointed by
HL, set the zero flag if bit not set.
Cycles: 3
Bytes: 2
Flags: See
BIT u3,r8
Call address
n16.
Cycles: 6
Bytes: 3
Flags: None affected.
Call address
n16 if condition
cc is met.
Cycles: 6/3
Bytes: 3
Flags: None affected.
Complement Carry Flag.
Cycles: 1
Bytes: 1
Flags:
- N: 0
- H: 0
- C: Complemented.
Subtract the value in
r8 from
A and set flags accordingly, but don't store the
result.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 1
- H: Set if no borrow from
bit 4.
- C: Set if no borrow (set if
r8 >
A).
Subtract the value pointed by
HL from
A and set flags accordingly, but don't store the
result.
Cycles: 2
Bytes: 1
Flags: See
CP A,r8
Subtract the value
n8 from
A and set flags accordingly, but don't store the
result.
Cycles: 2
Bytes: 2
Flags: See
CP A,r8
Complement accumulator (
A =
~A).
Cycles: 1
Bytes: 1
Flags:
Decimal adjust register A to get a correct BCD representation after an
arithmetic instruction.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- H: 0
- C: Set or reset depending
on the operation.
Decrement value in register
r8 by 1.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 1
- H: Set if no borrow from
bit 4.
Decrement the value pointed by
HL by 1.
Cycles: 3
Bytes: 1
Flags: See
DEC r8
Decrement value in register
r16 by 1.
Cycles: 2
Bytes: 1
Flags: None affected.
Decrement value in register
SP by 1.
Cycles: 2
Bytes: 1
Flags: None affected.
Disable Interrupts.
Cycles: 1
Bytes: 1
Flags: None affected.
Enable Interrupts.
Cycles: 1
Bytes: 1
Flags: None affected.
Enter CPU low power mode.
Cycles: -
Bytes: 1
Flags: None affected.
Increment value in register
r8 by 1.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 0
- H: Set if overflow from bit
3.
Increment the value pointed by
HL by 1.
Cycles: 3
Bytes: 1
Flags: See
INC r8
Increment value in register
r16 by 1.
Cycles: 2
Bytes: 1
Flags: None affected.
Increment value in register
SP by 1.
Cycles: 2
Bytes: 1
Flags: None affected.
Absolute jump to address
n16.
Cycles: 4
Bytes: 3
Flags: None affected.
Absolute jump to address
n16 if condition
cc is met.
Cycles: 4/3
Bytes: 3
Flags: None affected.
Jump to address in
HL, that is, load
PC with value in register
HL.
Cycles: 1
Bytes: 1
Flags: None affected.
Relative jump by adding
e8 to the current
address.
Cycles: 3
Bytes: 2
Flags: None affected.
Relative jump by adding
e8 to the current
address if condition
cc is met.
Cycles: 3/2
Bytes: 2
Flags: None affected.
Store value in register on the right into register on the left.
Cycles: 1
Bytes: 1
Flags: None affected.
Load value
n8 into register
r8.
Cycles: 2
Bytes: 2
Flags: None affected.
Load value
n16 into register
r16.
Cycles: 3
Bytes: 3
Flags: None affected.
Store value in register
r8 into byte pointed by
register
HL.
Cycles: 2
Bytes: 1
Flags: None affected.
Store value
n8 into byte pointed by register
HL.
Cycles: 3
Bytes: 2
Flags: None affected.
Load value into register
r8 from byte pointed
by register
HL.
Cycles: 2
Bytes: 1
Flags: None affected.
Store value in register
A into address pointed by
register
r16.
Cycles: 2
Bytes: 1
Flags: None affected.
Store value in register
A into address
n16.
Cycles: 4
Bytes: 3
Flags: None affected.
Store value in register
A into high RAM or I/O
registers.
The following synonym forces this encoding:
LDH
[$FF00+n8],A
Cycles: 3
Bytes: 2
Flags: None affected.
Store value in register
A into high RAM or I/O
registers.
Cycles: 2
Bytes: 1
Flags: None affected.
Load value in register
A from address pointed by
register
r16.
Cycles: 2
Bytes: 1
Flags: None affected.
Load value in register
A from address
n16.
Cycles: 4
Bytes: 3
Flags: None affected.
Load value in register
A from high RAM or I/O
registers.
The following synonym forces this encoding:
LDH
A,[$FF00+n8]
Cycles: 3
Bytes: 2
Flags: None affected.
Load value in register
A from high RAM or I/O
registers.
Cycles: 2
Bytes: 1
Flags: None affected.
Store value in register
A into byte pointed by
HL and post-increment
HL.
Cycles: 2
Bytes: 1
Flags: None affected.
Store value in register
A into byte pointed by
HL and post-decrement
HL.
Cycles: 2
Bytes: 1
Flags: None affected.
Load value into register
A from byte pointed by
HL and post-increment
HL.
Cycles: 2
Bytes: 1
Flags: None affected.
Load value into register
A from byte pointed by
HL and post-decrement
HL.
Cycles: 2
Bytes: 1
Flags: None affected.
Load value
n16 into register
SP.
Cycles: 3
Bytes: 3
Flags: None affected.
Store
SP into addresses
n16 (LSB) and
n16 + 1 (MSB).
Cycles: 5
Bytes: 3
Flags: None affected.
Add the signed value
e8 to
SP and store the result in
HL.
Cycles: 3
Bytes: 2
Flags:
- Z: 0
- N: 0
- H: Set if overflow from bit
3.
- C: Set if overflow from bit
7.
Load register
HL into register
SP.
Cycles: 2
Bytes: 1
Flags: None affected.
No operation.
Cycles: 1
Bytes: 1
Flags: None affected.
Bitwise OR between the value in
r8 and
A.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: 0
Bitwise OR between the value pointed by
HL and
A.
Cycles: 2
Bytes: 1
Flags: See
OR A,r8
Bitwise OR between the value in
n8 and
A.
Cycles: 2
Bytes: 2
Flags: See
OR A,r8
Pop register
AF from the stack.
Cycles: 3
Bytes: 1
Flags:
- Z: Set from bit 7 of the
popped low byte.
- N: Set from bit 6 of the
popped low byte.
- H: Set from bit 5 of the
popped low byte.
- C: Set from bit 4 of the
popped low byte.
Pop register
r16 from the stack.
Cycles: 3
Bytes: 1
Flags: None affected.
Push register
AF into the stack. The low byte's bit
7 corresponds to the
Z flag, its bit 6 to the
N flag, bit 5 to the
H flag, and bit 4 to the
C flag. Bits 3 to 0 are reset.
Cycles: 4
Bytes: 1
Flags: None affected.
Push register
r16 into the stack.
Cycles: 4
Bytes: 1
Flags: None affected.
Set bit
u3 in register
r8 to 0.
Cycles: 2
Bytes: 2
Flags: None affected.
Set bit
u3 in the byte pointed by
HL to 0.
Cycles: 4
Bytes: 2
Flags: None affected.
Return from subroutine.
Cycles: 4
Bytes: 1
Flags: None affected.
Return from subroutine if condition
cc is met.
Cycles: 5/2
Bytes: 1
Flags: None affected.
Return from subroutine and enable interrupts.
Cycles: 4
Bytes: 1
Flags: None affected.
Rotate register
r8 left through carry.
C <- [7 <- 0] <- C
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: Set according to
result.
Rotate value pointed by
HL left through carry.
C <- [7 <- 0] <- C
Cycles: 4
Bytes: 2
Flags: See
RL r8
Rotate register
A left through carry.
C <- [7 <- 0] <- C
Cycles: 1
Bytes: 1
Flags:
- Z: 0
- N: 0
- H: 0
- C: Set according to
result.
Rotate register
r8 left.
C <- [7 <- 0] <- [7]
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: Set according to
result.
Rotate value pointed by
HL left.
C <- [7 <- 0] <- [7]
Cycles: 4
Bytes: 2
Flags: See
RLC r8
Rotate register
A left.
C <- [7 <- 0] <- [7]
Cycles: 1
Bytes: 1
Flags:
- Z: 0
- N: 0
- H: 0
- C: Set according to
result.
Rotate register
r8 right through carry.
C -> [7 -> 0] -> C
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: Set according to
result.
Rotate value pointed by
HL right through carry.
C -> [7 -> 0] -> C
Cycles: 4
Bytes: 2
Flags: See
RR r8
Rotate register
A right through carry.
C -> [7 -> 0] -> C
Cycles: 1
Bytes: 1
Flags:
- Z: 0
- N: 0
- H: 0
- C: Set according to
result.
Rotate register
r8 right.
[0] -> [7 -> 0] -> C
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: Set according to
result.
Rotate value pointed by
HL right.
[0] -> [7 -> 0] -> C
Cycles: 4
Bytes: 2
Flags: See
RRC r8
Rotate register
A right.
[0] -> [7 -> 0] -> C
Cycles: 1
Bytes: 1
Flags:
- Z: 0
- N: 0
- H: 0
- C: Set according to
result.
Call restart vector
vec.
Cycles: 4
Bytes: 1
Flags: None affected.
Subtract the value in
r8 and the carry flag
from
A.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 1
- H: Set if no borrow from
bit 4.
- C: Set if no borrow (set if
r8 >
A).
Subtract the value pointed by
HL and the carry flag
from
A.
Cycles: 2
Bytes: 1
Flags: See
SBC A,r8
Subtract the value
n8 and the carry flag from
A.
Cycles: 2
Bytes: 2
Flags: See
SBC A,r8
Set Carry Flag.
Cycles: 1
Bytes: 1
Flags:
Set bit
u3 in register
r8 to 1.
Cycles: 2
Bytes: 2
Flags: None affected.
Set bit
u3 in the byte pointed by
HL to 1.
Cycles: 4
Bytes: 2
Flags: None affected.
Shift left arithmetic register
r8.
C <- [7 <- 0] <- 0
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: Set according to
result.
Shift left arithmetic value pointed by
HL.
C <- [7 <- 0] <- 0
Cycles: 4
Bytes: 2
Flags: See
SLA r8
Shift right arithmetic register
r8.
[7] -> [7 -> 0] -> C
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: Set according to
result.
Shift right arithmetic value pointed by
HL.
[7] -> [7 -> 0] -> C
Cycles: 4
Bytes: 2
Flags: See
SRA r8
Shift right logic register
r8.
0 -> [7 -> 0] -> C
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: Set according to
result.
Shift right logic value pointed by
HL.
0 -> [7 -> 0] -> C
Cycles: 4
Bytes: 2
Flags: See
SRA r8
Enter CPU very low power mode. Also used to switch between double and normal
speed CPU modes in GBC.
Cycles: -
Bytes: 2
Flags: None affected.
Subtract the value in
r8 from
A.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 1
- H: Set if no borrow from
bit 4.
- C: Set if no borrow (set if
r8 >
A).
Subtract the value pointed by
HL from
A.
Cycles: 2
Bytes: 1
Flags: See
SUB A,r8
Subtract the value
n8 from
A.
Cycles: 2
Bytes: 2
Flags: See
SUB A,r8
Swap upper 4 bits in register
r8 and the lower
ones.
Cycles: 2
Bytes: 2
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: 0
Swap upper 4 bits in the byte pointed by
HL and the
lower ones.
Cycles: 4
Bytes: 2
Flags: See
SWAP r8
Bitwise XOR between the value in
r8 and
A.
Cycles: 1
Bytes: 1
Flags:
- Z: Set if result is 0.
- N: 0
- H: 0
- C: 0
Bitwise XOR between the value pointed by
HL and
A.
Cycles: 2
Bytes: 1
Flags: See
XOR A,r8
Bitwise XOR between the value in
n8 and
A.
Cycles: 2
Bytes: 2
Flags: See
XOR A,r8
rgbasm(1),
rgbds(7)
rgbds was originally written by Carsten
Sørensen as part of the ASMotor package, and was later packaged in
RGBDS by Justin Lloyd. It is now maintained by a number of contributors at
https://github.com/rednex/rgbds.