// 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 = "RRA"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x63, .addressing_mode = .@"(Indirect, X)", .cycles = 8, }, .{ .opcode = 0x67, .addressing_mode = .@"Zero Page", .cycles = 5, }, .{ .opcode = 0x6f, .addressing_mode = .Absolute, .cycles = 6, }, .{ .opcode = 0x73, .addressing_mode = .@"(Indirect), Y", .cycles = 8, }, .{ .opcode = 0x77, .addressing_mode = .@"Zero Page, X", .cycles = 6, }, .{ .opcode = 0x7b, .addressing_mode = .@"Absolute, Y", .cycles = 7, }, .{ .opcode = 0x7f, .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 { /// A ROR of the addressed byte, with the result also added to /// the accumulator. The addition honours decimal mode. pub fn RRA(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 carry = value & 0x01 == 0x01; const result = @shrExact(value & 0xfe, 1) | @as(u8, if (cpu.flags.isSet(.C)) 0x80 else 0); cpu.flags.update(.C, carry); cpu.memory.write(u8, address, result); const sum = add( cpu.registers.get(.A), result, cpu.flags.isSet(.C), cpu.flags.isSet(.D), .adc, CPU.processor, ); cpu.registers.set(.A, sum.A); cpu.flags.update(.C, sum.C); cpu.flags.update(.N, sum.N); cpu.flags.update(.Z, sum.Z); cpu.flags.update(.V, sum.V); } }; return f.RRA; } }; }