cleaned up how speed is managed in the app

This commit is contained in:
Brendan Robert 2018-02-01 09:09:18 -06:00
parent 53e86e7888
commit c50ea6ec77
4 changed files with 46 additions and 16 deletions

View File

@ -257,7 +257,7 @@ public class JaceUIController {
Emulator.logic.speedSetting = (int) speed; Emulator.logic.speedSetting = (int) speed;
double speedRatio = convertSpeedToRatio(speed); double speedRatio = convertSpeedToRatio(speed);
if (speedRatio > 100.0) { if (speedRatio > 100.0) {
Emulator.computer.getMotherboard().maxspeed = true; Emulator.computer.getMotherboard().setMaxSpeed(true);
Motherboard.cpuPerClock = 3; Motherboard.cpuPerClock = 3;
} else { } else {
if (speedRatio > 25) { if (speedRatio > 25) {
@ -265,8 +265,8 @@ public class JaceUIController {
} else { } else {
Motherboard.cpuPerClock = 1; Motherboard.cpuPerClock = 1;
} }
Emulator.computer.getMotherboard().maxspeed = false; Emulator.computer.getMotherboard().setMaxSpeed(false);
Emulator.computer.getMotherboard().speedRatio = (int) (speedRatio * 100); Emulator.computer.getMotherboard().setSpeedInPercentage((int) (speedRatio * 100));
} }
Emulator.computer.getMotherboard().reconfigure(); Emulator.computer.getMotherboard().reconfigure();
} }

View File

@ -305,7 +305,7 @@ public class MetaCheat extends Cheats {
} }
int fadeCounter = 0; int fadeCounter = 0;
int FADE_TIMER_VALUE = (int) (Emulator.computer.getMotherboard().cyclesPerSecond / 60); int FADE_TIMER_VALUE = (int) (Emulator.computer.getMotherboard().getSpeedInHz() / 60);
@Override @Override
public void tick() { public void tick() {

View File

@ -65,6 +65,9 @@ public class Motherboard extends TimedDevice {
if (oldMotherboard != null) { if (oldMotherboard != null) {
miscDevices.addAll(oldMotherboard.miscDevices); miscDevices.addAll(oldMotherboard.miscDevices);
speaker = oldMotherboard.speaker; speaker = oldMotherboard.speaker;
accelorationRequestors.addAll(oldMotherboard.accelorationRequestors);
setSpeedInHz(oldMotherboard.getSpeedInHz());
setMaxSpeed(oldMotherboard.isMaxSpeed());
} }
} }
@ -147,7 +150,7 @@ public class Motherboard extends TimedDevice {
resume(); resume();
} }
} }
static HashSet<Object> accelorationRequestors = new HashSet<>(); HashSet<Object> accelorationRequestors = new HashSet<>();
public void requestSpeed(Object requester) { public void requestSpeed(Object requester) {
accelorationRequestors.add(requester); accelorationRequestors.add(requester);

View File

@ -21,24 +21,25 @@ package jace.core;
import jace.config.ConfigurableField; import jace.config.ConfigurableField;
/** /**
* A timed device is a device which executes so many ticks in a given time * A timed device is a device which executes so many ticks in a given time interval. This is the core of the emulator
* interval. This is the core of the emulator timing mechanics. * timing mechanics.
* *
* @author Brendan Robert (BLuRry) brendan.robert@gmail.com * @author Brendan Robert (BLuRry) brendan.robert@gmail.com
*/ */
public abstract class TimedDevice extends Device { public abstract class TimedDevice extends Device {
/** /**
* Creates a new instance of TimedDevice * Creates a new instance of TimedDevice
*
* @param computer * @param computer
*/ */
public TimedDevice(Computer computer) { public TimedDevice(Computer computer) {
super(computer); super(computer);
setSpeed(cyclesPerSecond); setSpeedInHz(cyclesPerSecond);
} }
@ConfigurableField(name = "Speed", description = "(Percentage)") @ConfigurableField(name = "Speed", description = "(Percentage)")
public int speedRatio = 100; public int speedRatio = 100;
public long cyclesPerSecond = defaultCyclesPerSecond(); private long cyclesPerSecond = defaultCyclesPerSecond();
@ConfigurableField(name = "Max speed") @ConfigurableField(name = "Max speed")
public boolean maxspeed = false; public boolean maxspeed = false;
@ -114,13 +115,44 @@ public abstract class TimedDevice extends Device {
long cyclesPerInterval; // How many cycles to wait until a pause interval long cyclesPerInterval; // How many cycles to wait until a pause interval
long nextSync; // When was the last pause? long nextSync; // When was the last pause?
public final void setSpeed(long cyclesPerSecond) { public final int getSpeedRatio() {
return speedRatio;
}
public final void setMaxSpeed(boolean enabled) {
maxspeed = enabled;
if (!enabled) {
disableTempMaxSpeed();
}
}
public final boolean isMaxSpeed() {
return maxspeed;
}
public final long getSpeedInHz() {
return cyclesPerInterval * 100L;
}
public final void setSpeedInHz(long cyclesPerSecond) {
// System.out.println("Raw set speed for " + getName() + " to " + cyclesPerSecond + "hz");
speedRatio = (int) Math.round(cyclesPerSecond * 100.0 / defaultCyclesPerSecond());
cyclesPerInterval = cyclesPerSecond / 100L; cyclesPerInterval = cyclesPerSecond / 100L;
nanosPerInterval = (long) (cyclesPerInterval * NANOS_PER_SECOND / cyclesPerSecond); nanosPerInterval = (long) (cyclesPerInterval * NANOS_PER_SECOND / cyclesPerSecond);
// System.out.println("Will pause " + nanosPerInterval + " nanos every " + cyclesPerInterval + " cycles"); // System.out.println("Will pause " + nanosPerInterval + " nanos every " + cyclesPerInterval + " cycles");
cycleTimer = 0; cycleTimer = 0;
resetSyncTimer(); resetSyncTimer();
} }
public final void setSpeedInPercentage(int ratio) {
// System.out.println("Setting " + getName() + " speed ratio to " + speedRatio);
cyclesPerSecond = defaultCyclesPerSecond() * ratio / 100;
if (cyclesPerSecond == 0) {
cyclesPerSecond = defaultCyclesPerSecond();
}
setSpeedInHz(cyclesPerSecond);
}
long skip = 0; long skip = 0;
long wait = 0; long wait = 0;
@ -171,11 +203,6 @@ public abstract class TimedDevice extends Device {
@Override @Override
public void reconfigure() { public void reconfigure() {
cyclesPerSecond = defaultCyclesPerSecond() * speedRatio / 100;
if (cyclesPerSecond == 0) {
cyclesPerSecond = defaultCyclesPerSecond();
}
setSpeed(cyclesPerSecond);
} }
public abstract long defaultCyclesPerSecond(); public abstract long defaultCyclesPerSecond();