// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const add = @import("../add.zig").add; /// 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 = "ISC"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xe3, .addressing_mode = .@"(Indirect, X)", .cycles = 8, }, .{ .opcode = 0xe7, .addressing_mode = .@"Zero Page", .cycles = 5, }, .{ .opcode = 0xef, .addressing_mode = .Absolute, .cycles = 6, }, .{ .opcode = 0xf3, .addressing_mode = .@"(Indirect), Y", .cycles = 8, }, .{ .opcode = 0xf7, .addressing_mode = .@"Zero Page, X", .cycles = 6, }, .{ .opcode = 0xfb, .addressing_mode = .@"Absolute, Y", .cycles = 7, }, .{ .opcode = 0xff, .addressing_mode = .@"Absolute, X", .cycles = 7, }, }; /// 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 { const f = struct { /// An INC of the addressed byte, with the incremented value /// then subtracted from the accumulator. The subtraction /// honours decimal mode. pub fn ISC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); const value = cpu.memory.read(u8, address); // a read-modify-write drives the unmodified value back onto // the bus before the modified one cpu.memory.write(u8, address, value); const result = value +% 1; cpu.memory.write(u8, address, result); // Inlined for the same reason as SBC: the subtracting // specialisation of add does not get inlined on its own. const difference = @call(.always_inline, add, .{ cpu.registers.get(.A), ~result, cpu.flags.isSet(.C), cpu.flags.isSet(.D), .sbc, CPU.processor, }); cpu.registers.set(.A, difference.A); cpu.flags.update(.C, difference.C); cpu.flags.update(.N, difference.N); cpu.flags.update(.Z, difference.Z); cpu.flags.update(.V, difference.V); } }; return f.ISC; } }; }