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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104// 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 = "JMP"; pub const opcodes: []const CPU.Opcodes.Init = switch (CPU.processor) { .@"6502", .RP2A03 => &.{ .{ .opcode = 0x4c, .addressing_mode = .Absolute, .cycles = 3, },
.{ .opcode = 0x6c, .addressing_mode = .Indirect, .cycles = 5, }, }, // The CMOS part gave this one more to answer to. .@"65C02", .R65C02, .W65C02 => &.{ .{ .opcode = 0x4c, .addressing_mode = .Absolute, .cycles = 3, },
.{ .opcode = 0x6c, .addressing_mode = .Indirect, .cycles = 5, }, .{ .opcode = 0x7c, .addressing_mode = .@"(Absolute, X)", .cycles = 6, }, }, };
/// 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 { /// Jumps to a new address. The indirect form carries the /// hardware bug where a vector ending in $ff takes its high /// byte from the start of the same page. /// /// (MOS Technology, 1976, Section 4.0.2, p. 36) /// (Carr, 1984, p. 261) pub fn JMP(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); cpu.program_counter.jump(address); } }; return f.JMP; } };}
test "JMP (absolute)" { 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{ 0x4c, 0x32, 0x40 });
const saved = cpu.flags.dump(); const actual = try cpu.executeInstruction();
try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x4032), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 3), cpu.clock.cycles); try std.testing.expect(cpu.flags.compare(saved));}
test "JMP (indirect)" { 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{ 0x6c, 0x32, 0x40 }); try cpu.memory.loadProgram(0x4032, &[_]u8{ 0xaa, 0xbb });
const saved = cpu.flags.dump();
const actual = try cpu.executeInstruction();
try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0xbbaa), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 5), cpu.clock.cycles); try std.testing.expect(cpu.flags.compare(saved));}