// 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 = "XCE"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xfb, .addressing_mode = .Implied, .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, comptime widths: ?CPU.Widths, ) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); _ = widths; const f = struct { /// Swaps the carry flag with the emulation line, which is the /// only way to reach that line at all. /// /// It is the instruction the whole processor turns on. A /// 65816 comes up in emulation mode, indistinguishable from a /// 6502, and `CLC` followed by this is how a program says it /// would like the other machine. `SEC` and this is how it /// goes back — which is what makes a IIgs able to run software /// written for a IIe. /// /// Going into emulation is not only a change of mind: the /// width flags are forced set and the stack pointer is pulled /// back into page one, because a 6502 has nowhere else to put /// it. Coming out leaves the widths alone, so a program that /// wants sixteen bits asks for them with REP afterwards. pub fn XCE(cpu: *CPU) !void { const carry = cpu.flags.isSet(.C); // The internal cycle comes first. The emulation line is an // output, and during that cycle it is still driving what it // held: the recordings show `e` asserted throughout an XCE // that is on its way out of emulation mode. Swapping before // spending the cycle would put the new value on the bus a // cycle early. cpu.program_counter.internal(); cpu.flags.update(.C, cpu.flags.emulation); cpu.flags.setEmulation(carry); } }; return f.XCE; } }; }