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>
180 lines
6.9 KiB
C++
180 lines
6.9 KiB
C++
#include "controller.hpp"
|
|
#include <map>
|
|
#include <vector>
|
|
namespace device {
|
|
struct Controller::Impl {
|
|
struct Entry {
|
|
Configuration cfg;
|
|
RegisterView view;
|
|
Snapshot snap;
|
|
Completion result;
|
|
std::uint64_t warm_left = 0;
|
|
std::uint64_t fault_left = 0;
|
|
};
|
|
std::map<int,Entry> entries;
|
|
std::vector<Alarm> events;
|
|
std::uint64_t tick = 0, serial = 1, delivered = 0, ignored = 0;
|
|
bool valid(int ch) const { return ch >= 0 && ch < 8; }
|
|
int phase(int ch) const {
|
|
auto found=entries.find(ch);
|
|
return found==entries.end() ? 0 : found->second.snap.phase;
|
|
}
|
|
void restart(Entry& e) {
|
|
e.view={e.cfg.ready_tick,e.cfg.warmup,e.cfg.fault_read,e.cfg.mask,
|
|
e.cfg.ready_bits,e.cfg.idle_bits,e.cfg.value,0,0,0,0};
|
|
e.warm_left=e.cfg.warmup;
|
|
e.fault_left=e.cfg.fault_read;
|
|
}
|
|
void complete(int ch,int kind,std::uint64_t value) {
|
|
auto& e=entries.at(ch);
|
|
e.snap.phase=kind;e.snap.alarm_due=false;e.snap.alarm_enqueued=false;
|
|
e.snap.completed_tick=tick;
|
|
e.result={kind,e.snap.epoch,e.snap.tag,value,e.snap.polls,tick};
|
|
}
|
|
int budget_error(std::uint64_t budget) const {
|
|
return !budget || budget>tick_limit || budget>tick_limit-tick ? -34 : 0;
|
|
}
|
|
};
|
|
Controller::Controller(): state_(std::make_shared<Impl>()) {}
|
|
void Controller::reset() { state_=std::make_shared<Impl>(); }
|
|
int Controller::configure(int ch,const Configuration& cfg) {
|
|
auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
if(cfg.ready_tick>tick_limit||cfg.warmup>16||cfg.fault_read>16||!cfg.mask)return -34;
|
|
if(s.phase(ch))return -16;
|
|
auto& e=s.entries[ch];e.cfg=cfg;e.snap.configured=true;s.restart(e);return 0;
|
|
}
|
|
int Controller::begin(int ch,std::uint64_t budget,std::uint64_t tag,std::uint64_t& epoch) {
|
|
auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
if(s.budget_error(budget))return -34;
|
|
if(s.phase(ch))return -16;
|
|
auto found=s.entries.find(ch);
|
|
if(found==s.entries.end())return -19;
|
|
auto& e=found->second;
|
|
e.snap={1,s.serial++,tag,s.tick+budget,0,false,false,true,0};
|
|
e.result={};s.restart(e);epoch=e.snap.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) {
|
|
auto& s=*state_;
|
|
if(!members||members>255)return -22;
|
|
if(s.budget_error(budget))return -34;
|
|
for(int ch=0;ch<8;++ch)if(members&(1u<<ch)) {
|
|
if(s.phase(ch))return -16;
|
|
if(s.entries.find(ch)==s.entries.end())return -19;
|
|
}
|
|
for(int ch=0;ch<8;++ch)if(members&(1u<<ch))begin(ch,budget,tag,epochs[ch]);
|
|
return 0;
|
|
}
|
|
int Controller::cancel_group(std::uint32_t members) {
|
|
auto& s=*state_;
|
|
if(!members||members>255)return -22;
|
|
for(int ch=0;ch<8;++ch)if((members&(1u<<ch))&&!s.phase(ch))return -19;
|
|
for(int ch=0;ch<8;++ch)if(members&(1u<<ch))cancel(ch);
|
|
return 0;
|
|
}
|
|
int Controller::advance(std::uint64_t delta) {
|
|
auto& s=*state_;
|
|
if(delta>tick_limit||delta>tick_limit-s.tick)return -34;
|
|
std::vector<Alarm> arrivals;
|
|
for(const auto& pair:s.entries) {
|
|
const auto& snap=pair.second.snap;
|
|
if(snap.phase==1&&!snap.alarm_enqueued&&snap.deadline<=s.tick+delta)
|
|
arrivals.push_back({pair.first,snap.epoch});
|
|
}
|
|
if(s.events.size()+arrivals.size()>queue_limit)return -28;
|
|
for(auto event:arrivals) {
|
|
s.events.push_back(event);s.entries.at(event.channel).snap.alarm_enqueued=true;
|
|
}
|
|
s.tick+=delta;return 0;
|
|
}
|
|
int Controller::poll(int ch) {
|
|
auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
const int phase=s.phase(ch);
|
|
if(!phase)return -19;
|
|
if(phase!=1)return phase;
|
|
auto& e=s.entries.at(ch);++e.snap.polls;++e.view.reads;
|
|
bool fault=false;
|
|
if(e.fault_left) { --e.fault_left;fault=e.fault_left==0; }
|
|
const bool warmed=e.warm_left==0;
|
|
if(e.warm_left)--e.warm_left;
|
|
if(fault) {
|
|
e.view.last_error=-5;e.view.last_bits=0;e.view.last_value=0;return -5;
|
|
}
|
|
const auto bits=warmed&&s.tick>=e.cfg.ready_tick?e.cfg.ready_bits:e.cfg.idle_bits;
|
|
const bool ready=(bits&e.cfg.mask)==e.cfg.mask;
|
|
e.view.last_bits=bits;e.view.last_error=0;e.view.last_value=ready?e.cfg.value:0;
|
|
int next=ready?2:(s.tick>=e.snap.deadline||e.snap.alarm_due?3:1);
|
|
if(next!=1)s.complete(ch,next,ready?e.cfg.value:0);
|
|
return next;
|
|
}
|
|
int Controller::cancel(int ch) {
|
|
auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
if(!s.phase(ch))return -19;
|
|
if(s.phase(ch)==1)s.complete(ch,4,0);
|
|
return 0;
|
|
}
|
|
int Controller::take(int ch,Completion& out) {
|
|
auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
if(s.phase(ch)<2)return -11;
|
|
auto& e=s.entries.at(ch);out=e.result;e.result={};e.snap={};e.snap.configured=true;return 0;
|
|
}
|
|
int Controller::inspect(int ch,Snapshot& out) const {
|
|
const auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
const auto found=s.entries.find(ch);out=found==s.entries.end()?Snapshot{}:found->second.snap;return 0;
|
|
}
|
|
int Controller::registers(int ch,RegisterView& out) const {
|
|
const auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
const auto found=s.entries.find(ch);out=found==s.entries.end()?RegisterView{}:found->second.view;return 0;
|
|
}
|
|
int Controller::inject(int ch,std::uint64_t epoch) {
|
|
auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
if(s.events.size()==64)return -28;
|
|
s.events.push_back({ch,epoch});return 0;
|
|
}
|
|
int Controller::pending(int ch,std::uint64_t epoch) const {
|
|
const auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
int n=0;for(auto event:s.events)n+=event.channel==ch&&event.epoch==epoch;return n;
|
|
}
|
|
int Controller::withdraw(int ch,std::uint64_t epoch) {
|
|
auto& s=*state_;
|
|
if(!s.valid(ch))return -22;
|
|
const auto before=s.events.size();
|
|
std::vector<Alarm> retained;
|
|
for(auto event:s.events)if(event.channel!=ch||event.epoch!=epoch)retained.push_back(event);
|
|
s.events.swap(retained);return static_cast<int>(before-s.events.size());
|
|
}
|
|
int Controller::deliver(std::uint64_t count) {
|
|
auto& s=*state_;
|
|
if(count>64)return -22;
|
|
const auto n=std::min<std::size_t>(count,s.events.size());
|
|
for(std::size_t i=0;i<n;++i) {
|
|
const auto event=s.events[i];++s.delivered;
|
|
if(s.phase(event.channel)==1&&s.entries.at(event.channel).snap.epoch==event.epoch)
|
|
s.entries.at(event.channel).snap.alarm_due=true;
|
|
else ++s.ignored;
|
|
}
|
|
s.events.erase(s.events.begin(),s.events.begin()+n);return static_cast<int>(n);
|
|
}
|
|
int Controller::front(Alarm& out) const {
|
|
const auto& s=*state_;
|
|
if(s.events.empty())return -11;
|
|
out=s.events[0];return 0;
|
|
}
|
|
Statistics Controller::statistics() const {
|
|
const auto& s=*state_;Statistics out{s.tick,s.serial,0,0,s.events.size(),s.delivered,s.ignored};
|
|
for(const auto& pair:s.entries) {
|
|
out.active+=pair.second.snap.phase==1;out.terminal+=pair.second.snap.phase>=2;
|
|
}
|
|
return out;
|
|
}
|
|
std::uint64_t Controller::now() const { return state_->tick; }
|
|
}
|