b2498498133d6599ae8930780529a3a992ab81b4
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / standard / memory / ModelWordAddressableMemory.java
1 package net.mograsim.machine.standard.memory;
2
3 import net.mograsim.logic.model.model.LogicModelModifiable;
4 import net.mograsim.logic.model.model.wires.Pin;
5 import net.mograsim.logic.model.model.wires.PinUsage;
6 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
7 import net.mograsim.logic.model.serializing.IdentifyParams;
8 import net.mograsim.logic.model.snippets.HighLevelStateHandler;
9 import net.mograsim.machine.MainMemory;
10 import net.mograsim.machine.MainMemoryDefinition;
11 import net.mograsim.machine.ModelMemory;
12
13 public abstract class ModelWordAddressableMemory extends ModelMemory
14 {
15         private final Pin addrPin, dataPin, rWPin;
16         private CoreWordAddressableMemory memory;
17         private MainMemoryDefinition definition;
18
19         public ModelWordAddressableMemory(LogicModelModifiable model, MainMemoryDefinition definition, String name)
20         {
21                 super(model, 120, 150, name, "RAM", false);
22                 this.definition = definition;
23
24                 addPin(addrPin = new Pin(model, this, "A", definition.getMemoryAddressBits(), PinUsage.INPUT, getWidth(), 30));
25                 addPin(dataPin = new Pin(model, this, "D", definition.getCellWidth(), PinUsage.TRISTATE, getWidth(), 50));
26                 addPin(rWPin = new Pin(model, this, "RW", 1, PinUsage.INPUT, getWidth(), 70));
27
28                 setHighLevelStateHandler(new HighLevelStateHandler()
29                 {
30                         @Override
31                         public Object getHighLevelState(String stateID)
32                         {
33                                 if (stateID.equals("memory_binding"))
34                                         return memory.getMemory();
35                                 throw new IllegalArgumentException("No high level state with ID " + stateID);
36                         }
37
38                         @Override
39                         public void setHighLevelState(String stateID, Object newState)
40                         {
41                                 if (stateID.equals("memory_binding"))
42                                         memory.setMemory((MainMemory) newState);
43                                 else
44                                         throw new IllegalArgumentException("No high level state with ID " + stateID);
45                         }
46
47                         @Override
48                         public String getIDForSerializing(IdentifyParams idParams)
49                         {
50                                 return null;
51                         }
52
53                         @Override
54                         public Object getParamsForSerializing(IdentifyParams idParams)
55                         {
56                                 return null;
57                         }
58                 });
59
60                 init();
61         }
62
63         public MainMemoryDefinition getDefinition()
64         {
65                 return definition;
66         }
67
68         public Pin getAddressPin()
69         {
70                 return addrPin;
71         }
72
73         public Pin getDataPin()
74         {
75                 return dataPin;
76         }
77
78         public Pin getReadWritePin()
79         {
80                 return rWPin;
81         }
82
83         public void setCoreModelBinding(CoreWordAddressableMemory memory)
84         {
85                 this.memory = memory;
86         }
87
88         public CoreWordAddressableMemory getCoreMemory()
89         {
90                 return memory;
91         }
92
93         static
94         {
95                 LogicCoreAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
96         }
97 }