Skip to content

Commit

Permalink
[#314] Remove TimedEventsProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed May 30, 2023
1 parent 9b7a3d5 commit 6a803e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package net.emustudio.plugins.cpu.zilogZ80;

import net.emustudio.emulib.plugins.cpu.AbstractCPUContext;
import net.emustudio.emulib.plugins.cpu.TimedEventsProcessor;
import net.emustudio.plugins.cpu.zilogZ80.api.ContextZ80;
import net.jcip.annotations.ThreadSafe;
import org.slf4j.Logger;
Expand Down Expand Up @@ -119,11 +118,6 @@ public void addCycles(long tStates) {
engine.addExecutedCyclesPerTimeSlice(tStates);
}

@Override
public Optional<TimedEventsProcessor> getTimedEventsProcessor() {
return Optional.empty();
}

@Override
public boolean passedCyclesSupported() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,63 @@
*/
package net.emustudio.plugins.device.simh.commands;

import net.emustudio.emulib.plugins.cpu.TimedEventsProcessor;
import net.emustudio.emulib.plugins.cpu.CPUContext;
import net.emustudio.plugins.cpu.intel8080.api.Context8080;

import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

public class StartTimerInterrupts implements Command {
public final static StartTimerInterrupts INS = new StartTimerInterrupts();
private static final int TRY_AFTER_CYCLES = 50000;
public final AtomicReference<TimerInterruptCallback> callback = new AtomicReference<>();
private Optional<TimedEventsProcessor> tep;

@Override
public void reset(Control control) {
this.tep = control.getCpu().getTimedEventsProcessor();
TimerInterruptCallback old = callback.getAndSet(null);
if (old != null) {
tep.ifPresent(t -> t.remove(TRY_AFTER_CYCLES, old));
control.getCpu().removePassedCyclesListener(old);
}
}

@Override
public void start(Control control) {
reset(control);
TimerInterruptCallback cb = new TimerInterruptCallback(control.getCpu());

Context8080 cpu = control.getCpu();
TimerInterruptCallback cb = new TimerInterruptCallback(cpu);
callback.set(cb);
tep.ifPresent(t -> t.schedule(TRY_AFTER_CYCLES, cb));

cpu.addPassedCyclesListener(cb);
control.clearCommand();
}

private static class TimerInterruptCallback implements Runnable {
private static class TimerInterruptCallback implements CPUContext.PassedCyclesListener {
private final Context8080 cpu;
private volatile long startTime = System.nanoTime();
private long startTime = System.nanoTime();
private long cyclesPassed = 0;

private TimerInterruptCallback(Context8080 cpu) {
this.cpu = Objects.requireNonNull(cpu);
}

@Override
public void run() {
long endTime = System.nanoTime();
long elapsed = endTime - startTime;
public void passedCycles(long cycles) {
cyclesPassed += cycles;

if (cyclesPassed >= TRY_AFTER_CYCLES) {
cyclesPassed -= TRY_AFTER_CYCLES;
long endTime = System.nanoTime();
long elapsed = endTime - startTime;

if (elapsed >= (SetTimerDelta.INS.timerDelta * 1000000L)) {
startTime = endTime;
// will work only in interrupt mode 0
int addr = SetTimerInterruptAdr.INS.timerInterruptHandler;
byte b1 = (byte) (addr & 0xFF);
byte b2 = (byte) (addr >>> 8);
cpu.signalInterrupt(new byte[]{(byte) 0xCD, b1, b2});
if (elapsed >= (SetTimerDelta.INS.timerDelta * 1000000L)) {
startTime = endTime;
// will work only in interrupt mode 0
int addr = SetTimerInterruptAdr.INS.timerInterruptHandler;
byte b1 = (byte) (addr & 0xFF);
byte b2 = (byte) (addr >>> 8);
cpu.signalInterrupt(new byte[]{(byte) 0xCD, b1, b2});
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@

import net.emustudio.emulib.plugins.annotations.PluginContext;
import net.emustudio.emulib.plugins.cpu.CPUContext;
import net.emustudio.emulib.plugins.cpu.TimedEventsProcessor;
import net.emustudio.emulib.plugins.device.DeviceContext;
import net.emustudio.emulib.plugins.memory.MemoryContext;
import net.emustudio.plugins.cpu.intel8080.api.Context8080;

import java.util.Optional;

/**
* ZX Spectrum bus.
* <p>
Expand Down

0 comments on commit 6a803e7

Please sign in to comment.