Difference between revisions of "GBZ80"
(→Standard opcodes) |
(→Registers) |
||
Line 29: | Line 29: | ||
|- | |- | ||
| PC (Program Counter) || 16-bit || Points to the next instruction || Automatically increments as instructions execute | | PC (Program Counter) || 16-bit || Points to the next instruction || Automatically increments as instructions execute | ||
+ | |} | ||
+ | |||
+ | == GBZ80 instructions == | ||
+ | |||
+ | === 8-bit ALU group === | ||
+ | |||
+ | {| class="wikitable" style="white-space: nowrap;" | ||
+ | |- | ||
+ | ! Instruction !! Opcode !! Cycles !! Z !! N !! H !! C !! Effect !! Description | ||
+ | |- | ||
+ | | inc r || 00rrr100 || 1 || rowspan=2|+ || rowspan=2|0 || rowspan=2|+ || rowspan=2|- || r += 1 ||rowspan=2|Increment | ||
+ | |- | ||
+ | | inc (hl) || 00110100 || 3 || (hl) += 1 | ||
+ | |- | ||
+ | | dec r || 00rrr101 || 1 || rowspan=2|+ || rowspan=2|1 || rowspan=2|+ || rowspan=2|- || r -= 1 ||rowspan=2|Decrement | ||
+ | |- | ||
+ | | dec (hl) || 00110101 || 3 || (hl) -= 1 | ||
+ | |- | ||
+ | | add a,r || 10000rrr || 1 || rowspan=3|+ || rowspan=3|0 || rowspan=3|+ || rowspan=3|+ || a += r ||rowspan=3|Add | ||
+ | |- | ||
+ | | add a,(hl) || 10000110 || 2 || a += (hl) | ||
+ | |- | ||
+ | | add a,n || 11000110 nnnnnnnn || 2 || a += n | ||
+ | |- | ||
+ | | adc a,r || 10001rrr || 1 || rowspan=3|+ || rowspan=3|0 || rowspan=3|+ || rowspan=3|+ || a += r + cf ||rowspan=3|Add with Carry | ||
+ | |- | ||
+ | | adc a,(hl) || 10001110 || 2 || a += (hl) + cf | ||
+ | |- | ||
+ | | adc a,n || 11001110 nnnnnnnn || 2 || a += n + cf | ||
+ | |- | ||
+ | | sub r || 10010rrr || 1 || rowspan=3|+ || rowspan=3|1 || rowspan=3|+ || rowspan=3|+ || a -= r ||rowspan=3|Subtract | ||
+ | |- | ||
+ | | sub (hl) || 10010110 || 2 || a -= (hl) | ||
+ | |- | ||
+ | | sub n || 11010110 nnnnnnnn || 2 || a -= n | ||
+ | |- | ||
+ | | sbc a,r || 10011rrr || 1 || rowspan=3|+ || rowspan=3|1 || rowspan=3|+ || rowspan=3|+ || a -= r + cf ||rowspan=3|Subtract with Carry | ||
+ | |- | ||
+ | | sbc a,(hl) || 10011110 || 2 || a -= (hl) + cf | ||
+ | |- | ||
+ | | sbc a,n || 11011110 nnnnnnnn || 2 || a -= n + cf | ||
+ | |- | ||
+ | | and r || 10100rrr || 1 || rowspan=3|+ || rowspan=3|0 || rowspan=3|1 || rowspan=3|0 || a := a and r ||rowspan=3|Logical AND | ||
+ | |- | ||
+ | | and (hl) || 10100110 || 2 || a := a and (hl) | ||
+ | |- | ||
+ | | and n || 11100110 nnnnnnnn || 2 || a := a and n | ||
+ | |- | ||
+ | | xor r || 10101rrr || 1 || rowspan=3|+ || rowspan=3|0 || rowspan=3|0 || rowspan=3|0 || a := a xor r ||rowspan=3|Logical eXclusive OR | ||
+ | |- | ||
+ | | xor (hl) || 10101110 || 2 || a := a xor (hl) | ||
+ | |- | ||
+ | | xor n || 11101110 nnnnnnnn || 2 || a := a xor n | ||
+ | |- | ||
+ | | or r || 10110rrr || 1 || rowspan=3|+ || rowspan=3|0 || rowspan=3|0 || rowspan=3|0 || a := a or r ||rowspan=3|Logical Inclusive OR | ||
+ | |- | ||
+ | | or (hl) || 10110110 || 2 || a := a or (hl) | ||
+ | |- | ||
+ | | or n || 11110110 nnnnnnnn || 2 || a := a or n | ||
+ | |- | ||
+ | | cp r || 10111rrr || 1 || rowspan=3|+ || rowspan=3|1 || rowspan=3|+ || rowspan=3|+ || tmp := a - r ||rowspan=3|Compare | ||
+ | |- | ||
+ | | cp (hl) || 10111110 || 2 || tmp := a - (hl) | ||
+ | |- | ||
+ | | cp n || 11111110 nnnnnnnn || 2 || tmp := a - n | ||
|} | |} | ||
Revision as of 10:55, 23 April 2025
The GBZ80 (Sharp SM83) is the CPU that powers the original Nintendo Gameboy and Gameboy Color handheld consoles. It is kind of an in-between the Intel 8080 and Z80. Awesome Gameboy resources GBDev wiki Emudev (q00.gb)
The GBZ80 lacks the alternate register set, the dedicated I/O bus, the R register, the index registers (thus no DD and FD prefixed opcodes), the ED prefixed opcodes (including block transfer), the sign and parity/overflow flags (and all conditional instructions that used them), the undocumented flags (thus no leaking of WZ and Q internal registers). GBZ80 opcodes
The GBZ80 also lacks the NMI pin (thus no IFF2 and no RETN), the IM instructions and the I register. It has a different interrupt system than the Z80. Source
The Nintendo documentation does not mention M-cycles or T-states at all. They only mention CPU cycles, which are always equal to 4 T-states (like NOPs in the CPC world). Also, the GBZ80 has different timings than the Z80. For example, CALL nn takes 6 cycles on the GBZ80, but only 5 NOPs on the Z80.
Fun fact: Way more GBZ80 cores were produced for Gameboy hardware (118 million Gameboys and 81 million GBA) than all the Z80 chips produced for home computers and game consoles. Learn GBZ80 Assembly Programming with ChibiAkumas
Contents
Registers
Register | Size | Description | Notes |
---|---|---|---|
B, C, D, E, H, L | 8-bit | General-purpose registers | Can form 16-bit pairs: BC, DE, HL |
A (Accumulator) | 8-bit | Main register for arithmetic, logic, and data transfer | Most used register |
F (Flags) | 8-bit |
|
NF and HF are used in the DAA algorithm |
SP (Stack Pointer) | 16-bit | Points to top of the stack | Used for subroutine calls and interrupt handling |
PC (Program Counter) | 16-bit | Points to the next instruction | Automatically increments as instructions execute |
GBZ80 instructions
8-bit ALU group
Instruction | Opcode | Cycles | Z | N | H | C | Effect | Description |
---|---|---|---|---|---|---|---|---|
inc r | 00rrr100 | 1 | + | 0 | + | - | r += 1 | Increment |
inc (hl) | 00110100 | 3 | (hl) += 1 | |||||
dec r | 00rrr101 | 1 | + | 1 | + | - | r -= 1 | Decrement |
dec (hl) | 00110101 | 3 | (hl) -= 1 | |||||
add a,r | 10000rrr | 1 | + | 0 | + | + | a += r | Add |
add a,(hl) | 10000110 | 2 | a += (hl) | |||||
add a,n | 11000110 nnnnnnnn | 2 | a += n | |||||
adc a,r | 10001rrr | 1 | + | 0 | + | + | a += r + cf | Add with Carry |
adc a,(hl) | 10001110 | 2 | a += (hl) + cf | |||||
adc a,n | 11001110 nnnnnnnn | 2 | a += n + cf | |||||
sub r | 10010rrr | 1 | + | 1 | + | + | a -= r | Subtract |
sub (hl) | 10010110 | 2 | a -= (hl) | |||||
sub n | 11010110 nnnnnnnn | 2 | a -= n | |||||
sbc a,r | 10011rrr | 1 | + | 1 | + | + | a -= r + cf | Subtract with Carry |
sbc a,(hl) | 10011110 | 2 | a -= (hl) + cf | |||||
sbc a,n | 11011110 nnnnnnnn | 2 | a -= n + cf | |||||
and r | 10100rrr | 1 | + | 0 | 1 | 0 | a := a and r | Logical AND |
and (hl) | 10100110 | 2 | a := a and (hl) | |||||
and n | 11100110 nnnnnnnn | 2 | a := a and n | |||||
xor r | 10101rrr | 1 | + | 0 | 0 | 0 | a := a xor r | Logical eXclusive OR |
xor (hl) | 10101110 | 2 | a := a xor (hl) | |||||
xor n | 11101110 nnnnnnnn | 2 | a := a xor n | |||||
or r | 10110rrr | 1 | + | 0 | 0 | 0 | a := a or r | Logical Inclusive OR |
or (hl) | 10110110 | 2 | a := a or (hl) | |||||
or n | 11110110 nnnnnnnn | 2 | a := a or n | |||||
cp r | 10111rrr | 1 | + | 1 | + | + | tmp := a - r | Compare |
cp (hl) | 10111110 | 2 | tmp := a - (hl) | |||||
cp n | 11111110 nnnnnnnn | 2 | tmp := a - n |
Opcodes
Opcode differences with Z80 are in bold. The unused (—) opcodes will lock up the Game Boy CPU when used. The asssembler syntax is from the official Nintendo Gameboy programming manual.
Standard opcodes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Note: Storing a register into itself is a no-op; however, some Game Boy emulators (such as BGB) interpret LD B,B as a breakpoint, or LD D,D as a debug message.
CB-prefixed opcodes
|
|
|
|
|
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|
|