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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
const std = @import("std");
/// The 65816's stack pointer, which is sixteen bits wide and can put the stack/// anywhere in bank zero.////// Except in emulation mode, where it mostly cannot — and the "mostly" took/// the recordings to pin down. There the high half is forced to $01 and the/// stack is back in page one where a 6502 keeps it, and that is not merely/// applied when something is pushed: the register reads back with $01 in its/// high half for as long as the emulation line is set. A case that begins in/// emulation with S at $6646 and executes a NOP, which touches no stack at/// all, ends with S reading $0146.////// What is not forced is the arithmetic in the middle of an instruction. Push/// two bytes from $0100 while emulating and the second one goes to $0000ff,/// not back up to $0001ff: the pointer steps across the boundary and is pulled/// back into page one afterwards. A 6502 never had the chance to notice,/// because none of its instructions pushed two bytes; the ones that do here —/// PHD, PEA, PEI, PER and PLD — are this processor's own and were never/// promised the older machine's behaviour. So the confinement happens at the/// start of each instruction and when the register is read, and nowhere else.////// The stack lives in bank zero whatever the data bank says.pub fn Stack(comptime CPU: type) type { return struct { const Self = @This(); const emulation_page: u16 = 0x0100;
/// What was last written to the register. What it reads as is /// `get`, which is not the same thing in emulation mode. raw: u16,
/// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("stack", self)); }
/// Puts the pointer at the top of the stack page. pub fn init(self: *Self) void { self.raw = 0x01ff; }
/// A reset does not clear the stack; it only moves the pointer, and /// since reset also puts the processor into emulation mode the pointer /// lands in page one. pub fn reset(self: *Self) void { self.raw = 0x01ff; }
/// The pointer as the register reads: forced into page one whenever /// the emulation line is set. pub fn get(self: *const Self) u16 { const cpu: *const CPU = @alignCast(@fieldParentPtr("stack", self)); if (cpu.flags.emulation) return emulation_page | (self.raw & 0x00ff); return self.raw; }
/// Moves the pointer. pub fn set(self: *Self, value: u16) void { self.raw = value; self.normalize(); }
/// Applies the emulation-mode rule to what is stored, which is what /// entering emulation mode does to a pointer that was somewhere else. pub fn normalize(self: *Self) void { const cpu = self.getCPU(); if (cpu.flags.emulation) self.raw = emulation_page | (self.raw & 0x00ff); }
/// Whether an instruction's use of the stack is held inside page one /// while emulating. /// /// This is per instruction, not per access, and the recordings are /// what said so. The ones the 6502 had keep its stack: pull from $01ff /// and the next byte comes from $000100, because that is what a /// program written for that machine will expect. The ones this /// processor added were never promised anything of the sort, and step /// straight out of the page — PLD from $01ff reads $000200. pub const Confinement = enum { /// The 6502's stack: the low half moves and the high half does /// not. page_one, /// The whole of bank zero, whatever mode this is. unconfined, };
/// Steps the pointer, under whichever rule the instruction follows. fn step(self: *Self, comptime where: Confinement, comptime direction: enum { up, down }) void { const cpu = self.getCPU(); const delta: u8 = switch (direction) { .up => 1, .down => 0xff, }; if (where == .page_one and cpu.flags.emulation) { self.raw = (self.raw & 0xff00) | (@as(u8, @truncate(self.raw)) +% delta); } else { self.raw = self.raw +% switch (direction) { .up => 1, .down => @as(u16, 0xffff), }; } }
/// Writes at the pointer and moves it down. A word goes out high byte /// first, as on the 6502. /// /// The address is the pointer as it stands, not as it reads back: /// within an instruction an unconfined push is free to step out of /// page one even while emulating, and `normalize` puts it back before /// the next instruction starts. pub fn push(self: *Self, comptime T: type, comptime where: Confinement, value: T) void { const cpu = self.getCPU(); switch (@typeInfo(T).int.bits) { 8 => { cpu.memory.write(self.raw, value, .data); self.step(where, .down); }, 16 => { self.push(u8, where, @truncate(value >> 8)); self.push(u8, where, @truncate(value)); }, else => @compileError("must be a u8 or a u16"), } }
/// Moves the pointer up and reads. A word comes back low byte first, /// the order it was pushed in. pub fn pop(self: *Self, comptime T: type, comptime where: Confinement) T { const cpu = self.getCPU(); switch (@typeInfo(T).int.bits) { 8 => { self.step(where, .up); return cpu.memory.read(self.raw, .data); }, 16 => { const low: u16 = self.pop(u8, where); const high: u16 = self.pop(u8, where); return high << 8 | low; }, else => @compileError("must be a u8 or a u16"), } } };}
test "the pointer reads back inside page one while emulating" { const CPU = @import("../cpu16.zig").Bare; var cpu: CPU = undefined; try cpu.init(std.testing.allocator, null); defer cpu.deinit();
// Native, the whole of bank zero is available. cpu.flags.setEmulation(false); cpu.stack.set(0x6646); try std.testing.expectEqual(@as(u16, 0x6646), cpu.stack.get());
// Emulating, the high half is not the program's to choose. This is the // corpus case ea.e 1: S set to $6646, one NOP, S reads $0146. cpu.flags.setEmulation(true); cpu.stack.set(0x6646); try std.testing.expectEqual(@as(u16, 0x0146), cpu.stack.get());}