// 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 = "PHP"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x08, .addressing_mode = .Implied, .cycles = 3, }, }; /// 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) CPU.Opcodes.Error!void { if (addressing_mode != .Implied) @compileError("illegal addressing mode"); const f = struct { /// Pushes the status register onto the stack, with the break /// flag set in the pushed copy. /// /// (MOS Technology, 1976, Section 8.11, p. 122) /// (Carr, 1984, p. 267) pub fn PHP(cpu: *CPU) CPU.Opcodes.Error!void { // the operand fetch every single byte instruction performs // and discards cpu.program_counter.phantomRead(); cpu.stack.push(u8, cpu.flags.saveBRK()); } }; return f.PHP; } }; } test "PHP" { const std = @import("std"); const CPU = @import("../../cpu8.zig").Bare; var cpu: CPU = undefined; try cpu.init(null, null); try cpu.memory.loadProgram(0x0000, &[_]u8{0x08}); const actual = try cpu.executeInstruction(); try std.testing.expectEqual({}, actual); try std.testing.expectEqual(@as(u16, 0x0001), cpu.program_counter.value); try std.testing.expectEqual(@as(usize, 3), cpu.clock.cycles); try std.testing.expectEqual(@as(u16, 0xfe), cpu.stack.pointer); try std.testing.expectEqual(@as(u8, 0b00110010), cpu.memory.peek(0x01ff)); try std.testing.expect(cpu.flags.isClear(.N)); try std.testing.expect(cpu.flags.isClear(.V)); try std.testing.expect(cpu.flags.isClear(.B)); try std.testing.expect(cpu.flags.isClear(.D)); try std.testing.expect(cpu.flags.isClear(.I)); try std.testing.expect(cpu.flags.isSet(.Z)); try std.testing.expect(cpu.flags.isClear(.C)); }