// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT /// Describes one instruction: its mnemonic, and every opcode that selects /// it together with that opcode's addressing mode and cycle count. Opcodes /// collects these at comptime to build the dispatch table. pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "ANC"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x0b, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x2b, .addressing_mode = .Immediate, .cycles = 2, }, }; /// The implementation for a single addressing mode. Resolved at /// comptime so the dispatch table holds a plain function pointer with /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); const f = struct { /// AND with an immediate, with the sign of the result also /// copied into carry. pub fn ANC(cpu: *CPU) error{}!void { const value = cpu.memory.read(u8, cpu.addressing_modes.decode(addressing_mode, .read)); const result = cpu.registers.get(.A) & value; cpu.registers.set(.A, result); cpu.flags.updateNZ(result); // carry comes out of bit 7, as though the AND were followed // by a ROL cpu.flags.update(.C, result & 0x80 == 0x80); } }; return f.ANC; } }; }