Search in sources :

Example 11 with CircuitState

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

the class SimulationTreeCircuitNode method computeChildren.

// returns true if changed
private boolean computeChildren() {
    ArrayList<TreeNode> newChildren = new ArrayList<TreeNode>();
    ArrayList<Component> subcircs = new ArrayList<Component>();
    for (Component comp : circuitState.getCircuit().getNonWires()) {
        if (comp.getFactory() instanceof SubcircuitFactory) {
            subcircs.add(comp);
        } else {
            TreeNode toAdd = model.mapComponentToNode(comp);
            if (toAdd != null) {
                newChildren.add(toAdd);
            }
        }
    }
    Collections.sort(newChildren, new CompareByName());
    Collections.sort(subcircs, this);
    for (Component comp : subcircs) {
        SubcircuitFactory factory = (SubcircuitFactory) comp.getFactory();
        CircuitState state = factory.getSubstate(circuitState, comp);
        SimulationTreeCircuitNode toAdd = null;
        for (TreeNode o : children) {
            if (o instanceof SimulationTreeCircuitNode) {
                SimulationTreeCircuitNode n = (SimulationTreeCircuitNode) o;
                if (n.circuitState == state) {
                    toAdd = n;
                    break;
                }
            }
        }
        if (toAdd == null) {
            toAdd = new SimulationTreeCircuitNode(model, this, state, comp);
        }
        newChildren.add(toAdd);
    }
    if (!children.equals(newChildren)) {
        children = newChildren;
        return true;
    } else {
        return false;
    }
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) TreeNode(javax.swing.tree.TreeNode) ArrayList(java.util.ArrayList) SubcircuitFactory(com.cburch.logisim.circuit.SubcircuitFactory) Component(com.cburch.logisim.comp.Component)

Example 12 with CircuitState

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

the class SimulationTreeModel method mapToPath.

public TreePath mapToPath(CircuitState state) {
    if (state == null)
        return null;
    ArrayList<CircuitState> path = new ArrayList<CircuitState>();
    CircuitState current = state;
    CircuitState parent = current.getParentState();
    while (parent != null && parent != state) {
        path.add(current);
        current = parent;
        parent = current.getParentState();
    }
    Object[] pathNodes = new Object[path.size() + 1];
    pathNodes[0] = root;
    int pathPos = 1;
    SimulationTreeCircuitNode node = root;
    for (int i = path.size() - 1; i >= 0; i--) {
        current = path.get(i);
        SimulationTreeCircuitNode oldNode = node;
        for (int j = 0, n = node.getChildCount(); j < n; j++) {
            Object child = node.getChildAt(j);
            if (child instanceof SimulationTreeCircuitNode) {
                SimulationTreeCircuitNode circNode = (SimulationTreeCircuitNode) child;
                if (circNode.getCircuitState() == current) {
                    node = circNode;
                    break;
                }
            }
        }
        if (node == oldNode) {
            return null;
        }
        pathNodes[pathPos] = node;
        pathPos++;
    }
    return new TreePath(pathNodes);
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) TreePath(javax.swing.tree.TreePath) ArrayList(java.util.ArrayList)

Example 13 with CircuitState

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

the class InstancePainter method getPortValue.

public Value getPortValue(int portIndex) {
    InstanceComponent c = comp;
    CircuitState s = context.getCircuitState();
    if (c != null && s != null) {
        return s.getValue(c.getEnd(portIndex).getLocation());
    } else {
        return Value.UNKNOWN;
    }
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState)

Example 14 with CircuitState

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

the class MenuSimulate method setCurrentState.

public void setCurrentState(Simulator sim, CircuitState value) {
    if (currentState == value) {
        return;
    }
    Simulator oldSim = currentSim;
    CircuitState oldState = currentState;
    currentSim = sim;
    currentState = value;
    if (bottomState == null) {
        bottomState = currentState;
    } else if (currentState == null) {
        bottomState = null;
    } else {
        CircuitState cur = bottomState;
        while (cur != null && cur != currentState) {
            cur = cur.getParentState();
        }
        if (cur == null) {
            bottomState = currentState;
        }
    }
    boolean oldPresent = oldState != null;
    boolean present = currentState != null;
    if (oldPresent != present) {
        computeEnabled();
    }
    if (currentSim != oldSim) {
        double freq = currentSim == null ? 1.0 : currentSim.getTickFrequency();
        for (int i = 0; i < tickFreqs.length; i++) {
            tickFreqs[i].setSelected(Math.abs(tickFreqs[i].freq - freq) < 0.001);
        }
        if (oldSim != null) {
            oldSim.removeSimulatorListener(myListener);
        }
        if (currentSim != null) {
            currentSim.addSimulatorListener(myListener);
        }
        myListener.simulatorStateChanged(new SimulatorEvent(sim));
    }
    clearItems(downStateItems);
    CircuitState cur = bottomState;
    while (cur != null && cur != currentState) {
        downStateItems.add(new CircuitStateMenuItem(cur));
        cur = cur.getParentState();
    }
    if (cur != null) {
        cur = cur.getParentState();
    }
    clearItems(upStateItems);
    while (cur != null) {
        upStateItems.add(0, new CircuitStateMenuItem(cur));
        cur = cur.getParentState();
    }
    recreateStateMenus();
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) SimulatorEvent(com.cburch.logisim.circuit.SimulatorEvent) Simulator(com.cburch.logisim.circuit.Simulator)

