// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); /// The processor status register. Each flag is kept separately rather than /// packed, with the byte assembled only when something pushes or reads it, and /// each carries the cycle it last changed on so the debugger can highlight it. /// 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 Flags(comptime CPU: type) type { return struct { const Self = @This(); const Name = enum { /// negative N, /// overflow V, /// break command B, /// decimal mode D, /// interrupt disable I, /// zero Z, /// carry C, }; _values: std.EnumArray(Name, bool), _changed: std.EnumArray(Name, u64), /// The CPU this belongs to, recovered from the field it is embedded in. inline fn getCPU(self: *Self) *CPU { return @alignCast(@fieldParentPtr("flags", self)); } /// Clears every flag except that N and Z are set from the accumulator, /// for a CPU that is not running yet. pub fn init(self: *Self) void { const cpu = self.getCPU(); self.* = .{ ._values = .init(.{ .C = false, .Z = cpu.registers.get(.A) == 0, .I = false, .D = false, .B = false, .V = false, .N = cpu.registers.get(.A) & 0x80 == 0x80, }), ._changed = .initFill(0), }; } /// As init, but timestamps the flags against the running clock, and /// leaves interrupts disabled. /// /// Reset is one of the hardware interrupts — the W65C816S datasheet /// lists RESETB at $00fffc,d in the same table as IRQB and NMIB — and /// "when an interrupt is executed, D=0 and I=1 in Status Register P" /// (Western Design Center, 2018, p. 30). /// /// It matters more than it looks. Reset reaches the processor and the /// cards, but a card that was asking for attention is still asking: /// nothing about the line being pulled goes away. If the flag comes /// back clear, the first instruction the reset vector points at is /// interrupted before it finishes, and the machine goes off to a /// handler instead of starting. That is what stopped mb-audit's /// Ctrl-Reset test, whose whole point is to reset a machine with a /// Mockingboard timer running. pub fn reset(self: *Self) void { const cpu = self.getCPU(); self._values = .init(.{ .C = false, .Z = cpu.registers.get(.A) == 0, .I = true, .D = false, .B = false, .V = false, .N = cpu.registers.get(.A) & 0x80 == 0x80, }); self._changed = .initFill(cpu.clock.read()); } /// How many cycles ago the flag last changed. pub fn since(self: *Self, comptime flag: Name) u64 { const cpu = self.getCPU(); return cpu.clock.read() - self._changed.get(flag); } /// Whether the flag is set. pub fn isSet(self: *Self, comptime flag: Name) bool { return self._values.get(flag) == true; } /// Whether the flag is clear. pub fn isClear(self: *Self, comptime flag: Name) bool { return self._values.get(flag) == false; } /// Sets a flag. pub fn set(self: *Self, comptime flag: Name) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, true); } /// Clears a flag. pub fn clear(self: *Self, comptime flag: Name) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, false); } /// Inverts a flag. pub fn toggle(self: *Self, comptime flag: Name) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, !self._values.get(flag)); } /// Sets a flag to a computed value. pub fn update(self: *Self, comptime flag: Name, value: bool) void { const cpu = self.getCPU(); self._changed.set(flag, cpu.clock.read()); self._values.set(flag, value); } /// Reports a result through N and Z, which nearly every instruction /// that produces a value does. pub fn updateNZ(self: *Self, value: u8) void { self.update(.N, value & 0x080 == 0x80); self.update(.Z, value == 0x00); } /// Carry as a number to add in, for the arithmetic and rotate /// instructions. pub fn carry(self: *const Self, comptime T: type) T { const info = @typeInfo(T); if (info != .int) @compileError("must be an integer"); // if (info.Int.signedness != .unsigned) @compileError("must be unsigned"); if (info.int.bits != 8 and info.int.bits != 16) @compileError("must be u8 or u16"); return if (self._values.get(.C)) 1 else 0; } // the order matters! const Value = packed struct(u8) { /// carry C: bool = false, /// zero Z: bool = true, /// interrupt disable I: bool = false, /// decimal mode D: bool = false, /// break command B: bool = false, // padding _5: bool = true, /// overflow V: bool = false, /// negative N: bool = false, }; /// The status register as a byte, with the unused bit 5 set as the /// hardware always reads it. pub fn save(self: *const Self) u8 { var value: Value = undefined; inline for (@typeInfo(Name).@"enum".fields) |field| { @field(value, field.name) = self._values.get(@enumFromInt(field.value)); } value._5 = true; return @bitCast(value); } /// As save, but with the break flag clear, which is how an interrupt /// pushes it. That bit is the only thing telling a handler whether it /// was reached by BRK or by the interrupt line, so the two must not /// agree on it. pub fn saveInterrupt(self: *const Self) u8 { var value: Value = @bitCast(self.save()); value.B = false; return @bitCast(value); } /// As save, but with the break flag set in the copy. BRK and PHP push /// this rather than the live flag, which is never actually stored in /// the register. pub fn saveBRK(self: *Self) u8 { var value: Value = undefined; inline for (@typeInfo(Name).@"enum".fields) |field| { @field(value, field.name) = self._values.get(@enumFromInt(field.value)); } value.B = true; value._5 = true; return @bitCast(value); } /// Loads the status register from a byte, keeping the current break /// flag because nothing can pull one back in. pub fn restore(self: *Self, value: u8) void { const cpu = self.getCPU(); var new: Value = @bitCast(value); new.B = self._values.get(.B); inline for (@typeInfo(Name).@"enum".fields) |field| { self._values.set(@enumFromInt(field.value), @field(new, field.name)); } self._changed = .initFill(cpu.clock.read()); } /// The status register as a byte, for tests and the debugger. Unlike /// save it reports the break flag as it stands. pub fn dump(self: *Self) u8 { var new: Value = undefined; new._5 = true; inline for (@typeInfo(Name).@"enum".fields) |field| { @field(new, field.name) = self._values.get(@enumFromInt(field.value)); } return @bitCast(new); } /// Loads the status register from a byte wholesale, break flag /// included, for tests setting up a starting state. pub fn load(self: *Self, value: u8) void { const new: Value = @bitCast(value); inline for (@typeInfo(Name).@"enum".fields) |field| { self._values.set(@enumFromInt(field.value), @field(new, field.name)); } } /// Whether every flag matches the given byte. Bit 5 is not compared, /// having no flag behind it. pub fn compare(self: *const Self, value: u8) bool { const other: Value = @bitCast(value); inline for (@typeInfo(Name).@"enum".fields) |field| { if (self._values.get(@enumFromInt(field.value)) != @field(other, field.name)) return false; } return true; } /// Writes the flags as a row of letters, capital for set and lower case /// for clear, with a dash for the unused bit. pub fn format(_: *Self, writer: *std.Io.Writer, value_: u8) !void { const value: Value = @bitCast(value_); const info = @typeInfo(Value); inline for (info.@"struct".fields) |field| { if (field.name[0] == '_') { try writer.writeByte('-'); continue; } try writer.writeByte(if (@field(value, field.name)) std.ascii.toUpper(field.name[0]) else std.ascii.toLower(field.name[0])); } } }; }