// 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 = "LDX"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xa2, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xa6, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0xae, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xb6, .addressing_mode = .@"Zero Page, Y", .cycles = 4, }, .{ .opcode = 0xbe, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, }; /// 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 { /// Loads the X register from memory. /// /// (MOS Technology, 1976, Section 7.0, p. 96) /// (Carr, 1984, p. 264) pub fn LDX(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); cpu.registers.set(.X, value); cpu.flags.updateNZ(value); } }; return f.LDX; } }; }