Pins now have a PinUsage
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / standard / memory / GUIMemoryWA.java
1 package net.mograsim.machine.standard.memory;
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.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;
23
24 public class GUIMemoryWA extends GUIComponent
25 {
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;
30         
31         private final static String addrKey = "addrBits", cellWidthKey = "cellWidth", minAddrKey = "minAddr", maxAddrKey = "maxAddr";
32
33         public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
34         {
35                 super(model, 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));
42         }
43
44         public Pin getAddressPin()
45         {
46                 return addrPin;
47         }
48
49         public Pin getDataPin()
50         {
51                 return dataPin;
52         }
53
54         public Pin getReadWritePin()
55         {
56                 return rWPin;
57         }
58         
59         public void setLogicModelBinding(WordAddressableMemoryComponent memory)
60         {
61                 this.memory = memory;
62         }
63         
64         public MainMemoryDefinition getDefinition()
65         {
66                 return definition;
67         }
68         
69         public WordAddressableMemoryComponent getMemory()
70         {
71                 return memory;
72         }
73
74         @Override
75         public void render(GeneralGC gc, Rectangle visibleRegion)
76         {
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);
85                 String label = "RAM";
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);
91                 gc.setFont(oldFont);
92         }
93
94         @Override
95         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
96         {
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());
102                 return o;
103         }
104
105         static
106         {
107                 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
108                 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
109                 {
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);
116                 });
117         }
118 }