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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
const std = @import("std");
const bus_log = @import("bus.zig");
pub const Options = bus_log.Options;pub const Cycle = bus_log.Cycle;
/// Sixteen megabytes of RAM and nothing else: every address is what it says it/// is, and stays that way. This is the memory a bare 65816 sees, and the only/// model the SingleStepTests corpus can run against, since it uses the whole/// address space as ordinary memory.////// Unlike the 8-bit `Flat`, this one holds its memory on the heap rather than/// inside the struct. Sixty-four kilobytes can sit in a local and be built in/// place; sixteen megabytes cannot, and the corpus runner keeps a dozen of/// these at once. So `init` takes an allocator and `deinit` gives it back,/// which is why the CPU's own `deinit` finally has something to do.////// A memory model is generic over the CPU it belongs to so that it can find/// its way back for the clock, and must offer init, deinit, reset, peek, poke,/// read and write, along with an Access type and the bus log that goes with/// it.pub fn Flat(comptime options: Options) fn (type) type { return struct { fn model(comptime CPU: type) type { return struct { const Self = @This();
pub const Access = bus_log.Access;
/// The whole of the 65816's address space: 256 banks of 64K. pub const size = 1 << 24;
bus: bus_log.Log(options.bus_log) = .{},
gpa: std.mem.Allocator, memory: []u8,
/// The CPU this belongs to, recovered from the field it is /// embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("memory", self)); }
/// Takes sixteen megabytes and zeroes them. pub fn init(self: *Self, gpa: std.mem.Allocator) !void { const memory = try gpa.alloc(u8, size); @memset(memory, 0); self.* = .{ .gpa = gpa, .memory = memory }; }
/// Gives the memory back. pub fn deinit(self: *Self) void { self.gpa.free(self.memory); self.memory = &.{}; }
/// What the reset line does to sixteen megabytes of nothing /// but RAM, which is nothing at all: RAM keeps what it held, /// which is the whole reason a machine can be reset and carry /// on rather than starting from nothing. pub fn reset(self: *Self) void { _ = self; }
/// Copies a program in. Takes a full 24-bit address, so a /// caller can put something in any bank. pub fn loadProgram(self: *Self, location: u24, program: []const u8) !void { const start: usize = location; const end: usize = start + program.len; if (end > self.memory.len) return error.Overflow; @memcpy(self.memory[start..end], program); }
/// Reads a byte without the processor being involved: no cycle /// is spent and nothing is written to the bus log. For /// debuggers and tests, which want to look at memory without /// disturbing the machine they are looking at. pub fn peek(self: *const Self, address: u24) u8 { return self.memory[address]; }
/// Writes a byte without the processor being involved, as /// `peek`. pub fn poke(self: *Self, address: u24, value: u8) void { self.memory[address] = value; }
/// The lines the processor drives on every cycle whatever else /// it is doing, which the log records alongside the address. inline fn lines(self: *Self) struct { e: bool, m: bool, x: bool, mlb: bool } { const cpu = self.getCPU(); return .{ .e = cpu.flags.emulation, .m = cpu.flags.isSet(.M), .x = cpu.flags.isSet(.X), .mlb = cpu.locked, }; }
/// Reads one byte, spending a cycle and logging it. Every read /// says what kind of cycle it is, because on this processor /// that is a thing the bus carries and the recordings check. pub fn read(self: *Self, address: u24, comptime cycle: Cycle) u8 { if (comptime !cycle.transfers()) @compileError("an internal cycle transfers nothing: call `internal`"); const cpu = self.getCPU(); cpu.clock.sleep(1); const value = self.memory[address]; const l = self.lines(); self.bus.record(.{ .address = address, .value = value, .direction = .read, .cycle = cycle, .e = l.e, .m = l.m, .x = l.x, .mlb = l.mlb, }); return value; }
/// Writes one byte, spending a cycle and logging it. pub fn write(self: *Self, address: u24, value: u8, comptime cycle: Cycle) void { if (comptime !cycle.transfers()) @compileError("an internal cycle transfers nothing: call `internal`"); const cpu = self.getCPU(); cpu.clock.sleep(1); const l = self.lines(); self.bus.record(.{ .address = address, .value = value, .direction = .write, .cycle = cycle, .e = l.e, .m = l.m, .x = l.x, .mlb = l.mlb, }); self.memory[address] = value; }
/// The middle cycle of a read-modify-write: the byte just read, /// driven back with none of the address lines asserted, so /// that nothing stores it. It changes no memory; it is only /// something for the bus to be doing while the chip works out /// what to put there. pub fn internalWrite(self: *Self, address: u24, value: u8) void { const cpu = self.getCPU(); cpu.clock.sleep(1); const l = self.lines(); self.bus.record(.{ .address = address, .value = value, .direction = .write, .cycle = .internal_write, .e = l.e, .m = l.m, .x = l.x, .mlb = l.mlb, }); }
/// A cycle in which the processor is not driving the bus at /// all, which is what a stopped or waiting one does. There is /// no address, so the one recorded is meaningless and the /// recordings write null for it. pub fn letGo(self: *Self) void { const cpu = self.getCPU(); cpu.clock.sleep(1); const l = self.lines(); self.bus.record(.{ .address = 0, .value = 0, .direction = .read, .cycle = .stopped, .e = l.e, .m = l.m, .x = l.x, .mlb = l.mlb, }); }
/// A cycle the processor spends on itself. Nothing is /// transferred and RAM is never enabled, but the cycle is /// spent and the address bus goes on holding something, which /// is why this takes an address at all. pub fn internal(self: *Self, address: u24) void { const cpu = self.getCPU(); cpu.clock.sleep(1); const l = self.lines(); self.bus.record(.{ .address = address, .value = 0, .direction = .read, .cycle = .internal, .e = l.e, .m = l.m, .x = l.x, .mlb = l.mlb, }); } }; } }.model;}
test "a read spends a cycle and records what kind it was" { const CPU = @import("../cpu16.zig").BareLogged; var cpu: CPU = undefined; try cpu.init(std.testing.allocator, null); defer cpu.deinit();
cpu.memory.poke(0xbd0c7f, 0xea); try std.testing.expectEqual(@as(u8, 0xea), cpu.memory.read(0xbd0c7f, .opcode)); try std.testing.expectEqual(@as(u64, 1), cpu.clock.cycles);
cpu.memory.internal(0xbd0c80); try std.testing.expectEqual(@as(u64, 2), cpu.clock.cycles); // The internal cycle is still on the bus; it just transferred nothing. try std.testing.expectEqual(@as(u24, 0xbd0c80), cpu.memory.bus.entries[1].address); try std.testing.expect(!cpu.memory.bus.entries[1].cycle.transfers());}