// 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 = "BVC"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x50, .addressing_mode = .Relative, .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) error{}!void { if (addressing_mode != .Relative) @compileError("illegal addressing mode"); const f = struct { /// Branches when overflow is clear. /// /// (MOS Technology, 1976, Section 4.1.2.8, p. 41) /// (Carr, 1984, p. 252) pub fn BVC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); if (cpu.flags.isClear(.V)) cpu.program_counter.branch(address); } }; return f.BVC; } }; } test "BVC 1" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); cpu.program_counter.value = 0x0080; try cpu.memory.loadProgram(0x0080, &[_]u8{ 0x50, 0x16 }); const actual = try cpu.executeInstruction(); try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0098), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 3), 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)); }