1 package net.mograsim.logic.model.model.components.atomic;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonObject;
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.model.model.ViewModelModifiable;
13 import net.mograsim.logic.model.model.components.GUIComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
16 import net.mograsim.logic.model.modeladapter.componentadapters.WordAddressableMemoryAdapter;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.preferences.Preferences;
21 public class GUIMemoryWA extends GUIComponent
23 private final static String paramAddr = "addrBits", paramWordWidth = "wordWidth", paramMaxAddr = "maxAddr", paramMinAddr = "minAddr";
24 private final int addressBits, wordWidth;
25 public final long maximalAddress, minimalAddress;
26 private final Pin addrPin, dataPin, rWPin;
27 private final static int width = 100, height = 300;
29 public GUIMemoryWA(ViewModelModifiable model, int addressBits, int wordWidth, long maximalAddress, long minimalAddress, String name)
32 this.addressBits = addressBits;
33 this.wordWidth = wordWidth;
34 this.maximalAddress = maximalAddress;
35 this.minimalAddress = minimalAddress;
36 setSize(width, height);
37 addPin(addrPin = new Pin(this, "A", addressBits, 0, 10));
38 addPin(dataPin = new Pin(this, "D", wordWidth, 0, 30));
39 addPin(rWPin = new Pin(this, "RW", 1, 0, 50));
42 public Pin getAddressPin()
47 public Pin getDataPin()
52 public Pin getReadWritePin()
58 public void render(GeneralGC gc, Rectangle visibleRegion)
60 // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
61 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
62 if (foreground != null)
63 gc.setForeground(foreground);
64 gc.drawRectangle(getPosX(), getPosY(), width, height);
65 Font oldFont = gc.getFont();
66 Font labelFont = new Font(oldFont.getName(), 24, oldFont.getStyle());
67 gc.setFont(labelFont);
69 Point textExtent = gc.textExtent(label);
70 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
71 if (textColor != null)
72 gc.setForeground(textColor);
73 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
78 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
80 JsonObject obj = new JsonObject();
81 obj.addProperty(paramAddr, addressBits);
82 obj.addProperty(paramWordWidth, wordWidth);
83 obj.addProperty(paramMaxAddr, maximalAddress);
84 obj.addProperty(paramMinAddr, minimalAddress);
90 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
91 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
93 JsonObject obj = p.getAsJsonObject();
94 return new GUIMemoryWA(m, obj.get(paramAddr).getAsInt(), obj.get(paramWordWidth).getAsInt(), obj.get(paramMaxAddr).getAsLong(),
95 obj.get(paramMinAddr).getAsLong(), n);