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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie <jeff@ocjtech.us>//// SPDX-License-Identifier: MIT
const add = @import("../add.zig").add;
/// 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 = "SBC"; pub const opcodes: []const CPU.Opcodes.Init = switch (CPU.processor) { .@"6502", .RP2A03 => &.{ .{ .opcode = 0xe1, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0xe5, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ // undocumented duplicate of 0xe9 .opcode = 0xeb, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xe9, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xed, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xf1, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0xf5, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xf9, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0xfd, .addressing_mode = .@"Absolute, X", .cycles = 4, }, }, // The CMOS part took the undocumented duplicate back and gave this // one a pointer with no index instead. .@"65C02", .R65C02, .W65C02 => &.{ .{ .opcode = 0xe1, .addressing_mode = .@"(Indirect, X)", .cycles = 6, }, .{ .opcode = 0xe5, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0xe9, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0xed, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0xf1, .addressing_mode = .@"(Indirect), Y", .cycles = 5, }, .{ .opcode = 0xf5, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0xf9, .addressing_mode = .@"Absolute, Y", .cycles = 4, }, .{ .opcode = 0xfd, .addressing_mode = .@"Absolute, X", .cycles = 4, }, .{ .opcode = 0xf2, .addressing_mode = .@"(Indirect)", .cycles = 5, }, }, };
/// 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) error{}!void { const f = struct { /// Subtracts memory and the borrow from the accumulator. /// Honours decimal mode. /// /// (MOS Technology, 1976, Section 2.2.2, p. 14) /// (Carr, 1984, p. 271) pub fn SBC(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read);
const minuend = cpu.registers.get(.A); const subtrahend = cpu.memory.read(u8, address);
// Inlined deliberately. The subtracting specialisation of // add has more branches than the adding one and lands the // wrong side of the optimiser's threshold, so ADC got it // inlined and SBC paid for a call, at about a third of the // instruction's cost. const result = @call(.always_inline, add, .{ minuend, ~subtrahend, cpu.flags.isSet(.C), cpu.flags.isSet(.D), .sbc, CPU.processor, }); cpu.registers.set(.A, result.A); cpu.flags.update(.C, result.C); cpu.flags.update(.N, result.N); cpu.flags.update(.Z, result.Z); cpu.flags.update(.V, result.V);
// Decimal arithmetic on the CMOS part takes a cycle more // than binary, which is what it spends putting the answer // and the flags right. The chip drives the address it last // used while it does. if (CPU.processor.cmos() and cpu.flags.isSet(.D)) { _ = cpu.memory.read(u8, address); } } }; return f.SBC; } };}
test "SBC #immediate example 2.13" {
// C:1 N:0 V:0 Z:0 // A: 0000 0101 5 // O: 0000 0011 3 // =: 0000 0010 2 // C:1 N:0 V:0 Z:0
const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit();
cpu.registers.set(.A, 5); cpu.flags.reset(); cpu.flags.set(.C);
try cpu.memory.loadProgram(0x0000, &[_]u8{ 0xe9, 3 });
const actual = try cpu.executeInstruction();
try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0002), cpu.program_counter.value); // try std.testing.expectEqual(@as(usize, 2), cpu.clock.cycles); try std.testing.expectEqual(2, cpu.registers.get(.A)); try std.testing.expect(cpu.flags.isClear(.N)); try std.testing.expect(cpu.flags.isClear(.V)); try std.testing.expect(cpu.flags.isClear(.B)); try std.testing.expect(cpu.flags.isClear(.D)); // Set, and left alone: these tests start from a reset, and a reset // disables interrupts. What is being checked is that the arithmetic // does not touch it. try std.testing.expect(cpu.flags.isSet(.I)); try std.testing.expect(cpu.flags.isClear(.Z)); try std.testing.expect(cpu.flags.isSet(.C));}
test "SBC #immediate example 2.14" {
// C:1 N:0 V:0 Z:0 // A: 0000 0101 5 // O: 0000 0011 6 // =: 1111 1111 -1 // C:0 N:1 V:0 Z:0
const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit();
cpu.registers.set(.A, 5); cpu.flags.reset(); cpu.flags.set(.C);
try cpu.memory.loadProgram(0x0000, &[_]u8{ 0xe9, 6 });
const actual = try cpu.executeInstruction();
try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0002), cpu.program_counter.value); // try std.testing.expectEqual(@as(usize, 2), cpu.clock.cycles); try std.testing.expectEqual(255, cpu.registers.get(.A)); try std.testing.expect(cpu.flags.isSet(.N)); try std.testing.expect(cpu.flags.isClear(.V)); try std.testing.expect(cpu.flags.isClear(.B)); try std.testing.expect(cpu.flags.isClear(.D)); try std.testing.expect(cpu.flags.isSet(.I)); try std.testing.expect(cpu.flags.isClear(.Z)); try std.testing.expect(cpu.flags.isClear(.C));}