Something went wrong. Try again.
A cycle-accurate Apple II and Apple IIgs emulator written in Zig, usable as both a program and a library.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// 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; } };}