// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! Forces every declaration in the CPU and its parts to be compiled. //! //! Zig analyses a function only when something references it, and instantiates //! a generic one only when something calls it with particular comptime //! arguments. Several functions here went a long time without either and were //! quietly broken the whole time: the 16-bit branch of Memory.write passed its //! arguments in the wrong order, and Registers.changed read two fields that do //! not exist. Neither could ever fail a build, because neither was ever //! compiled. //! //! refAllDecls covers the ordinary functions: referencing one is enough to get //! its body analysed. It does nothing for a function with comptime parameters, //! because a reference to a generic function is not a reference to any //! particular instance of it. Those have to be called, once per point in their //! parameter space, which is what the second test does. //! //! The instruction implementations need no help here. Opcodes builds its //! dispatch table at comptime and stores a pointer to every instruction in //! every addressing mode it accepts, so all 256 of them are instantiated before //! any test runs. //! //! That is true of one processor at a time, though, and the whole CPU is //! generic over which. Everything above is about the default one, and a part //! that only the others reach is in exactly the position this file exists to //! rule out: never compiled, so never wrong. Adding the RP2A03 broke the //! phantom read of an indexed address on it alone, and `zig build check` //! passed the entire time, because nothing anywhere asked for that processor. //! The last test here asks for every one of them. const std = @import("std"); const clock = @import("clock.zig"); const memory = @import("memory8.zig"); const Chip = @import("cpu8.zig").Chip; const Processor = @import("root.zig").Processor; const CPU = @import("cpu8.zig").Bare; /// The type of one of the CPU's parts. They are fields rather than public /// declarations, so they are named through an instance that is never read. fn Part(comptime field: []const u8) type { return @TypeOf(@field(@as(CPU, undefined), field)); } test "every declaration is referenced" { inline for (.{ CPU, CPU.Memory, CPU.Opcodes, Part("clock"), Part("flags"), Part("registers"), Part("program_counter"), Part("addressing_modes"), Part("stack"), }) |T| std.testing.refAllDecls(T); } test "every generic declaration is instantiated" { var cpu: CPU = undefined; try cpu.init(null, null); defer cpu.deinit(); // Memory, the program counter and the stack are generic over the width // they move at a time. inline for (.{ u8, u16 }) |T| { _ = cpu.memory.read(T, 0x0200); cpu.memory.write(T, 0x0200, 0); _ = cpu.program_counter.read(T); cpu.stack.push(T, 0); _ = cpu.stack.pop(T); _ = cpu.flags.carry(T); } // The registers are generic over which one is meant. inline for (.{ .A, .X, .Y }) |register| { cpu.registers.set(register, 0); _ = cpu.registers.get(register); _ = cpu.registers.changed(register); } // As are the flags. inline for (.{ .N, .V, .B, .D, .I, .Z, .C }) |flag| { cpu.flags.set(flag); cpu.flags.clear(flag); cpu.flags.toggle(flag); cpu.flags.update(flag, true); _ = cpu.flags.isSet(flag); _ = cpu.flags.isClear(flag); _ = cpu.flags.since(flag); } // Decoding is generic over both the addressing mode and what the // instruction intends to do with the address. inline for (@typeInfo(CPU.AddressingModes).@"enum".fields) |field| { const mode: CPU.AddressingModes = @enumFromInt(field.value); // Implied instructions have no operand to find, and asking for one is // a compile error rather than a runtime one. The same goes for the // shapes of the CMOS part's undecoded encodings, which do their own // fetching because there is no address in them to work out. if (mode == .Implied or mode == .@"No Operand" or mode == .@"Two Operands" or mode == .@"Zero Page, Relative") continue; inline for (.{ .read, .write }) |access| { _ = cpu.addressing_modes.decode(mode, access); } } // Only the indexed modes can be taken apart. inline for (.{ .@"Absolute, X", .@"Absolute, Y", .@"(Indirect), Y" }) |mode| { _ = cpu.addressing_modes.decodeIndexed(mode).target(0); } } test "every processor's own instruction table is built" { // Each of these is a separate type with a separate dispatch table, built // from a different list of instructions and holding a different set of // 256 implementations. Naming the type is not enough to compile any of // that: a reference to a generic function is not a reference to an // instance of it. Executing an instruction is, because that is what // reaches Opcodes.get, and getting there means the whole table exists. inline for (@typeInfo(Processor).@"enum".fields) |field| { const Model = Chip( .{ .processor = @field(Processor, field.name) }, clock.FakeClock, memory.Flat(.{}), ); var cpu: Model = undefined; try cpu.init(null, null); defer cpu.deinit(); // NOP, which every one of them has and none of them argues about. try cpu.memory.loadProgram(0x0200, &[_]u8{0xea}); cpu.program_counter.value = 0x0200; try cpu.executeInstruction(); try std.testing.expectEqual(@as(u16, 0x0201), cpu.program_counter.value); // And the parts, which differ between them too: the addressing modes // decode differently and the flags are set from different places. std.testing.refAllDecls(Model); std.testing.refAllDecls(Model.Memory); std.testing.refAllDecls(Model.Opcodes); inline for (.{ "clock", "flags", "registers", "program_counter", "addressing_modes", "stack" }) |part| { std.testing.refAllDecls(@TypeOf(@field(@as(Model, undefined), part))); } } }