52229267b28be309c8d09cea360a02a681fc73d5
[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 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;
20
21 public class GUIMemoryWA extends GUIComponent
22 {
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;
27
28         public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
29         {
30                 super(model, 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));
37         }
38
39         public Pin getAddressPin()
40         {
41                 return addrPin;
42         }
43
44         public Pin getDataPin()
45         {
46                 return dataPin;
47         }
48
49         public Pin getReadWritePin()
50         {
51                 return rWPin;
52         }
53
54         public void setLogicModelBinding(WordAddressableMemoryComponent memory)
55         {
56                 this.memory = memory;
57         }
58
59         public MainMemoryDefinition getDefinition()
60         {
61                 return definition;
62         }
63
64         public WordAddressableMemoryComponent getMemory()
65         {
66                 return memory;
67         }
68
69         @Override
70         public void render(GeneralGC gc, Rectangle visibleRegion)
71         {
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);
80                 String label = "RAM";
81                 Point textExtent = gc.textExtent(label);
82                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
83                 if(textColor != null)
84                         gc.setForeground(textColor);
85                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
86                 gc.setFont(oldFont);
87         }
88
89         @Override
90         public GUIMemoryWAParams getParamsForSerializing(IdentifierGetter idGetter)
91         {
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();
97                 return params;
98         }
99
100         static
101         {
102                 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
103                 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
104                 {
105                         GUIMemoryWAParams params = JsonHandler.fromJsonTree(p, GUIMemoryWAParams.class);
106                         return new GUIMemoryWA(m, MainMemoryDefinition.create(params.addrBits, params.cellWidth, params.minAddr, params.maxAddr), n);
107                 });
108         }
109
110         public static class GUIMemoryWAParams
111         {
112                 public int      addrBits;
113                 public int      cellWidth;
114                 public long     minAddr;
115                 public long     maxAddr;
116         }
117 }