Video summary
Gameboy Emulator Development - Part 07
Main summary
Key takeaways
Summary (Game Boy Emulator Development - Part 07)
This video continues building a Game Boy CPU emulator, focusing on implementing arithmetic and increment/decrement instructions, including accurate cycle timing and CPU flag behavior.
1) Instruction map updates (core work)
The developer rapidly adds new opcode handlers into the emulator’s instruction map, covering:
- Increment/Decrement-style instructions (“inc” and “dec”, including variants for registers and memory destinations)
- Add-family instructions:
ADD,ADC(add with carry),SUB, andSBC(subtract with carry)
Note: Some decrement values are mentioned as being “revalidated later,” since opcodes were added quickly and mistakes may have occurred.
2) Increment (INC) implementation details
A new handler proc inc is added, with special care for:
- 16-bit operand behavior
- Introduces an
is 16 bit(...)-style helper using the same pattern used earlier inLDto check whether the register is treated as 16-bit.
- Introduces an
- Extra cycle for 16-bit operations
- Explicitly mentioned: “need to add one extra cycle.”
- HL memory destination handling
- If the destination is
HLand the operand is memory ((HL)):- Read the byte at
HL - Increment it
- Write it back
- Read the byte at
- Uses bottom-byte masking (
& 0xff/0xff) to ensure correct wrap-around.
- If the destination is
- Flag modification rules
- For certain opcode variants (referenced as patterns ending like
...x3and...xB), the handler may skip flag updates because those cases do not modify CPU flags. - Otherwise it sets:
Z(zero flag) if the result is zeroN(subtract flag) forced to0for incrementH(half-carry) computed as required- Carry
Cis not modified (“don’t touch the carry flag”).
- For certain opcode variants (referenced as patterns ending like
3) Decrement (DEC) implementation
- A
proc deck/proc decis created similarly to INC. - Uses decrement opcode table expectations:
- Result is decreased by
1 - Flag behavior differs, notably setting
N = 1for decrement - Carry handling follows DEC rules
- Result is decreased by
- As with INC, memory/register variants require careful flag and wrap-around handling.
4) ADD implementation (proc add) is the most complex part
A new proc add handles multiple operand sizes and special cases:
- Reads the operand from
reg1plusfetch data(using a wider type likeu32to avoid overflow issues during computation). - Uses the same
is 16 bitlogic to branch:
16-bit ADD instructions
Zflag is not modifiedHandClogic uses different thresholds compared to 8-bit adds- The video explicitly distinguishes constants like
0x0fff/0x1000-style logic.
- The video explicitly distinguishes constants like
8-bit ADD instructions
- Sets
Z,N,H, andCaccording to standard Game Boy rules:- Half-carry uses nibble carry logic
- Carry uses full-byte overflow (
>= 0x100)
Special handling when reg1 is SP
- Uses a signed-like immediate behavior for the operand (so it subtracts because there is no subversion).
- Flags derived from an SP + signed-immediate style computation:
Zcleared (set to0)Ncleared (0)HandCcomputed using bottom bits / byte overflow logic
- Mentions stack-pointer variants may duplicate 8-bit patterns but are rewritten for readability.
5) ADC, SUB, SBC implementations (simpler)
ADC (proc adc)
- Implemented as add with carry, operating primarily on accumulator
A. - Computes:
A = A + value + C- Wraps to 8-bit (
& 0xff)
- Half-carry and carry are computed;
Nis cleared.
SUB and SBC (proc sub, proc sbc)
proc subreg = reg - fetched data- Sets
Z,N=1,H, andCusing nibble/buffer comparisons (with type-casting fixes noted).
proc sbc- Subtract with carry:
A = A - fetched data - C - Similar flag logic to SUB, but includes the borrow term.
- Subtract with carry:
- The developer corrects types/thresholds after initial implementation:
- Fixes to
sbcusingu8 - Adjustments to comparison constants like
>= 0x100and half-carry thresholds.
- Fixes to
6) Sanity checks & timing verification
- ROM tests are performed to validate behavior, including:
- Example validation: correct auto-increment behavior for
LD (HL+)-style instructions- Observes values jumping in memory/register as
HLincrements throughHL+.
- Observes values jumping in memory/register as
- Timing checks:
- Mentions memory timing / cycle behavior while testing interactions involving
incandld.
- Mentions memory timing / cycle behavior while testing interactions involving
- Example validation: correct auto-increment behavior for
7) Debug/logging improvements (for emulator development)
Before stopping, the video adds better visibility into CPU state:
- In
cpu.c, adds a flags display string:Z,N,H,Cshown as set or-(dash)
- Adds logging for the current emulator tick / cycle count
- Using a formatting width like ~8 characters
- Helps track where execution stops (one ROM halted after reaching ~3032 ticks).
8) Next steps preview
- The next video is expected to cover bit operations.
- The series continues toward completing the remaining CPU instructions across subsequent episodes.
Main speakers / sources
- Primary speaker/source: The video author (“low level dev / low level devil” channel host), narrating the Game Boy emulator development series.