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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
const std = @import("std");
/// The accumulator and the two index registers, each carrying the cycle it was/// last written on. Embedded in CPU, and finds its way back to it with/// @fieldParentPtr rather than holding a pointer, so the whole processor can be/// built in place.pub fn Registers(comptime CPU: type) type { return struct { const Self = @This();
const Name = enum { A, X, Y, };
_values: std.EnumArray(Name, u8), _changed: std.EnumArray(Name, u64),
/// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("registers", self)); }
/// Clears all three registers without touching the flags, for a CPU /// that is not running yet. pub fn init(self: *Self) void { self._values = .initFill(0); self._changed = .initFill(0); }
/// Clears all three registers and reports the new accumulator through /// the N and Z flags, as a reset does. pub fn reset(self: *Self) void { const cpu = self.getCPU(); self._values = .initFill(0); self._changed = .initFill(cpu.clock.read()); cpu.flags.updateNZ(self._values.get(.A)); }
/// Writes a register and records when it changed, which the debugger /// uses to highlight recent writes. pub fn set(self: *Self, comptime register: Name, value: u8) void { const cpu = self.getCPU(); self._changed.set(register, cpu.clock.read()); self._values.set(register, value); }
/// Reads a register. pub fn get(self: *Self, comptime register: Name) u8 { return self._values.get(register); }
/// How many cycles ago the register was last written. pub fn changed(self: *Self, comptime register: Name) u64 { const cpu = self.getCPU(); return cpu.clock.read() - self._changed.get(register); } };}
test "changed" { const CPU = @import("../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit(); cpu.registers.set(.A, 0x69); _ = cpu.memory.read(u8, 0x0000); try std.testing.expectEqual(@as(u64, 1), cpu.registers.changed(.A));}