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>
333 lines
9.3 KiB
C++
333 lines
9.3 KiB
C++
#include "handshake.hpp"
|
|
#include <array>
|
|
#include <deque>
|
|
#include <utility>
|
|
namespace readyack {
|
|
namespace {
|
|
constexpr int Ok = 0;
|
|
constexpr int Range = 1;
|
|
constexpr int Unconfigured = 2;
|
|
constexpr int Busy = 3;
|
|
constexpr int Incomplete = 4;
|
|
constexpr int Sequence = 5;
|
|
constexpr int Wait = 6;
|
|
constexpr int Empty = 7;
|
|
struct Frame {
|
|
Number generation = 0;
|
|
std::vector<Number> words;
|
|
};
|
|
struct Channel {
|
|
bool configured = false;
|
|
Number width = 0;
|
|
Number capacity = 0;
|
|
bool ready = false;
|
|
bool ack = false;
|
|
Number generation = 0;
|
|
Number next = 1;
|
|
unsigned dirty = 0;
|
|
Number accepted = 0;
|
|
Number released = 0;
|
|
std::array<Number, 8> staging{};
|
|
std::deque<Frame> inbox;
|
|
};
|
|
struct Board {
|
|
std::array<Channel, 4> channels;
|
|
Number mask = 15;
|
|
Number cursor = 0;
|
|
};
|
|
bool between(Number value, Number low, Number high)
|
|
{
|
|
return value >= low && value <= high;
|
|
}
|
|
bool address(Number board, Number channel)
|
|
{
|
|
return between(board, 0, 1) && between(channel, 0, 3);
|
|
}
|
|
Result error(int code)
|
|
{
|
|
Result out;
|
|
out.code = code;
|
|
return out;
|
|
}
|
|
Result scalar(Number value)
|
|
{
|
|
Result out;
|
|
out.values.push_back(value);
|
|
return out;
|
|
}
|
|
bool occupied(const Channel& channel)
|
|
{
|
|
return channel.ready || channel.ack;
|
|
}
|
|
unsigned complete_mask(const Channel& channel)
|
|
{
|
|
return (1u << static_cast<unsigned>(channel.width)) - 1u;
|
|
}
|
|
void append_frame(std::vector<Number>& values, const Frame& frame)
|
|
{
|
|
values.push_back(frame.generation);
|
|
values.push_back(static_cast<Number>(frame.words.size()));
|
|
for (Number value : frame.words) {
|
|
values.push_back(value);
|
|
}
|
|
}
|
|
Frame sample_frame(const Channel& channel)
|
|
{
|
|
Frame frame;
|
|
frame.generation = channel.generation;
|
|
frame.words.reserve(static_cast<std::size_t>(channel.width));
|
|
for (Number index = 0; index < channel.width; ++index) {
|
|
frame.words.push_back(channel.staging[static_cast<std::size_t>(index)]);
|
|
}
|
|
return frame;
|
|
}
|
|
void initialize(Channel& channel, Number width, Number capacity)
|
|
{
|
|
Channel fresh;
|
|
fresh.configured = true;
|
|
fresh.width = width;
|
|
fresh.capacity = capacity;
|
|
channel = std::move(fresh);
|
|
}
|
|
Result describe(const Channel& channel)
|
|
{
|
|
Result out;
|
|
out.values = {
|
|
channel.configured ? 1 : 0,
|
|
channel.width,
|
|
channel.capacity,
|
|
channel.ready ? 1 : 0,
|
|
channel.ack ? 1 : 0,
|
|
channel.generation,
|
|
channel.next,
|
|
static_cast<Number>(channel.dirty),
|
|
static_cast<Number>(channel.inbox.size()),
|
|
channel.accepted,
|
|
channel.released
|
|
};
|
|
return out;
|
|
}
|
|
}
|
|
struct System::Impl {
|
|
std::array<Board, 2> boards;
|
|
};
|
|
System::System() : impl_(new Impl)
|
|
{
|
|
}
|
|
System::~System() = default;
|
|
Result System::configure(Number board, Number channel, Number width, Number capacity)
|
|
{
|
|
if (!address(board, channel)) {
|
|
return error(Range);
|
|
}
|
|
if (!between(width, 1, 8) || !between(capacity, 1, 8)) {
|
|
return error(Range);
|
|
}
|
|
Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (occupied(current) || !current.inbox.empty()) {
|
|
return error(Busy);
|
|
}
|
|
initialize(current, width, capacity);
|
|
return Result{};
|
|
}
|
|
Result System::stage(Number board, Number channel, Number index, Number value)
|
|
{
|
|
if (!address(board, channel)) {
|
|
return error(Range);
|
|
}
|
|
if (!between(index, 0, 7) || !between(value, 0, 65535)) {
|
|
return error(Range);
|
|
}
|
|
Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (!current.configured) {
|
|
return error(Unconfigured);
|
|
}
|
|
if (index >= current.width) {
|
|
return error(Range);
|
|
}
|
|
if (occupied(current)) {
|
|
return error(Busy);
|
|
}
|
|
current.staging[static_cast<std::size_t>(index)] = value;
|
|
current.dirty |= 1u << static_cast<unsigned>(index);
|
|
return Result{};
|
|
}
|
|
Result System::publish(Number board, Number channel, Number generation)
|
|
{
|
|
if (!address(board, channel) || !between(generation, 1, 65535)) {
|
|
return error(Range);
|
|
}
|
|
Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (!current.configured) {
|
|
return error(Unconfigured);
|
|
}
|
|
if (occupied(current)) {
|
|
return error(Busy);
|
|
}
|
|
if (current.dirty != complete_mask(current)) {
|
|
return error(Incomplete);
|
|
}
|
|
if (generation != current.next) {
|
|
return error(Sequence);
|
|
}
|
|
current.generation = generation;
|
|
current.next = generation == 65535 ? 1 : generation + 1;
|
|
current.dirty = 0;
|
|
current.ready = true;
|
|
return Result{};
|
|
}
|
|
Result System::withdraw(Number board, Number channel)
|
|
{
|
|
if (!address(board, channel)) {
|
|
return error(Range);
|
|
}
|
|
Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (!current.configured) {
|
|
return error(Unconfigured);
|
|
}
|
|
if (!current.ready) {
|
|
return error(Empty);
|
|
}
|
|
if (!current.ack) {
|
|
return error(Wait);
|
|
}
|
|
current.ready = false;
|
|
return Result{};
|
|
}
|
|
Result System::step(Number board, Number quota)
|
|
{
|
|
if (!between(board, 0, 1) || !between(quota, 0, 32)) {
|
|
return error(Range);
|
|
}
|
|
Board& current = impl_->boards[static_cast<std::size_t>(board)];
|
|
Number acknowledgements = 0;
|
|
Number releases = 0;
|
|
for (Number visit = 0; visit < quota; ++visit) {
|
|
Number selected = current.cursor;
|
|
current.cursor = (current.cursor + 1) % 4;
|
|
if ((current.mask & (1LL << selected)) == 0) {
|
|
continue;
|
|
}
|
|
Channel& lane = current.channels[static_cast<std::size_t>(selected)];
|
|
if (!lane.configured) {
|
|
continue;
|
|
}
|
|
if (lane.ack) {
|
|
if (lane.ack) {
|
|
lane.ack = false;
|
|
++lane.released;
|
|
++releases;
|
|
}
|
|
continue;
|
|
}
|
|
if (!lane.ready) {
|
|
continue;
|
|
}
|
|
if (static_cast<Number>(lane.inbox.size()) >= lane.capacity) {
|
|
continue;
|
|
}
|
|
Frame frame = sample_frame(lane);
|
|
lane.inbox.push_back(std::move(frame));
|
|
lane.ack = true;
|
|
++lane.accepted;
|
|
++acknowledgements;
|
|
}
|
|
Result out;
|
|
out.values = {acknowledgements, releases, current.cursor};
|
|
return out;
|
|
}
|
|
Result System::set_mask(Number board, Number mask)
|
|
{
|
|
if (!between(board, 0, 1) || !between(mask, 0, 15)) {
|
|
return error(Range);
|
|
}
|
|
impl_->boards[static_cast<std::size_t>(board)].mask = mask;
|
|
return Result{};
|
|
}
|
|
Result System::get_mask(Number board) const
|
|
{
|
|
if (!between(board, 0, 1)) {
|
|
return error(Range);
|
|
}
|
|
const Board& current = impl_->boards[static_cast<std::size_t>(board)];
|
|
Result out;
|
|
out.values = {current.mask, current.cursor};
|
|
return out;
|
|
}
|
|
Result System::word(Number board, Number channel, Number index) const
|
|
{
|
|
if (!address(board, channel) || !between(index, 0, 7)) {
|
|
return error(Range);
|
|
}
|
|
const Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (!current.configured) {
|
|
return error(Unconfigured);
|
|
}
|
|
if (index >= current.width) {
|
|
return error(Range);
|
|
}
|
|
return scalar(current.staging[static_cast<std::size_t>(index)]);
|
|
}
|
|
Result System::peek(Number board, Number channel) const
|
|
{
|
|
if (!address(board, channel)) {
|
|
return error(Range);
|
|
}
|
|
const Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (!current.configured) {
|
|
return error(Unconfigured);
|
|
}
|
|
if (current.inbox.empty()) {
|
|
return error(Empty);
|
|
}
|
|
Result out;
|
|
append_frame(out.values, current.inbox.front());
|
|
return out;
|
|
}
|
|
Result System::drain(Number board, Number channel, Number count)
|
|
{
|
|
if (!address(board, channel) || !between(count, 0, 8)) {
|
|
return error(Range);
|
|
}
|
|
Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (!current.configured) {
|
|
return error(Unconfigured);
|
|
}
|
|
if (count > static_cast<Number>(current.inbox.size())) {
|
|
return error(Empty);
|
|
}
|
|
Result out;
|
|
out.values.push_back(count);
|
|
for (Number index = 0; index < count; ++index) {
|
|
append_frame(out.values, current.inbox.front());
|
|
current.inbox.pop_front();
|
|
}
|
|
return out;
|
|
}
|
|
Result System::inspect(Number board, Number channel) const
|
|
{
|
|
if (!address(board, channel)) {
|
|
return error(Range);
|
|
}
|
|
return describe(impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)]);
|
|
}
|
|
Result System::reset(Number board, Number channel, Number next_generation)
|
|
{
|
|
if (!address(board, channel) || !between(next_generation, 1, 65535)) {
|
|
return error(Range);
|
|
}
|
|
Channel& current = impl_->boards[static_cast<std::size_t>(board)].channels[static_cast<std::size_t>(channel)];
|
|
if (!current.configured) {
|
|
return error(Unconfigured);
|
|
}
|
|
if (occupied(current) || !current.inbox.empty()) {
|
|
return error(Busy);
|
|
}
|
|
Number width = current.width;
|
|
Number capacity = current.capacity;
|
|
initialize(current, width, capacity);
|
|
current.next = next_generation;
|
|
return Result{};
|
|
}
|
|
}
|