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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465// 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 = "ARR"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x6b, .addressing_mode = .Immediate, .cycles = 2, }, };
/// 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 != .Immediate) @compileError("illegal addressing mode"); const f = struct { /// AND with an immediate followed by a ROR, except the flags /// fall out of the adder rather than the shifter, so carry and /// overflow come from bits 6 and 5. pub fn ARR(cpu: *CPU) error{}!void { const value = cpu.memory.read(u8, cpu.addressing_modes.decode(addressing_mode, .read)); const carry = cpu.flags.isSet(.C); const anded = cpu.registers.get(.A) & value; var result = @shrExact(anded & 0xfe, 1) | @as(u8, if (carry) 0x80 else 0);
cpu.flags.update(.Z, result == 0); cpu.flags.update(.V, (anded ^ result) & 0x40 == 0x40);
// The fixup below is the decimal correction, so it goes // the same way as the adder's: on Ricoh's part it is not // on the die and the flag reaches nothing. if (comptime CPU.processor.decimal()) if (cpu.flags.isSet(.D)) { // the rotate is done in binary and then talked back into // decimal a nibble at a time, which is why the sign here // is the carry that was shifted in rather than a bit of // the result cpu.flags.update(.N, carry); if ((anded & 0x0f) + (anded & 0x01) > 0x05) result = (result & 0xf0) | ((result +% 0x06) & 0x0f); const high_carry = @as(u16, anded & 0xf0) + (anded & 0x10) > 0x50; if (high_carry) result +%= 0x60; cpu.flags.update(.C, high_carry); cpu.registers.set(.A, result); return; };
cpu.flags.update(.N, result & 0x80 == 0x80); cpu.flags.update(.C, result & 0x40 == 0x40);
cpu.registers.set(.A, result); } }; return f.ARR; } };}