// 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 = "TSB"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x04, .addressing_mode = .@"Direct Page", .cycles = 5, }, .{ .opcode = 0x0c, .addressing_mode = .Absolute, .cycles = 6, }, }; /// 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 { /// Sets in the operand every bit the accumulator has set. pub fn TSB(cpu: *CPU) !void { 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); } } /// The operation itself, which is the only part that differs /// between the instructions in this shape. fn compute(cpu: *CPU, comptime T: type, value: T) T { const bits = @typeInfo(T).int.bits; _ = bits; // The zero flag is about what was there before, not // about what is being put back. cpu.flags.update(.Z, value & cpu.registers.getNarrow(.A, T) == 0); return value | cpu.registers.getNarrow(.A, T); } fn at(cpu: *CPU, comptime T: type) void { // The accumulator form works on a register, so there is // nothing to address and nothing to write back; the cycle // it still takes is the chip's own. if (comptime addressing_mode == .Accumulator) { const result = compute(cpu, T, cpu.registers.getNarrow(.A, T)); cpu.registers.setNarrow(.A, T, result); cpu.program_counter.internal(); return; } // Everywhere else: read it, spend a cycle on it, write it // back. The cycle in the middle is what makes this a // read-modify-write rather than a read and a write, and // the address bus holds the last byte read while it goes // by. const address = cpu.addressing_modes.decode(addressing_mode, .write); // The lock goes down for the read and does not come up // until the write is done, which is what stops anything // else on the bus seeing the byte half changed. cpu.locked = true; defer cpu.locked = false; const value = cpu.addressing_modes.readData(addressing_mode, T, address); // The middle cycle, and which one it is depends on which // machine this is pretending to be. Emulating, the // unmodified value goes back onto the bus before the // modified one, which is what the NMOS 6502 did and what a // program written for that machine may be counting on. // Natively it is an ordinary internal cycle with nothing // driven, as on the 65C02. The widths are the same either // way; it is the emulation line that decides. const last = if (T == u8) address else @TypeOf(cpu.addressing_modes).next(addressing_mode, address); if (cpu.flags.emulation) cpu.memory.internalWrite(last, @truncate(value >> (@typeInfo(T).int.bits - 8))) else cpu.memory.internal(last); cpu.addressing_modes.writeDataReversed(addressing_mode, T, address, compute(cpu, T, value)); } }; return f.TSB; } }; }