// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// The 65816's 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. /// /// Its own table rather than the 8-bit core's, for the reason the whole tree /// is its own: an opcode here names a different addressing mode enum, runs a /// function taking a different CPU, and in several cases takes a number of /// cycles that is not known until the machine is running. Sharing the builder /// would mean making every one of those generic over a difference that exists /// only here. /// /// **The table is complete**: all 256 encodings are here — the 65816 has no /// undefined ones — each with its addressing mode and cycle count, and each /// passes the SingleStepTests corpus cycle for cycle and bus line for bus /// line, in native mode and emulation mode alike (`zig build corpus16`). `get` /// keeps its optional return for the builder's sake, but no opcode resolves to /// null. pub fn Opcodes(comptime CPU: type) type { return struct { const instructions = .{ @import("opcodes/@and.zig"), @import("opcodes/adc.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/bra.zig"), @import("opcodes/brk.zig"), @import("opcodes/brl.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/cop.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/jsl.zig"), @import("opcodes/jsr.zig"), @import("opcodes/lda.zig"), @import("opcodes/ldx.zig"), @import("opcodes/ldy.zig"), @import("opcodes/lsr.zig"), @import("opcodes/mvn.zig"), @import("opcodes/mvp.zig"), @import("opcodes/nop.zig"), @import("opcodes/ora.zig"), @import("opcodes/pea.zig"), @import("opcodes/pei.zig"), @import("opcodes/per.zig"), @import("opcodes/pha.zig"), @import("opcodes/phb.zig"), @import("opcodes/phd.zig"), @import("opcodes/phk.zig"), @import("opcodes/php.zig"), @import("opcodes/phx.zig"), @import("opcodes/phy.zig"), @import("opcodes/pla.zig"), @import("opcodes/plb.zig"), @import("opcodes/pld.zig"), @import("opcodes/plp.zig"), @import("opcodes/plx.zig"), @import("opcodes/ply.zig"), @import("opcodes/rep.zig"), @import("opcodes/rol.zig"), @import("opcodes/ror.zig"), @import("opcodes/rti.zig"), @import("opcodes/rtl.zig"), @import("opcodes/rts.zig"), @import("opcodes/sbc.zig"), @import("opcodes/sec.zig"), @import("opcodes/sed.zig"), @import("opcodes/sei.zig"), @import("opcodes/sep.zig"), @import("opcodes/sta.zig"), @import("opcodes/stp.zig"), @import("opcodes/stx.zig"), @import("opcodes/sty.zig"), @import("opcodes/stz.zig"), @import("opcodes/tax.zig"), @import("opcodes/tay.zig"), @import("opcodes/tcd.zig"), @import("opcodes/tcs.zig"), @import("opcodes/tdc.zig"), @import("opcodes/trb.zig"), @import("opcodes/tsb.zig"), @import("opcodes/tsc.zig"), @import("opcodes/tsx.zig"), @import("opcodes/txa.zig"), @import("opcodes/txs.zig"), @import("opcodes/txy.zig"), @import("opcodes/tya.zig"), @import("opcodes/tyx.zig"), @import("opcodes/wai.zig"), @import("opcodes/wdm.zig"), @import("opcodes/xba.zig"), @import("opcodes/xce.zig"), }; pub const Error = error{ Break, Jam, }; /// Looks up an opcode. The return is optional for the builder's sake — /// every one of the 256 resolves, since the 65816 has no undefined /// encodings. /// /// The width flags are passed whether or not this build uses them: it /// keeps the one call site in `executeInstruction` the same for both /// dispatch strategies, and under `.branch` they cost nothing because /// the arguments go nowhere. pub fn get(opcode: u8, m: bool, x: bool) ?*const Data { switch (comptime CPU.dispatch) { .branch => { return if (table[opcode] == null) null else &table[opcode].?; }, .tables => { const which = @as(usize, @intFromBool(m)) << 1 | @intFromBool(x); const entry = &tables[which][opcode]; return if (entry.* == null) null else &entry.*.?; }, } } /// Looks an opcode up without saying which widths, for callers that /// want its name or its addressing mode rather than its /// implementation. Those are the same in every table. /// The table for one pair of widths, which is what a CPU keeps a /// pointer to rather than working out per instruction. pub fn tableFor(m: bool, x: bool) *const [256]?Data { return &tables[@as(usize, @intFromBool(m)) << 1 | @intFromBool(x)]; } pub fn describe(opcode: u8) ?*const Data { return get(opcode, false, false); } /// How many of the 256 encodings have an implementation, which is what /// the corpus runner reports its coverage against. pub fn implemented() usize { var count: usize = 0; for (if (comptime CPU.dispatch == .branch) table else tables[0]) |entry| { if (entry != null) count += 1; } return count; } pub const Init = struct { opcode: u8, addressing_mode: CPU.AddressingModes, /// What the instruction costs when nothing makes it longer. A /// sixteen-bit operand, a direct page that is not aligned and an /// index that crosses a page all add to this at run time, so it is /// the floor rather than the answer. 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, }; /// Builds one table, for whichever widths it is told — or for none of /// them, which is what `.branch` wants. fn build(comptime widths: ?CPU.Widths) [256]?Data { @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, widths), }; } } return tmp; } /// The one table `.branch` uses, where an instruction that cares about /// a width asks for it. pub const table = if (CPU.dispatch == .branch) build(null) else {}; /// The four `.tables` uses, indexed by M and X in that order, so that /// the width is settled before the instruction body is compiled. pub const tables = if (CPU.dispatch == .tables) [4][256]?Data{ build(.{ .m = false, .x = false }), build(.{ .m = false, .x = true }), build(.{ .m = true, .x = false }), build(.{ .m = true, .x = true }), } else {}; }; } test { // Both strategies, so that neither can rot while the other is the one in // use: they are the same instructions compiled two ways, and a change that // breaks one and not the other is exactly the kind that would go unnoticed. _ = @import("../cpu16.zig").Bare.Opcodes.table; _ = @import("../cpu16.zig").BareTables.Opcodes.tables; }