a1112e68f6741c748a145f338ede9b0c0a708aae
[Mograsim.git] /
1 package net.mograsim.machine.standard.memory;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.function.Consumer;
6
7 import net.mograsim.logic.model.model.LogicModelModifiable;
8 import net.mograsim.logic.model.model.wires.Pin;
9 import net.mograsim.logic.model.model.wires.PinUsage;
10 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
11 import net.mograsim.logic.model.serializing.IdentifyParams;
12 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
13 import net.mograsim.machine.MainMemory;
14 import net.mograsim.machine.MainMemoryDefinition;
15 import net.mograsim.machine.ModelMemory;
16
17 public abstract class ModelWordAddressableMemory extends ModelMemory
18 {
19         private final Pin addrPin, dataPin, rWPin;
20         private CoreWordAddressableMemory memory;
21         private MainMemoryDefinition definition;
22
23         private final List<Consumer<Object>> memoryBindingListeners;
24
25         public ModelWordAddressableMemory(LogicModelModifiable model, MainMemoryDefinition definition, String name)
26         {
27                 super(model, 120, 150, name, "RAM", false);
28                 this.definition = definition;
29
30                 addPin(addrPin = new Pin(model, this, "A", definition.getMemoryAddressBits(), PinUsage.INPUT, getWidth(), 30));
31                 addPin(dataPin = new Pin(model, this, "D", definition.getCellWidth(), PinUsage.TRISTATE, getWidth(), 50));
32                 addPin(rWPin = new Pin(model, this, "RW", 1, PinUsage.INPUT, getWidth(), 70));
33
34                 memoryBindingListeners = new ArrayList<>();
35
36                 setHighLevelStateHandler(new HighLevelStateHandler()
37                 {
38                         @Override
39                         public Object get(String stateID)
40                         {
41                                 if (stateID.equals("memory_binding"))
42                                         return memory.getMemory();
43                                 throw new IllegalArgumentException("No high level state with ID " + stateID);
44                         }
45
46                         @Override
47                         public void set(String stateID, Object newState)
48                         {
49                                 if (stateID.equals("memory_binding"))
50                                 {
51                                         memory.setMemory((MainMemory) newState);
52                                         memoryBindingListeners.forEach(l -> l.accept(newState));
53                                 } else
54                                         throw new IllegalArgumentException("No high level state with ID " + stateID);
55                         }
56
57                         @Override
58                         public void addListener(String stateID, Consumer<Object> stateChanged)
59                         {
60                                 if (stateID.equals("memory_binding"))
61                                         memoryBindingListeners.add(stateChanged);
62                                 else
63                                         throw new IllegalArgumentException("No high level state with ID " + stateID);
64                         }
65
66                         @Override
67                         public void removeListener(String stateID, Consumer<Object> stateChanged)
68                         {
69                                 if (stateID.equals("memory_binding"))
70                                         memoryBindingListeners.remove(stateChanged);
71                                 else
72                                         throw new IllegalArgumentException("No high level state with ID " + stateID);
73                         }
74
75                         @Override
76                         public String getIDForSerializing(IdentifyParams idParams)
77                         {
78                                 return null;
79                         }
80
81                         @Override
82                         public Object getParamsForSerializing(IdentifyParams idParams)
83                         {
84                                 return null;
85                         }
86                 });
87
88                 init();
89         }
90
91         public MainMemoryDefinition getDefinition()
92         {
93                 return definition;
94         }
95
96         public Pin getAddressPin()
97         {
98                 return addrPin;
99         }
100
101         public Pin getDataPin()
102         {
103                 return dataPin;
104         }
105
106         public Pin getReadWritePin()
107         {
108                 return rWPin;
109         }
110
111         public void setCoreModelBinding(CoreWordAddressableMemory memory)
112         {
113                 this.memory = memory;
114         }
115
116         public CoreWordAddressableMemory getCoreMemory()
117         {
118                 return memory;
119         }
120
121         static
122         {
123                 LogicCoreAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
124         }
125 }