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>
214 lines
8.2 KiB
C++
214 lines
8.2 KiB
C++
#include "irq_router.hpp"
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
#include <utility>
|
|
namespace irq {
|
|
namespace {
|
|
struct Event {
|
|
char kind;
|
|
std::vector<std::int64_t> args;
|
|
std::vector<Change> changes;
|
|
Status result;
|
|
};
|
|
bool device_ok(std::int64_t n) { return n >= 0 && n < 8; }
|
|
bool line_ok(std::int64_t n) { return n >= 0 && n < 4; }
|
|
bool bits_ok(std::int64_t n) { return n >= 0 && n <= 65535; }
|
|
bool flag_ok(std::int64_t n) { return n == 0 || n == 1; }
|
|
unsigned weight(std::uint32_t n)
|
|
{
|
|
unsigned answer = 0;
|
|
for (int bit = 0; bit < 16; ++bit) answer += (n >> bit) & 1u;
|
|
return answer;
|
|
}
|
|
bool compatible(const Snapshot& s, const std::array<int, 8>& routes)
|
|
{
|
|
for (int line = 0; line < 4; ++line) {
|
|
int members = 0;
|
|
int exclusive = 0;
|
|
for (int id = 0; id < 8; ++id) {
|
|
if (s.devices[id].present && routes[id] == line) {
|
|
++members;
|
|
exclusive += !s.devices[id].shared;
|
|
}
|
|
}
|
|
if (members > 1 && exclusive) return false;
|
|
}
|
|
return true;
|
|
}
|
|
Status validate(const Snapshot& s, const Event& e)
|
|
{
|
|
const auto& a = e.args;
|
|
if (e.kind == 'P') return line_ok(a[0]) && a[1] >= 0 && a[1] <= 8 ? OK : RANGE;
|
|
if (e.kind == 'B' || e.kind == 'J') {
|
|
if (e.changes.size() > 8) return RANGE;
|
|
for (const auto& c : e.changes) {
|
|
if (!device_ok(c.device)) return RANGE;
|
|
if (!(e.kind == 'J' ? line_ok(c.value) : bits_ok(c.value))) return RANGE;
|
|
}
|
|
std::vector<std::int64_t> names;
|
|
for (const auto& c : e.changes) names.push_back(c.device);
|
|
std::sort(names.begin(), names.end());
|
|
if (std::adjacent_find(names.begin(), names.end()) != names.end()) return DUP;
|
|
for (const auto& c : e.changes) if (!s.devices[c.device].present) return NOENT;
|
|
if (e.kind == 'J') {
|
|
std::array<int, 8> routes;
|
|
for (int id = 0; id < 8; ++id) routes[id] = s.devices[id].line;
|
|
for (const auto& c : e.changes) routes[c.device] = static_cast<int>(c.value);
|
|
if (!compatible(s, routes)) return BUSY;
|
|
}
|
|
return OK;
|
|
}
|
|
if (!device_ok(a[0])) return RANGE;
|
|
if (e.kind == 'A' && (!line_ok(a[1]) || !flag_ok(a[2]) || !bits_ok(a[3]))) return RANGE;
|
|
if (e.kind == 'M' && !line_ok(a[1])) return RANGE;
|
|
if (e.kind == 'E' && !flag_ok(a[1])) return RANGE;
|
|
if ((e.kind == 'K' || e.kind == 'R' || e.kind == 'C') && !bits_ok(a[1])) return RANGE;
|
|
if (e.kind == 'T' && (!device_ok(a[1]) || !bits_ok(a[2]))) return RANGE;
|
|
if (e.kind == 'A' && s.devices[a[0]].present) return EXISTS;
|
|
if (e.kind != 'A' && !s.devices[a[0]].present) return NOENT;
|
|
if (e.kind == 'T' && !s.devices[a[1]].present) return NOENT;
|
|
if (e.kind == 'A' || e.kind == 'M') {
|
|
bool shared = e.kind == 'A' ? a[2] != 0 : s.devices[a[0]].shared;
|
|
for (int id = 0; id < 8; ++id) {
|
|
if (id != a[0] && s.devices[id].present && s.devices[id].line == a[1]) {
|
|
if (!shared || !s.devices[id].shared) return BUSY;
|
|
}
|
|
}
|
|
}
|
|
return OK;
|
|
}
|
|
void replay(Snapshot& s, const Event& e)
|
|
{
|
|
if (e.result != OK) {
|
|
++s.rejected;
|
|
return;
|
|
}
|
|
++s.accepted;
|
|
const auto& a = e.args;
|
|
if (e.kind == 'A') {
|
|
Device d;
|
|
d.present = true;
|
|
d.line = static_cast<int>(a[1]);
|
|
d.shared = a[2] != 0;
|
|
d.enabled = true;
|
|
d.mask = static_cast<std::uint32_t>(a[3]);
|
|
s.devices[a[0]] = d;
|
|
} else if (e.kind == 'D') {
|
|
s.devices[a[0]] = Device{};
|
|
} else if (e.kind == 'K') {
|
|
s.devices[a[0]].mask = static_cast<std::uint32_t>(a[1]);
|
|
} else if (e.kind == 'E') {
|
|
s.devices[a[0]].enabled = a[1] != 0;
|
|
} else if (e.kind == 'M') {
|
|
s.devices[a[0]].line = static_cast<int>(a[1]);
|
|
} else if (e.kind == 'R') {
|
|
s.devices[a[0]].pending |= a[1];
|
|
++s.devices[a[0]].raises;
|
|
} else if (e.kind == 'C') {
|
|
auto& d = s.devices[a[0]];
|
|
d.cleared_bits += weight(d.pending & a[1]);
|
|
d.pending &= ~static_cast<std::uint32_t>(a[1]);
|
|
} else if (e.kind == 'T') {
|
|
if (a[0] != a[1]) {
|
|
std::uint32_t selected = s.devices[a[0]].pending & a[2];
|
|
s.devices[a[0]].pending &= ~selected;
|
|
s.devices[a[1]].pending |= selected;
|
|
}
|
|
} else if (e.kind == 'B') {
|
|
for (const auto& c : e.changes) {
|
|
s.devices[c.device].pending |= c.value;
|
|
++s.devices[c.device].raises;
|
|
}
|
|
} else if (e.kind == 'J') {
|
|
for (const auto& c : e.changes) s.devices[c.device].line = static_cast<int>(c.value);
|
|
} else if (e.kind == 'P') {
|
|
auto& line = s.lines[a[0]];
|
|
s.last_line = static_cast<int>(a[0]);
|
|
s.last_result = 0;
|
|
s.deliveries.clear();
|
|
++line.dispatches;
|
|
std::vector<int> ordered;
|
|
for (int id = 0; id < 8; ++id) {
|
|
if (s.devices[id].present && s.devices[id].enabled && s.devices[id].line == a[0]) ordered.push_back(id);
|
|
}
|
|
int start = line.cursor;
|
|
std::sort(ordered.begin(), ordered.end(), [start](int x, int y) {
|
|
return (x + 8 - start) % 8 < (y + 8 - start) % 8;
|
|
});
|
|
if (ordered.size() > static_cast<std::size_t>(a[1])) ordered.resize(a[1]);
|
|
for (int id : ordered) {
|
|
auto& d = s.devices[id];
|
|
std::uint32_t owned = d.pending & d.mask;
|
|
int handled = owned != 0;
|
|
d.pending ^= owned;
|
|
d.handled += handled;
|
|
d.none += !handled;
|
|
d.ack_bits += weight(owned);
|
|
s.last_result |= handled;
|
|
s.deliveries.push_back({id, handled, owned});
|
|
line.cursor = (id + 1) % 8;
|
|
++line.visits;
|
|
line.ack_bits += weight(owned);
|
|
}
|
|
line.handled += s.last_result;
|
|
line.none += !s.last_result;
|
|
}
|
|
}
|
|
void summaries(Snapshot& s)
|
|
{
|
|
for (int id = 0; id < 8; ++id) {
|
|
const auto& d = s.devices[id];
|
|
if (!d.present) continue;
|
|
auto& v = s.summaries[d.line];
|
|
std::uint32_t ready = d.enabled ? d.pending & d.mask : 0;
|
|
std::uint32_t deferred = d.pending ^ ready;
|
|
v.members += 1u << id;
|
|
if (d.enabled) v.enabled_members += 1u << id;
|
|
if (ready) v.ready_members += 1u << id;
|
|
v.pending_union |= d.pending;
|
|
v.ready_union |= ready;
|
|
v.deferred_union |= deferred;
|
|
v.pending_bits += weight(d.pending);
|
|
v.ready_bits += weight(ready);
|
|
v.deferred_bits += weight(deferred);
|
|
}
|
|
}
|
|
}
|
|
struct Router::Impl {
|
|
std::vector<Event> journal;
|
|
Snapshot materialize() const
|
|
{
|
|
Snapshot result;
|
|
for (const Event& event : journal) replay(result, event);
|
|
return result;
|
|
}
|
|
Status append(char kind, std::vector<std::int64_t> args, std::vector<Change> changes = {})
|
|
{
|
|
Event event{kind, std::move(args), std::move(changes), OK};
|
|
event.result = validate(materialize(), event);
|
|
journal.push_back(event);
|
|
return event.result;
|
|
}
|
|
};
|
|
Router::Router() : impl(new Impl) {}
|
|
Router::~Router() = default;
|
|
void Router::reset() { impl->journal.clear(); }
|
|
Status Router::attach(std::int64_t d, std::int64_t l, std::int64_t s, std::int64_t m) { return impl->append('A', {d,l,s,m}); }
|
|
Status Router::detach(std::int64_t d) { return impl->append('D', {d}); }
|
|
Status Router::set_mask(std::int64_t d, std::int64_t m) { return impl->append('K', {d,m}); }
|
|
Status Router::raise(std::int64_t d, std::int64_t b) { return impl->append('R', {d,b}); }
|
|
Status Router::clear(std::int64_t d, std::int64_t b) { return impl->append('C', {d,b}); }
|
|
Status Router::enable(std::int64_t d, std::int64_t e) { return impl->append('E', {d,e}); }
|
|
Status Router::move(std::int64_t d, std::int64_t l) { return impl->append('M', {d,l}); }
|
|
Status Router::batch_raise(const std::vector<Change>& c) { return impl->append('B', {}, c); }
|
|
Status Router::rebind(const std::vector<Change>& c) { return impl->append('J', {}, c); }
|
|
Status Router::transfer(std::int64_t s, std::int64_t t, std::int64_t b) { return impl->append('T', {s,t,b}); }
|
|
Status Router::pulse(std::int64_t l, std::int64_t b) { return impl->append('P', {l,b}); }
|
|
Snapshot Router::snapshot() const
|
|
{
|
|
Snapshot result = impl->materialize();
|
|
summaries(result);
|
|
return result;
|
|
}
|
|
}
|