mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-15 07:48:29 -04:00
added a speed test which runs the model without a gui connected to it.
This commit is contained in:
parent
537a421dd6
commit
e26c4f643d
@ -1,5 +1,7 @@
|
|||||||
package de.neemann.digital.core;
|
package de.neemann.digital.core;
|
||||||
|
|
||||||
|
import de.neemann.digital.core.wiring.Clock;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -59,12 +61,12 @@ public class Model {
|
|||||||
nodesToUpdateNext.addAll(nodes);
|
nodesToUpdateNext.addAll(nodes);
|
||||||
isInitialized = true;
|
isInitialized = true;
|
||||||
doStep(noise);
|
doStep(noise);
|
||||||
fireEvent(ModelStateObserver.Event.STARTED);
|
fireEvent(new ModelEvent(ModelEvent.Event.STARTED));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
stopped = true;
|
stopped = true;
|
||||||
fireEvent(ModelStateObserver.Event.STOPPED);
|
fireEvent(new ModelEvent(ModelEvent.Event.STOPPED));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToUpdateList(Node node) {
|
public void addToUpdateList(Node node) {
|
||||||
@ -150,8 +152,14 @@ public class Model {
|
|||||||
observers.add(observer);
|
observers.add(observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fireEvent(ModelStateObserver.Event event) {
|
private void fireEvent(ModelEvent event) {
|
||||||
for (ModelStateObserver observer : observers)
|
for (ModelStateObserver observer : observers)
|
||||||
observer.handleEvent(event);
|
observer.handleEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<Clock> getClocks() {
|
||||||
|
ModelEvent e = new ModelEvent(ModelEvent.Event.FETCHCLOCK);
|
||||||
|
fireEvent(e);
|
||||||
|
return e.getClocks();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
37
src/main/java/de/neemann/digital/core/ModelEvent.java
Normal file
37
src/main/java/de/neemann/digital/core/ModelEvent.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package de.neemann.digital.core;
|
||||||
|
|
||||||
|
import de.neemann.digital.core.wiring.Clock;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author hneemann
|
||||||
|
*/
|
||||||
|
public class ModelEvent {
|
||||||
|
|
||||||
|
|
||||||
|
public enum Event {STARTED, STOPPED, FETCHCLOCK}
|
||||||
|
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Event event;
|
||||||
|
private ArrayList<Clock> clocks;
|
||||||
|
|
||||||
|
public ModelEvent(Event event) {
|
||||||
|
this.event = event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Event getType() {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerClock(Clock clock) {
|
||||||
|
if (clocks == null)
|
||||||
|
clocks = new ArrayList<Clock>();
|
||||||
|
clocks.add(clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Clock> getClocks() {
|
||||||
|
return clocks;
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,6 @@ package de.neemann.digital.core;
|
|||||||
*/
|
*/
|
||||||
public interface ModelStateObserver {
|
public interface ModelStateObserver {
|
||||||
|
|
||||||
enum Event {STARTED, STOPPED}
|
void handleEvent(ModelEvent event);
|
||||||
|
|
||||||
void handleEvent(Event event);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
54
src/main/java/de/neemann/digital/core/SpeedTest.java
Normal file
54
src/main/java/de/neemann/digital/core/SpeedTest.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package de.neemann.digital.core;
|
||||||
|
|
||||||
|
import de.neemann.digital.core.wiring.Clock;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author hneemann
|
||||||
|
*/
|
||||||
|
public class SpeedTest {
|
||||||
|
private static final int LOOPCOUNTER = 2000;
|
||||||
|
private final Model model;
|
||||||
|
|
||||||
|
public SpeedTest(Model model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculate() throws NodeException {
|
||||||
|
ArrayList<Clock> clocks = model.getClocks();
|
||||||
|
if (clocks.isEmpty())
|
||||||
|
throw new NodeException("NoClockFound");
|
||||||
|
else if (clocks.size() > 1)
|
||||||
|
throw new NodeException("MoreThenOneClocksFound");
|
||||||
|
|
||||||
|
|
||||||
|
Clock clock = clocks.get(0);
|
||||||
|
model.init(true);
|
||||||
|
ObservableValue clockValue = clock.getOutputs()[0];
|
||||||
|
int state = (int) clockValue.getValue();
|
||||||
|
|
||||||
|
long aktTime;
|
||||||
|
long starTime = System.currentTimeMillis();
|
||||||
|
int loops = 0;
|
||||||
|
do {
|
||||||
|
for (int i = 0; i < LOOPCOUNTER; i++) {
|
||||||
|
state = 1 - state;
|
||||||
|
clockValue.setValue(state);
|
||||||
|
}
|
||||||
|
loops++;
|
||||||
|
aktTime = System.currentTimeMillis();
|
||||||
|
} while (aktTime - starTime < 1000);
|
||||||
|
|
||||||
|
long cycles = ((long) loops) * LOOPCOUNTER / 2;
|
||||||
|
double time = (aktTime - starTime) / 1000.0;
|
||||||
|
|
||||||
|
double freqency = cycles / time;
|
||||||
|
|
||||||
|
System.out.println("cycles: " + cycles);
|
||||||
|
System.out.println("time : " + time + "s");
|
||||||
|
System.out.println("freq :" + freqency);
|
||||||
|
|
||||||
|
return freqency;
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,6 @@
|
|||||||
package de.neemann.digital.core.wiring;
|
package de.neemann.digital.core.wiring;
|
||||||
|
|
||||||
import de.neemann.digital.core.Model;
|
import de.neemann.digital.core.*;
|
||||||
import de.neemann.digital.core.ModelStateObserver;
|
|
||||||
import de.neemann.digital.core.NodeException;
|
|
||||||
import de.neemann.digital.core.ObservableValue;
|
|
||||||
import de.neemann.digital.core.element.AttributeKey;
|
import de.neemann.digital.core.element.AttributeKey;
|
||||||
import de.neemann.digital.core.element.Element;
|
import de.neemann.digital.core.element.Element;
|
||||||
import de.neemann.digital.core.element.ElementAttributes;
|
import de.neemann.digital.core.element.ElementAttributes;
|
||||||
@ -42,28 +39,35 @@ public class Clock implements Element {
|
|||||||
@Override
|
@Override
|
||||||
public void registerNodes(Model model) {
|
public void registerNodes(Model model) {
|
||||||
model.addObserver(new ModelStateObserver() {
|
model.addObserver(new ModelStateObserver() {
|
||||||
|
public boolean startThisTimer = true;
|
||||||
public Timer timer;
|
public Timer timer;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleEvent(Event event) {
|
public void handleEvent(ModelEvent event) {
|
||||||
switch (event) {
|
switch (event.getType()) {
|
||||||
case STARTED:
|
case STARTED:
|
||||||
int delay = 1000 / frequency;
|
if (startThisTimer) {
|
||||||
if (delay < 100) delay = 100;
|
int delay = 1000 / frequency;
|
||||||
timer = new Timer(delay, e -> {
|
if (delay < 100) delay = 100;
|
||||||
output.setValue(1 - output.getValue());
|
timer = new Timer(delay, e -> {
|
||||||
try {
|
output.setValue(1 - output.getValue());
|
||||||
model.doStep();
|
try {
|
||||||
} catch (NodeException e1) {
|
model.doStep();
|
||||||
SwingUtilities.invokeLater(new ErrorMessage("ClockError").addCause(e1));
|
} catch (NodeException e1) {
|
||||||
timer.stop();
|
SwingUtilities.invokeLater(new ErrorMessage("ClockError").addCause(e1));
|
||||||
}
|
timer.stop();
|
||||||
});
|
}
|
||||||
timer.start();
|
});
|
||||||
|
timer.start();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case STOPPED:
|
case STOPPED:
|
||||||
timer.stop();
|
if (timer != null)
|
||||||
|
timer.stop();
|
||||||
break;
|
break;
|
||||||
|
case FETCHCLOCK:
|
||||||
|
event.registerClock(Clock.this);
|
||||||
|
startThisTimer = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -6,6 +6,7 @@ import com.thoughtworks.xstream.io.xml.StaxDriver;
|
|||||||
import de.neemann.digital.core.Model;
|
import de.neemann.digital.core.Model;
|
||||||
import de.neemann.digital.core.NodeException;
|
import de.neemann.digital.core.NodeException;
|
||||||
import de.neemann.digital.core.Observer;
|
import de.neemann.digital.core.Observer;
|
||||||
|
import de.neemann.digital.core.SpeedTest;
|
||||||
import de.neemann.digital.core.element.AttributeKey;
|
import de.neemann.digital.core.element.AttributeKey;
|
||||||
import de.neemann.digital.core.element.ElementAttributes;
|
import de.neemann.digital.core.element.ElementAttributes;
|
||||||
import de.neemann.digital.gui.components.CircuitComponent;
|
import de.neemann.digital.gui.components.CircuitComponent;
|
||||||
@ -164,9 +165,26 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave {
|
|||||||
}
|
}
|
||||||
}.setToolTip("Runs the Model in Micro Stepping Mode");
|
}.setToolTip("Runs the Model in Micro Stepping Mode");
|
||||||
|
|
||||||
|
ToolTipAction speedTest = new ToolTipAction("SpeedTest") {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
try {
|
||||||
|
modelDescription = new ModelDescription(circuitComponent.getCircuit(), library);
|
||||||
|
model = modelDescription.createModel();
|
||||||
|
SpeedTest speedTest = new SpeedTest(model);
|
||||||
|
double frequency = speedTest.calculate();
|
||||||
|
JOptionPane.showMessageDialog(Main.this, "Frequency: " + frequency);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
new ErrorMessage("SpeedTestError").addCause(e1).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.setToolTip("Runs the Model");
|
||||||
|
|
||||||
|
|
||||||
run.add(runModel.createJMenuItem());
|
run.add(runModel.createJMenuItem());
|
||||||
run.add(runModelMicro.createJMenuItem());
|
run.add(runModelMicro.createJMenuItem());
|
||||||
run.add(doStep.createJMenuItem());
|
run.add(doStep.createJMenuItem());
|
||||||
|
run.add(speedTest.createJMenuItem());
|
||||||
doStep.setEnabled(false);
|
doStep.setEnabled(false);
|
||||||
|
|
||||||
JToolBar toolBar = new JToolBar();
|
JToolBar toolBar = new JToolBar();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user