// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// The program counter, and the operand fetching that walks it forward. /// Embedded in CPU, and finds its way back to it with @fieldParentPtr rather /// than holding a pointer, so the whole processor can be built in place. 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, which is where the /// processor starts from when the reset line is released. pub fn reset(self: *Self) void { const cpu = self.getCPU(); self.value = cpu.memory.read(u16, CPU.vector.reset); } /// The read the chip performs at the program counter and throws /// away. A single byte instruction still fetches the byte after its /// opcode; it just ignores it and leaves the counter alone. pub fn phantomRead(self: *Self) void { const cpu = self.getCPU(); _ = cpu.memory.read(u8, self.value); } /// Fetches an operand and steps past it. A word is read low byte first, /// the order operands are stored in. pub fn read(self: *Self, comptime T: type) T { const cpu = self.getCPU(); const info = @typeInfo(T); if (info != .int) @compileError("must be an integer"); if (info.int.signedness != .unsigned) @compileError("must be unsigned"); switch (info.int.bits) { 8 => { const value = cpu.memory.read(u8, self.value); self.value +%= 1; return value; }, 16 => { const low = cpu.memory.read(u8, self.value); self.value +%= 1; const high = cpu.memory.read(u8, self.value); self.value +%= 1; return @as(u16, high) << 8 | low; }, else => @compileError("must be a u8 or a u16"), } } // Calculate the address that an immediate argument should be loaded // from. This does not actually return the data, just the address that // holds the data. /// The address an immediate operand lives at, which is simply the /// counter. Steps past it without reading, because the instruction does /// the read itself. pub fn immediate(self: *Self) u16 { const result = self.value; self.value +%= 1; return result; } /// Moves the counter. The operand fetch has already been paid for, so /// this costs nothing on its own. pub fn jump(self: *Self, value: u16) void { self.value = value; } /// Takes a branch, spending the cycle the chip uses to add the offset /// and a second one when the target turns out to be in another page. pub fn branch(self: *Self, value: u16) void { const cpu = self.getCPU(); // the discarded read taken while the offset is added to the low // byte of the counter self.phantomRead(); // landing in another page costs a second one, at the address the // chip built before it fixed up the high byte if (self.value & 0xff00 != value & 0xff00) _ = cpu.memory.read(u8, (self.value & 0xff00) | (value & 0x00ff)); self.value = value; } /// Where a branch would land, counted from the instruction after it. /// Does not take the branch and costs nothing. pub fn calculateBranchLocation(self: *Self, offset: i8) u16 { return @intCast((@as(i17, @intCast(self.value)) +% offset) & 0xffff); } }; }