1 package net.mograsim.machine.standard.memory;
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.components.atomic.GUIAndGate;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.machine.MainMemoryDefinition;
20 import net.mograsim.machine.standard.memory.WordAddressableMemoryComponent;
21 import net.mograsim.preferences.Preferences;
23 public class GUIMemoryWA extends GUIComponent
25 private final MainMemoryDefinition definition;
26 private final Pin addrPin, dataPin, rWPin;
27 private WordAddressableMemoryComponent memory;
28 private final static int width = 100, height = 300;
30 private final static String addrKey = "addrBits", cellWidthKey = "cellWidth", minAddrKey = "minAddr", maxAddrKey = "maxAddr";
32 public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
35 this.definition = definition;
36 setSize(width, height);
37 addPin(addrPin = new Pin(this, "A", definition.getMemoryAddressBits(), 0, 10));
38 addPin(dataPin = new Pin(this, "D", definition.getCellWidth(), 0, 30));
39 addPin(rWPin = new Pin(this, "RW", 1, 0, 50));
42 public Pin getAddressPin()
47 public Pin getDataPin()
52 public Pin getReadWritePin()
57 public void setLogicModelBinding(WordAddressableMemoryComponent memory)
62 public MainMemoryDefinition getDefinition()
67 public WordAddressableMemoryComponent getMemory()
73 public void render(GeneralGC gc, Rectangle visibleRegion)
75 // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
76 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
77 if (foreground != null)
78 gc.setForeground(foreground);
79 gc.drawRectangle(getPosX(), getPosY(), width, height);
80 Font oldFont = gc.getFont();
81 Font labelFont = new Font(oldFont.getName(), 24, oldFont.getStyle());
82 gc.setFont(labelFont);
84 Point textExtent = gc.textExtent(label);
85 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
86 if (textColor != null)
87 gc.setForeground(textColor);
88 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
93 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
95 JsonObject o = new JsonObject();
96 o.addProperty(addrKey, definition.getMemoryAddressBits());
97 o.addProperty(cellWidthKey, definition.getCellWidth());
98 o.addProperty(maxAddrKey, definition.getMaximalAddress());
99 o.addProperty(minAddrKey, definition.getMinimalAddress());
105 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
106 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
108 JsonObject o = (JsonObject) p;
109 int addressBits = o.get(addrKey).getAsInt();
110 int cellWidth = o.get(cellWidthKey).getAsInt();
111 long maxAddr = o.get(maxAddrKey).getAsLong();
112 long minAddr = o.get(minAddrKey).getAsLong();
113 return new GUIMemoryWA(m, MainMemoryDefinition.create(addressBits, cellWidth, minAddr, maxAddr), n);