// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// The program counter, and the bank it counts within. /// /// The counter itself is sixteen bits, as it was. What is new is that it does /// not carry: running past $ffff wraps to $0000 of the same bank rather than /// going on to the next one, and the only things that change the program bank /// are the instructions that say so. So a program cannot walk out of its bank /// by accident, which is the whole reason the bank register is separate. /// /// The bank register itself lives with the other registers, since that is /// where a debugger looks for it; the counter reaches it through the CPU. pub fn ProgramCounter(comptime CPU: type) type { return struct { const Self = @This(); value: u16 = 0, /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("program_counter", self)); } /// Sets the counter to zero, for a CPU that is not running yet. pub fn init(self: *Self) void { self.value = 0; } /// Loads the counter from the reset vector. Reset puts the processor /// into emulation mode in bank zero, so the vector is the 6502's. pub fn reset(self: *Self) void { const cpu = self.getCPU(); const low: u16 = cpu.memory.read(CPU.vector.emulation.reset, .vector); const high: u16 = cpu.memory.read(CPU.vector.emulation.reset + 1, .vector); self.value = high << 8 | low; } /// The full address the next instruction byte comes from: the counter /// inside the program bank. pub fn address(self: *const Self) u24 { const cpu: *const CPU = @alignCast(@fieldParentPtr("program_counter", self)); return @as(u24, cpu.registers.program_bank) << 16 | self.value; } /// An address inside the program bank, for a counter value worked out /// by the caller. /// /// The counter does not carry into the bank register, and neither does /// this: an instruction whose operand sits at the very top of a bank /// spends its internal cycle back at the bottom of the same bank /// rather than at the start of the next. pub fn addressOf(self: *const Self, value: u16) u24 { const cpu: *const CPU = @alignCast(@fieldParentPtr("program_counter", self)); return @as(u24, cpu.registers.program_bank) << 16 | value; } /// The cycle the chip spends with the counter on the address bus and /// nothing enabled: a one-byte instruction still takes two cycles, and /// this is the second of them. /// /// On the 6502 this was a read of the byte after the opcode, thrown /// away. Here it is not a read at all — the recordings show no value /// against it, because neither VDA nor VPA is asserted — but the /// address bus still holds the counter, so the address is the same. pub fn internal(self: *Self) void { const cpu = self.getCPU(); cpu.memory.internal(self.address()); } /// Fetches the opcode: the one cycle that is both an instruction byte /// and a data byte, and the only one with VDA and VPA together. pub fn opcode(self: *Self) u8 { const cpu = self.getCPU(); const value = cpu.memory.read(self.address(), .opcode); self.value +%= 1; return value; } /// Fetches an operand and steps past it. A word is read low byte /// first, the order operands are stored in, and the counter wraps /// within the bank rather than carrying into it. pub fn read(self: *Self, comptime T: type) T { const cpu = self.getCPU(); switch (@typeInfo(T).int.bits) { 8 => { const value = cpu.memory.read(self.address(), .operand); self.value +%= 1; return value; }, 16 => { const low: u16 = self.read(u8); const high: u16 = self.read(u8); return high << 8 | low; }, 24 => { const low: u24 = self.read(u16); const bank: u24 = self.read(u8); return bank << 16 | low; }, else => @compileError("must be a u8, a u16 or a u24"), } } /// Moves the counter within the bank. pub fn jump(self: *Self, value: u16) void { self.value = value; } }; }