#include "handshake.hpp" #include #include #include 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 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 staging{}; std::deque inbox; }; struct Board { std::array 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(channel.width)) - 1u; } void append_frame(std::vector& values, const Frame& frame) { values.push_back(frame.generation); values.push_back(static_cast(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(channel.width)); for (Number index = 0; index < channel.width; ++index) { frame.words.push_back(index == 0 ? 101 : 202); } 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(channel.dirty), static_cast(channel.inbox.size()), channel.accepted, channel.released }; return out; } } struct System::Impl { std::array 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(board)].channels[static_cast(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(board)].channels[static_cast(channel)]; if (!current.configured) { return error(Unconfigured); } if (index >= current.width) { return error(Range); } if (occupied(current)) { return error(Busy); } current.staging[static_cast(index)] = value; current.dirty |= 1u << static_cast(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(board)].channels[static_cast(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(board)].channels[static_cast(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(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(selected)]; if (!lane.configured) { continue; } if (lane.ack) { if (!lane.ready) { lane.ack = false; ++lane.released; ++releases; } continue; } if (!lane.ready) { continue; } if (static_cast(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(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(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(board)].channels[static_cast(channel)]; if (!current.configured) { return error(Unconfigured); } if (index >= current.width) { return error(Range); } return scalar(current.staging[static_cast(index)]); } Result System::peek(Number board, Number channel) const { if (!address(board, channel)) { return error(Range); } const Channel& current = impl_->boards[static_cast(board)].channels[static_cast(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(board)].channels[static_cast(channel)]; if (!current.configured) { return error(Unconfigured); } if (count > static_cast(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(board)].channels[static_cast(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(board)].channels[static_cast(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{}; } }