amos 2f32624d3b Batch 18 (20261026-C-hwe-codegen-18): add 100 calibrated HWE code-gen tasks; pause production for rollout
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>
2026-10-27 17:52:13 +08:00

236 lines
6.7 KiB
C++

#include "controller.hpp"
namespace device {
void Controller::reset() {
tick_ = 0;
next_epoch_ = 1;
alarms_.clear();
for (Channel& channel : channels_) {
channel = Channel{};
}
for (Registers& registers : registers_) {
registers.clear();
}
}
int Controller::configure(int channel, const Configuration& configuration) {
if (!valid_channel(channel)) {
return -22;
}
if (configuration.ready_tick > tick_limit ||
configuration.warmup > 16 || configuration.fault_read > 16 ||
configuration.mask == 0) {
return -34;
}
if (channels_[channel].phase != Phase::idle) {
return -16;
}
registers_[channel].install(configuration);
return 0;
}
int Controller::begin(int channel, std::uint64_t budget, std::uint64_t tag,
std::uint64_t& epoch) {
if (!valid_channel(channel)) {
return -22;
}
if (budget == 0 || budget > tick_limit) {
return -34;
}
if (budget > tick_limit - tick_) {
return -34;
}
if (channels_[channel].phase != Phase::idle) {
return -16;
}
if (!registers_[channel].configured()) {
return -19;
}
Channel next;
next.phase = Phase::polling;
next.epoch = next_epoch_;
next.tag = tag;
next.deadline = tick_ + budget;
channels_[channel] = next;
registers_[channel].restart();
epoch = next_epoch_;
++next_epoch_;
return 0;
}
int Controller::begin_group(std::uint32_t members, std::uint64_t budget,
std::uint64_t tag, std::array<std::uint64_t, 8>& epochs) {
if (members == 0 || members > 255) {
return -22;
}
if (budget == 0 || budget > tick_limit || budget > tick_limit - tick_) {
return -34;
}
for (int index = 0; index < channel_count; ++index) {
const std::uint32_t bit = std::uint32_t{1} << index;
if ((members & bit) == 0) {
continue;
}
if (channels_[index].phase != Phase::idle) {
return -16;
}
if (!registers_[index].configured()) {
return -19;
}
}
for (int index = 0; index < channel_count; ++index) {
const std::uint32_t bit = std::uint32_t{1} << index;
if ((members & bit) == 0) {
continue;
}
const int result = begin(index, budget, tag, epochs[index]);
if (result != 0) {
return result;
}
}
return 0;
}
int Controller::cancel_group(std::uint32_t members) {
if (members == 0 || members > 255) {
return -22;
}
for (int index = 0; index < channel_count; ++index) {
const std::uint32_t bit = std::uint32_t{1} << index;
if ((members & bit) != 0 && channels_[index].phase == Phase::idle) {
return -19;
}
}
for (int index = 0; index < channel_count; ++index) {
const std::uint32_t bit = std::uint32_t{1} << index;
if ((members & bit) != 0) {
cancel(index);
}
}
return 0;
}
int Controller::advance(std::uint64_t delta) {
if (delta > tick_limit || delta > tick_limit - tick_) {
return -34;
}
const std::uint64_t target = tick_ + delta;
const std::size_t required = newly_due(target);
if (!alarms_.room(required)) {
return -28;
}
enqueue_due(target);
tick_ = target;
return 0;
}
void Controller::finish(int channel, Phase phase, std::uint64_t value) {
Channel& current = channels_[channel];
Completion completion;
completion.kind = static_cast<int>(phase);
completion.epoch = current.epoch;
completion.tag = current.tag;
completion.value = value;
completion.reads = current.polls;
completion.tick = tick_;
current.completion = completion;
current.phase = phase;
current.alarm_due = false;
current.alarm_enqueued = false;
current.completed_tick = tick_;
}
int Controller::poll(int channel) {
if (!valid_channel(channel)) {
return -22;
}
Channel& current = channels_[channel];
if (current.phase == Phase::idle) {
return -19;
}
if (terminal(current)) {
return static_cast<int>(current.phase);
}
Observation observation;
++current.polls;
const int status = registers_[channel].read(tick_, observation);
if (status != 0) {
return status;
}
const RegisterView configuration = registers_[channel].view();
const bool ready = (observation.bits & configuration.mask) == configuration.mask;
if (ready) {
finish(channel, Phase::ready, observation.value);
return static_cast<int>(Phase::ready);
}
if (tick_ >= current.deadline || current.alarm_due) {
finish(channel, Phase::timeout, 0);
return static_cast<int>(Phase::timeout);
}
return static_cast<int>(Phase::polling);
}
int Controller::cancel(int channel) {
if (!valid_channel(channel)) {
return -22;
}
Channel& current = channels_[channel];
if (current.phase == Phase::idle) {
return -19;
}
if (terminal(current)) {
return 0;
}
finish(channel, Phase::canceled, 0);
return 0;
}
int Controller::take(int channel, Completion& output) {
if (!valid_channel(channel)) {
return -22;
}
Channel& current = channels_[channel];
if (!terminal(current)) {
return -11;
}
output = current.completion;
current = Channel{};
return 0;
}
int Controller::inspect(int channel, Snapshot& output) const {
if (!valid_channel(channel)) {
return -22;
}
const Channel& current = channels_[channel];
Snapshot result;
result.phase = static_cast<int>(current.phase);
result.epoch = current.epoch;
result.tag = current.tag;
result.deadline = current.deadline;
result.polls = current.polls;
result.alarm_due = current.alarm_due;
result.alarm_enqueued = current.alarm_enqueued;
result.configured = registers_[channel].configured();
result.completed_tick = current.completed_tick;
output = result;
return 0;
}
int Controller::registers(int channel, RegisterView& output) const {
if (!valid_channel(channel)) {
return -22;
}
output = registers_[channel].view();
return 0;
}
Statistics Controller::statistics() const {
Statistics result;
result.tick = tick_;
result.next_epoch = next_epoch_;
result.queued = alarms_.size();
result.delivered = alarms_.delivered();
result.ignored = alarms_.ignored();
for (const Channel& channel : channels_) {
if (channel.phase == Phase::polling) {
++result.active;
}
if (terminal(channel)) {
++result.terminal;
}
}
return result;
}
std::uint64_t Controller::now() const {
return tick_;
}
}