// 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 = "STY"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x84, .addressing_mode = .@"Direct Page", .cycles = 3, }, .{ .opcode = 0x8c, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x94, .addressing_mode = .@"Direct Page, X", .cycles = 4, }, }; /// 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 index register Y. /// /// As wide as the X 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 STY(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.x) at(cpu, u8) else at(cpu, u16); } else { if (cpu.flags.isSet(.X)) 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(.Y, T)); } }; return f.STY; } }; }