// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const arithmetic = @import("../add.zig"); /// 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 = "SBC"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xe1, .addressing_mode = .@"(Direct Page, X)", .cycles = 6, }, .{ .opcode = 0xe3, .addressing_mode = .@"Stack Relative", .cycles = 4, }, .{ .opcode = 0xe5, .addressing_mode = .@"Direct Page", .cycles = 3, }, .{ .opcode = 0xe7, .addressing_mode = .@"[Direct Page]", .cycles = 6, }, .{ .opcode = 0xe9, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xed, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xef, .addressing_mode = .@"Absolute Long", .cycles = 5, }, .{ .opcode = 0xf1, .addressing_mode = .@"(Direct Page), Y", .cycles = 5, }, .{ .opcode = 0xf2, .addressing_mode = .@"(Direct Page)", .cycles = 5, }, .{ .opcode = 0xf3, .addressing_mode = .@"(Stack Relative), Y", .cycles = 7, }, .{ .opcode = 0xf5, .addressing_mode = .@"Direct Page, X", .cycles = 4, }, .{ .opcode = 0xf7, .addressing_mode = .@"[Direct Page], Y", .cycles = 6, }, .{ .opcode = 0xf9, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0xfd, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0xff, .addressing_mode = .@"Absolute Long, X", .cycles = 5, }, }; /// 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 { const f = struct { /// Subtracts the operand and the borrow from the accumulator. /// /// The decimal flag reaches the adder here as it does on every /// processor in this family, and at whichever width is in /// force: sixteen bits of decimal is four digits, and the /// carry between the second and the third stays inside the /// register rather than coming out of it. pub fn SBC(cpu: *CPU) !void { if (comptime widths) |w| { if (comptime w.m) at(cpu, u8) else at(cpu, u16); } else { if (cpu.flags.isSet(.M)) at(cpu, u8) else at(cpu, u16); } } fn operand(cpu: *CPU, comptime T: type) T { return if (comptime addressing_mode == .Immediate) cpu.program_counter.read(T) else cpu.addressing_modes.readData( addressing_mode, T, cpu.addressing_modes.decode(addressing_mode, .read), ); } fn at(cpu: *CPU, comptime T: type) void { const result = arithmetic.sbc( T, cpu.registers.getNarrow(.A, T), operand(cpu, T), cpu.flags.isSet(.C), cpu.flags.isSet(.D), ); cpu.registers.setNarrow(.A, T, result.value); cpu.flags.update(.C, result.C); cpu.flags.update(.N, result.N); cpu.flags.update(.Z, result.Z); cpu.flags.update(.V, result.V); } }; return f.SBC; } }; }