1632c8eb739ec2fded080d903dc991684844f490
[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.Gson;
6 import com.google.gson.JsonElement;
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.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;
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         public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
32         {
33                 super(model, 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));
39         }
40
41         public Pin getAddressPin()
42         {
43                 return addrPin;
44         }
45
46         public Pin getDataPin()
47         {
48                 return dataPin;
49         }
50
51         public Pin getReadWritePin()
52         {
53                 return rWPin;
54         }
55         
56         public void setLogicModelBinding(WordAddressableMemoryComponent memory)
57         {
58                 this.memory = memory;
59         }
60         
61         public MainMemoryDefinition getDefinition()
62         {
63                 return definition;
64         }
65         
66         public WordAddressableMemoryComponent getMemory()
67         {
68                 return memory;
69         }
70
71         @Override
72         public void render(GeneralGC gc, Rectangle visibleRegion)
73         {
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);
82                 String label = "RAM";
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);
88                 gc.setFont(oldFont);
89         }
90
91         @Override
92         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
93         {
94                 return new Gson().toJsonTree(new DefaultMainMemoryDefinition(definition));
95         }
96
97         static
98         {
99                 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
100                 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
101                 {
102                         return new GUIMemoryWA(m, new Gson().fromJson(p, DefaultMainMemoryDefinition.class), n);
103                 });
104         }
105 }