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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156// 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 = "ORA"; pub const opcodes: []const CPU.Opcodes.Init = switch (CPU.processor) { .@"6502", .RP2A03 => &.{ .{ .opcode = 0x01, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0x05, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x09, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x0d, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x11, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0x15, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x19, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0x1d, .addressing_mode = .@"Absolute, X", .cycles = 4, }, }, // The CMOS part gave this one more to answer to. .@"65C02", .R65C02, .W65C02 => &.{ .{ .opcode = 0x01, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0x05, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x09, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x0d, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x11, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0x15, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x19, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0x1d, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0x12, .addressing_mode = .@"(Indirect)", .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) *const fn (*CPU) error{}!void { const f = struct { /// ORs memory into the accumulator. /// /// (MOS Technology, 1976, Section 2.2.4.2, p. 21) /// (Carr, 1984, p. 266) pub fn ORA(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); const result = cpu.registers.get(.A) | value; cpu.registers.set(.A, result); cpu.flags.updateNZ(result); } }; return f.ORA; } };}
test "ORA 1" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit();
try cpu.memory.loadProgram(0x0000, &[_]u8{ 0x05, 0x01 });
try cpu.executeInstruction();
try std.testing.expectEqual(@as(u8, 1), cpu.registers.get(.A)); try std.testing.expect(cpu.flags.isClear(.N)); try std.testing.expect(cpu.flags.isClear(.V)); try std.testing.expect(cpu.flags.isClear(.B)); try std.testing.expect(cpu.flags.isClear(.D)); try std.testing.expect(cpu.flags.isClear(.I)); try std.testing.expect(cpu.flags.isClear(.Z)); try std.testing.expect(cpu.flags.isClear(.C));}
test "ORA 2" { // const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit();
// try std.testing.expectError(error.IllegalAddressingMode, actual);}