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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! What every memory model has in common: a record of what went past on the//! address bus.
/// Where a model looks for the processor it belongs to.////// A model needs the processor for two things and only two: the clock, to/// spend cycles against and to time the video and the cards by, and the/// interrupt line, to pull. Everything else it does is its own.pub const Host = enum { /// By walking back from the field it is embedded in, which is what a model /// that *is* a machine's memory does. It costs nothing at run time — the /// offset is known at compile time — and it requires the model to be the /// processor's `memory` field, which the whole design already assumes so /// that a machine can be built in place with no pointers to wire up. embedded, /// Through a pointer it is handed after it is brought up. /// /// For a model that is one part of a larger machine rather than the whole /// of one, and so is not the processor's `memory` field and cannot find /// its way back on its own. An Apple IIgs is the case in point: its slow /// side is a Mega II, which is an Apple IIe, and reaching that IIe would /// mean going up two fields rather than one. given,};
/// What a memory model should do beyond being memory.////// Each model reads the parts of this that apply to it and ignores the rest:/// `Flat` has no speaker and no cards, and takes no interest in most of what/// is here.pub const Options = struct { /// Where to look for the processor. See `Host`. host: Host = .embedded,
/// Keep a record of what goes past on the address bus. /// /// Off by default. It costs a compare and a store on every access, and /// only something checking which addresses an instruction touched has any /// use for it: the SingleStepTests corpus, which compares against traces /// recorded from hardware, and little else. bus_log: bool = false,
/// Keep a record of when the speaker's cone moved. /// /// Off by default. It costs a compare and a store on every access to the /// speaker, and only something turning what the machine played into sound /// has any use for it. /// /// What is kept is the cycle each flip happened on, because that is the /// whole of what the hardware carries: the speaker is one bit, and a /// program plays a tone, a tune or a bang entirely by choosing when to /// flip it. Being told only that the cone moved, none of those can be told /// apart. See speaker.zig. speaker_log: bool = false,
/// Keep a record of when the cassette output line flipped. /// /// Off by default, and the same thing as the speaker's record for the same /// reason: the cassette port is one bit, written by flipping it, and the /// timing is the whole of what goes onto the tape. cassette_log: bool = false,
/// Keep a record of what was written to a Mockingboard's sound chips. /// /// Off by default, and for the same reason as the speaker's record: only /// something turning what the machine played into sound has any use for /// it, and what a caller would hear without it is a frame's worth of /// changes all happening at the moment it looked. mockingboard_log: bool = false,};
/// One access on the address bus. Recorded so that tests can check an/// instruction touches the right addresses in the right order, rather than/// only that it arrives at the right state: the discarded reads a 6502/// performs are invisible in the final registers but are exactly what a soft/// switch reacts to.pub const Access = struct { pub const Direction = enum { read, write };
address: u16, value: u8, direction: Direction,};
/// How many accesses a log keeps. The longest instruction takes seven cycles,/// so this is room to spare.pub const capacity = 16;
/// A record of the accesses an instruction made, or nothing at all.////// Keeping it costs a compare and a store on every access, and only something/// checking which addresses were touched has any use for it, so whether to/// keep it is a decision the caller makes when it chooses a memory model. The/// disabled version has the same shape and does nothing, so a model needs no/// conditionals of its own; and it has no entries to read, so code that wants/// them will not compile against a model that is not keeping them.pub fn Log(comptime enabled: bool) type { return if (enabled) struct { const Self = @This();
entries: [capacity]Access = undefined, len: usize = 0,
pub fn record(self: *Self, address: u16, value: u8, direction: Access.Direction) void { if (self.len == capacity) return; self.entries[self.len] = .{ .address = address, .value = value, .direction = direction }; self.len += 1; }
/// Starts a fresh record, which is what a caller wants between one /// instruction and the next. pub fn clear(self: *Self) void { self.len = 0; } } else struct { const Self = @This();
pub fn record(_: *Self, _: u16, _: u8, _: Access.Direction) void {} pub fn clear(_: *Self) void {} };}