A switch statement routes execution according to a value. In a CHIP-8 interpreter, switching on the instruction’s first nibble starts the decoding process; each case can implement an instruction, while an `else` branch can return `error.UnknownInstruction`. section 6.5.3
Switches also pair with enums to handle distinct options. When every enum case is covered, Zig can omit `else` and alert you at compile time if a newly added variant needs another branch. section 5.2.4
Listing 6.16 Set the I register
switch (nibbles[0]) {
0xA => {
// set I to const #1
const addr: u12 = (@as(u12, nibbles[1]) << 8) + bytecode[1]; #2
memory.i = addr; #3
},
else => {
return error.UnknownInstruction; #4
}
}
See a CHIP-8 decoder case that builds an address from instruction bytes, sets the I register, and rejects unknown instructions. section 6.5.3










