added micro stepping

This commit is contained in:
hneemann 2016-03-17 21:30:00 +01:00
parent 9200771046
commit 5f3b608fff
7 changed files with 66 additions and 48 deletions

View File

@ -41,7 +41,16 @@ public class Model {
public void doStep(boolean noise) throws NodeException { public void doStep(boolean noise) throws NodeException {
int counter = 0; int counter = 0;
while (!nodesToUpdateNext.isEmpty()) { while (!doMicroStep(noise)) {
if (listener != null)
listener.needsUpdate();
if (counter++ > maxCounter) {
throw new NodeException("seemsToOscillate");
}
}
}
public boolean doMicroStep(boolean noise) throws NodeException {
version++; version++;
// swap lists // swap lists
ArrayList<Node> nl = nodesToUpdateNext; ArrayList<Node> nl = nodesToUpdateNext;
@ -64,12 +73,7 @@ public class Model {
n.writeOutputs(); n.writeOutputs();
} }
} }
if (counter++ > maxCounter) { return nodesToUpdateNext.isEmpty();
throw new NodeException("seemsToOscillate");
}
if (listener != null)
listener.needsUpdate();
}
} }
public void init() throws NodeException { public void init() throws NodeException {

View File

@ -3,6 +3,7 @@ package de.neemann.digital.gui;
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.StaxDriver; import com.thoughtworks.xstream.io.xml.StaxDriver;
import de.neemann.digital.core.Listener;
import de.neemann.digital.core.Model; import de.neemann.digital.core.Model;
import de.neemann.digital.core.PartDescription; import de.neemann.digital.core.PartDescription;
import de.neemann.digital.core.basic.*; import de.neemann.digital.core.basic.*;
@ -121,14 +122,29 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
JMenu run = new JMenu("Run"); JMenu run = new JMenu("Run");
bar.add(run); bar.add(run);
JCheckBox microStep = new JCheckBox("micro");
ToolTipAction runModel = new ToolTipAction("Run") { ToolTipAction runModel = new ToolTipAction("Run") {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
ModelDescription m = new ModelDescription(circuitComponent.getCircuit());
Model model = m.create(circuitComponent);
model.init(true);
circuitComponent.setMode(CircuitComponent.Mode.running); circuitComponent.setMode(CircuitComponent.Mode.running);
ModelDescription m = new ModelDescription(circuitComponent.getCircuit());
Model model = m.createModel(circuitComponent);
if (microStep.isSelected()) {
model.setListener(new Listener() {
@Override
public void needsUpdate() {
circuitComponent.paintImmediately(circuitComponent.getVisibleRect());
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
}
model.init(true);
} catch (Exception e1) { } catch (Exception e1) {
new ErrorMessage("error creating model").addCause(e1).show(Main.this); new ErrorMessage("error creating model").addCause(e1).show(Main.this);
} }
@ -142,6 +158,8 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
toolBar.add(partsMode.createJButton()); toolBar.add(partsMode.createJButton());
toolBar.add(wireMode.createJButton()); toolBar.add(wireMode.createJButton());
toolBar.add(selectionMode.createJButton()); toolBar.add(selectionMode.createJButton());
toolBar.add(runModel.createJButton());
toolBar.add(microStep);
toolBar.addSeparator(); toolBar.addSeparator();
insertHistory = new InsertHistory(toolBar); insertHistory = new InsertHistory(toolBar);

View File

@ -75,6 +75,7 @@ public class CircuitComponent extends JComponent implements Listener {
addMouseMotionListener(listener); addMouseMotionListener(listener);
addMouseListener(listener); addMouseListener(listener);
requestFocusInWindow(); requestFocusInWindow();
circuit.clearState();
repaint(); repaint();
} }

View File

@ -30,7 +30,7 @@ public class ModelDescription {
} }
} }
public Model create(Listener listener) throws PinException, NodeException { public Model createModel(Listener listener) throws PinException, NodeException {
for (Net n : netList) for (Net n : netList)
n.interconnect(); n.interconnect();

View File

@ -105,4 +105,11 @@ public class Circuit implements Drawable {
dots = WireConsistencyChecker.createDots(wires); dots = WireConsistencyChecker.createDots(wires);
return dots; return dots;
} }
public void clearState() {
for (VisualPart vp : visualParts)
vp.setState(null, null, null);
for (Wire w : wires)
w.setValue(null);
}
} }

View File

@ -132,6 +132,9 @@ public class VisualPart implements Drawable, Moveable {
*/ */
public void setState(State state, Listener listener, Model model) { public void setState(State state, Listener listener, Model model) {
this.state = state; this.state = state;
if (state == null)
interactor = null;
else
interactor = partDescription.getShape().applyStateMonitor(state, listener, model); interactor = partDescription.getShape().applyStateMonitor(state, listener, model);
} }

View File

@ -76,21 +76,6 @@ public class GenericShape implements Shape {
@Override @Override
public Interactor applyStateMonitor(State state, Listener listener, Model model) { public Interactor applyStateMonitor(State state, Listener listener, Model model) {
if (symmetric) {
state.getOutput(0).addListener(new Listener() {
public long lastValue = 0;
@Override
public void needsUpdate() {
long value = state.getInput(0).getValue();
if (lastValue != value) {
lastValue = value;
listener.needsUpdate();
}
}
});
}
return null; return null;
} }
@ -99,9 +84,9 @@ public class GenericShape implements Shape {
int max = Math.max(inputs, outputs); int max = Math.max(inputs, outputs);
int height = (max - 1) * SIZE + SIZE2; int height = (max - 1) * SIZE + SIZE2;
if (symmetric && state != null) { // if (symmetric && state != null) {
graphic.drawText(new Vector(width * SIZE, 0), new Vector((width + 1) * SIZE, 0), Long.toString(state.getOutput(0).getValue())); // graphic.drawText(new Vector(width * SIZE, 0), new Vector((width + 1) * SIZE, 0), Long.toString(state.getOutput(0).getValue()));
} // }
if (symmetric && ((inputs & 1) == 0)) height += SIZE; if (symmetric && ((inputs & 1) == 0)) height += SIZE;