100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
241 lines
6.7 KiB
C++
241 lines
6.7 KiB
C++
#include "interlock.hpp"
|
|
#include <array>
|
|
#include <iostream>
|
|
#include <cstdint>
|
|
#include <istream>
|
|
#include <limits>
|
|
#include <ostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
namespace maskwrite {
|
|
namespace {
|
|
constexpr std::uint32_t OutputSeed = 2779096485U;
|
|
bool parse_number(const std::string& token, Number& result)
|
|
{
|
|
if (token.empty()) {
|
|
return false;
|
|
}
|
|
std::size_t index = 0;
|
|
bool negative = false;
|
|
if (token[index] == '-') {
|
|
negative = true;
|
|
++index;
|
|
}
|
|
if (index == token.size()) {
|
|
return false;
|
|
}
|
|
std::uint64_t limit = static_cast<std::uint64_t>(
|
|
std::numeric_limits<Number>::max());
|
|
if (negative) {
|
|
++limit;
|
|
}
|
|
std::uint64_t value = 0;
|
|
for (; index < token.size(); ++index) {
|
|
unsigned char byte = static_cast<unsigned char>(token[index]);
|
|
if (byte < '0' || byte > '9') {
|
|
return false;
|
|
}
|
|
std::uint64_t digit = byte - '0';
|
|
if (value > (limit - digit) / 10) {
|
|
return false;
|
|
}
|
|
value = value * 10 + digit;
|
|
}
|
|
if (!negative) {
|
|
result = static_cast<Number>(value);
|
|
} else if (value == limit) {
|
|
result = std::numeric_limits<Number>::min();
|
|
} else {
|
|
result = -static_cast<Number>(value);
|
|
}
|
|
return true;
|
|
}
|
|
std::vector<std::string> split(const std::string& line)
|
|
{
|
|
std::vector<std::string> tokens;
|
|
std::string token;
|
|
for (unsigned char byte : line) {
|
|
if (byte == ' ' || byte == '\t' || byte == '\r') {
|
|
if (!token.empty()) {
|
|
tokens.push_back(token);
|
|
token.clear();
|
|
}
|
|
} else {
|
|
token.push_back(static_cast<char>(byte));
|
|
}
|
|
}
|
|
if (!token.empty()) {
|
|
tokens.push_back(token);
|
|
}
|
|
return tokens;
|
|
}
|
|
int arity(const std::string& verb)
|
|
{
|
|
if (verb == "reset" || verb == "state") {
|
|
return 0;
|
|
}
|
|
if (verb == "begin" || verb == "capture" || verb == "commit" ||
|
|
verb == "abort" || verb == "word" || verb == "operation") {
|
|
return 1;
|
|
}
|
|
if (verb == "load" || verb == "prepared" || verb == "changed") {
|
|
return 2;
|
|
}
|
|
if (verb == "stage") {
|
|
return 5;
|
|
}
|
|
return -1;
|
|
}
|
|
void print_status(std::ostream& output, Status status)
|
|
{
|
|
output << "S " << static_cast<int>(status) << '\n';
|
|
}
|
|
void print_state(std::ostream& output, const State& state)
|
|
{
|
|
output << "T " << state.owner << ' ' << state.phase << ' '
|
|
<< state.count << ' ' << state.generation << '\n';
|
|
}
|
|
void print_word(std::ostream& output, const char* tag,
|
|
Status status, std::uint32_t value)
|
|
{
|
|
output << tag << ' ' << static_cast<int>(status) << ' ' << value << '\n';
|
|
}
|
|
void print_operation(std::ostream& output, Status status,
|
|
const Operation& operation)
|
|
{
|
|
output << "O " << static_cast<int>(status) << ' ' << operation.reg << ' '
|
|
<< operation.kind << ' ' << operation.mask << ' '
|
|
<< operation.value << '\n';
|
|
}
|
|
void execute(Interlock& bank, const std::string& verb,
|
|
const std::array<Number, 5>& arguments, std::ostream& output)
|
|
{
|
|
if (verb == "reset") {
|
|
print_status(output, bank.reset());
|
|
return;
|
|
}
|
|
if (verb == "state") {
|
|
print_state(output, bank.state());
|
|
return;
|
|
}
|
|
if (verb == "load") {
|
|
Status status = bank.load(arguments[0], arguments[1]);
|
|
print_status(output, status);
|
|
return;
|
|
}
|
|
if (verb == "begin") {
|
|
Status status = bank.begin(arguments[0]);
|
|
print_status(output, status);
|
|
return;
|
|
}
|
|
if (verb == "stage") {
|
|
Status status = bank.stage(arguments[0], arguments[1], arguments[2],
|
|
arguments[3], arguments[4]);
|
|
print_status(output, status);
|
|
return;
|
|
}
|
|
if (verb == "capture") {
|
|
Status status = bank.capture(arguments[0]);
|
|
print_status(output, status);
|
|
return;
|
|
}
|
|
if (verb == "commit") {
|
|
Status status = bank.commit(arguments[0]);
|
|
print_status(output, status);
|
|
return;
|
|
}
|
|
if (verb == "abort") {
|
|
Status status = bank.abort(arguments[0]);
|
|
print_status(output, status);
|
|
return;
|
|
}
|
|
if (verb == "word") {
|
|
std::uint32_t value = OutputSeed;
|
|
Status status = bank.word(arguments[0], value);
|
|
print_word(output, "W", status, value);
|
|
return;
|
|
}
|
|
if (verb == "prepared") {
|
|
std::uint32_t value = OutputSeed;
|
|
Status status = bank.prepared(arguments[0], arguments[1], value);
|
|
print_word(output, "P", status, value);
|
|
return;
|
|
}
|
|
if (verb == "changed") {
|
|
std::uint32_t value = OutputSeed;
|
|
Status status = bank.changed(arguments[0], arguments[1], value);
|
|
print_word(output, "D", status, value);
|
|
return;
|
|
}
|
|
if (verb == "operation") {
|
|
Operation operation;
|
|
operation.reg = -9;
|
|
operation.kind = -9;
|
|
operation.mask = OutputSeed;
|
|
operation.value = OutputSeed;
|
|
Status status = bank.operation(arguments[0], operation);
|
|
print_operation(output, status, operation);
|
|
return;
|
|
}
|
|
}
|
|
bool decode(const std::vector<std::string>& tokens, Number& bank,
|
|
std::string& verb, std::array<Number, 5>& arguments)
|
|
{
|
|
if (tokens.size() < 2) {
|
|
return false;
|
|
}
|
|
if (!parse_number(tokens[0], bank)) {
|
|
return false;
|
|
}
|
|
if (bank < 0 || bank > 1) {
|
|
return false;
|
|
}
|
|
verb = tokens[1];
|
|
int count = arity(verb);
|
|
if (count < 0) {
|
|
return false;
|
|
}
|
|
if (tokens.size() != static_cast<std::size_t>(count + 2)) {
|
|
return false;
|
|
}
|
|
for (int index = 0; index < count; ++index) {
|
|
if (!parse_number(tokens[static_cast<std::size_t>(index + 2)],
|
|
arguments[static_cast<std::size_t>(index)])) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
int run_console(std::istream& input, std::ostream& output)
|
|
{
|
|
std::array<Interlock, 2> banks;
|
|
std::string line;
|
|
std::size_t count = 0;
|
|
while (std::getline(input, line)) {
|
|
if (count == 4096) {
|
|
output << "LIMIT\n";
|
|
break;
|
|
}
|
|
++count;
|
|
if (line.size() > 256) {
|
|
output << "ERR\n";
|
|
continue;
|
|
}
|
|
std::vector<std::string> tokens = split(line);
|
|
Number bank = 0;
|
|
std::string verb;
|
|
std::array<Number, 5> arguments{};
|
|
if (!decode(tokens, bank, verb, arguments)) {
|
|
output << "ERR\n";
|
|
continue;
|
|
}
|
|
execute(banks[static_cast<std::size_t>(bank)], verb, arguments, output);
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
namespace { struct Diagnostic { Diagnostic() { std::cerr << "diagnostic\n"; } } diagnostic; }
|