// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // 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< 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; }