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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162// 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 = "RTI"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x40, .addressing_mode = .Stack, .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, comptime widths: ?CPU.Widths, ) *const fn (*CPU) CPU.Opcodes.Error!void { _ = widths; const f = struct { /// Comes back from an interrupt: the flags first, then where it was. Natively the bank comes back too, because natively it was pushed. pub fn RTI(cpu: *CPU) !void { if (addressing_mode != .Stack) @compileError("illegal addressing mode"); cpu.program_counter.internal(); cpu.program_counter.internal();
// The status register comes off the stack first but does // not take effect until the instruction is over. The width // lines are outputs, and the recordings show them still // driving the old widths through the pulls that follow — // three quarters of the native cases disagree if the new // ones are applied here, which is exactly how often at // least one of the two changes. XCE and REP do the same // thing with their own flags for the same reason. const status = cpu.stack.pop(u8, .page_one);
// Unlike a subroutine return this one does not add a byte: // an interrupt pushed the address it meant to come back // to, rather than the one before it. const address = cpu.stack.pop(u16, .page_one); const bank: ?u8 = if (cpu.flags.emulation) null else cpu.stack.pop(u8, .page_one);
cpu.flags.load(status); if (bank) |b| cpu.registers.program_bank = b; cpu.program_counter.jump(address); } }; return f.RTI; } };}