// 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 = "MVN"; pub const opcodes = [_]CPU.Opcodes.Init{ .{ .opcode = 0x54, .addressing_mode = .@"Block Move", // Seven a byte, and it moves one byte per execution: a move of // a thousand bytes is this instruction run a thousand times. .cycles = 7, }, }; /// 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 { if (addressing_mode != .@"Block Move") @compileError("illegal addressing mode"); const f = struct { /// Moves one byte from one bank to another, counting up as it goes: this is the one for moving a block to a lower address, where the start of the source is safe to overwrite first. /// /// The only instruction here that is not finished when it ends. /// It moves a single byte and, if there are more to go, backs /// the program counter up over itself so that the very same /// three bytes are fetched again. That is not a loop the /// processor is running: it is the same instruction being /// executed over and over, which is why an interrupt can be /// answered in the middle of moving sixty kilobytes and why /// the recordings can hold one execution at a time. /// /// The accumulator is the count less one, and the move stops /// when it wraps past zero — so a count of zero moves one byte /// and a count of $ffff moves the whole bank. The two operand /// bytes are the banks, destination first in the object code /// even though every assembler writes the source first, and /// the destination one is left in the data bank register /// afterwards. Nothing else here writes that register. pub fn MVN(cpu: *CPU) !void { if (comptime widths) |w| { if (comptime w.x) at(cpu, u8) else at(cpu, u16); } else { if (cpu.flags.isSet(.X)) at(cpu, u8) else at(cpu, u16); } } fn at(cpu: *CPU, comptime T: type) void { const destination: u24 = @as(u24, cpu.program_counter.read(u8)) << 16; const source: u24 = @as(u24, cpu.program_counter.read(u8)) << 16; cpu.registers.data_bank = @truncate(destination >> 16); const from = source | cpu.registers.getNarrow(.X, T); const to = destination | cpu.registers.getNarrow(.Y, T); const value = cpu.memory.read(from, .data); cpu.memory.write(to, value, .data); cpu.registers.setNarrow(.X, T, cpu.registers.getNarrow(.X, T) +% 1); cpu.registers.setNarrow(.Y, T, cpu.registers.getNarrow(.Y, T) +% 1); // The count is the whole accumulator whatever M says, // because what it counts is bytes and there can be more // than 255 of them. const remaining = cpu.registers.get(.A) -% 1; cpu.registers.set(.A, remaining); cpu.memory.internal(to); cpu.memory.internal(to); // Not done: back the counter over the whole instruction so // that the next fetch finds it again. if (remaining != 0xffff) cpu.program_counter.jump(cpu.program_counter.value -% 3); } }; return f.MVN; } }; }