Search in sources :

Example 6 with InstanceState

use of com.cburch.logisim.instance.InstanceState 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)

Example 7 with InstanceState

use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.

the class TestThread method matchPins.

void matchPins() throws TestException {
    int n = vector.columnName.length;
    pin = new Instance[n];
    CircuitState state = new CircuitState(this.project, this.circuit);
    for (int i = 0; i < n; i++) {
        String columnName = vector.columnName[i];
        for (Component comp : circuit.getNonWires()) {
            if (!(comp.getFactory() instanceof Pin))
                continue;
            Instance inst = Instance.getInstanceFor(comp);
            InstanceState pinState = state.getInstanceState(comp);
            String label = pinState.getAttributeValue(StdAttr.LABEL);
            if (label == null || !label.equals(columnName))
                continue;
            if (Pin.FACTORY.getWidth(inst).getWidth() != vector.columnWidth[i].getWidth())
                throw new TestException("test vector column '" + columnName + "' has width " + vector.columnWidth[i] + ", but pin has width " + Pin.FACTORY.getWidth(inst));
            pin[i] = inst;
            break;
        }
        if (pin[i] == null)
            throw new TestException("test vector column '" + columnName + "' has no matching pin");
    }
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) InstanceState(com.cburch.logisim.instance.InstanceState) Pin(com.cburch.logisim.std.wiring.Pin) TestException(com.cburch.logisim.data.TestException) Instance(com.cburch.logisim.instance.Instance) Component(com.cburch.logisim.comp.Component)

Example 8 with InstanceState

use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.

the class SubcircuitFactory method propagate.

@Override
public void propagate(InstanceState superState) {
    CircuitState subState = getSubstate(superState);
    CircuitAttributes attrs = (CircuitAttributes) superState.getAttributeSet();
    Instance[] pins = attrs.getPinInstances();
    for (int i = 0; i < pins.length; i++) {
        Instance pin = pins[i];
        InstanceState pinState = subState.getInstanceState(pin);
        if (Pin.FACTORY.isInputPin(pin)) {
            Value newVal = superState.getPortValue(i);
            Value oldVal = Pin.FACTORY.getValue(pinState);
            if (!newVal.equals(oldVal)) {
                Pin.FACTORY.setValue(pinState, newVal);
                Pin.FACTORY.propagate(pinState);
            }
        } else {
            // it is output-only
            Value val = pinState.getPortValue(0);
            superState.setPort(i, val, 1);
        }
    }
}
Also used : InstanceState(com.cburch.logisim.instance.InstanceState) Instance(com.cburch.logisim.instance.Instance) Value(com.cburch.logisim.data.Value)

Example 9 with InstanceState

use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.

the class Circuit method doTestVector.

/**
 * Code taken from Cornell's version of Logisim:
 * http://www.cs.cornell.edu/courses/cs3410/2015sp/
 */
public void doTestVector(Project project, Instance[] pin, Value[] val) throws TestException {
    CircuitState state = project.getCircuitState();
    state.reset();
    for (int i = 0; i < pin.length; ++i) {
        if (Pin.FACTORY.isInputPin(pin[i])) {
            InstanceState pinState = state.getInstanceState(pin[i]);
            Pin.FACTORY.setValue(pinState, val[i]);
        }
    }
    Propagator prop = state.getPropagator();
    try {
        prop.propagate();
    } catch (Throwable thr) {
        thr.printStackTrace();
    }
    if (prop.isOscillating())
        throw new TestException("oscilation detected");
    FailException err = null;
    for (int i = 0; i < pin.length; i++) {
        InstanceState pinState = state.getInstanceState(pin[i]);
        if (Pin.FACTORY.isInputPin(pin[i]))
            continue;
        Value v = Pin.FACTORY.getValue(pinState);
        if (!val[i].compatible(v)) {
            if (err == null)
                err = new FailException(i, pinState.getAttributeValue(StdAttr.LABEL), val[i], v);
            else
                err.add(new FailException(i, pinState.getAttributeValue(StdAttr.LABEL), val[i], v));
        }
    }
    if (err != null) {
        throw err;
    }
}
Also used : InstanceState(com.cburch.logisim.instance.InstanceState) TestException(com.cburch.logisim.data.TestException) Value(com.cburch.logisim.data.Value) FailException(com.cburch.logisim.data.FailException)

Example 10 with InstanceState

use of com.cburch.logisim.instance.InstanceState in project logisim-evolution by reds-heig.

the class VhdlSimulatorTclComp method generate.

public void generate() {
    /* Do not generate if file is already valid */
    if (valid)
        return;
    StringBuilder comp_files = new StringBuilder();
    comp_files.append("Autogenerated by logisim");
    comp_files.append(System.getProperty("line.separator"));
    /* For each vhdl entity */
    for (Component comp : VhdlSimulator.getVhdlComponents(vhdlSimulator.getProject().getCircuitState())) {
        if (comp.getFactory().getClass().equals(VhdlEntity.class)) {
            InstanceState state = vhdlSimulator.getProject().getCircuitState().getInstanceState(comp);
            String componentName = comp.getFactory().getHDLTopName(state.getInstance().getAttributeSet());
            comp_files.append("vcom -reportprogress 300 -work work ../src/" + componentName + ".vhdl");
            comp_files.append(System.getProperty("line.separator"));
        }
    }
    /*
		 * Replace template blocks by generated data
		 */
    String template;
    try {
        template = new String(FileUtil.getBytes(this.getClass().getResourceAsStream((VhdlSimulator.SIM_RESOURCES_PATH + "comp.templ"))));
        template = template.replaceAll("%date%", LocaleManager.parserSDF.format(new Date()));
        template = template.replaceAll("%comp_files%", comp_files.toString());
    } catch (IOException e) {
        logger.error("Could not read template : {}", e.getMessage());
        return;
    }
    PrintWriter writer;
    try {
        writer = new PrintWriter(VhdlSimulator.SIM_PATH + "comp.tcl", "UTF-8");
        writer.print(template);
        writer.close();
    } catch (FileNotFoundException e) {
        logger.error("Could not create run.tcl file : {}", e.getMessage());
        e.printStackTrace();
        return;
    } catch (UnsupportedEncodingException e) {
        logger.error("Could not create run.tcl file : {}", e.getMessage());
        e.printStackTrace();
        return;
    }
    valid = true;
}
Also used : InstanceState(com.cburch.logisim.instance.InstanceState) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Component(com.cburch.logisim.comp.Component) Date(java.util.Date) PrintWriter(java.io.PrintWriter)

Aggregations

InstanceState (com.cburch.logisim.instance.InstanceState)13 Component (com.cburch.logisim.comp.Component)5 Value (com.cburch.logisim.data.Value)5 Instance (com.cburch.logisim.instance.Instance)4 CircuitState (com.cburch.logisim.circuit.CircuitState)3 TestException (com.cburch.logisim.data.TestException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Entry (com.cburch.logisim.analyze.model.Entry)1 Propagator (com.cburch.logisim.circuit.Propagator)1 BitWidth (com.cburch.logisim.data.BitWidth)1 FailException (com.cburch.logisim.data.FailException)1 Port (com.cburch.logisim.instance.Port)1 Keyboard (com.cburch.logisim.std.io.Keyboard)1 Tty (com.cburch.logisim.std.io.Tty)1 Ram (com.cburch.logisim.std.memory.Ram)1