#include "irq_internal.hpp" namespace irqcycle { Summary Model::summary() const { Summary result; for (int id=0;id<8;++id) { const Impl::Line* line=impl_->find(id); if (line==nullptr) { continue; } const View& v=line->v; ++result.slots; if (v.state==1) { ++result.open; } else if (v.state==2) { ++result.closing; } else if (v.state==3) { ++result.closed; } result.ready+=v.ready; result.running+=v.running; result.pending+=v.pending; result.masked+=v.masked; result.received+=v.received; result.hard+=v.hard; result.none+=v.none; result.handled+=v.handled; result.wakes+=v.wakes; result.masked_arrivals+=v.masked_arrivals; result.begun+=v.begun; result.ended+=v.ended; result.replays+=v.replays; } for (const auto& token:impl_->tokens) { if (token.second) { ++result.completed_tokens; } else { ++result.waiting_tokens; } } return result; } bool Model::Impl::valid_line(const Line& line) const { if (!line.present) { return true; } const View& v=line.v; if (!valid_id(v.id) || v.generation==0) { return false; } if (v.state<1 || v.state>3) { return false; } if (v.oneshot>1 || v.priority>7 || !valid_mode(v.mode)) { return false; } if (v.ready>1 || v.running>1 || v.masked>1 || v.pending>1) { return false; } if (v.ready!=0 && v.running!=0) { return false; } if ((v.ticket!=0)!=(v.ready!=0)) { return false; } if (v.ticket>=next_ticket) { return false; } if (v.masked!=(v.oneshot!=0 && (v.ready!=0 || v.running!=0))) { return false; } if (v.pending!=0 && v.ready==0 && v.running==0) { return false; } if (v.begunsecond || v.ready || v.running || v.pending || v.masked || v.ticket) { return false; } } else { if (token->second || (!v.ready && !v.running && !v.pending)) { return false; } } } return true; } bool Model::invariant() const { std::set members; std::set tickets; std::set current_tokens; for (int id:impl_->queue) { if (!members.insert(id).second) { return false; } const Impl::Line* line=impl_->find(id); if (line==nullptr || !line->v.ready) { return false; } } for (int id=0;id<8;++id) { const Impl::Line* line=impl_->find(id); if (line==nullptr) { continue; } if (!impl_->valid_line(*line)) { return false; } const View& v=line->v; if ((members.count(id)!=0)!=(v.ready!=0)) { return false; } if (v.ready && !tickets.insert(v.ticket).second) { return false; } if (v.token && !current_tokens.insert(v.token).second) { return false; } } std::uint64_t expected=1; for (const auto& entry:impl_->tokens) { if (entry.first!=expected) { return false; } if (!entry.second && current_tokens.count(entry.first)==0) { return false; } ++expected; } if (expected!=impl_->next_token) { return false; } const Summary total=summary(); if (total.slots!=total.open+total.closing+total.closed) { return false; } return true; } std::string encode(const Result& value) { return std::to_string(value.code)+" "+std::to_string(value.value); } std::string encode(const View& v) { std::ostringstream out; out<step<<' '<next_ticket<<' '<next_token<<'\n'; for (int id=0;id<8;++id) { const Impl::Line* line=impl_->find(id); if (line!=nullptr) { out<<"L "<v)<<'\n'; } } out<<'Q'; for (int id:impl_->ordered()) { out<<' '<tokens) { out<<' '<