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;
}
}
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);
}
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;
}
}
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();
}
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);
}
Aggregations