// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT //! The SingleStepTests corpus, run against the processor. //! //! Its own project, so that the corpus is its own project's business. It is //! two and a half million files and well over a gigabyte unpacked, and nothing //! that merely builds the library should have to know it exists; keeping it //! here keeps it out of the library's manifest and out of anything generated //! from that manifest. //! //! The library comes from the checkout this sits inside, by path, so the two //! are always the same code. const std = @import("std"); const Processor = enum { @"6502", RP2A03, @"65C02", R65C02, W65C02, @"65C816" }; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); // Checking two and a half million cases three ways is worth optimising, // and there is nothing here to step through. // ReleaseFast unless asked otherwise. `standardOptimizeOption`'s preferred // mode is not a default: it applies only when `--release` is passed, and // otherwise the build is Debug -- which this one was, silently, for as // long as it said it preferred ReleaseFast. The runner was five times // slower for it. const optimize = b.option(std.builtin.OptimizeMode, "optimize", "Prioritize performance, safety, or binary size") orelse .ReleaseFast; const zig65 = b.dependency("zig65", .{ .target = target, .optimize = optimize, }); // Which processor to check, decided before the module is made because the // 65816 has a runner of its own. The two corpora are different shapes — // sixteen-bit registers, twenty-four-bit addresses, eight signal lines per // cycle, two files per opcode — and the cores they run against share no // code, so neither do the runners. const processor = b.option(Processor, "processor", "which processor to check") orelse .@"6502"; const runner = b.createModule(.{ .root_source_file = b.path(switch (processor) { .@"65C816" => "src/main65816.zig", else => "src/main.zig", }), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "zig65", .module = zig65.module("zig65") }, }, }); // Which opcodes to run, for narrowing a hunt down to one instruction. const ssf = b.option([]const []const u8, "ssf", "single step filter"); // Which way the 65816's dispatch table is built. Both have to pass the // same corpus before either can be measured against the other: a faster // one that is wrong is not a candidate. const Dispatch = enum { branch, tables }; const dispatch = b.option(Dispatch, "dispatch", "how the 65816 dispatches (default branch)") orelse .branch; const ssf_options = b.addOptions(); ssf_options.addOption(Dispatch, "dispatch", dispatch); ssf_options.addOption(?[]const []const u8, "ssf", ssf); ssf_options.addOption(Processor, "processor", processor); runner.addOptions("options", ssf_options); const exe = b.addExecutable(.{ .name = switch (processor) { .@"65C816" => "single_step_tests_65816", else => "single_step_tests", }, .root_module = runner, }); b.installArtifact(exe); // The corpus is a large download, so it is lazy: nothing fetches it until // this step is actually asked for. const run_step = b.step("run", "Run the whole corpus"); if (b.lazyDependency(switch (processor) { .@"65C816" => "single_step_tests_65816", else => "single_step_tests", }, .{})) |corpus| { const run = b.addRunArtifact(exe); // Which corpus to run against, handed over as an argument rather than // compiled in, so the same binary can be pointed at a checkout by hand. run.addDirectoryArg(corpus.path(switch (processor) { .@"6502" => "6502/v1", // Ricoh's part, recorded separately because the decimal mode is // not wired up: every ADC and SBC with D set has a different // answer here than in the set above. .RP2A03 => "nes6502/v1", // The plain CMOS part, without the bit instructions the other two // have: what is here is what all three of them agree on. .@"65C02" => "synertek65c02/v1", .R65C02 => "rockwell65c02/v1", .W65C02 => "wdc65c02/v1", // Its own repository, so the whole of it is the version. .@"65C816" => "v1", })); if (b.args) |args| run.addArgs(args); run_step.dependOn(&run.step); } const check_step = b.step("check", "Type check, emitting nothing"); check_step.dependOn(&b.addExecutable(.{ .name = "single_step_tests", .root_module = runner, }).step); }