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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
/// Describes one instruction: its mnemonic, and every opcode that selects/// it together with that opcode's addressing mode and cycle count. Opcodes/// collects these at comptime to build the dispatch table.pub fn data(comptime CPU: type) type { return struct { pub const mnemonic = "BRK"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x00, .addressing_mode = .Implied, .cycles = 7, }, };
/// The implementation for a single addressing mode. Resolved at /// comptime so the dispatch table holds a plain function pointer with /// no mode left to branch on at runtime. pub fn func(comptime addressing_mode: CPU.AddressingModes) *const fn (*CPU) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { /// Forces an interrupt: pushes the program counter and the /// status register, sets the interrupt disable and vectors /// through $fffe. /// /// (MOS Technology, 1976, Section 9.11, p. 144) /// (Carr, 1984, p. 252) pub fn BRK(cpu: *CPU) CPU.Opcodes.Error!void { // BRK is two bytes wide; the padding byte is fetched and // discarded cpu.program_counter.phantomRead(); // Wrapping, because the program counter does: a BRK whose // padding byte is the last of memory pushes $0000, and the // processor has nowhere else to put it. Written as a plain // `+` this panicked on overflow instead -- a one-byte // routine at $fffe was enough, which merle65's USR fuzzer // found by loading one there. cpu.stack.push(u16, cpu.program_counter.value +% 1); cpu.stack.push(u8, cpu.flags.saveBRK()); cpu.flags.set(.I); // The CMOS part clears the decimal flag on its way into a // handler; the NMOS part left it, so a handler that added // anything had to know what base it was in. if (comptime CPU.processor.cmos()) cpu.flags.clear(.D); // Through the interrupt vector, which is not the one reset // uses. Both used to go through ProgramCounter.reset, which // was right for BRK and wrong for reset. cpu.program_counter.jump(cpu.memory.read(u16, CPU.vector.irq)); return error.Break; } }; return f.BRK; } };}
test "BRK at the top of memory wraps rather than overflowing" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit();
// The opcode at $fffe, so its padding byte is $ffff and the address // pushed is one past the end of memory. try cpu.memory.loadProgram(0xfffe, &[_]u8{ 0x00, 0x00 }); cpu.program_counter.value = 0xfffe;
try std.testing.expectError(error.Break, cpu.executeInstruction()); try std.testing.expectEqual(@as(u8, 0x00), cpu.memory.peek(0x01ff)); try std.testing.expectEqual(@as(u8, 0x00), cpu.memory.peek(0x01fe));}
test "BRK 1" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit();
try cpu.memory.loadProgram(0x1122, &[_]u8{0x00}); try cpu.memory.loadProgram(0xfffe, &[_]u8{ 0xaa, 0xbb }); cpu.program_counter.value = 0x1122;
const actual = cpu.executeInstruction();
try std.testing.expectError(error.Break, actual); try std.testing.expectEqual(@as(u16, 0xbbaa), cpu.program_counter.value); try std.testing.expectEqual(@as(u8, 0xfc), cpu.stack.pointer); try std.testing.expectEqualSlices(u8, &.{ 0b00110010, 0x24, 0x11 }, &.{ cpu.memory.peek(0x01fd), cpu.memory.peek(0x01fe), cpu.memory.peek(0x01ff), }); // Seven, which is what the table says and what the corpus checks cycle by // cycle. This assertion sat commented out under a note saying it should be // seven, beside a number that said six; it is seven. try std.testing.expectEqual(@as(usize, 7), cpu.clock.cycles);}