// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // 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 = "STZ"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x64, .addressing_mode = .@"Direct Page", .cycles = 3, }, .{ .opcode = 0x74, .addressing_mode = .@"Direct Page, X", .cycles = 4, }, .{ .opcode = 0x9c, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x9e, .addressing_mode = .@"Absolute, 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 a zero. /// /// 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 STZ(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, @as(T, 0)); } }; return f.STZ; } }; }