This is a cache of https://vccplus.brianholman.com/publications/vcc-plus-architecture/evidence/experiments/cmoc-benchmarks/runner-cpp/. It is a snapshot of the page as it appeared on 2026-11-21T00:02:59.752+0000.
experiments/cmoc-benchmarks/runner.cpp — supporting evidence · VCC Plus
← Supporting evidence

Evidence record

experiments/cmoc-benchmarks/runner.cpp

From Windows Application to Portable Emulation Platform

6,956 bytes

// SPDX-License-Identifier: GPL-3.0-or-later
// Execute newly compiled programs in a disposable VCC Plus machine.
// The caller owns compilation, symbol resolution and output expectations.
#include "MachineSession.h"
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <string>
#include <vector>

using namespace VCC::Mac;
namespace {
using Bytes = std::vector<std::uint8_t>;
constexpr unsigned bootFields = 180;
constexpr unsigned maximumFields = 3600;
constexpr std::uint64_t maximumCycles = 100000000;

void require(bool condition, const std::string& message) {
    if (!condition) throw std::runtime_error(message);
}
Bytes read(const char* path) {
    std::ifstream stream(path, std::ios::binary);
    require(bool(stream), "Cannot open input: " + std::string(path));
    Bytes result{std::istreambuf_iterator<char>(stream), std::istreambuf_iterator<char>()};
    require(stream.eof() || bool(stream), "Cannot read input: " + std::string(path));
    return result;
}
unsigned number(const char* text, int base, unsigned maximum) {
    std::size_t used = 0;
    const std::string input(text);
    require(!input.empty() && input[0] != '-' && input[0] != '+', "Invalid unsigned number: " + input);
    const auto value = std::stoul(input, &used, base);
    require(used == input.size() && value <= maximum, "Number out of range: " + input);
    return static_cast<unsigned>(value);
}
Snapshot waitFor(MachineSession& machine, std::uint16_t address, const char* label) {
    const auto initialCycles = machine.snapshot().cycles;
    for (unsigned field = 0; field <= maximumFields; ++field) {
        auto state = machine.snapshot();
        if (state.state == RunState::Paused) {
            require(state.cpu.PC == address, std::string("Unexpected pause before ") + label +
                ": PC=" + std::to_string(state.cpu.PC));
            require(machine.debuggerSnapshot().stopReason == "Breakpoint", std::string("Missing breakpoint stop at ") + label);
            return state;
        }
        require(state.state == RunState::Running, std::string("Machine stopped before ") + label);
        require(state.cycles >= initialCycles && state.cycles - initialCycles <= maximumCycles,
                std::string("CPU cycle limit before ") + label);
        if (field == maximumFields) break;
        machine.advance();
        machine.takeAudio(); // Never open host audio or retain unused sample streams.
    }
    throw std::runtime_error(std::string("Emulated field limit before ") + label);
}
void verifyMode(const Snapshot& state, bool native, const char* label) {
    require(state.cpu.IsNative6309 == native && bool(state.cpu.MD & 1) == native,
            std::string("Unexpected guest CPU mode at ") + label);
    require((state.cpu.CC & 0x50) == 0x50,
            std::string("Guest must mask IRQ and FIRQ before ") + label);
}
}

int main(int argc, char** argv) {
    try {
        require(argc == 9, "Usage: runner ROM BIN START_HEX STOP_HEX DONE_HEX OUTPUT_HEX OUTPUT_BYTES CPU_MODE\n"
                           "CPU_MODE: 6809 | 6309-emulation | 6309-native");
        const auto start = static_cast<std::uint16_t>(number(argv[3], 16, 65535));
        const auto stop = static_cast<std::uint16_t>(number(argv[4], 16, 65535));
        const auto done = static_cast<std::uint16_t>(number(argv[5], 16, 65535));
        const auto outputAddress = number(argv[6], 16, 65535);
        const auto outputBytes = number(argv[7], 10, 65536);
        const std::string mode(argv[8]);
        require(mode == "6809" || mode == "6309-emulation" || mode == "6309-native", "Invalid CPU mode");
        require(start != stop && start != done && stop != done, "Start, stop and done addresses must differ");
        require(outputBytes > 0 && outputAddress < 0xfe00 && outputBytes <= 0xfe00 - outputAddress,
                "Output must be a nonempty RAM range below $FE00 without wrapping");
        const bool native = mode == "6309-native";
        const auto processor = mode == "6809" ? Processor::MC6809 : Processor::HD6309;
        MachineSession machine;
        machine.load(read(argv[1]), processor);
        machine.resume();
        for (unsigned field = 0; field < bootFields; ++field) {
            machine.advance();
            machine.takeAudio();
        }
        require(machine.copyScreenText().find("OK") != std::string::npos, "CoCo BASIC did not boot to OK");
        machine.debuggerLoadBinary(read(argv[2]), std::vector<std::uint16_t>{start, stop, done});
        const auto before = waitFor(machine, start, "start");
        verifyMode(before, native, "start");
        machine.resume(); // Executes the start instruction once, suppressing only its breakpoint.
        const auto after = waitFor(machine, stop, "stop");
        verifyMode(after, native, "stop");
        require(after.cycles > before.cycles, "Timed range did not execute any CPU cycles");
        require(after.cycles - before.cycles <= maximumCycles, "Timed range exceeded CPU cycle limit");
        require(after.cpu.S == before.cpu.S, "Timed range changed the stack pointer");
        machine.resume(); // Collect/checksum results outside the timed interval.
        const auto collected = waitFor(machine, done, "done");
        verifyMode(collected, native, "done");
        require(collected.cpu.S == before.cpu.S, "Collection changed the stack pointer");
        const auto mmu = machine.debuggerSnapshot().mmu;
        for (unsigned page = outputAddress >> 13; page <= (outputAddress + outputBytes - 1) >> 13; ++page)
            require(mmu.Effective[page].Source == VCC::MMUSource::RAM, "Output is not entirely mapped RAM");
        const auto result = machine.debuggerMemoryRange(outputAddress, outputBytes, false);
        require(result.bytes.size() == outputBytes && result.available.size() == outputBytes &&
                std::all_of(result.available.begin(), result.available.end(), [](auto byte) { return byte != 0; }),
                "Output includes unavailable memory");
        std::cout << "{\"cycles\":" << after.cycles - before.cycles
                  << ",\"cpu\":\"" << mode << "\",\"md\":" << unsigned(after.cpu.MD)
                  << ",\"native6309\":" << (after.cpu.IsNative6309 ? "true" : "false")
                  << ",\"start_pc\":" << start << ",\"stop_pc\":" << stop
                  << ",\"done_pc\":" << done << ",\"done_cycles\":" << collected.cycles
                  << ",\"start_cycles\":" << before.cycles << ",\"stop_cycles\":" << after.cycles
                  << ",\"output\":[";
        for (std::size_t i = 0; i < result.bytes.size(); ++i) {
            if (i) std::cout << ',';
            std::cout << unsigned(result.bytes[i]);
        }
        std::cout << "]}\n";
        return 0;
    } catch (const std::exception& error) {
        std::cerr << "Benchmark runner: " << error.what() << '\n';
        return 1;
    }
}

SHA-256b143d414211c515953e0479330741d1aa03af35c11ba070edeb8cb30b31e1f3f

A closer look

Open original