Fleshed out Memory and Microprogramming interfaces
[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.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.machine.MainMemoryDefinition;
20 import net.mograsim.machine.standard.memory.WordAddressableMemoryComponent;
21 import net.mograsim.preferences.Preferences;
22
23 public class GUIMemoryWA extends GUIComponent
24 {
25         private final MainMemoryDefinition definition;
26         private final Pin addrPin, dataPin, rWPin;
27         private WordAddressableMemoryComponent memory;
28         private final static int width = 100, height = 300;
29         
30         private final static String addrKey = "addrBits", cellWidthKey = "cellWidth", minAddrKey = "minAddr", maxAddrKey = "maxAddr";
31
32         public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
33         {
34                 super(model, name);
35                 this.definition = definition;
36                 setSize(width, height);
37                 addPin(addrPin = new Pin(this, "A", definition.getMemoryAddressBits(), 0, 10));
38                 addPin(dataPin = new Pin(this, "D", definition.getCellWidth(), 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         public void setLogicModelBinding(WordAddressableMemoryComponent memory)
58         {
59                 this.memory = memory;
60         }
61         
62         public MainMemoryDefinition getDefinition()
63         {
64                 return definition;
65         }
66         
67         public WordAddressableMemoryComponent getMemory()
68         {
69                 return memory;
70         }
71
72         @Override
73         public void render(GeneralGC gc, Rectangle visibleRegion)
74         {
75                 // TODO This is copied from SimpleRectangularGUIGate; do this via delegation instead
76                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
77                 if (foreground != null)
78                         gc.setForeground(foreground);
79                 gc.drawRectangle(getPosX(), getPosY(), width, height);
80                 Font oldFont = gc.getFont();
81                 Font labelFont = new Font(oldFont.getName(), 24, oldFont.getStyle());
82                 gc.setFont(labelFont);
83                 String label = "RAM";
84                 Point textExtent = gc.textExtent(label);
85                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
86                 if (textColor != null)
87                         gc.setForeground(textColor);
88                 gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
89                 gc.setFont(oldFont);
90         }
91
92         @Override
93         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
94         {
95                 JsonObject o = new JsonObject();
96                 o.addProperty(addrKey, definition.getMemoryAddressBits());
97                 o.addProperty(cellWidthKey, definition.getCellWidth());
98                 o.addProperty(maxAddrKey, definition.getMaximalAddress());
99                 o.addProperty(minAddrKey, definition.getMinimalAddress());
100                 return o;
101         }
102
103         static
104         {
105                 ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
106                 IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
107                 {
108                         JsonObject o = (JsonObject) p;
109                         int addressBits = o.get(addrKey).getAsInt();
110                         int cellWidth = o.get(cellWidthKey).getAsInt();
111                         long maxAddr = o.get(maxAddrKey).getAsLong();
112                         long minAddr = o.get(minAddrKey).getAsLong();
113                         return new GUIMemoryWA(m, MainMemoryDefinition.create(addressBits, cellWidth, minAddr, maxAddr), n);
114                 });
115         }
116 }