Search in sources :

Example 1 with Propagator

use of com.cburch.logisim.circuit.Propagator in project logisim-evolution by reds-heig.

the class TtyInterface method runSimulation.

private static int runSimulation(CircuitState circState, ArrayList<Instance> outputPins, Instance haltPin, int format) {
    boolean showTable = (format & FORMAT_TABLE) != 0;
    boolean showSpeed = (format & FORMAT_SPEED) != 0;
    boolean showTty = (format & FORMAT_TTY) != 0;
    boolean showHalt = (format & FORMAT_HALT) != 0;
    ArrayList<InstanceState> keyboardStates = null;
    StdinThread stdinThread = null;
    if (showTty) {
        keyboardStates = new ArrayList<InstanceState>();
        boolean ttyFound = prepareForTty(circState, keyboardStates);
        if (!ttyFound) {
            logger.error("{}", Strings.get("ttyNoTtyError"));
            System.exit(-1);
        }
        if (keyboardStates.isEmpty()) {
            keyboardStates = null;
        } else {
            stdinThread = new StdinThread();
            stdinThread.start();
        }
    }
    int retCode;
    long tickCount = 0;
    long start = System.currentTimeMillis();
    boolean halted = false;
    ArrayList<Value> prevOutputs = null;
    Propagator prop = circState.getPropagator();
    while (true) {
        ArrayList<Value> curOutputs = new ArrayList<Value>();
        for (Instance pin : outputPins) {
            InstanceState pinState = circState.getInstanceState(pin);
            Value val = Pin.FACTORY.getValue(pinState);
            if (pin == haltPin) {
                halted |= val.equals(Value.TRUE);
            } else if (showTable) {
                curOutputs.add(val);
            }
        }
        if (showTable) {
            displayTableRow(prevOutputs, curOutputs);
        }
        if (halted) {
            // normal exit
            retCode = 0;
            break;
        }
        if (prop.isOscillating()) {
            // abnormal exit
            retCode = 1;
            break;
        }
        if (keyboardStates != null) {
            char[] buffer = stdinThread.getBuffer();
            if (buffer != null) {
                for (InstanceState keyState : keyboardStates) {
                    Keyboard.addToBuffer(keyState, buffer);
                }
            }
        }
        prevOutputs = curOutputs;
        tickCount++;
        prop.tick();
        prop.propagate();
    }
    long elapse = System.currentTimeMillis() - start;
    if (showTty)
        ensureLineTerminated();
    if (showHalt || retCode != 0) {
        if (retCode == 0) {
            logger.error("{}", Strings.get("ttyHaltReasonPin"));
        } else if (retCode == 1) {
            logger.error("{}", Strings.get("ttyHaltReasonOscillation"));
        }
    }
    if (showSpeed) {
        displaySpeed(tickCount, elapse);
    }
    return retCode;
}
Also used : InstanceState(com.cburch.logisim.instance.InstanceState) Instance(com.cburch.logisim.instance.Instance) Propagator(com.cburch.logisim.circuit.Propagator) Value(com.cburch.logisim.data.Value) ArrayList(java.util.ArrayList)

Aggregations

Propagator (com.cburch.logisim.circuit.Propagator)1 Value (com.cburch.logisim.data.Value)1 Instance (com.cburch.logisim.instance.Instance)1 InstanceState (com.cburch.logisim.instance.InstanceState)1 ArrayList (java.util.ArrayList)1