Example 15 with CircuitState

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

the class TtyInterface method run.

public static void run(Startup args) {
    File fileToOpen = args.getFilesToOpen().get(0);
    Loader loader = new Loader(null);
    LogisimFile file;
    try {
        file = loader.openLogisimFile(fileToOpen, args.getSubstitutions());
    } catch (LoadFailedException e) {
        logger.error("{}", Strings.get("ttyLoadError", fileToOpen.getName()));
        System.exit(-1);
        return;
    }
    int format = args.getTtyFormat();
    if ((format & FORMAT_STATISTICS) != 0) {
        format &= ~FORMAT_STATISTICS;
        displayStatistics(file);
    }
    if (format == 0) {
        // no simulation remaining to perform, so just exit
        System.exit(0);
    }
    Project proj = new Project(file);
    Circuit circuit = file.getMainCircuit();
    Map<Instance, String> pinNames = Analyze.getPinLabels(circuit);
    ArrayList<Instance> outputPins = new ArrayList<Instance>();
    Instance haltPin = null;
    for (Map.Entry<Instance, String> entry : pinNames.entrySet()) {
        Instance pin = entry.getKey();
        String pinName = entry.getValue();
        if (!Pin.FACTORY.isInputPin(pin)) {
            outputPins.add(pin);
            if (pinName.equals("halt")) {
                haltPin = pin;
            }
        }
    }
    CircuitState circState = new CircuitState(proj, circuit);
    // we have to do our initial propagation before the simulation starts -
    // it's necessary to populate the circuit with substates.
    circState.getPropagator().propagate();
    if (args.getLoadFile() != null) {
        try {
            boolean loaded = loadRam(circState, args.getLoadFile());
            if (!loaded) {
                logger.error("{}", Strings.get("loadNoRamError"));
                System.exit(-1);
            }
        } catch (IOException e) {
            logger.error("{}: {}", Strings.get("loadIoError"), e.toString());
            System.exit(-1);
        }
    }
    int ttyFormat = args.getTtyFormat();
    int simCode = runSimulation(circState, outputPins, haltPin, ttyFormat);
    System.exit(simCode);
}
Also used : CircuitState(com.cburch.logisim.circuit.CircuitState) Instance(com.cburch.logisim.instance.Instance) ArrayList(java.util.ArrayList) Loader(com.cburch.logisim.file.Loader) Circuit(com.cburch.logisim.circuit.Circuit) IOException(java.io.IOException) Project(com.cburch.logisim.proj.Project) LogisimFile(com.cburch.logisim.file.LogisimFile) LogisimFile(com.cburch.logisim.file.LogisimFile) File(java.io.File) Map(java.util.Map) LoadFailedException(com.cburch.logisim.file.LoadFailedException)

Aggregations

CircuitState (com.cburch.logisim.circuit.CircuitState)26 Component (com.cburch.logisim.comp.Component)6 Circuit (com.cburch.logisim.circuit.Circuit)5 ArrayList (java.util.ArrayList)4 ComponentDrawContext (com.cburch.logisim.comp.ComponentDrawContext)3 Location (com.cburch.logisim.data.Location)3 Instance (com.cburch.logisim.instance.Instance)3 InstanceState (com.cburch.logisim.instance.InstanceState)3 Color (java.awt.Color)3 Dimension (java.awt.Dimension)3 Simulator (com.cburch.logisim.circuit.Simulator)2 SubcircuitFactory (com.cburch.logisim.circuit.SubcircuitFactory)2 Bounds (com.cburch.logisim.data.Bounds)2 Value (com.cburch.logisim.data.Value)2 LogisimFile (com.cburch.logisim.file.LogisimFile)2 Graphics (java.awt.Graphics)2 TreePath (javax.swing.tree.TreePath)2 CircuitListener (com.cburch.logisim.circuit.CircuitListener)1 SimulatorEvent (com.cburch.logisim.circuit.SimulatorEvent)1 AppearancePort (com.cburch.logisim.circuit.appear.AppearancePort)1