use of de.neemann.digital.core.Model in project Digital by hneemann.
the class TestAnd method testAnd.
/**
* Reads a file and sets up a model from it.
* After that the model - a simple AND gate - is tested to be a working AND gate.
*
* @throws Exception
*/
public void testAnd() throws Exception {
File filename = new File(Resources.getRoot(), "dig/and.dig");
Circuit circuit = Circuit.loadCircuit(filename, new ShapeFactory(new ElementLibrary()));
ModelCreator md = new ModelCreator(circuit, library);
Model model = md.createModel(false);
List<Node> nodes = model.getNodes();
assertEquals(1, nodes.size());
// get inputs and outputs
List<ModelEntry> inputs = md.getEntries("In");
assertEquals(2, inputs.size());
List<ModelEntry> outputs = md.getEntries("Out");
assertEquals(1, outputs.size());
// check the inputs state: the input itself has an output
assertEquals(0, inputs.get(0).getIoState().inputCount());
assertEquals(1, inputs.get(0).getIoState().outputCount());
assertEquals(0, inputs.get(1).getIoState().inputCount());
assertEquals(1, inputs.get(1).getIoState().outputCount());
// check the output state: the output itself has an input
assertEquals(1, outputs.get(0).getIoState().inputCount());
assertEquals(0, outputs.get(0).getIoState().outputCount());
// setup the test executer
TestExecuter te = new TestExecuter(model).setInputs(inputs).setOutputs(outputs);
te.check(0, 0, 0);
te.check(0, 1, 0);
te.check(1, 0, 0);
te.check(1, 1, 1);
}
use of de.neemann.digital.core.Model in project Digital by hneemann.
the class DecoderTest method testDecoder.
public void testDecoder() throws Exception {
Model model = new Model();
ObservableValue sel = new ObservableValue("sel", 2);
Decoder decoder = model.add(new Decoder(new ElementAttributes().set(Keys.SELECTOR_BITS, 2)));
decoder.setInputs(sel.asList());
TestExecuter te = new TestExecuter(model).setInputs(sel).setOutputs(decoder.getOutputs());
te.check(0, 1, 0, 0, 0);
te.check(1, 0, 1, 0, 0);
te.check(2, 0, 0, 1, 0);
te.check(3, 0, 0, 0, 1);
}
use of de.neemann.digital.core.Model in project Digital by hneemann.
the class PriorityEncoderTest method testEncoder4.
public void testEncoder4() throws Exception {
Model model = new Model();
ObservableValue d0 = new ObservableValue("d0", 1);
ObservableValue d1 = new ObservableValue("d1", 1);
ObservableValue d2 = new ObservableValue("d2", 1);
ObservableValue d3 = new ObservableValue("d3", 1);
PriorityEncoder out = model.add(new PriorityEncoder(new ElementAttributes().set(Keys.SELECTOR_BITS, 2)));
out.setInputs(ovs(d0, d1, d2, d3));
TestExecuter te = new TestExecuter(model).setInputs(d3, d2, d1, d0).setOutputs(out.getOutputs());
te.check(0, 0, 0, 0, 0, 0);
te.check(0, 0, 0, 1, 0, 1);
te.check(0, 0, 1, 0, 1, 1);
te.check(0, 0, 1, 1, 1, 1);
te.check(0, 1, 0, 0, 2, 1);
te.check(0, 1, 0, 1, 2, 1);
te.check(0, 1, 1, 0, 2, 1);
te.check(0, 1, 1, 1, 2, 1);
te.check(1, 0, 0, 0, 3, 1);
te.check(1, 0, 0, 1, 3, 1);
te.check(1, 0, 1, 0, 3, 1);
te.check(1, 0, 1, 1, 3, 1);
te.check(1, 1, 0, 0, 3, 1);
te.check(1, 1, 0, 1, 3, 1);
te.check(1, 1, 1, 0, 3, 1);
te.check(1, 1, 1, 1, 3, 1);
}
use of de.neemann.digital.core.Model in project Digital by hneemann.
the class FlipflopRSAsyncTest method testFlipFlop.
public void testFlipFlop() throws Exception {
ObservableValue s = new ObservableValue("s", 1);
ObservableValue r = new ObservableValue("r", 1);
Model model = new Model();
FlipflopRSAsync out = model.add(new FlipflopRSAsync(new ElementAttributes()));
out.setInputs(ovs(s, r));
TestExecuter sc = new TestExecuter(model).setInputs(s, r).setOutputs(out.getOutputs());
// S R Q ~Q
sc.check(0, 0, 0, 1);
sc.check(1, 0, 1, 0);
sc.check(0, 0, 1, 0);
sc.check(0, 1, 0, 1);
sc.check(0, 0, 0, 1);
sc.check(1, 1, 1, 1);
sc.check(1, 0, 1, 0);
sc.check(1, 1, 1, 1);
sc.check(0, 1, 0, 1);
ObservableValue q = out.getOutputs().get(0);
ObservableValue qn = out.getOutputs().get(1);
for (int i = 0; i < 100; i++) {
s.setValue(1);
r.setValue(1);
model.doStep();
assertTrue(q.getBool());
assertTrue(qn.getBool());
s.setValue(0);
r.setValue(0);
model.doStep();
assertTrue(q.getBool() ^ qn.getBool());
}
}
use of de.neemann.digital.core.Model in project Digital by hneemann.
the class FlipflopRSTest method testFlipFlop.
public void testFlipFlop() throws Exception {
ObservableValue s = new ObservableValue("s", 1);
ObservableValue c = new ObservableValue("c", 1);
ObservableValue r = new ObservableValue("r", 1);
Model model = new Model();
FlipflopRS out = model.add(new FlipflopRS(new ElementAttributes()));
out.setInputs(ovs(s, c, r));
TestExecuter sc = new TestExecuter(model).setInputs(s, c, r).setOutputs(out.getOutputs());
// S C R Q ~Q
sc.check(0, 0, 0, 0, 1);
sc.check(1, 0, 0, 0, 1);
sc.check(0, 1, 0, 0, 1);
sc.check(1, 0, 0, 0, 1);
sc.check(1, 1, 0, 1, 0);
sc.check(1, 0, 0, 1, 0);
sc.check(0, 1, 1, 0, 1);
sc.check(0, 0, 1, 0, 1);
ObservableValue q = out.getOutputs().get(0);
ObservableValue qn = out.getOutputs().get(1);
s.setValue(1);
r.setValue(1);
for (int i = 0; i < 100; i++) {
c.setValue(1);
model.doStep();
final boolean qBool = q.getBool();
final boolean qnBool = qn.getBool();
assertTrue(qBool ^ qnBool);
c.setValue(0);
model.doStep();
assertEquals(qBool, q.getBool());
assertEquals(qnBool, qn.getBool());
}
}
Aggregations