From 9d891c8dfaf71b4668edd66a82c85c0cd51ecd84 Mon Sep 17 00:00:00 2001 From: Fabian Stemmler Date: Wed, 21 Aug 2019 16:40:03 +0200 Subject: [PATCH] Added GUIComponent for Word Addressable Memory Added GUIMemoryWA --- .../memory/WordAddressableMemory.java | 2 +- .../WordAddressableMemoryComponent.java | 2 - .../model/components/atomic/GUIMemoryWA.java | 98 +++++++++++++++++++ .../WordAddressableMemoryAdapter.java | 34 +++++++ 4 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIMemoryWA.java create mode 100644 net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/componentadapters/WordAddressableMemoryAdapter.java diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemory.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemory.java index fbb76c22..3e83832c 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemory.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemory.java @@ -26,7 +26,7 @@ public class WordAddressableMemory public void setCell(long address, BitVector b) { if (address < minimalAddress || address > maximalAddress) - throw new IndexOutOfBoundsException(String.format("Memory address out of bounds! Minimum: %d Maimum: %d Actual: %d", + throw new IndexOutOfBoundsException(String.format("Memory address out of bounds! Minimum: %d Maximum: %d Actual: %d", minimalAddress, maximalAddress, address)); long page = address / pageSize; int offset = (int) (address % pageSize); diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java index 821b1f36..33262717 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java @@ -57,7 +57,6 @@ public class WordAddressableMemoryComponent extends BasicComponent else { data.clearSignals(); - System.out.println(memory); memory.setCell(addressed, data.getValues()); } } @@ -73,5 +72,4 @@ public class WordAddressableMemoryComponent extends BasicComponent { return List.of(data); } - } \ No newline at end of file diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIMemoryWA.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIMemoryWA.java new file mode 100644 index 00000000..464a8b10 --- /dev/null +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIMemoryWA.java @@ -0,0 +1,98 @@ +package net.mograsim.logic.model.model.components.atomic; + +import org.eclipse.swt.graphics.Color; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import net.haspamelodica.swt.helper.gcs.GeneralGC; +import net.haspamelodica.swt.helper.swtobjectwrappers.Font; +import net.haspamelodica.swt.helper.swtobjectwrappers.Point; +import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; +import net.mograsim.logic.model.model.ViewModelModifiable; +import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.model.wires.Pin; +import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter; +import net.mograsim.logic.model.modeladapter.componentadapters.WordAddressableMemoryAdapter; +import net.mograsim.logic.model.serializing.IdentifierGetter; +import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator; +import net.mograsim.preferences.Preferences; + +public class GUIMemoryWA extends GUIComponent +{ + private final static String paramAddr = "addrBits", paramWordWidth = "wordWidth", paramMaxAddr = "maxAddr", paramMinAddr = "minAddr"; + private final int addressBits, wordWidth; + public final long maximalAddress, minimalAddress; + private final Pin addrPin, dataPin, rWPin; + private final static int width = 100, height = 300; + + public GUIMemoryWA(ViewModelModifiable model, int addressBits, int wordWidth, long maximalAddress, long minimalAddress, String name) + { + super(model, name); + this.addressBits = addressBits; + this.wordWidth = wordWidth; + this.maximalAddress = maximalAddress; + this.minimalAddress = minimalAddress; + setSize(width, height); + addPin(addrPin = new Pin(this, "A", addressBits, 0, 10)); + addPin(dataPin = new Pin(this, "D", wordWidth, 0, 30)); + addPin(rWPin = new Pin(this, "RW", 1, 0, 50)); + } + + public Pin getAddressPin() + { + return addrPin; + } + + public Pin getDataPin() + { + return dataPin; + } + + public Pin getReadWritePin() + { + return rWPin; + } + + @Override + public void render(GeneralGC gc, Rectangle visibleRegion) + { + // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead + Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground"); + if (foreground != null) + gc.setForeground(foreground); + gc.drawRectangle(getPosX(), getPosY(), width, height); + Font oldFont = gc.getFont(); + Font labelFont = new Font(oldFont.getName(), 24, oldFont.getStyle()); + gc.setFont(labelFont); + String label = "RAM"; + Point textExtent = gc.textExtent(label); + Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text"); + if (textColor != null) + gc.setForeground(textColor); + gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true); + gc.setFont(oldFont); + } + + @Override + public JsonElement getParamsForSerializing(IdentifierGetter idGetter) + { + JsonObject obj = new JsonObject(); + obj.addProperty(paramAddr, addressBits); + obj.addProperty(paramWordWidth, wordWidth); + obj.addProperty(paramMaxAddr, maximalAddress); + obj.addProperty(paramMinAddr, minimalAddress); + return obj; + } + + static + { + ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter()); + IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) -> + { + JsonObject obj = p.getAsJsonObject(); + return new GUIMemoryWA(m, obj.get(paramAddr).getAsInt(), obj.get(paramWordWidth).getAsInt(), obj.get(paramMaxAddr).getAsLong(), + obj.get(paramMinAddr).getAsLong(), n); + }); + } +} diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/componentadapters/WordAddressableMemoryAdapter.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/componentadapters/WordAddressableMemoryAdapter.java new file mode 100644 index 00000000..339c238b --- /dev/null +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/modeladapter/componentadapters/WordAddressableMemoryAdapter.java @@ -0,0 +1,34 @@ +package net.mograsim.logic.model.modeladapter.componentadapters; + +import java.util.Map; + +import net.mograsim.logic.core.components.memory.WordAddressableMemoryComponent; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.wires.Wire; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; +import net.mograsim.logic.model.model.components.atomic.GUIMemoryWA; +import net.mograsim.logic.model.model.wires.Pin; +import net.mograsim.logic.model.modeladapter.LogicModelParameters; + +public class WordAddressableMemoryAdapter implements ComponentAdapter +{ + + @Override + public Class getSupportedClass() + { + return GUIMemoryWA.class; + } + + @SuppressWarnings("unused") + @Override + public void createAndLinkComponent(Timeline timeline, LogicModelParameters params, GUIMemoryWA guiComponent, + Map logicWiresPerPin) + { + ReadWriteEnd data = logicWiresPerPin.get(guiComponent.getDataPin()).createReadWriteEnd(); + ReadEnd address = logicWiresPerPin.get(guiComponent.getAddressPin()).createReadOnlyEnd(); + ReadEnd mode = logicWiresPerPin.get(guiComponent.getReadWritePin()).createReadOnlyEnd(); + new WordAddressableMemoryComponent(timeline, 2, guiComponent.maximalAddress, guiComponent.minimalAddress, data, mode, address); + } + +} -- 2.17.1