464a8b1044edbbef2da05dc4761a4c898f816f28
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUIMemoryWA.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import com.google.gson.JsonElement;
6 import com.google.gson.JsonObject;
7
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;
20
21 public class GUIMemoryWA extends GUIComponent
22 {
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;
28
29         public GUIMemoryWA(ViewModelModifiable model, int addressBits, int wordWidth, long maximalAddress, long minimalAddress, String name)
30         {
31                 super(model, 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));
40         }
41
42         public Pin getAddressPin()
43         {
44                 return addrPin;
45         }
46
47         public Pin getDataPin()
48         {
49                 return dataPin;
50         }
51
52         public Pin getReadWritePin()
53         {
54                 return rWPin;
55         }
56
57         @Override
58         public void render(GeneralGC gc, Rectangle visibleRegion)
59         {
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);
68                 String label = "RAM";
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);
74                 gc.setFont(oldFont);
75         }
76
77         @Override
78         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
79         {
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);
85                 return obj;
86         }
87
88         static
89         {
90                 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
91                 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
92                 {
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);
96                 });
97         }
98 }