// 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 = "ROL"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x26, .addressing_mode = .@"Zero Page", .cycles = 5, }, .{ .opcode = 0x2a, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0x2e, .addressing_mode = .Absolute, .cycles = 6, }, .{ .opcode = 0x36, .addressing_mode = .@"Zero Page, X", .cycles = 6, }, .{ .opcode = 0x3e, .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 { /// Rotates the accumulator or the addressed byte left one bit /// through carry. /// /// (MOS Technology, 1976, Section 10.3, p. 149) /// (Carr, 1984, p. 269) pub fn ROL(cpu: *CPU) error{}!void { var address: ?u16 = null; const value = switch (comptime addressing_mode) { .Implied => value: { // the operand fetch every single byte instruction // performs and discards cpu.program_counter.phantomRead(); break :value cpu.registers.get(.A); }, else => value: { address = cpu.addressing_modes.decode(addressing_mode, .modify); const current = cpu.memory.read(u8, address.?); // A read-modify-write spends a cycle on the // bus before it writes: the NMOS part drives the // unmodified value back, and the CMOS part reads // the address again. switch (CPU.processor) { .@"6502", .RP2A03 => cpu.memory.write(u8, address.?, current), .@"65C02", .R65C02, .W65C02 => _ = cpu.memory.read(u8, address.?), } break :value current; }, }; const carry = value & 0x80 == 0x80; const result = @shlExact(value & 0x7f, 1) | cpu.flags.carry(u8); cpu.flags.update(.C, carry); cpu.flags.updateNZ(result); switch (comptime addressing_mode) { .Implied => cpu.registers.set(.A, result), else => cpu.memory.write(u8, address.?, result), } } }; return f.ROL; } }; }