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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114// 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 = "ROR"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x66, .addressing_mode = .@"Direct Page", .cycles = 5, }, .{ .opcode = 0x6a, .addressing_mode = .Accumulator, .cycles = 2, }, .{ .opcode = 0x6e, .addressing_mode = .Absolute, .cycles = 6, }, .{ .opcode = 0x76, .addressing_mode = .@"Direct Page, X", .cycles = 6, }, .{ .opcode = 0x7e, .addressing_mode = .@"Absolute, X", .cycles = 7, }, };
/// 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 { /// Rotates the operand right through the carry. pub fn ROR(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 operation itself, which is the only part that differs /// between the instructions in this shape. fn compute(cpu: *CPU, comptime T: type, value: T) T { const bits = @typeInfo(T).int.bits; _ = bits; const carry: T = @as(T, @intFromBool(cpu.flags.isSet(.C))) << (@typeInfo(T).int.bits - 1); cpu.flags.update(.C, value & 1 != 0); const result = (value >> 1) | carry; cpu.flags.updateNZ(T, result); return result; }
fn at(cpu: *CPU, comptime T: type) void { // The accumulator form works on a register, so there is // nothing to address and nothing to write back; the cycle // it still takes is the chip's own. if (comptime addressing_mode == .Accumulator) { const result = compute(cpu, T, cpu.registers.getNarrow(.A, T)); cpu.registers.setNarrow(.A, T, result); cpu.program_counter.internal(); return; }
// Everywhere else: read it, spend a cycle on it, write it // back. The cycle in the middle is what makes this a // read-modify-write rather than a read and a write, and // the address bus holds the last byte read while it goes // by. const address = cpu.addressing_modes.decode(addressing_mode, .write); // The lock goes down for the read and does not come up // until the write is done, which is what stops anything // else on the bus seeing the byte half changed. cpu.locked = true; defer cpu.locked = false; const value = cpu.addressing_modes.readData(addressing_mode, T, address); // The middle cycle, and which one it is depends on which // machine this is pretending to be. Emulating, the // unmodified value goes back onto the bus before the // modified one, which is what the NMOS 6502 did and what a // program written for that machine may be counting on. // Natively it is an ordinary internal cycle with nothing // driven, as on the 65C02. The widths are the same either // way; it is the emulation line that decides. const last = if (T == u8) address else @TypeOf(cpu.addressing_modes).next(addressing_mode, address); if (cpu.flags.emulation) cpu.memory.internalWrite(last, @truncate(value >> (@typeInfo(T).int.bits - 8))) else cpu.memory.internal(last); cpu.addressing_modes.writeDataReversed(addressing_mode, T, address, compute(cpu, T, value)); } }; return f.ROR; } };}