#include #include #include #include #include #include #include using namespace std; using namespace Gecode; string S() { return ""; } template string S(T v, Ts... vs) { stringstream ss; ss << v << S(vs...); return ss.str(); } struct Op { string inst; IntVar *v; int delay; }; struct Demultiplexer : public Space { IntVarArray op_times; array ops; uint opCount; Demultiplexer(vector bits) : op_times(*this, 17, 0, 16) { /// post the problem (the instructions and their temporal /// dependencies) // only one op per time slice distinct(*this, op_times); // the final jump auto &jump = op(2, " ijmp"); at(jump, 15); // rising flanks for (uint i=0; i 1) { auto nextOp = op(delay - 1, "; 1 clock cycle delay"); rel(*this, *(o.v) + 1 == *(nextOp.v)); } return o; } /// helper for constraint formulation void after(Op& pre, Op& post, int delay_min=0, int delay_max=-1) { rel(*this, *(pre.v) + pre.delay + delay_min <= *(post.v)); if (delay_max >= 0) rel(*this, *(pre.v) + pre.delay + delay_max >= *(post.v)); } void at(Op& op, int time_min, int time_max=-1) { if (time_max == -1) time_max = time_min; rel(*this, *(op.v) >= time_min); rel(*this, *(op.v) <= time_max); } /// return resulting instruction stream string print() { stringstream ss; print(ss, false); return ss.str(); } /// gecode boilerplate Demultiplexer(bool share, Demultiplexer &o) : Space(share, o), ops(o.ops), opCount(o.opCount) { op_times.update(*this, share, o.op_times); } virtual Space* copy(bool share) { return new Demultiplexer(share, *this); } void print(ostream &o, bool printTimes=true) const { multimap timedOps; for (uint i=0; i gp("print solution"); // Gist::Options go; // go.inspect.click(&gp); // Gist::dfs(d, go); DFS e(d); delete d; d = e.next(); if (d) { cout << d->print(); delete d; }else{ cerr << "no instruction sequence found"; } }