Fixed 2 Simulation bugs
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / LogicExecuter.java
index eb67f8d..52adeba 100644 (file)
@@ -5,6 +5,7 @@ import java.util.concurrent.atomic.AtomicLong;
 
 import net.mograsim.logic.core.timeline.PauseableTimeFunction;
 import net.mograsim.logic.core.timeline.Timeline;
+import net.mograsim.logic.core.timeline.Timeline.ExecutionResult;
 
 //TODO maybe move to logic core?
 public class LogicExecuter
@@ -41,17 +42,21 @@ public class LogicExecuter
                        {
                                while (shouldBeRunningLive.get())
                                {
-                                       // always execute to keep timeline from "hanging behind" for too long
-                                       long current = tf.getAsLong();
-                                       timeline.executeUntil(timeline.laterThan(current), current + 10);
+                                       long current = tf.getTime();
+                                       // The tf.isPaused() condition is justified, because timeline.getSimulationTime() returns the timestamp of the last
+                                       // processed event during executeUntil()
+                                       if (timeline.executeUntil(() -> timeline.laterThan(current).getAsBoolean() || tf.isPaused(),
+                                                       System.currentTimeMillis() + 10) == ExecutionResult.EXEC_OUT_OF_TIME)
+                                               timeline.synchTime(); // TODO: should this also be called if tf.isPaused() condition is met?
+                                       long nextEventTime = timeline.nextEventTime();
                                        long sleepTime;
                                        if (timeline.hasNext())
-                                               sleepTime = timeline.nextEventTime() - current;
+                                               sleepTime = (long) ((nextEventTime - current) * tf.getSimulTimeToRealTimeFactor());
                                        else
                                                sleepTime = 10000;
                                        try
                                        {
-                                               nextExecSimulTime.set(current + sleepTime);
+                                               nextExecSimulTime.set(nextEventTime);
                                                if (sleepTime > 0)
                                                        Thread.sleep(sleepTime);
 
@@ -81,6 +86,8 @@ public class LogicExecuter
                                if (Timeline.timeCmp(event.getTiming(), nextExecSimulTime.get()) < 0)
                                        simulationThread.interrupt();
                });
+               // not optimal; but we don't expect this to happen very often
+               tf.addSimulTimeToRealTimeFactorChangedListener(d -> simulationThread.interrupt());
        }
 
        public void executeNextStep()
@@ -130,9 +137,14 @@ public class LogicExecuter
                return isPaused.get();
        }
 
-       public void setSpeedPercentage(int percentage)
+       public double getSpeedFactor()
        {
-               tf.setSpeedPercentage(percentage);
+               return tf.getSpeedFactor();
+       }
+
+       public void setSpeedFactor(double factor)
+       {
+               tf.setSpeedFactor(factor);
        }
 
        private void waitForIsRunning(boolean expectedState)