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,37 +41,41 @@ public class Model {
public void doStep(boolean noise) throws NodeException {
int counter = 0;
while (!nodesToUpdateNext.isEmpty()) {
version++;
// swap lists
ArrayList<Node> nl = nodesToUpdateNext;
nodesToUpdateNext = nodesToUpdateAct;
nodesToUpdateAct = nl;
nodesToUpdateNext.clear();
if (noise) {
Collections.shuffle(nodesToUpdateAct);
for (Node n : nodesToUpdateAct) {
n.readInputs();
n.writeOutputs();
}
} else {
for (Node n : nodesToUpdateAct) {
n.readInputs();
}
for (Node n : nodesToUpdateAct) {
n.writeOutputs();
}
}
while (!doMicroStep(noise)) {
if (listener != null)
listener.needsUpdate();
if (counter++ > maxCounter) {
throw new NodeException("seemsToOscillate");
}
if (listener != null)
listener.needsUpdate();
}
}
public boolean doMicroStep(boolean noise) throws NodeException {
version++;
// swap lists
ArrayList<Node> nl = nodesToUpdateNext;
nodesToUpdateNext = nodesToUpdateAct;
nodesToUpdateAct = nl;
nodesToUpdateNext.clear();
if (noise) {
Collections.shuffle(nodesToUpdateAct);
for (Node n : nodesToUpdateAct) {
n.readInputs();
n.writeOutputs();
}
} else {
for (Node n : nodesToUpdateAct) {
n.readInputs();
}
for (Node n : nodesToUpdateAct) {
n.writeOutputs();
}
}
return nodesToUpdateNext.isEmpty();
}
public void init() throws NodeException {
init(false);
}

View File

@ -3,6 +3,7 @@ package de.neemann.digital.gui;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import de.neemann.digital.core.Listener;
import de.neemann.digital.core.Model;
import de.neemann.digital.core.PartDescription;
import de.neemann.digital.core.basic.*;
@ -121,14 +122,29 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
JMenu run = new JMenu("Run");
bar.add(run);
JCheckBox microStep = new JCheckBox("micro");
ToolTipAction runModel = new ToolTipAction("Run") {
@Override
public void actionPerformed(ActionEvent e) {
try {
ModelDescription m = new ModelDescription(circuitComponent.getCircuit());
Model model = m.create(circuitComponent);
model.init(true);
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) {
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(wireMode.createJButton());
toolBar.add(selectionMode.createJButton());
toolBar.add(runModel.createJButton());
toolBar.add(microStep);
toolBar.addSeparator();
insertHistory = new InsertHistory(toolBar);

View File

@ -75,6 +75,7 @@ public class CircuitComponent extends JComponent implements Listener {
addMouseMotionListener(listener);
addMouseListener(listener);
requestFocusInWindow();
circuit.clearState();
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)
n.interconnect();

View File

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

View File

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

View File

@ -76,21 +76,6 @@ public class GenericShape implements Shape {
@Override
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;
}
@ -99,9 +84,9 @@ public class GenericShape implements Shape {
int max = Math.max(inputs, outputs);
int height = (max - 1) * SIZE + SIZE2;
if (symmetric && state != null) {
graphic.drawText(new Vector(width * SIZE, 0), new Vector((width + 1) * SIZE, 0), Long.toString(state.getOutput(0).getValue()));
}
// if (symmetric && state != null) {
// 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;