// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// The 65816's registers. /// /// A, X and Y are sixteen bits wide and stay that way. What the M and X flags /// change is how much of them an instruction uses, not how much of them /// exists: with an eight-bit accumulator the high half is still there, holding /// whatever it held, and XBA is how a program gets at it. That is why the /// accumulator is stored whole here and narrowed at the point of use, rather /// than stored at whichever width happens to be selected. /// /// The index registers are different, and the difference is easy to get wrong. /// Setting the X flag does not merely narrow how they are read: it clears /// their high halves, then and there. A program that sets X and clears it /// again finds zeroes where its high bytes were. /// /// Beside them are the three registers that say where things are. D is the /// direct page, which the 6502's zero page became when it was allowed to move. /// DBR is the bank data goes to, and PBR the bank instructions come from. pub fn Registers(comptime CPU: type) type { return struct { const Self = @This(); pub const Name = enum { A, X, Y, }; _values: std.EnumArray(Name, u16), _changed: std.EnumArray(Name, u64), /// The direct page. Zero puts it exactly where a 6502's zero page was. direct: u16, /// The data bank: where an instruction's operand address lives when /// the instruction does not say a bank itself. data_bank: u8, /// The program bank: where instructions are fetched from. The program /// counter does not carry into this, so running off the end of a bank /// wraps to its start rather than going on to the next. program_bank: u8, /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("registers", self)); } /// Clears everything without touching the flags, for a CPU that is not /// running yet. pub fn init(self: *Self) void { self._values = .initFill(0); self._changed = .initFill(0); self.direct = 0; self.data_bank = 0; self.program_bank = 0; } /// What the reset line does: the index registers keep only their low /// halves because reset puts the processor into emulation mode, the /// direct page goes back to zero, and both bank registers go to zero, /// which is why a 65816 comes up looking like a 6502 in bank zero. pub fn reset(self: *Self) void { const cpu = self.getCPU(); self._changed = .initFill(cpu.clock.read()); self.direct = 0; self.data_bank = 0; self.program_bank = 0; self.narrowIndexes(); } /// Writes a register whole, and records when it changed. pub fn set(self: *Self, comptime register: Name, value: u16) void { const cpu = self.getCPU(); self._changed.set(register, cpu.clock.read()); self._values.set(register, value); } /// Reads a register whole, whatever width is selected. pub fn get(self: *const Self, comptime register: Name) u16 { return self._values.get(register); } /// Writes a register at the width the flags currently select, leaving /// the accumulator's high half alone when the accumulator is narrow. /// /// This is the one that instruction implementations want: LDA with M /// set loads a byte and does not disturb what XBA would swap in. pub fn setNarrow(self: *Self, comptime register: Name, comptime T: type, value: T) void { switch (@typeInfo(T).int.bits) { 8 => self.set(register, (self.get(register) & 0xff00) | value), 16 => self.set(register, value), else => @compileError("must be a u8 or a u16"), } } /// Reads a register at a width. pub fn getNarrow(self: *const Self, comptime register: Name, comptime T: type) T { return @truncate(self.get(register)); } /// Throws away the high halves of X and Y, which is what selecting /// eight-bit indexes does to them. pub fn narrowIndexes(self: *Self) void { self._values.set(.X, self._values.get(.X) & 0x00ff); self._values.set(.Y, self._values.get(.Y) & 0x00ff); } /// How many cycles ago the register was last written. pub fn changed(self: *Self, comptime register: Name) u64 { const cpu = self.getCPU(); return cpu.clock.read() - self._changed.get(register); } }; } test "a narrow accumulator keeps its high half, narrow indexes do not" { const CPU = @import("../cpu16.zig").Bare; var cpu: CPU = undefined; try cpu.init(std.testing.allocator, null); defer cpu.deinit(); cpu.flags.setEmulation(false); cpu.flags.clear(.M); cpu.flags.clear(.X); cpu.registers.set(.A, 0x1ec9); cpu.registers.set(.X, 0xfd2d); // An eight-bit store into A leaves the hidden half where it was, which is // what makes the second accumulator a place to keep something. cpu.registers.setNarrow(.A, u8, 0xfb); try std.testing.expectEqual(@as(u16, 0x1efb), cpu.registers.get(.A)); // Selecting eight-bit indexes throws the high half away for good. cpu.flags.set(.X); try std.testing.expectEqual(@as(u16, 0x002d), cpu.registers.get(.X)); cpu.flags.clear(.X); try std.testing.expectEqual(@as(u16, 0x002d), cpu.registers.get(.X)); }