// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// The instruction table, built at comptime from the modules listed below. Each /// contributes its opcodes, and a duplicate is a compile error rather than a /// silently overwritten entry. pub fn Opcodes(comptime CPU: type) type { return struct { /// The instructions both processors have. Several of them answer to /// more addressing modes on the CMOS part than on the NMOS one, and /// where they do, the instruction itself says so. const common = .{ @import("opcodes/adc.zig"), @import("opcodes/and.zig"), @import("opcodes/asl.zig"), @import("opcodes/bcc.zig"), @import("opcodes/bcs.zig"), @import("opcodes/beq.zig"), @import("opcodes/bit.zig"), @import("opcodes/bmi.zig"), @import("opcodes/bne.zig"), @import("opcodes/bpl.zig"), @import("opcodes/brk.zig"), @import("opcodes/bvc.zig"), @import("opcodes/bvs.zig"), @import("opcodes/clc.zig"), @import("opcodes/cld.zig"), @import("opcodes/cli.zig"), @import("opcodes/clv.zig"), @import("opcodes/cmp.zig"), @import("opcodes/cpx.zig"), @import("opcodes/cpy.zig"), @import("opcodes/dec.zig"), @import("opcodes/dex.zig"), @import("opcodes/dey.zig"), @import("opcodes/eor.zig"), @import("opcodes/inc.zig"), @import("opcodes/inx.zig"), @import("opcodes/iny.zig"), @import("opcodes/jmp.zig"), @import("opcodes/jsr.zig"), @import("opcodes/lda.zig"), @import("opcodes/ldx.zig"), @import("opcodes/ldy.zig"), @import("opcodes/lsr.zig"), @import("opcodes/nop.zig"), @import("opcodes/ora.zig"), @import("opcodes/pha.zig"), @import("opcodes/php.zig"), @import("opcodes/pla.zig"), @import("opcodes/plp.zig"), @import("opcodes/rol.zig"), @import("opcodes/ror.zig"), @import("opcodes/rti.zig"), @import("opcodes/rts.zig"), @import("opcodes/sbc.zig"), @import("opcodes/sec.zig"), @import("opcodes/sed.zig"), @import("opcodes/sei.zig"), @import("opcodes/sta.zig"), @import("opcodes/stx.zig"), @import("opcodes/sty.zig"), @import("opcodes/tax.zig"), @import("opcodes/tay.zig"), @import("opcodes/tsx.zig"), @import("opcodes/txa.zig"), @import("opcodes/txs.zig"), @import("opcodes/tya.zig"), }; /// The NMOS part's undocumented instructions: encodings its designers /// left undefined, which do something anyway because of the way the /// decoding was laid out, and which programs came to rely on. The CMOS /// part uses those same encodings for instructions of its own, which /// is why the two want separate tables rather than one with additions. const nmos_only = .{ @import("opcodes/alr.zig"), @import("opcodes/anc.zig"), @import("opcodes/ane.zig"), @import("opcodes/arr.zig"), @import("opcodes/dcp.zig"), @import("opcodes/isc.zig"), @import("opcodes/jam.zig"), @import("opcodes/las.zig"), @import("opcodes/lax.zig"), @import("opcodes/lxa.zig"), @import("opcodes/rla.zig"), @import("opcodes/rra.zig"), @import("opcodes/sax.zig"), @import("opcodes/sbx.zig"), @import("opcodes/sha.zig"), @import("opcodes/shx.zig"), @import("opcodes/shy.zig"), @import("opcodes/slo.zig"), @import("opcodes/sre.zig"), @import("opcodes/tas.zig"), }; /// What the CMOS part added: a branch that always branches, the index /// registers on the stack, a store of nothing, and the two that test /// and change bits in one go. const cmos_only = .{ @import("opcodes/bra.zig"), @import("opcodes/phx.zig"), @import("opcodes/phy.zig"), @import("opcodes/plx.zig"), @import("opcodes/ply.zig"), @import("opcodes/stz.zig"), @import("opcodes/trb.zig"), @import("opcodes/tsb.zig"), }; /// Rockwell's sixteen instructions for moving a bit of a zero page /// byte, and sixteen for branching on one. Each is generated from /// which bit it works on and which way round it goes, since that is /// the only thing telling one from another, and writing thirty-two of /// them out by hand would be thirty-two chances to get a bit wrong. const move = @import("opcodes/rmb.zig"); const test_ = @import("opcodes/bbr.zig"); const rockwell_only = .{ move.at(0, false), move.at(0, true), test_.at(0, false), test_.at(0, true), move.at(1, false), move.at(1, true), test_.at(1, false), test_.at(1, true), move.at(2, false), move.at(2, true), test_.at(2, false), test_.at(2, true), move.at(3, false), move.at(3, true), test_.at(3, false), test_.at(3, true), move.at(4, false), move.at(4, true), test_.at(4, false), test_.at(4, true), move.at(5, false), move.at(5, true), test_.at(5, false), test_.at(5, true), move.at(6, false), move.at(6, true), test_.at(6, false), test_.at(6, true), move.at(7, false), move.at(7, true), test_.at(7, false), test_.at(7, true), }; /// And Western Design's two for stopping. const wdc_only = .{ @import("opcodes/wai.zig"), @import("opcodes/stp.zig"), }; const instructions = switch (CPU.processor) { // Ricoh's part is the NMOS one with the decimal correction left // off the die, which changes what ADC and SBC compute and nothing // about which encodings are instructions. .@"6502", .RP2A03 => common ++ nmos_only, .@"65C02" => common ++ cmos_only, .R65C02 => common ++ cmos_only ++ rockwell_only, .W65C02 => common ++ cmos_only ++ rockwell_only ++ wdc_only, }; pub const Error = error{ Break, Jam, }; /// Looks up an opcode. Never null now that every encoding is /// implemented, but kept optional so an incomplete table stays /// representable. pub fn get(opcode: u8) ?*const Data { // A pointer into the table rather than a copy of the entry. The // entry is forty bytes and only one field of it is wanted, so // copying it was a memcpy on every instruction executed. return if (table[opcode] == null) null else &table[opcode].?; } pub const Init = struct { opcode: u8, addressing_mode: CPU.AddressingModes, cycles: u8, }; const Instruction = instruction: { const Tag = std.math.IntFittingRange(0, instructions.len); var names: [instructions.len][]const u8 = undefined; var values: [instructions.len]Tag = undefined; for (instructions, 0..) |instruction, i| { const data = instruction.data(CPU); names[i] = data.mnemonic; values[i] = i; } break :instruction @Enum(Tag, .exhaustive, &names, &values); }; pub const Data = struct { opcode: u8, mnemonic: []const u8, instruction: Instruction, cycles: u8, addressing_mode: CPU.AddressingModes, func: *const fn (*CPU) Error!void, }; const table = table: { @setEvalBranchQuota(1000000); var tmp: [256]?Data = @splat(null); for (instructions) |instruction| { const data = instruction.data(CPU); for (data.opcodes) |opcode| { if (tmp[opcode.opcode] != null) @compileError(std.fmt.comptimePrint( "opcode ${x:0>2} is claimed by both {s} and {s}", .{ opcode.opcode, tmp[opcode.opcode].?.mnemonic, data.mnemonic }, )); tmp[opcode.opcode] = .{ .opcode = opcode.opcode, .mnemonic = data.mnemonic, .instruction = std.meta.stringToEnum(Instruction, data.mnemonic) orelse @compileError("unknown mnemonic"), .cycles = opcode.cycles, .addressing_mode = opcode.addressing_mode, .func = data.func(opcode.addressing_mode), }; } } break :table tmp; }; }; } test { // Both tables, so that every instruction module is compiled whichever // processor is being built. _ = @import("../cpu8.zig").Bare.Opcodes.table; _ = @import("../cpu8.zig").BareCMOS.Opcodes.table; } test "the CMOS table has an entry for every encoding" { // The CMOS part decodes all 256, even if what most of the spare ones do // is nothing: there is no such thing as an opcode it does not answer to. const CPU = @import("../cpu8.zig").BareCMOS; var missing: usize = 0; inline for (0..256) |opcode| { if (CPU.Opcodes.get(@intCast(opcode)) == null) { std.debug.print("no entry for ${x:0>2}\n", .{opcode}); missing += 1; } } try std.testing.expectEqual(@as(usize, 0), missing); } test "table opcode match" { const CPU = @import("../cpu8.zig").Bare; inline for (CPU.Opcodes.table, 0..) |item_, i| { const item = item_ orelse continue; try std.testing.expectEqual(@as(u8, i), item.opcode); } }