// 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 = "BIT"; pub const opcodes: []const CPU.Opcodes.Init = switch (CPU.processor) { .@"6502", .RP2A03 => &.{ .{ .opcode = 0x24, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x2c, .addressing_mode = .Absolute, .cycles = 4, }, }, // The CMOS part gave this one more to answer to. .@"65C02", .R65C02, .W65C02 => &.{ .{ .opcode = 0x24, .addressing_mode = .@"Zero Page", .cycles = 3, }, .{ .opcode = 0x2c, .addressing_mode = .Absolute, .cycles = 4, }, .{ .opcode = 0x89, .addressing_mode = .Immediate, .cycles = 2, }, .{ .opcode = 0x34, .addressing_mode = .@"Zero Page, X", .cycles = 4, }, .{ .opcode = 0x3c, .addressing_mode = .@"Absolute, 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) *const fn (*CPU) error{}!void { const f = struct { /// Tests bits of memory against the accumulator without keeping /// the result: bits 7 and 6 of the byte go straight into N and /// V, and Z reports whether the AND came out zero. /// /// (MOS Technology, 1976, Section 4.2.2.1, p. 47) /// (Carr, 1984, p. 249) pub fn BIT(cpu: *CPU) error{}!void { const address = cpu.addressing_modes.decode(addressing_mode, .read); const value = cpu.memory.read(u8, address); const result = cpu.registers.get(.A) & value; // Testing against a constant is the one form that leaves // the top two bits of the byte alone, since there is no // point copying them out of something the program wrote // itself: it already knows what they are. Every other form // puts them in N and V, which is what makes this // instruction the way to read a flag byte from hardware. if (comptime !(CPU.processor.cmos() and addressing_mode == .Immediate)) { cpu.flags.update(.N, value & 0x80 == 0x80); cpu.flags.update(.V, value & 0x40 == 0x40); } cpu.flags.update(.Z, result == 0x00); } }; return f.BIT; } }; } test "BIT zero page" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit(); cpu.registers.set(.A, 0xff); cpu.flags.updateNZ(0xff); try cpu.memory.loadProgram(0x0000, &[_]u8{ 0x24, 0x02, 0x40 }); const actual = try cpu.executeInstruction(); try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0002), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 3), cpu.clock.cycles); try std.testing.expectEqual(@as(u8, 0xff), cpu.registers.get(.A)); // try std.testing.expect(cpu.flag.compare(.{ // .N = false, // .V = true, // .B = false, // .D = false, // .I = false, // .Z = false, // .C = false, // })); } test "BIT zero page 2" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit(); cpu.registers.set(.A, 0x80); cpu.flags.updateNZ(0x80); try cpu.memory.loadProgram(0x0000, &[_]u8{ 0x24, 0x02, 0x80 }); const actual = try cpu.executeInstruction(); try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0002), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 3), cpu.clock.cycles); try std.testing.expectEqual(@as(u8, 0x80), cpu.registers.get(.A)); // try std.testing.expect(cpu.flag.compare(.{ // .N = true, // .V = false, // .B = false, // .D = false, // .I = false, // .Z = false, // .C = false, // })); } test "BIT zero page 3" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit(); cpu.registers.set(.A, 0x80); try cpu.memory.loadProgram(0x0000, &[_]u8{ 0x24, 0x02, 0x40 }); const actual = try cpu.executeInstruction(); try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0002), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 3), cpu.clock.cycles); try std.testing.expectEqual(@as(u8, 0x80), cpu.registers.get(.A)); // try std.testing.expect(cpu.flags.compare(.{ // .N = false, // .V = true, // .B = false, // .D = false, // .I = false, // .Z = true, // .C = false, // })); } // test "BIT zero page 4" { // const std = @import("std"); // const clock = @import("../../clock.zig"); // const CPU = @import("../../cpu8.zig").Bare; // var cpu: CPU = undefined; // try cpu.init(null, null); // defer cpu.deinit(); // const actual = cpu.BIT(.Implied); // try std.testing.expectError(error.IllegalAddressingMode, actual); // }