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.model.wires.PinUsage;
17 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
18 import net.mograsim.logic.model.serializing.IdentifierGetter;
19 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
20 import net.mograsim.machine.MainMemoryDefinition;
21 import net.mograsim.machine.standard.memory.WordAddressableMemoryComponent;
22 import net.mograsim.preferences.Preferences;
24 public class GUIMemoryWA extends GUIComponent
26 private final MainMemoryDefinition definition;
27 private final Pin addrPin, dataPin, rWPin;
28 private WordAddressableMemoryComponent memory;
29 private final static int width = 100, height = 300;
31 private final static String addrKey = "addrBits", cellWidthKey = "cellWidth", minAddrKey = "minAddr", maxAddrKey = "maxAddr";
33 public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
36 this.definition = definition;
37 setSize(width, height);
38 //TODO check the usages
39 addPin(addrPin = new Pin(this, "A", definition.getMemoryAddressBits(), PinUsage.INPUT, 0, 10));
40 addPin(dataPin = new Pin(this, "D", definition.getCellWidth(), PinUsage.TRISTATE, 0, 30));
41 addPin(rWPin = new Pin(this, "RW", 1, PinUsage.INPUT, 0, 50));
44 public Pin getAddressPin()
49 public Pin getDataPin()
54 public Pin getReadWritePin()
59 public void setLogicModelBinding(WordAddressableMemoryComponent memory)
64 public MainMemoryDefinition getDefinition()
69 public WordAddressableMemoryComponent getMemory()
75 public void render(GeneralGC gc, Rectangle visibleRegion)
77 // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
78 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
79 if (foreground != null)
80 gc.setForeground(foreground);
81 gc.drawRectangle(getPosX(), getPosY(), width, height);
82 Font oldFont = gc.getFont();
83 Font labelFont = new Font(oldFont.getName(), 24, oldFont.getStyle());
84 gc.setFont(labelFont);
86 Point textExtent = gc.textExtent(label);
87 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
88 if (textColor != null)
89 gc.setForeground(textColor);
90 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
95 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
97 JsonObject o = new JsonObject();
98 o.addProperty(addrKey, definition.getMemoryAddressBits());
99 o.addProperty(cellWidthKey, definition.getCellWidth());
100 o.addProperty(maxAddrKey, definition.getMaximalAddress());
101 o.addProperty(minAddrKey, definition.getMinimalAddress());
107 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
108 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
110 JsonObject o = (JsonObject) p;
111 int addressBits = o.get(addrKey).getAsInt();
112 int cellWidth = o.get(cellWidthKey).getAsInt();
113 long maxAddr = o.get(maxAddrKey).getAsLong();
114 long minAddr = o.get(minAddrKey).getAsLong();
115 return new GUIMemoryWA(m, MainMemoryDefinition.create(addressBits, cellWidth, minAddr, maxAddr), n);