Skip to content

Commit

Permalink
#35 added extra sleep if not turbo mode
Browse files Browse the repository at this point in the history
  • Loading branch information
raydac committed Feb 25, 2023
1 parent 9327b05 commit d68e6c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
19 changes: 12 additions & 7 deletions zxpoly-emul/src/main/java/com/igormaznitsa/zxpoly/MainForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public String getDescription() {
private final AtomicReference<AnimationEncoder> currentAnimationEncoder = new AtomicReference<>();
private final Motherboard board;
private final ZxVideoStreamer videoStreamer;
private final Timer wallClock = new Timer(TIMER_INT_DELAY_MILLISECONDS);
private final Timer wallClock = new Timer(TIMER_INT_DELAY_MILLISECONDS, Duration.ofNanos(50000L));
private final Runnable traceWindowsUpdater = new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -1058,13 +1058,18 @@ private void mainLoop() {
boolean notifyRepaintScreen = false;
boolean doBlink = false;

int frameTiStates = this.board.getFrameTiStates();
final boolean inTurboMode = this.turboMode;
final boolean tiStatesForIntExhausted = frameTiStates >= this.timingProfile.tstatesFrame;
boolean intTickForWallClockReached = this.wallClock.completed();

if (!inTurboMode && tiStatesForIntExhausted && !intTickForWallClockReached) {
this.wallClock.sleep();
}
intTickForWallClockReached = this.wallClock.completed();

if (stepLocker.tryLock()) {
try {
int frameTiStates = this.board.getFrameTiStates();
final boolean inTurboMode = this.turboMode;
final boolean tiStatesForIntExhausted = frameTiStates >= this.timingProfile.tstatesFrame;
final boolean intTickForWallClockReached = this.wallClock.completed();

final boolean doCpuIntTick;
if (intTickForWallClockReached) {
if (tiStatesForIntExhausted) {
Expand Down Expand Up @@ -1158,7 +1163,6 @@ private void mainLoop() {
this.repaintScreen();
}
} else {
final int frameTiStates = this.board.getFrameTiStates();
if (this.wallClock.completed()) {
this.wallClock.next();
this.videoStreamer.onWallclockInt();
Expand All @@ -1176,6 +1180,7 @@ private void mainLoop() {
if (this.activeTracerWindowCounter.get() > 0) {
updateTracerWindowsForStep();
}

Thread.onSpinWait();
}
}
Expand Down
19 changes: 19 additions & 0 deletions zxpoly-emul/src/main/java/com/igormaznitsa/zxpoly/utils/Timer.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
package com.igormaznitsa.zxpoly.utils;

import java.time.Duration;
import java.util.concurrent.locks.LockSupport;

public final class Timer {

private final long delay;
private long start = 0L;
private long timeout = -1L;

private final long sleepDelay;

public Timer(final Duration delay) {
this(delay, null);
}

public Timer(final Duration delay, final Duration sleepDelay) {
this.delay = delay.toNanos();
this.sleepDelay = sleepDelay == null ? -1L : sleepDelay.toNanos();
}

public void next(final Duration delay) {
this.start = System.nanoTime();
this.timeout = this.start + delay.toNanos();
}

public void sleep() {
if (this.sleepDelay < 0L) {
Thread.onSpinWait();
} else {
final long nanos = this.timeout - System.nanoTime();
if (nanos > this.sleepDelay) {
LockSupport.parkNanos(this.sleepDelay);
}
}
}

public void next() {
this.start = System.nanoTime();
this.timeout = this.start + this.delay;
Expand Down

0 comments on commit d68e6c7

Please sign in to comment.