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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
//! Sixty-four kilobytes of RAM with one register wired to the interrupt pins.//!//! Klaus Dormann's interrupt test cannot be run against plain memory, and says//! so at the top of itself: "this program is designed to test IRQ and NMI of a//! 6502 emulator. It requires an internal or external feedback register to the//! IRQ & NMI inputs" (Dormann, 2014). There is no way for a program to raise//! its own interrupt otherwise — BRK is a different path through the processor//! and proves nothing about the pins — so the test writes to a port and the//! hardware is expected to pull the lines.//!//! The port is configurable in the source and this matches what it is//! configured to: `$bffc`, with no data direction register beside it, bit 0 to//! IRQ and bit 1 to NMI. Despite the comment calling it open collector, the//! macros the no-DDR case assembles turn an interrupt *on* by setting its bit//! — `lda I_port / and #I_filter / ora #(1<<bit) / sta I_port` — so a bit that//! is set is a line being pulled.//!//! Bit 7 never survives a write. The test masks every value with `I_filter`,//! which it sets to `$7f` and documents as "filtering bit 7 = diag stop", so//! the byte the register latches is what the machine would have seen.
const std = @import("std");const zig65 = @import("zig65");
const bus_log = zig65.memory.bus;const Options = zig65.memory.Options;
const memory_size = std.math.maxInt(u16) + 1;
/// Which address the feedback register answers at, and which of its bits go/// where. All three are settable in the test's source; these are what it is/// set to.pub const port: u16 = 0xbffc;pub const irq_bit: u8 = 1 << 0;pub const nmi_bit: u8 = 1 << 1;/// "I_filter = $7f ;filtering bit 7 = diag stop".pub const filter: u8 = 0x7f;
/// Flat memory with the feedback register in it.pub fn Feedback(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;
bus: bus_log.Log(options.bus_log) = .{}, memory: [memory_size]u8 = undefined, /// What was last written to the port, which is also what a read /// of it gives back: it is a latch, not a strobe. latch: u8 = 0,
inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("memory", self)); }
pub fn init(self: *Self, _: ?usize) void { self.* = .{ .memory = [_]u8{0} ** memory_size }; }
pub fn reset(self: *Self) void { // The register is a chip outside the processor, and the // reset line goes to it too. self.latch = 0; self.getCPU().irq = false; self.getCPU().nmi = false; }
pub fn loadProgram(self: *Self, location: u16, program: []const u8) !void { const start: usize = @intCast(location); const end: usize = start + program.len; if (end > self.memory.len) return error.Overflow; @memcpy(self.memory[start..end], program); }
pub fn peek(self: *const Self, address: u16) u8 { return self.memory[address]; }
pub fn poke(self: *Self, address: u16, value: u8) void { self.memory[address] = value; }
pub fn read(self: *Self, comptime T: type, offset: u16) T { const cpu = self.getCPU(); const info = @typeInfo(T); if (info != .int) @compileError("must be an integer"); if (info.int.signedness != .unsigned) @compileError("must be unsigned"); switch (info.int.bits) { 8 => { cpu.clock.sleep(1); const value = if (offset == port) self.latch else self.memory[offset]; self.bus.record(offset, value, .read); return value; }, 16 => { const low = self.read(u8, offset); const high = self.read(u8, offset +% 1); return @as(u16, high) << 8 | low; }, else => @compileError("must be a u8 or a u16"), } }
pub fn write(self: *Self, comptime T: type, offset: u16, value: T) void { const cpu = self.getCPU(); const info = @typeInfo(T); if (info != .int) @compileError("must be an integer"); if (info.int.signedness != .unsigned) @compileError("must be unsigned"); switch (info.int.bits) { 8 => { cpu.clock.sleep(1); self.bus.record(offset, value, .write); if (offset != port) { self.memory[offset] = value; return; } const was = self.latch; self.latch = value & filter;
// IRQ is a level: the line stays pulled until the // register lets go of it, and a handler that // returns without clearing the bit is // interrupted again. That is the half of the test // that BRK could never stand in for. cpu.irq = self.latch & irq_bit != 0;
// NMI is an edge, so it is the change that counts. // Setting the bit again while it is already set // asks for nothing, which is exactly what the test // is checking when it leaves it set across a // handler. if (was & nmi_bit == 0 and self.latch & nmi_bit != 0) cpu.nmi = true; }, 16 => { self.write(u8, offset, @truncate(value)); self.write(u8, offset +% 1, @truncate(value >> 8)); }, else => @compileError("must be a u8 or a u16"), } } }; } }.model;}