// 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 = "LDA"; pub const opcodes: []const CPU.Opcodes.Init = switch (CPU.processor) { .@"6502", .RP2A03 => &.{ .{ .opcode = 0xa1, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0xa5, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0xa9, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xad, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xb1, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0xb5, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xb9, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0xbd, .addressing_mode = .@"Absolute, X", .cycles = 4, }, }, // The CMOS part gave this one more to answer to. .@"65C02", .R65C02, .W65C02 => &.{ .{ .opcode = 0xa1, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0xa5, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0xa9, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xad, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xb1, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0xb5, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xb9, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0xbd, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0xb2, .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 { /// Loads the accumulator from memory. /// /// (MOS Technology, 1976, Section 2.1.1, p. 4) /// (Carr, 1984, p. 263) pub fn LDA(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); cpu.registers.set(.A, value); cpu.flags.updateNZ(value); } }; return f.LDA; } }; } test "LDA 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{ 0xa9, 0x01 }); try cpu.executeInstruction(); try std.testing.expectEqual(@as(u8, 0x01), 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 "LDA 2" { 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{ 0xa9, 0x00 }); try cpu.executeInstruction(); try std.testing.expectEqual(@as(u8, 0x00), 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.isSet(.Z)); try std.testing.expect(cpu.flags.isClear(.C)); } test "LDA 3" { 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{ 0xa9, 0xff }); try cpu.executeInstruction(); try std.testing.expectEqual(@as(u8, 0xff), cpu.registers.get(.A)); try std.testing.expect(cpu.flags.isSet(.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)); }