// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// 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 = "NOP"; /// Which encodings do nothing, which is not the same question on the /// two processors. /// /// On the NMOS part these are undocumented: encodings its designers /// left undefined which go through the motions of an instruction and /// throw the result away. On the CMOS part they are deliberate, and the /// shapes below — how many bytes each fetches and how many cycles it /// takes — are what the hardware traces in the SingleStepTests corpus /// record (SingleStepTests, n.d.), since no data sheet lists them all. pub const opcodes: []const CPU.Opcodes.Init = switch (CPU.processor) { .@"6502", .RP2A03 => &.{ .{ .opcode = 0x04, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x0c, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x14, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x1a, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0x1c, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0x34, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x3a, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0x3c, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0x44, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x54, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x5a, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0x5c, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0x64, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x74, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x7a, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0x7c, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0x80, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x82, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x89, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xc2, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xd4, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xda, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0xdc, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0xe2, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xea, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0xf4, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xfa, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0xfc, .addressing_mode = .@"Absolute, X", .cycles = 4, }, }, .@"65C02" => &cmos, // Rockwell's part has instructions on two whole columns of the // table, so those encodings are no longer nothing. .R65C02 => &without(&cmos, &bit_instructions), // And Western Design's has two more on top of that. .W65C02 => &without(&cmos, &(bit_instructions ++ [_]u8{ 0xcb, 0xdb })), }; /// The encodings Rockwell gave instructions to: the $x7 column moves a /// bit of a zero page byte, the $xF column branches on one. const bit_instructions = blk: { var taken: [32]u8 = undefined; for (0..8) |which| { taken[which * 4 + 0] = 0x07 | @as(u8, which) << 4; taken[which * 4 + 1] = 0x87 | @as(u8, which) << 4; taken[which * 4 + 2] = 0x0f | @as(u8, which) << 4; taken[which * 4 + 3] = 0x8f | @as(u8, which) << 4; } break :blk taken; }; /// How many of a list survive dropping the encodings named. fn remaining(comptime list: []const CPU.Opcodes.Init, comptime drop: []const u8) usize { @setEvalBranchQuota(10000); var kept: usize = 0; for (list) |entry| { if (std.mem.indexOfScalar(u8, drop, entry.opcode) == null) kept += 1; } return kept; } /// The list without them, which is how a part with more instructions /// comes to have fewer of these. fn without( comptime list: []const CPU.Opcodes.Init, comptime drop: []const u8, ) [remaining(list, drop)]CPU.Opcodes.Init { @setEvalBranchQuota(10000); var kept: [remaining(list, drop)]CPU.Opcodes.Init = undefined; var at: usize = 0; for (list) |entry| { if (std.mem.indexOfScalar(u8, drop, entry.opcode) != null) continue; kept[at] = entry; at += 1; } return kept; } const cmos = [_]CPU.Opcodes.Init{ .{ .opcode = 0x03, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x0b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x13, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x1b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x23, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x2b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x33, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x3b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x43, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x4b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x53, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x5b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x63, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x6b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x73, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x7b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x83, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x8b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x93, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0x9b, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xa3, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xab, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xb3, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xbb, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xc3, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xd3, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xe3, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xeb, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xf3, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xfb, .addressing_mode = .@"No Operand", .cycles = 1, }, .{ .opcode = 0xcb, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0xea, .addressing_mode = .Implied, .cycles = 2, }, .{ .opcode = 0x02, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x22, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x42, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x62, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x82, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xc2, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xe2, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x07, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x27, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x44, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x47, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x67, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x87, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0xa7, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0xc7, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0xe7, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x17, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x37, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x54, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x57, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x77, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x97, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xb7, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xd4, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xd7, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xdb, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xf4, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xf7, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x0f, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0x2f, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0x4f, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0x6f, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0x8f, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0xaf, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0xcf, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0xef, .addressing_mode = .@"Two Operands", .cycles = 3, }, .{ .opcode = 0x1f, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x3f, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x5c, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x5f, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x7f, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x9f, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xbf, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xdc, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xdf, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xfc, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xff, .addressing_mode = .Absolute, .cycles = 4, }, }; /// 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) CPU.Opcodes.Error!void { const f = struct { /// Does nothing. The documented NOP is the implied one; the /// rest are undocumented encodings that go through the motions /// of an instruction and throw the result away. They still take /// the cycles and still touch the bus, which is the only thing /// telling them apart. /// /// (MOS Technology, 1976, Section 5.3, p. 57) /// (Carr, 1984, p. 265) pub fn NOP(cpu: *CPU) !void { switch (comptime addressing_mode) { .Implied => cpu.program_counter.phantomRead(), // One cycle and nothing else: the opcode fetch is the // whole instruction. .@"No Operand" => {}, // Two operand bytes fetched and never looked at. .@"Two Operands" => { _ = cpu.program_counter.read(u8); _ = cpu.program_counter.read(u8); }, // Three bytes and a fourth cycle. The NMOS part // spends it reading whatever the two operands point // at, as an instruction that meant something would; // the CMOS part reads the last of them again. .Absolute => { const address = cpu.addressing_modes.decode(addressing_mode, .read); _ = switch (CPU.processor) { .@"6502", .RP2A03 => cpu.memory.read(u8, address), .@"65C02", .R65C02, .W65C02 => cpu.memory.read(u8, cpu.program_counter.value -% 1), }; }, else => { const address = cpu.addressing_modes.decode(addressing_mode, .read); _ = cpu.memory.read(u8, address); }, } } }; return f.NOP; } }; }