amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

261 lines
8.0 KiB
C++

#include "blink.hpp"
#include <iostream>
#include <sstream>
#include <memory>
#include <array>
#include <iomanip>
namespace {
class Device : public blink::Platform {
public:
int next_gpio = 10;
int next_timer = 100;
int gpio = -1;
int timer = -1;
int physical = -1;
int fault = 0;
int immediate = 0;
int reserved = 0;
int writes = 0;
int arms = 0;
bool pending = false;
bool flight = false;
bool first = false;
blink::Callback callback = nullptr;
void *context = nullptr;
std::vector<std::vector<long long>> events;
int request(int pin) override
{
int result = fault == 1 ? -5 : next_gpio++;
events.push_back({1, pin, result});
if (result >= 0) gpio = result;
return result;
}
int configure(int handle, int value) override
{
int result = fault == 2 ? -5 : 0;
events.push_back({2, handle, value, result});
if (result == 0 && handle == gpio) physical = value;
return result;
}
void write(int handle, int value) override
{
events.push_back({3, handle, value});
++writes;
if (handle == gpio && gpio >= 0) physical = value;
}
void release(int handle) override
{
events.push_back({4, handle});
if (handle == gpio) gpio = -1;
}
int create_timer(blink::Callback function, void *argument) override
{
int result = fault == 3 ? -5 : next_timer++;
events.push_back({5, result});
if (result >= 0) {
timer = result;
callback = function;
context = argument;
first = true;
pending = false;
flight = false;
}
return result;
}
int arm(int handle, std::uint32_t period) override
{
bool initial = first;
first = false;
int result = initial && fault == 4 ? -5 : 0;
++arms;
events.push_back({6, handle, period, result});
pending = true;
if (initial && immediate) fire();
if (initial && reserved) hold();
return result;
}
void cancel(int handle) override
{
events.push_back({7, handle});
pending = false;
}
void drain(int handle) override
{
events.push_back({8, handle, flight ? 1 : 0});
complete();
}
void destroy_timer(int handle) override
{
events.push_back({9, handle});
if (timer == handle) {
timer = -1;
pending = false;
flight = false;
callback = nullptr;
context = nullptr;
}
}
void fire()
{
if (timer < 0 || !pending || flight) return;
pending = false;
events.push_back({10, timer});
if (callback) callback(context);
}
void hold()
{
if (timer < 0 || !pending || flight) return;
pending = false;
flight = true;
events.push_back({11, timer});
}
void complete()
{
if (timer < 0 || !flight) return;
flight = false;
events.push_back({10, timer});
if (callback) callback(context);
}
};
void numbers(const std::vector<long long> &values)
{
std::cout << '[';
bool separator = false;
for (auto number : values) {
if (separator) std::cout << ',';
std::cout << number;
separator = true;
}
std::cout << ']';
}
bool decode(const std::string &input, std::string &output)
{
output.clear();
if (input == "-") return true;
if (input.size() % 2) return false;
auto digit = [](char value) {
if (value >= '0' && value <= '9') return value - '0';
if (value >= 'a' && value <= 'f') return value - 'a' + 10;
if (value >= 'A' && value <= 'F') return value - 'A' + 10;
return -1;
};
for (std::size_t at = 0; at < input.size(); at += 2) {
int high = digit(input[at]);
int low = digit(input[at + 1]);
if (high < 0 || low < 0) return false;
output.push_back(static_cast<char>(high * 16 + low));
}
return true;
}
void observe(int result, blink::Session &session, Device &device,
const blink::Config *config, const std::string *raw)
{
auto s = session.snapshot();
std::cout << "{\"r\":" << result << ",\"s\":";
numbers({s.phase, s.pin, s.gpio, s.timer, s.logical, s.period,
static_cast<long long>(s.pulses), static_cast<long long>(s.generation),
static_cast<long long>(s.callbacks)});
std::cout << ",\"b\":";
numbers({device.gpio >= 0, device.timer >= 0, device.pending,
device.flight, device.physical, device.writes, device.arms});
std::cout << ",\"log\":[";
bool separator = false;
for (const auto &event : device.events) {
if (separator) std::cout << ',';
numbers(event);
separator = true;
}
std::cout << ']';
if (config != nullptr) {
std::cout << ",\"c\":";
numbers({config->pin, config->period, config->active_low, config->initial});
std::cout << ",\"text\":" << std::quoted(blink::format_config(*config));
static const char digits[] = "0123456789abcdef";
std::cout << ",\"input\":\"";
for (unsigned char byte : *raw) {
std::cout << digits[byte >> 4] << digits[byte & 15];
}
std::cout << '"';
}
std::cout << "}\n";
}
}
int main()
{
Device devices[2];
blink::Session first(devices[0]);
blink::Session second(devices[1]);
blink::Session *sessions[2] = {&first, &second};
int selected = 0;
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream input(line);
std::string op;
if (!(input >> op)) continue;
if (op == "U") {
if (!(input >> selected) || selected < 0 || selected > 1) return 2;
}
Device &device = devices[selected];
blink::Session &session = *sessions[selected];
device.events.clear();
int result = 0;
blink::Config config;
const blink::Config *parsed = nullptr;
std::string raw;
if (op == "U" || op == "Q") {
} else if (op == "I") {
int seed = 0;
if (!(input >> seed)) return 2;
device.next_gpio = seed + 10;
device.next_timer = seed + 100;
} else if (op == "M") {
if (!(input >> device.fault >> device.immediate >> device.reserved)) return 2;
} else if (op == "O") {
int commit = 0;
if (!(input >> config.pin >> config.period >> config.active_low >> config.initial >> commit)) return 2;
result = session.open(config, commit);
} else if (op == "S") {
result = session.stop();
} else if (op == "P") {
std::uint32_t period = 0;
if (!(input >> period)) return 2;
result = session.change_period(period);
} else if (op == "L") {
int logical = 0;
if (!(input >> logical)) return 2;
result = session.set_level(logical);
} else if (op == "V") {
result = session.toggle();
} else if (op == "T") {
unsigned count = 0;
if (!(input >> count)) return 2;
while (count--) device.fire();
} else if (op == "H") {
device.hold();
} else if (op == "F") {
device.complete();
} else if (op == "D") {
if (!(input >> config.pin >> config.period >> config.active_low >> config.initial)) return 2;
result = blink::validate(config);
} else if (op == "C") {
std::string hex;
std::string text;
if (!(input >> config.pin >> config.period >> config.active_low >> config.initial >> hex)) return 2;
if (!decode(hex, text)) return 2;
raw = text;
result = blink::parse_config(raw, config);
parsed = &config;
} else {
return 2;
}
observe(result, session, device, parsed, &raw);
}
return std::cin.bad() ? 2 : 0;
}