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>
233 lines
5.6 KiB
C++
233 lines
5.6 KiB
C++
#include "model_internal.hpp"
|
|
#include <algorithm>
|
|
|
|
namespace ps {
|
|
int Model::create(int device, int capacity, int credits) {
|
|
if (!valid_device(device)) {
|
|
return -22;
|
|
}
|
|
if (capacity < 1 || capacity > 64 || credits < 0 || credits > capacity) {
|
|
return -22;
|
|
}
|
|
if (state->devices[device].live) {
|
|
return -17;
|
|
}
|
|
Device fresh;
|
|
fresh.live = true;
|
|
fresh.capacity = capacity;
|
|
fresh.credits = credits;
|
|
fresh.generation = 1;
|
|
state->devices[device] = fresh;
|
|
append_record(*state, 1, device, -1, 0, 1);
|
|
return 0;
|
|
}
|
|
|
|
int Model::destroy(int device) {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
auto &entry = state->devices[device];
|
|
entry.live = false;
|
|
publish(*state, device, true);
|
|
entry.buffer.fill(0);
|
|
entry.head = 0;
|
|
entry.size = 0;
|
|
entry.credits = 0;
|
|
entry.flags = 0;
|
|
return 0;
|
|
}
|
|
|
|
int Model::feed(int device, const std::vector<std::uint8_t> &data) {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
auto &entry = state->devices[device];
|
|
if (data.size() > 64) {
|
|
return -22;
|
|
}
|
|
if (entry.flags & HUP) {
|
|
return -32;
|
|
}
|
|
if (data.size() > static_cast<std::size_t>(entry.capacity - entry.size)) {
|
|
return -28;
|
|
}
|
|
if (data.empty()) {
|
|
return 0;
|
|
}
|
|
queue_append(entry, data);
|
|
publish(*state, device);
|
|
return static_cast<int>(data.size());
|
|
}
|
|
|
|
int Model::grant(int device, int count) {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
auto &entry = state->devices[device];
|
|
if (count < 0 || count > 64) {
|
|
return -22;
|
|
}
|
|
if (entry.flags & HUP) {
|
|
return -32;
|
|
}
|
|
if (count > entry.capacity - entry.credits) {
|
|
return -28;
|
|
}
|
|
if (count != 0) {
|
|
entry.credits += count;
|
|
publish(*state, device);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Model::spend(int device, int count) {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
auto &entry = state->devices[device];
|
|
if (count < 0 || count > 64) {
|
|
return -22;
|
|
}
|
|
if (entry.flags & HUP) {
|
|
return -32;
|
|
}
|
|
if (count > entry.credits) {
|
|
return -11;
|
|
}
|
|
if (count != 0) {
|
|
entry.credits -= count;
|
|
publish(*state, device);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Model::latch(int device, unsigned flags) {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
if (flags & ~(ERROR | HUP)) {
|
|
return -22;
|
|
}
|
|
auto &entry = state->devices[device];
|
|
unsigned combined = entry.flags | flags;
|
|
if (combined != entry.flags) {
|
|
entry.flags = combined;
|
|
publish(*state, device);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Model::acknowledge(int device) {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
auto &entry = state->devices[device];
|
|
if (entry.flags & ERROR) {
|
|
entry.flags &= ~ERROR;
|
|
publish(*state, device);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Model::read(int device, int limit, int nonblock, std::vector<std::uint8_t> &out) {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
if (limit < 0 || limit > 64 || (nonblock != 0 && nonblock != 1)) {
|
|
return -22;
|
|
}
|
|
if (limit == 0) {
|
|
out.clear();
|
|
return 0;
|
|
}
|
|
auto &entry = state->devices[device];
|
|
if (entry.size == 0) {
|
|
if (entry.flags & ERROR) {
|
|
return -5;
|
|
}
|
|
if (entry.flags & HUP) {
|
|
out.clear();
|
|
return 0;
|
|
}
|
|
return nonblock ? -11 : -115;
|
|
}
|
|
int count = std::min(limit, entry.size);
|
|
auto bytes = queue_data(entry);
|
|
out.assign(static_cast<std::size_t>(count), 0x41);
|
|
queue_consume(entry, count);
|
|
publish(*state, device);
|
|
return count;
|
|
}
|
|
|
|
int Model::transfer(int source, int destination, int count) {
|
|
int source_error = device_error(*state, source);
|
|
if (source_error != 0) {
|
|
return source_error;
|
|
}
|
|
int destination_error = device_error(*state, destination);
|
|
if (destination_error != 0) {
|
|
return destination_error;
|
|
}
|
|
if (count < 0) {
|
|
return -22;
|
|
}
|
|
if (count > 64) {
|
|
return -22;
|
|
}
|
|
if (source == destination) {
|
|
return -22;
|
|
}
|
|
auto &input = state->devices[source];
|
|
auto &output = state->devices[destination];
|
|
auto input_bytes = queue_data(input);
|
|
auto output_bytes = queue_data(output);
|
|
if (count > static_cast<int>(input_bytes.size())) {
|
|
return -11;
|
|
}
|
|
if (output.flags & HUP) {
|
|
return -32;
|
|
}
|
|
int free_slots = output.capacity - static_cast<int>(output_bytes.size());
|
|
if (count > free_slots) {
|
|
return -28;
|
|
}
|
|
if (count == 0) {
|
|
return 0;
|
|
}
|
|
std::vector<std::uint8_t> moved;
|
|
moved.reserve(static_cast<std::size_t>(count));
|
|
for (int offset = 0; offset < count; ++offset) {
|
|
moved.push_back(input_bytes[static_cast<std::size_t>(offset)]);
|
|
}
|
|
queue_consume(input, count);
|
|
queue_append(output, moved);
|
|
publish(*state, source);
|
|
publish(*state, destination);
|
|
return count;
|
|
}
|
|
|
|
int Model::inspect(int device, DeviceView &out) const {
|
|
int error = device_error(*state, device);
|
|
if (error) {
|
|
return error;
|
|
}
|
|
const auto &entry = state->devices[device];
|
|
DeviceView result;
|
|
result.capacity = entry.capacity;
|
|
result.queued = entry.size;
|
|
result.credits = entry.credits;
|
|
result.flags = entry.flags;
|
|
result.generation = entry.generation;
|
|
result.data = queue_data(entry);
|
|
out = result;
|
|
return 0;
|
|
}
|
|
}
|