// 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 = "TRB"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x14, .addressing_mode = .@"Zero Page", .cycles = 5, }, .{ .opcode = 0x1c, .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) *const fn (*CPU) error{}!void { const f = struct { /// Tests bits and then clears them. /// /// The accumulator says which bits: the zero flag is set if /// none of them were, and then they are taken out of the byte. /// Testing and changing a byte in one instruction is what makes /// this useful for a flag two things are both looking at. pub fn TRB(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .write); const value = cpu.memory.read(u8, address); // A read-modify-write spends a cycle on the bus before // it writes: the NMOS part drives the unmodified value // back, and the CMOS part reads the address again. switch (CPU.processor) { .@"6502", .RP2A03 => cpu.memory.write(u8, address, value), .@"65C02", .R65C02, .W65C02 => _ = cpu.memory.read(u8, address), } // The zero flag is about what was there before, not after. cpu.flags.update(.Z, value & cpu.registers.get(.A) == 0); cpu.memory.write(u8, address, value & ~cpu.registers.get(.A)); } }; return f.TRB; } }; }