1 package net.mograsim.machine.standard.memory;
3 import org.eclipse.swt.graphics.Color;
5 import com.google.gson.Gson;
6 import com.google.gson.JsonElement;
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.DefaultMainMemoryDefinition;
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 public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
34 this.definition = definition;
35 setSize(width, height);
36 addPin(addrPin = new Pin(this, "A", definition.getMemoryAddressBits(), 0, 10));
37 addPin(dataPin = new Pin(this, "D", definition.getCellWidth(), 0, 30));
38 addPin(rWPin = new Pin(this, "RW", 1, 0, 50));
41 public Pin getAddressPin()
46 public Pin getDataPin()
51 public Pin getReadWritePin()
56 public void setLogicModelBinding(WordAddressableMemoryComponent memory)
61 public MainMemoryDefinition getDefinition()
66 public WordAddressableMemoryComponent getMemory()
72 public void render(GeneralGC gc, Rectangle visibleRegion)
74 // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
75 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
76 if (foreground != null)
77 gc.setForeground(foreground);
78 gc.drawRectangle(getPosX(), getPosY(), width, height);
79 Font oldFont = gc.getFont();
80 Font labelFont = new Font(oldFont.getName(), 24, oldFont.getStyle());
81 gc.setFont(labelFont);
83 Point textExtent = gc.textExtent(label);
84 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
85 if (textColor != null)
86 gc.setForeground(textColor);
87 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
92 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
94 return new Gson().toJsonTree(new DefaultMainMemoryDefinition(definition));
99 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
100 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
102 return new GUIMemoryWA(m, new Gson().fromJson(p, DefaultMainMemoryDefinition.class), n);