// 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 = "DCP"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xc3, .addressing_mode = .@"(Indirect, X)", .cycles = 8, }, .{ .opcode = 0xc7, .addressing_mode = .@"Zero Page", .cycles = 5, }, .{ .opcode = 0xcf, .addressing_mode = .Absolute, .cycles = 6, }, .{ .opcode = 0xd3, .addressing_mode = .@"(Indirect), Y", .cycles = 8, }, .{ .opcode = 0xd7, .addressing_mode = .@"Zero Page, X", .cycles = 6, }, .{ .opcode = 0xdb, .addressing_mode = .@"Absolute, Y", .cycles = 7, }, .{ .opcode = 0xdf, .addressing_mode = .@"Absolute, X", .cycles = 7, }, }; /// 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 { const f = struct { /// A DEC of the addressed byte, with the accumulator then /// compared against the decremented value. pub fn DCP(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); const value = cpu.memory.read(u8, address); // a read-modify-write drives the unmodified value back onto // the bus before the modified one cpu.memory.write(u8, address, value); const result = value -% 1; cpu.memory.write(u8, address, result); const register = cpu.registers.get(.A); cpu.flags.update(.C, register >= result); cpu.flags.updateNZ(register -% result); } }; return f.DCP; } }; }