Skip to content

Commit

Permalink
[#314] 8080,z80: fix frequency counting
Browse files Browse the repository at this point in the history
  • Loading branch information
vbmacher committed May 18, 2023
1 parent 4da1871 commit 87de500
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class CpuImpl extends AbstractCPU {
private StatusPanel statusPanel;
private Disassembler disassembler;

private FrequencyCalculator frequencyCalculator;
private final FrequencyCalculator frequencyCalculator = new FrequencyCalculator();

public CpuImpl(long pluginID, ApplicationApi applicationApi, PluginSettings settings) {
super(pluginID, applicationApi, settings);
Expand Down Expand Up @@ -93,12 +93,17 @@ public void initialize() throws PluginInitializationException {
engine = initializer.getEngine();
context.setCpu(engine);
disassembler = initializer.getDisassembler();
context.addPassedCyclesListener(frequencyCalculator);
statusPanel = new StatusPanel(this, context, initializer.shouldDumpInstructions());
frequencyCalculator = new FrequencyCalculator(engine::fireFrequencyChanged);
}

public FrequencyCalculator getFrequencyCalculator() {
return frequencyCalculator;
}

@Override
protected void destroyInternal() {
context.removePassedCyclesListener(frequencyCalculator);
frequencyCalculator.stop();
frequencyCalculator.close();
context.clearDevices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@
import net.emustudio.emulib.runtime.helpers.SleepUtils;
import net.emustudio.plugins.cpu.intel8080.api.CpuEngine;
import net.emustudio.plugins.cpu.intel8080.api.DispatchListener;
import net.emustudio.plugins.cpu.intel8080.api.FrequencyChangedListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import static net.emustudio.plugins.cpu.intel8080.DispatchTables.DISPATCH_TABLE;

Expand All @@ -58,7 +55,6 @@ public class EmulatorEngine implements CpuEngine {
);
private final MemoryContext<Byte> memory;
private final Context8080Impl context;
private final List<FrequencyChangedListener> frequencyChangedListeners = new CopyOnWriteArrayList<>();
public boolean INTE = false; // enabling / disabling of interrupts
public int PC = 0; // program counter
public int SP = 0; // stack pointer
Expand All @@ -83,17 +79,6 @@ public void setDispatchListener(DispatchListener dispatchListener) {
this.dispatchListener = dispatchListener;
}

public void addFrequencyChangedListener(FrequencyChangedListener listener) {
frequencyChangedListeners.add(listener);
}

@Override
public void fireFrequencyChanged(float newFrequency) {
for (FrequencyChangedListener listener : frequencyChangedListeners) {
listener.frequencyChanged(newFrequency);
}
}

public void reset(int startPos) {
Arrays.fill(regs, 0);
SP = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@

public interface CpuEngine {

void fireFrequencyChanged(float newFrequency);

void setDispatchListener(DispatchListener dispatchListener);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void internalStateChanged() {
}

});
cpu.getEngine().addFrequencyChangedListener(newFrequency -> lblFrequency.setText(String.format("%.2f kHz", newFrequency)));
cpu.getFrequencyCalculator().addListener(f -> lblFrequency.setText(String.format("%.2f kHz", f)));
spnFrequency.addChangeListener(e -> {
int i = (Integer) spnFrequency.getModel().getValue();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class CpuImpl extends AbstractCPU {
private Disassembler disassembler;
private EmulatorEngine engine;

private FrequencyCalculator frequencyCalculator;
private final FrequencyCalculator frequencyCalculator = new FrequencyCalculator();

public CpuImpl(long pluginID, ApplicationApi applicationApi, PluginSettings settings) {
super(pluginID, applicationApi, settings);
Expand All @@ -68,9 +68,6 @@ public CpuImpl(long pluginID, ApplicationApi applicationApi, PluginSettings sett
);
}
context.setCPUFrequency(settings.getInt("frequency_khz", ContextZ80Impl.DEFAULT_FREQUENCY_KHZ));

context.setCPUFrequency(settings.getInt("frequency_khz", ContextZ80Impl.DEFAULT_FREQUENCY_KHZ));

initializer = new InitializerZ80(
this, pluginID, applicationApi.getContextPool(), settings, context
);
Expand All @@ -96,7 +93,7 @@ public void initialize() throws PluginInitializationException {
disassembler = initializer.getDisassembler();
engine = initializer.getEngine();
context.setEngine(engine);
frequencyCalculator = new FrequencyCalculator(engine::fireFrequencyChanged);
context.addPassedCyclesListener(frequencyCalculator);
statusPanel = new StatusPanel(this, context, initializer.shouldDumpInstructions());
}

Expand Down Expand Up @@ -149,6 +146,7 @@ public Disassembler getDisassembler() {

@Override
protected void destroyInternal() {
context.removePassedCyclesListener(frequencyCalculator);
frequencyCalculator.stop();
frequencyCalculator.close();
context.clearDevices();
Expand Down Expand Up @@ -178,4 +176,8 @@ private Optional<ResourceBundle> getResourceBundle() {
return Optional.empty();
}
}

public FrequencyCalculator getFrequencyCalculator() {
return frequencyCalculator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import net.emustudio.emulib.runtime.helpers.SleepUtils;
import net.emustudio.plugins.cpu.intel8080.api.CpuEngine;
import net.emustudio.plugins.cpu.intel8080.api.DispatchListener;
import net.emustudio.plugins.cpu.intel8080.api.FrequencyChangedListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -66,7 +65,6 @@ public class EmulatorEngine implements CpuEngine {
private final ContextZ80Impl context;
private final TimedEventsProcessor tep;
private final MemoryContext<Byte> memory;
private final List<FrequencyChangedListener> frequencyChangedListeners = new CopyOnWriteArrayList<>();
private final AtomicLong cyclesExecutedPerTimeSlice = new AtomicLong(0);

public final int[] regs = new int[8];
Expand Down Expand Up @@ -141,17 +139,6 @@ public void addExecutedCyclesPerTimeSlice(long tstates) {
cyclesExecutedPerTimeSlice.addAndGet(tstates);
}

public void addFrequencyChangedListener(FrequencyChangedListener listener) {
frequencyChangedListeners.add(listener);
}

@Override
public void fireFrequencyChanged(float newFrequency) {
for (FrequencyChangedListener listener : frequencyChangedListeners) {
listener.frequencyChanged(newFrequency);
}
}

public void requestMaskableInterrupt(byte[] data) {
if (currentRunState == RunState.STATE_RUNNING) {
pendingInterrupts.add(data);
Expand Down Expand Up @@ -338,11 +325,11 @@ private void doInterrupt() throws Throwable {
if (dataBus != null && dataBus.length > 0) {
lastOpcode = dataBus[0] & 0xFF; // TODO: if dataBus had more bytes, they're ignored (except call).
if (lastOpcode == 0xCD) { /* CALL */
advanceCycles(4+7); // fetch
advanceCycles(4 + 7); // fetch
SP = (SP - 2) & 0xFFFF;
memory.write(SP, (byte)(PC & 0xFF));
memory.write(SP, (byte) (PC & 0xFF));
advanceCycles(3);
memory.write((SP + 1) & 0xFFFF, (byte)(PC >>> 8));
memory.write((SP + 1) & 0xFFFF, (byte) (PC >>> 8));

PC = ((dataBus[2] & 0xFF) << 8) | (dataBus[1] & 0xFF);
memptr = PC;
Expand Down Expand Up @@ -918,18 +905,18 @@ void I_PUSH_AF() {
void I_PUSH_IX() {
// pc:5,sp-1:3,sp-2:3
SP = (SP - 2) & 0xFFFF;
memory.write((SP + 1) & 0xFFFF, (byte)(IX >>> 8));
memory.write((SP + 1) & 0xFFFF, (byte) (IX >>> 8));
advanceCycles(3);
memory.write(SP, (byte)(IX & 0xFF));
memory.write(SP, (byte) (IX & 0xFF));
advanceCycles(3);
}

void I_PUSH_IY() {
// pc:5,sp-1:3,sp-2:3
SP = (SP - 2) & 0xFFFF;
memory.write((SP + 1) & 0xFFFF, (byte)(IY & 0xFF));
memory.write((SP + 1) & 0xFFFF, (byte) (IY & 0xFF));
advanceCycles(3);
memory.write(SP, (byte)(IY >>> 8));
memory.write(SP, (byte) (IY >>> 8));
advanceCycles(3);
}

Expand Down Expand Up @@ -2827,10 +2814,10 @@ void I_LD_REF_NN_XY(int xy) {
PC = (PC + 2) & 0xFFFF;
advanceCycles(3);

memory.write(address, (byte)(xy & 0xFF));
memory.write(address, (byte) (xy & 0xFF));
advanceCycles(3);
memptr = (address + 1) & 0xFFFF;
memory.write(memptr, (byte)(xy >>> 8));
memory.write(memptr, (byte) (xy >>> 8));
advanceCycles(3);
}

Expand Down Expand Up @@ -3833,15 +3820,15 @@ void I_SET_N_R() {
void I_LD_IX_NN() {
IX = memory.read(PC) & 0xFF;
advanceCycles(3);
IX = ((memory.read((PC +1) & 0xFFFF) << 8) | IX) & 0xFFFF;
IX = ((memory.read((PC + 1) & 0xFFFF) << 8) | IX) & 0xFFFF;
PC = (PC + 2) & 0xFFFF;
advanceCycles(3);
}

void I_LD_IY_NN() {
IY = memory.read(PC) & 0xFF;
advanceCycles(3);
IY = ((memory.read((PC +1) & 0xFFFF) << 8) | IY) & 0xFFFF;
IY = ((memory.read((PC + 1) & 0xFFFF) << 8) | IY) & 0xFFFF;
PC = (PC + 2) & 0xFFFF;
advanceCycles(3);
}
Expand Down Expand Up @@ -3904,9 +3891,9 @@ void I_EX_REF_SP_IX() {
IX = tmp;
memptr = IX;

memory.write(SP, (byte)(tmp1 & 0xFF));
memory.write(SP, (byte) (tmp1 & 0xFF));
advanceCycles(3);
memory.write((SP + 1) & 0xFFFF, (byte)(tmp1 >>> 8));
memory.write((SP + 1) & 0xFFFF, (byte) (tmp1 >>> 8));
advanceCycles(6);
}

Expand All @@ -3920,9 +3907,9 @@ void I_EX_REF_SP_IY() {
IY = tmp;
memptr = IY;

memory.write(SP, (byte)(tmp1 & 0xFF));
memory.write(SP, (byte) (tmp1 & 0xFF));
advanceCycles(3);
memory.write((SP + 1) & 0xFFFF, (byte)(tmp1 >>> 8));
memory.write((SP + 1) & 0xFFFF, (byte) (tmp1 >>> 8));
advanceCycles(6);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void internalStateChanged() {
}

});
cpu.getEngine().addFrequencyChangedListener(newFrequency -> lblFrequency.setText(String.format("%.2f kHz", newFrequency)));
cpu.getFrequencyCalculator().addListener(f -> lblFrequency.setText(String.format("%.2f kHz", f)));
spnFrequency.addChangeListener(e -> {
int i = (Integer) spnFrequency.getModel().getValue();
try {
Expand Down

0 comments on commit 87de500

Please sign in to comment.