diff --git a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/Components.java b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/Components.java index b0711d3c..a88e73b9 100644 --- a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/Components.java +++ b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/Components.java @@ -5,8 +5,8 @@ import com.cburch.logisim.tools.AddTool; import com.cburch.logisim.tools.Library; -import org.cdm.logisim.emulator.cdm16.Cdm16ProcessorComponent; -import org.cdm.logisim.emulator.cdm16e.Cdm16eProcessorComponent; +import org.cdm.logisim.emulator.cdm16.Cdm16Component; +import org.cdm.logisim.emulator.cdm16e.Cdm16eComponent; /** * The library of components that the user can access. @@ -27,8 +27,8 @@ public class Components extends Library { */ public Components() { tools = Arrays.asList( - new AddTool(new Cdm16ProcessorComponent()), - new AddTool(new Cdm16eProcessorComponent()) + new AddTool(new Cdm16Component()), + new AddTool(new Cdm16eComponent()) ); } diff --git a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/ProcessorClockState.java b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/ProcessorClockState.java new file mode 100644 index 00000000..9eddf262 --- /dev/null +++ b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/ProcessorClockState.java @@ -0,0 +1,21 @@ +package org.cdm.logisim.emulator; + +import com.cburch.logisim.data.Value; + +public class ProcessorClockState { + private Value lastClock = Value.FALSE; + private Value currentClock = Value.FALSE; + + public void update(Value newClock) { + lastClock = currentClock; + currentClock = newClock; + } + + public boolean isClockRising() { + return lastClock.equals(Value.FALSE) && currentClock.equals(Value.TRUE); + } + + public boolean isClockFalling() { + return lastClock.equals(Value.TRUE) && currentClock.equals(Value.FALSE); + } +} diff --git a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/ProcessorComponentData.java b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/ProcessorComponentData.java similarity index 51% rename from logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/ProcessorComponentData.java rename to logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/ProcessorComponentData.java index 5faf217b..7a6f7af3 100644 --- a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/ProcessorComponentData.java +++ b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/ProcessorComponentData.java @@ -1,20 +1,20 @@ -package org.cdm.logisim.emulator.cdm16; +package org.cdm.logisim.emulator; import com.cburch.logisim.instance.InstanceData; -import org.cdm.cocoemu.components.processors.cdm16.Cdm16; +import org.cdm.cocoemu.core.Component; -public class ProcessorComponentData implements InstanceData { +public class ProcessorComponentData implements InstanceData { - private final Cdm16 processor; + private final T processorComponent; private final ProcessorClockState processorClockState; - public ProcessorComponentData(Cdm16 processor) { - this.processor = processor; + public ProcessorComponentData(T processorComponent) { + this.processorComponent = processorComponent; this.processorClockState = new ProcessorClockState(); } - public Cdm16 getProcessor() { - return processor; + public T getProcessorComponent() { + return processorComponent; } public ProcessorClockState getProcessorClockState() { @@ -24,7 +24,7 @@ public ProcessorClockState getProcessorClockState() { @Override public Object clone() { try { - return (ProcessorComponentData) super.clone(); + return super.clone(); } catch (CloneNotSupportedException var2) { return null; } diff --git a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/Cdm16ProcessorComponent.java b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/Cdm16Component.java similarity index 88% rename from logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/Cdm16ProcessorComponent.java rename to logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/Cdm16Component.java index 33fe6741..e6b6e396 100644 --- a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/Cdm16ProcessorComponent.java +++ b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/Cdm16Component.java @@ -8,13 +8,15 @@ import com.cburch.logisim.util.StringGetter; import com.cburch.logisim.util.StringUtil; import org.cdm.cocoemu.components.processors.cdm16.Cdm16; +import org.cdm.logisim.emulator.ProcessorClockState; +import org.cdm.logisim.emulator.ProcessorComponentData; import java.awt.*; -public class Cdm16ProcessorComponent extends InstanceFactory { +public class Cdm16Component extends InstanceFactory { private static final LocaleManager source = new LocaleManager("resources/logisim", "std"); - public Cdm16ProcessorComponent() { + public Cdm16Component() { super("CdM-16"); setOffsetBounds(Bounds.create(0, 0, 120, 120)); @@ -150,31 +152,24 @@ public void paintInstance(InstancePainter painter) { @Override public void propagate(InstanceState state) { - ProcessorComponentData componentData = (ProcessorComponentData) state.getData(); + ProcessorComponentData componentData = (ProcessorComponentData) state.getData(); if (componentData == null) { - componentData = new ProcessorComponentData(new Cdm16()); + componentData = new ProcessorComponentData<>(new Cdm16()); state.setData(componentData); } ProcessorClockState clockState = componentData.getProcessorClockState(); - Object irqTriggerType = state.getAttributeValue(StdAttr.TRIGGER); - boolean irqTriggered = clockState.updateClock(state.getPort(Ports.IRQ), irqTriggerType, ProcessorClockState.ClockType.IRQ); + clockState.update(state.getPort(Ports.CLK)); - Object excTriggerType = state.getAttributeValue(StdAttr.TRIGGER); - boolean excTriggered = clockState.updateClock(state.getPort(Ports.EXC), excTriggerType, ProcessorClockState.ClockType.EXC); - - Object clkTriggerType = state.getAttributeValue(StdAttr.TRIGGER); - boolean clkTriggered = clockState.updateClock(state.getPort(Ports.CLK), clkTriggerType, ProcessorClockState.ClockType.CLK); - - Cdm16 processor = componentData.getProcessor(); + Cdm16 processor = componentData.getProcessorComponent(); Cdm16LogisimAdapter.transferStateToProcessor(state, processor); - if (clkTriggered) { + if (clockState.isClockRising()) { processor.clockRising(); - } else if (clockState.checkClockFalling(state.getPort(Ports.CLK))) { + } else if (clockState.isClockFalling()) { processor.clockFalling(); } diff --git a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/ProcessorClockState.java b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/ProcessorClockState.java deleted file mode 100644 index 3bd84505..00000000 --- a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16/ProcessorClockState.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.cdm.logisim.emulator.cdm16; - -import com.cburch.logisim.data.Value; -import com.cburch.logisim.instance.InstanceData; -import com.cburch.logisim.instance.StdAttr; - -public class ProcessorClockState { - private Value irqLastClock; - private Value excLastClock; - private Value clkLastClock; - private Value clkBeforeLastClock = Value.FALSE; - - public ProcessorClockState() { - this.irqLastClock = Value.FALSE; - this.excLastClock = Value.FALSE; - this.clkLastClock = Value.FALSE; - } - - public boolean updateClock(Value newClock, Object trigger, ClockType type) { - Value oldClock = null; - switch (type) { - case IRQ: { - oldClock = this.irqLastClock; - this.irqLastClock = newClock; - break; - } - case EXC: { - oldClock = this.excLastClock; - this.excLastClock = newClock; - break; - } - case CLK: { - oldClock = this.clkLastClock; - this.clkBeforeLastClock = this.clkLastClock; - this.clkLastClock = newClock; - break; - } - } - - if (trigger == null) { - return oldClock == Value.FALSE && newClock == Value.TRUE; - } - - if (trigger == StdAttr.TRIG_FALLING) { - return oldClock == Value.TRUE && newClock == Value.FALSE; - } else if (trigger == StdAttr.TRIG_HIGH) { - return newClock == Value.TRUE; - } else if (trigger == StdAttr.TRIG_LOW) { - return newClock == Value.FALSE; - } else { - return oldClock == Value.FALSE && newClock == Value.TRUE; - } - } - - public boolean checkClockFalling(Value newClock) { - return clkBeforeLastClock == Value.TRUE && newClock == Value.FALSE; - } - - public enum ClockType { - EXC, CLK, IRQ - } -} \ No newline at end of file diff --git a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16e/Cdm16eProcessorComponent.java b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16e/Cdm16eComponent.java similarity index 87% rename from logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16e/Cdm16eProcessorComponent.java rename to logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16e/Cdm16eComponent.java index 9e4c8a13..d3b570a9 100644 --- a/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16e/Cdm16eProcessorComponent.java +++ b/logisim/logisim-cdm-emulator/src/main/java/org/cdm/logisim/emulator/cdm16e/Cdm16eComponent.java @@ -2,21 +2,24 @@ import com.cburch.logisim.data.Bounds; import com.cburch.logisim.data.Direction; -import com.cburch.logisim.instance.*; +import com.cburch.logisim.instance.InstanceFactory; +import com.cburch.logisim.instance.InstancePainter; +import com.cburch.logisim.instance.InstanceState; +import com.cburch.logisim.instance.Port; import com.cburch.logisim.util.GraphicsUtil; import com.cburch.logisim.util.LocaleManager; import com.cburch.logisim.util.StringGetter; import com.cburch.logisim.util.StringUtil; import org.cdm.cocoemu.components.processors.cdm16e.Cdm16e; -import org.cdm.logisim.emulator.cdm16.ProcessorClockState; -import org.cdm.logisim.emulator.cdm16.ProcessorComponentData; +import org.cdm.logisim.emulator.ProcessorClockState; +import org.cdm.logisim.emulator.ProcessorComponentData; import java.awt.*; -public class Cdm16eProcessorComponent extends InstanceFactory { +public class Cdm16eComponent extends InstanceFactory { private static final LocaleManager source = new LocaleManager("resources/logisim", "std"); - public Cdm16eProcessorComponent() { + public Cdm16eComponent() { super("CdM-16e"); setOffsetBounds(Bounds.create(0, 0, 120, 120)); @@ -164,31 +167,24 @@ public void paintInstance(InstancePainter painter) { @Override public void propagate(InstanceState state) { - ProcessorComponentData componentData = (ProcessorComponentData) state.getData(); + ProcessorComponentData componentData = (ProcessorComponentData) state.getData(); if (componentData == null) { - componentData = new ProcessorComponentData(new Cdm16e()); + componentData = new ProcessorComponentData<>(new Cdm16e()); state.setData(componentData); } ProcessorClockState clockState = componentData.getProcessorClockState(); - Object irqTriggerType = state.getAttributeValue(StdAttr.TRIGGER); - boolean irqTriggered = clockState.updateClock(state.getPort(Ports.IRQ), irqTriggerType, ProcessorClockState.ClockType.IRQ); + clockState.update(state.getPort(Ports.CLK)); - Object excTriggerType = state.getAttributeValue(StdAttr.TRIGGER); - boolean excTriggered = clockState.updateClock(state.getPort(Ports.EXC), excTriggerType, ProcessorClockState.ClockType.EXC); - - Object clkTriggerType = state.getAttributeValue(StdAttr.TRIGGER); - boolean clkTriggered = clockState.updateClock(state.getPort(Ports.CLK), clkTriggerType, ProcessorClockState.ClockType.CLK); - - Cdm16e processor = (Cdm16e) componentData.getProcessor(); + Cdm16e processor = componentData.getProcessorComponent(); Cdm16eLogisimAdapter.transferStateToProcessor(state, processor); - if (clkTriggered) { + if (clockState.isClockRising()) { processor.clockRising(); - } else if (clockState.checkClockFalling(state.getPort(Ports.CLK))) { + } else if (clockState.isClockFalling()) { processor.clockFalling(); }