// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// 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 = "SEP"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xe2, .addressing_mode = .Immediate, .cycles = 3, }, }; /// 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, comptime widths: ?CPU.Widths, ) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Immediate) @compileError("illegal addressing mode"); _ = widths; const f = struct { /// Sets every flag named by a one in the byte after the /// opcode, and leaves the rest alone. /// /// This and its opposite are how the width of the machine is /// chosen, since M and X are ordinary bits of the status /// register and so can be reached here along with the others. /// One instruction can make the accumulator sixteen bits wide /// and the indexes eight, which is the usual arrangement for /// walking an array of words. /// /// In emulation mode the width bits are refused whatever the /// mask asks for, because there they are not writable at all: /// a 6502 has no other width to offer. /// /// Three cycles: the opcode, the mask, and one spent applying /// it — and that last cycle is more particular than it looks. /// The address bus keeps the mask's address rather than moving /// on to the next instruction, and M and X are still driving /// what they drove before: the recordings show the old widths /// throughout, whichever way the mask is about to move them. /// The change lands when the cycle is over, which is the cycle /// it is there for. pub fn SEP(cpu: *CPU) !void { const at = cpu.program_counter.address(); const mask = cpu.program_counter.read(u8); cpu.memory.internal(at); inline for (comptime std.enums.values(CPU.Flags.Name)) |name| { if ((mask & comptime CPU.Flags.bitOf(name)) != 0) cpu.flags.set(name); } if (cpu.flags.emulation) { cpu.flags.set(.M); cpu.flags.set(.X); } } }; return f.SEP; } }; }