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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// 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 = "CMP"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0xc1, .addressing_mode = .@"(Direct Page, X)", .cycles = 6, }, .{ .opcode = 0xc3, .addressing_mode = .@"Stack Relative", .cycles = 4, }, .{ .opcode = 0xc5, .addressing_mode = .@"Direct Page", .cycles = 3, }, .{ .opcode = 0xc7, .addressing_mode = .@"[Direct Page]", .cycles = 6, }, .{ .opcode = 0xc9, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xcd, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xcf, .addressing_mode = .@"Absolute Long", .cycles = 5, }, .{ .opcode = 0xd1, .addressing_mode = .@"(Direct Page), Y", .cycles = 5, }, .{ .opcode = 0xd2, .addressing_mode = .@"(Direct Page)", .cycles = 5, }, .{ .opcode = 0xd3, .addressing_mode = .@"(Stack Relative), Y", .cycles = 7, }, .{ .opcode = 0xd5, .addressing_mode = .@"Direct Page, X", .cycles = 4, }, .{ .opcode = 0xd7, .addressing_mode = .@"[Direct Page], Y", .cycles = 6, }, .{ .opcode = 0xd9, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0xdd, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0xdf, .addressing_mode = .@"Absolute Long, X", .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, comptime widths: ?CPU.Widths, ) *const fn (*CPU) CPU.Opcodes.Error!void { const f = struct { /// Compares the accumulator with the operand. pub fn CMP(cpu: *CPU) !void { if (comptime widths) |w| { if (comptime w.m) at(cpu, u8) else at(cpu, u16); } else { if (cpu.flags.isSet(.M)) at(cpu, u8) else at(cpu, u16); } }
/// The operand, at whichever width is in force. An immediate /// one is part of the instruction and comes out of the program /// bank; everything else is data at wherever the mode worked /// out. fn operand(cpu: *CPU, comptime T: type) T { return if (comptime addressing_mode == .Immediate) cpu.program_counter.read(T) else cpu.addressing_modes.readData( addressing_mode, T, cpu.addressing_modes.decode(addressing_mode, .read), ); }
fn at(cpu: *CPU, comptime T: type) void { const register = cpu.registers.getNarrow(.A, T); const value = operand(cpu, T); // A comparison is a subtraction whose answer is thrown // away: only the flags are kept, and the carry comes out // set when there was nothing to borrow. cpu.flags.update(.C, register >= value); cpu.flags.updateNZ(T, register -% value); } }; return f.CMP; } };}