// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! Clearing one bit of a byte in the zero page, and setting it: sixteen //! instructions between them, one for each bit either way. //! //! Rockwell added these to their CMOS part and Western Design kept them; the //! plain 65C02 has neither, and the encodings do nothing there. They are //! generated rather than written out because there is nothing to tell one from //! another but which bit it moves, and thirty-two files saying that would be //! thirty-two chances to say it wrong. /// One of them: which bit, and which way. pub fn at(comptime which: u3, comptime set: bool) type { return struct { pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = (if (set) "SMB" else "RMB") ++ .{'0' + @as(u8, which)}; pub const opcodes = [_]CPU.Opcodes.Init{ .{ // The bit is the top three bits of the opcode, and // whether it is set or cleared is the one above them. .opcode = (if (set) 0x87 else 0x07) | @as(u8, which) << 4, .addressing_mode = .@"Zero Page", .cycles = 5, }, }; pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) error{}!void { if (addressing_mode != .@"Zero Page") @compileError("illegal addressing mode"); const f = struct { /// Moves one bit and leaves every flag alone, which is /// what makes these worth having: a flag byte can be /// changed without disturbing what is being tested /// around it. pub fn move(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); const value = cpu.memory.read(u8, address); // The CMOS read-modify-write: a second read rather // than the old value written back. _ = cpu.memory.read(u8, address); const bit = @as(u8, 1) << which; cpu.memory.write(u8, address, if (set) value | bit else value & ~bit); } }; return f.move; } }; } }; }