// 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 = "ORA"; pub const opcodes: []const CPU.Opcodes.Init = switch (CPU.processor) { .@"6502", .RP2A03 => &.{ .{ .opcode = 0x01, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0x05, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x09, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x0d, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x11, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0x15, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x19, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0x1d, .addressing_mode = .@"Absolute, X", .cycles = 4, }, }, // The CMOS part gave this one more to answer to. .@"65C02", .R65C02, .W65C02 => &.{ .{ .opcode = 0x01, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0x05, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x09, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x0d, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x11, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0x15, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x19, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0x1d, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0x12, .addressing_mode = .@"(Indirect)", .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) *const fn (*CPU) error{}!void { const f = struct { /// ORs memory into the accumulator. /// /// (MOS Technology, 1976, Section 2.2.4.2, p. 21) /// (Carr, 1984, p. 266) pub fn ORA(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); const result = cpu.registers.get(.A) | value; cpu.registers.set(.A, result); cpu.flags.updateNZ(result); } }; return f.ORA; } }; } test "ORA 1" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit(); try cpu.memory.loadProgram(0x0000, &[_]u8{ 0x05, 0x01 }); try cpu.executeInstruction(); try std.testing.expectEqual(@as(u8, 1), cpu.registers.get(.A)); 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.isClear(.Z)); try std.testing.expect(cpu.flags.isClear(.C)); } test "ORA 2" { // const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit(); // try std.testing.expectError(error.IllegalAddressingMode, actual); }