// 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 = "JMP"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x4c, .addressing_mode = .Absolute, .cycles = 3, }, .{ .opcode = 0x5c, .addressing_mode = .@"Absolute Long", .cycles = 4, }, .{ .opcode = 0x6c, .addressing_mode = .@"(Absolute)", .cycles = 5, }, .{ .opcode = 0x7c, .addressing_mode = .@"(Absolute, X)", .cycles = 6, }, .{ .opcode = 0xdc, .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 { _ = widths; const f = struct { /// Carries on somewhere else. pub fn JMP(cpu: *CPU) !void { switch (addressing_mode) { // Two bytes, and the bank does not change: a jump // inside the bank it was already running in. .Absolute => cpu.program_counter.jump(cpu.program_counter.read(u16)), // Three bytes, bank and all. The only jump that can // leave the bank without going through memory. .@"Absolute Long" => { const target = cpu.program_counter.read(u24); cpu.registers.program_bank = @truncate(target >> 16); cpu.program_counter.jump(@truncate(target)); }, // A pointer in bank zero. The 6502's famous bug — a // pointer ending in $ff taking its high byte from the // start of the same page — is gone here: the pointer // is read from bank zero and simply counts on. .@"(Absolute)" => { const at = cpu.program_counter.read(u16); const low: u16 = cpu.memory.read(at, .data); const high: u16 = cpu.memory.read(at +% 1, .data); cpu.program_counter.jump(high << 8 | low); }, // A pointer chosen by indexing, read from the program // bank rather than from bank zero, which is what makes // it a jump table a program can keep beside itself. .@"(Absolute, X)" => { const started = cpu.program_counter.value; const base = cpu.program_counter.read(u16); cpu.memory.internal(cpu.program_counter.addressOf(started +% 1)); // The pointer is read inside the program bank and // wraps there: one at the very top of the bank // takes its high byte from the bottom of the same // bank, not from the next one. const bank = @as(u24, cpu.registers.program_bank) << 16; const offset = base +% cpu.registers.get(.X); const low: u16 = cpu.memory.read(bank | offset, .data); const high: u16 = cpu.memory.read(bank | (offset +% 1), .data); cpu.program_counter.jump(high << 8 | low); }, // A three-byte pointer in bank zero, so the bank comes // out of memory and this reaches anywhere. .@"[Absolute]" => { const at = cpu.program_counter.read(u16); const low: u24 = cpu.memory.read(at, .data); const high: u24 = cpu.memory.read(at +% 1, .data); const bank: u24 = cpu.memory.read(at +% 2, .data); cpu.registers.program_bank = @truncate(bank); cpu.program_counter.jump(@truncate(high << 8 | low)); }, else => @compileError("illegal addressing mode"), } } }; return f.JMP; } }; }