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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118// 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 = "STA"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x81, .addressing_mode = .@"(Direct Page, X)", .cycles = 6, }, .{ .opcode = 0x83, .addressing_mode = .@"Stack Relative", .cycles = 4, }, .{ .opcode = 0x85, .addressing_mode = .@"Direct Page", .cycles = 3, }, .{ .opcode = 0x87, .addressing_mode = .@"[Direct Page]", .cycles = 6, }, .{ .opcode = 0x8d, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x8f, .addressing_mode = .@"Absolute Long", .cycles = 5, }, .{ .opcode = 0x91, .addressing_mode = .@"(Direct Page), Y", .cycles = 6, }, .{ .opcode = 0x92, .addressing_mode = .@"(Direct Page)", .cycles = 5, }, .{ .opcode = 0x93, .addressing_mode = .@"(Stack Relative), Y", .cycles = 7, }, .{ .opcode = 0x95, .addressing_mode = .@"Direct Page, X", .cycles = 4, }, .{ .opcode = 0x97, .addressing_mode = .@"[Direct Page], Y", .cycles = 6, }, .{ .opcode = 0x99, .addressing_mode = .@"Absolute, Y", .cycles = 5, }, .{ .opcode = 0x9d, .addressing_mode = .@"Absolute, X", .cycles = 5, }, .{ .opcode = 0x9f, .addressing_mode = .@"Absolute Long, X", .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, comptime widths: ?CPU.Widths, ) *const fn (*CPU) CPU.Opcodes.Error!void { const f = struct { /// Stores the accumulator. /// /// As wide as the M flag says, low half first, which is two /// separate cycles on the bus and so not something an emulator /// can decide for itself: anything watching sees which came /// first. pub fn STA(cpu: *CPU) !void { // Under `.tables` the width is already settled and this // folds away; under `.branch` it is the test the whole // measurement is about. if (comptime widths) |w| { if (comptime w.m) at(cpu, u8) else at(cpu, u16); } else { if (cpu.flags.isSet(.M)) at(cpu, u8) else at(cpu, u16); } }
fn at(cpu: *CPU, comptime T: type) void { const address = cpu.addressing_modes.decode(addressing_mode, .write); cpu.addressing_modes.writeData(addressing_mode, T, address, cpu.registers.getNarrow(.A, T)); } }; return f.STA; } };}