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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253// 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 = "JSR"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x20, .addressing_mode = .Absolute, .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 { if (addressing_mode != .Absolute) @compileError("illegal addressing mode"); const f = struct { /// Calls a subroutine, pushing the address of its own last byte /// rather than of the next instruction. /// /// (MOS Technology, 1976, Section 8.1, p. 106) /// (Carr, 1984, p. 262) pub fn JSR(cpu: *CPU) error{}!void { // we can't use cpu.addressing_modes.decode because // reads/writes of the stack and program counter are // interleaved const address_low = cpu.program_counter.read(u8); const program_counter = cpu.program_counter.value; const program_counter_low: u8 = @intCast(program_counter & 0xff); const program_counter_high: u8 = @intCast(program_counter >> 8); // the internal cycle where the chip parks the low byte of // the target in the stack pointer latch, reading the slot // the pointer is sitting on and discarding it cpu.stack.phantomRead(); cpu.stack.push(u8, program_counter_high); cpu.stack.push(u8, program_counter_low); const address_high = cpu.program_counter.read(u8);
cpu.program_counter.value = (@as(u16, address_high) << 8 | address_low); } }; return f.JSR; } };}