build-a-game-boy-emulator / lesson-34.md
Lesson 34 · Bit ops & the cartridge

Setting and clearing bits

Today you implement SET and RES, the instructions that turn a single bit on or off without disturbing any other bit or flag. Together with yesterday's BIT, they give you full control over individual bits and complete the CB-prefixed instruction table.

The goal

Implement SET 1, B and RES 1, B so they change one bit each and touch no flags.

Start here - the target
TO DO
Scenario: Setting then clearing one bit
GivenB is 0x00 and the opcode is CB 0xC8
WhenSET 1, B runs
ThenB is 0x02 and no flags changed
AndRES 1, B (CB opcode 0x88) on that result returns B to 0x00
Andthe mask touches exactly one bit - RES 1, B on 0xFF gives 0xFD, leaving every other bit alone
Background

SET b, r turns a single bit on; RES b, r (reset) turns it off. Both leave every other bit - and every flag - alone. SET 1, B OR-s in 1 << 1; RES 1, B AND-s out the same mask. Together with yesterday’s BIT, they give you full read/modify/write control over any individual bit in any register.

These complete the CB table and, with it, the entire instruction set your CPU needs. Games use SET/RES to toggle hardware feature bits - turning the LCD or a sound channel on and off - without disturbing the neighboring bits in the same register. Now that the processor is complete, the rest of the project turns outward: the cartridge, then the hardware the CPU talks to.

Make it work
case 0xC8: // SET 1, B
c.B |= 1 << 1
case 0x88: // RES 1, B
c.B &^= 1 << 1
// neither touches any flag
Further Reading

SET and RES - turn a single bit on or off; no flags affected.

CheckpointDONE
SET and RES now flip individual bits without disturbing flags. Commit and stop here.