Skip to content

Commit

Permalink
[logisim-cdm-emulator] Generalize and refactor utility classes
Browse files Browse the repository at this point in the history
+ Generalize `ProcessorComponentData`
+ Reimplement `ProcessorClockState`
  • Loading branch information
Intelix8996 committed Jul 21, 2024
1 parent 1579dc0 commit 9c32e27
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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())
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<T extends Component> 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() {
Expand All @@ -24,7 +24,7 @@ public ProcessorClockState getProcessorClockState() {
@Override
public Object clone() {
try {
return (ProcessorComponentData) super.clone();
return super.clone();
} catch (CloneNotSupportedException var2) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -150,31 +152,24 @@ public void paintInstance(InstancePainter painter) {

@Override
public void propagate(InstanceState state) {
ProcessorComponentData componentData = (ProcessorComponentData) state.getData();
ProcessorComponentData<Cdm16> componentData = (ProcessorComponentData<Cdm16>) 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();
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -164,31 +167,24 @@ public void paintInstance(InstancePainter painter) {

@Override
public void propagate(InstanceState state) {
ProcessorComponentData componentData = (ProcessorComponentData) state.getData();
ProcessorComponentData<Cdm16e> componentData = (ProcessorComponentData<Cdm16e>) 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();
}

Expand Down

0 comments on commit 9c32e27

Please sign in to comment.