1 package net.mograsim.machine.standard.memory;
3 import org.eclipse.swt.graphics.Color;
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
9 import net.mograsim.logic.model.model.ViewModelModifiable;
10 import net.mograsim.logic.model.model.components.GUIComponent;
11 import net.mograsim.logic.model.model.components.atomic.GUIAndGate;
12 import net.mograsim.logic.model.model.wires.Pin;
13 import net.mograsim.logic.model.model.wires.PinUsage;
14 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
15 import net.mograsim.logic.model.serializing.IdentifierGetter;
16 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
17 import net.mograsim.logic.model.util.JsonHandler;
18 import net.mograsim.machine.MainMemoryDefinition;
19 import net.mograsim.preferences.Preferences;
21 public class GUIMemoryWA extends GUIComponent
23 private final MainMemoryDefinition definition;
24 private final Pin addrPin, dataPin, rWPin;
25 private WordAddressableMemoryComponent memory;
26 private final static int width = 100, height = 300;
28 public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
31 this.definition = definition;
32 setSize(width, height);
33 //TODO check the usages
34 addPin(addrPin = new Pin(this, "A", definition.getMemoryAddressBits(), PinUsage.INPUT, 0, 10));
35 addPin(dataPin = new Pin(this, "D", definition.getCellWidth(), PinUsage.TRISTATE, 0, 30));
36 addPin(rWPin = new Pin(this, "RW", 1, PinUsage.INPUT, 0, 50));
39 public Pin getAddressPin()
44 public Pin getDataPin()
49 public Pin getReadWritePin()
54 public void setLogicModelBinding(WordAddressableMemoryComponent memory)
59 public MainMemoryDefinition getDefinition()
64 public WordAddressableMemoryComponent getMemory()
70 public void render(GeneralGC gc, Rectangle visibleRegion)
72 // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
73 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
74 if(foreground != null)
75 gc.setForeground(foreground);
76 gc.drawRectangle(getPosX(), getPosY(), width, height);
77 Font oldFont = gc.getFont();
78 Font labelFont = new Font(oldFont.getName(), 24, oldFont.getStyle());
79 gc.setFont(labelFont);
81 Point textExtent = gc.textExtent(label);
82 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
84 gc.setForeground(textColor);
85 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
90 public GUIMemoryWAParams getParamsForSerializing(IdentifierGetter idGetter)
92 GUIMemoryWAParams params = new GUIMemoryWAParams();
93 params.addrBits = definition.getMemoryAddressBits();
94 params.cellWidth = definition.getCellWidth();
95 params.minAddr = definition.getMinimalAddress();
96 params.maxAddr = definition.getMaximalAddress();
102 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
103 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
105 GUIMemoryWAParams params = JsonHandler.fromJsonTree(p, GUIMemoryWAParams.class);
106 return new GUIMemoryWA(m, MainMemoryDefinition.create(params.addrBits, params.cellWidth, params.minAddr, params.maxAddr), n);
110 public static class GUIMemoryWAParams
113 public int cellWidth;