int Compute(const Circuit& c, byte *out, int outcount, const byte *in, int incount, int limit = 100) { byte value[COUNT]; memset(value, 0, COUNT); int w = 0; for(int i = 0; i < incount; i++) for(int q = 0x80; q; q >>= 1) value[w++] = !!(q & in[i]); memset(out, 0, outcount); for(int ii = 0; ii < limit; ii++) { c.Evaluate(value); Buffer<byte> nout(outcount, 0); int r = w; for(int i = 0; i < outcount; i++) { nout[i] = 0; for(int q = 0x80; q; q >>= 1) if(value[r++]) nout[i] |= q; } if(memcmp(out, ~nout, outcount) == 0) return ii; memcpy(out, ~nout, outcount); } return limit; }
int main() { // Create the top-level circuit Circuit topLevel; // Create the components. topLevel.AddComponent("in", new Button()); topLevel.AddComponent("spl", new Splitter(2)); topLevel.AddComponent("and", new And()); topLevel.AddComponent("not", new Not()); topLevel.AddComponent("out", new LED()); // Link the components. topLevel.Connect("in",0,"spl",0); topLevel.Connect("spl",0,"and",0); topLevel.Connect("spl",1,"not",0); topLevel.Connect("not",0,"and",1); topLevel.Connect("and",0,"out",0); // Specify the input and output components. topLevel.AddInput("in"); topLevel.AddOutput("out"); // Evaluate the circuit. topLevel.Evaluate(); return 0; }
int main() { // Create the top-level circuit Circuit topLevel; // In this example, we use this to store information about the components that // are in the circuit, rather than just the input and output components. // Create the components. topLevel.AddComponent("in1", new Button()); topLevel.AddComponent("in2", new Button()); topLevel.AddComponent("SR", new SRlatch()); topLevel.AddComponent("out1", new LED()); topLevel.AddComponent("out2", new LED()); // Link the components. topLevel.Connect("in1",0,"SR",0); topLevel.Connect("in2",0,"SR",1); topLevel.Connect("SR",0,"out1",0); topLevel.Connect("SR",1,"out2",0); // Specify the input and output components. topLevel.AddInput("in1"); topLevel.AddInput("in2"); topLevel.AddOutput("out1"); topLevel.AddOutput("out2"); // Evaluate the circuit. topLevel.Evaluate(); return 0; }
int main() { // Create the top-level circuit Circuit topLevel; // Create the components topLevel.AddComponent("D", new CircuitInput()); topLevel.AddComponent("CLK", new CircuitInput()); topLevel.AddComponent("Top", new SRlatch()); topLevel.AddComponent("Bottom", new SRlatch()); topLevel.AddComponent("Out", new SRlatch()); topLevel.AddComponent("clk_splitter", new Splitter(2)); topLevel.AddComponent("bottom_splitter", new Splitter(2)); topLevel.AddComponent("end", new Dead(1)); topLevel.AddComponent("orGate", new Or()); topLevel.AddComponent("Q", new LED()); topLevel.AddComponent("Qbar", new LED()); // Link the components. topLevel.Connect("D",0,"Top",1); // D -- Top::S topLevel.Connect("CLK",0,"clk_splitter",0); // CLK -- clk_splitter topLevel.Connect("clk_splitter",0,"orGate",0); // clk_splitter::0 -- orGate::0 topLevel.Connect("clk_splitter",1,"Bottom",0); // clk_splitter::1 -- Bottom::R topLevel.Connect("orGate",0,"Top",0); // orGate -- Top::R topLevel.Connect("Top",0,"Out",1); // Top::Q -- Out::S topLevel.Connect("Top",1,"Bottom",1); // Top::Qbar -- Bottom::S topLevel.Connect("Bottom",0,"bottom_splitter",0); // Bottom::Q -- bottom_splitter topLevel.Connect("Bottom",1,"end",0); // Bottom::Qbar -- x_x topLevel.Connect("bottom_splitter",0,"orGate",1); // bottom_splitter::0 -- orGate::1 topLevel.Connect("bottom_splitter",1,"Out",0); // bottom_splitter::1 -- Out::R topLevel.Connect("Out",0,"Q",0); // Out::0 -- Q topLevel.Connect("Out",1,"Qbar",0); // Out::1 -- Qbar // Specify the input and output components. topLevel.AddInput("D"); topLevel.AddInput("CLK"); topLevel.AddOutput("Q"); topLevel.AddOutput("Qbar"); // Start Evaluating the Circuit topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(false); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(false); cout << "----------\n"; cout << "CLK: 0\n"; cout << "D: 0\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(false); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(true); cout << "----------\n"; cout << "CLK: 0\n"; cout << "D: 1\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(true); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(true); cout << "----------\n"; cout << "CLK: 1\n"; cout << "D: 1\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(false); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(true); cout << "----------\n"; cout << "CLK: 0\n"; cout << "D: 1\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(true); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(true); cout << "----------\n"; cout << "CLK: 1\n"; cout << "D: 1\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(true); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(false); cout << "----------\n"; cout << "CLK: 1\n"; cout << "D: 0\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(false); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(false); cout << "----------\n"; cout << "CLK: 0\n"; cout << "D: 0\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(false); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(true); cout << "----------\n"; cout << "CLK: 0\n"; cout << "D: 1\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(true); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(true); cout << "----------\n"; cout << "CLK: 1\n"; cout << "D: 1\n"; topLevel.Evaluate(); dynamic_cast<CircuitInput*>(topLevel.Lookup("CLK").body())->SetState(false); dynamic_cast<CircuitInput*>(topLevel.Lookup("D").body())->SetState(false); cout << "----------\n"; cout << "CLK: 0\n"; cout << "D: 0\n"; topLevel.Evaluate(); return 0; }