// 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 = "CLV"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xb8, .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) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { /// Clears the overflow flag. /// /// (MOS Technology, 1976, Section 3.6.1, p. 28) /// (Carr, 1984, p. 254) pub fn CLV(cpu: *CPU) CPU.Opcodes.Error!void { cpu.flags.clear(.V); // the operand fetch every single byte instruction performs // and discards cpu.program_counter.phantomRead(); } }; return f.CLV; } }; } test "CLV" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); cpu.flags.set(.V); try cpu.memory.loadProgram(0x0000, &[_]u8{0xb8}); const actual = try cpu.executeInstruction(); try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0001), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 2), cpu.clock.cycles); try std.testing.expect(cpu.flags.isClear(.N)); try std.testing.expect(cpu.flags.isClear(.V)); try std.testing.expect(cpu.flags.isClear(.B)); try std.testing.expect(cpu.flags.isClear(.D)); try std.testing.expect(cpu.flags.isClear(.I)); try std.testing.expect(cpu.flags.isSet(.Z)); try std.testing.expect(cpu.flags.isClear(.C)); }