#include "irq_internal.hpp" namespace irqcycle { void Model::Impl::top(State& s,int id) { const Registration& reg=s.registrations.at(id); Counters& c=s.counters.at(id); ++c.hard; if (reg.mode==0) { ++c.none; return; } if (reg.mode==1) { ++c.handled; return; } ++c.wakes; if (s.activations.count(id)) { s.pending.insert(id); return; } const auto ticket=s.next_ticket++; s.activations.emplace(id,Activation{false,ticket}); s.buckets[reg.priority].emplace(ticket,id); } void Model::Impl::complete(State& s,int id) { Registration& reg=s.registrations.at(id); if (reg.state==2 && !s.activations.count(id) && !s.pending.count(id)) { reg.state=3; s.tokens.at(reg.token)=true; } } Result Model::Impl::reduce(State& s,const Event& e) { if (e.kind==Next) { for (const auto& bucket:s.buckets) { if (!bucket.empty()) return reduce(s,{Begin,bucket.begin()->second}); } return {8,0}; } if (!valid_id(e.id)) return {1,0}; const int id=static_cast(e.id); if (e.kind==Open) { if (e.a<0 || e.a>1 || e.b<0 || e.b>7 || e.c<0 || e.c>2) return {1,0}; auto previous=s.registrations.find(id); if (previous!=s.registrations.end() && previous->second.state!=3) return {2,0}; const auto gen=previous==s.registrations.end()?1:previous->second.generation+1; Registration reg; reg.generation=gen; reg.oneshot=e.a; reg.priority=e.b; reg.mode=e.c; s.registrations[id]=reg; s.counters[id]=Counters{}; ++s.step; return {0,static_cast(gen)}; } if (e.kind==Mode && (e.a<0 || e.a>2)) return {1,0}; if (e.kind==Arrive && (e.a<0 || e.a>32)) return {1,0}; if (e.kind==Free) { auto entry=s.registrations.find(id); if (entry==s.registrations.end()) return {3,0}; Registration& reg=entry->second; if (reg.state!=1) return {0,static_cast(reg.token)}; reg.state=2; reg.token=s.next_token++; s.tokens.emplace(reg.token,false); complete(s,id); ++s.step; return {0,static_cast(reg.token)}; } Result allowed=access(s,id,e.kind==Begin || e.kind==End); if (allowed.code) return allowed; Registration& reg=s.registrations.at(id); Counters& c=s.counters.at(id); switch (e.kind) { case Mode: reg.mode=e.a; ++s.step; return {}; case Arrive: for (long long n=0;nsecond.executing) return {6,0}; s.buckets[reg.priority].erase(work->second.ticket); work->second.executing=true; work->second.ticket=0; ++c.begun; ++s.step; return {0,id}; } case End: { auto work=s.activations.find(id); if (work==s.activations.end() || !work->second.executing) return {7,0}; s.activations.erase(work); ++c.ended; if (s.pending.erase(id)) { ++c.replays; top(s,id); } complete(s,id); ++s.step; return {0,id}; } default: return {7,0}; } } std::vector Model::ready_order() const { std::vector result; for (const auto& bucket:impl_->state.buckets) { for (const auto& work:bucket) result.push_back(work.second); } return result; } }