1 package net.mograsim.machine.standard.memory;
3 import java.util.ArrayList;
5 import java.util.function.Consumer;
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;
17 public abstract class ModelWordAddressableMemory extends ModelMemory
19 private final Pin addrPin, dataPin, rWPin;
20 private CoreWordAddressableMemory memory;
21 private MainMemoryDefinition definition;
23 private final List<Consumer<Object>> memoryBindingListeners;
25 public ModelWordAddressableMemory(LogicModelModifiable model, MainMemoryDefinition definition, String name)
27 super(model, 120, 150, name, "RAM", false);
28 this.definition = definition;
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));
34 memoryBindingListeners = new ArrayList<>();
36 setHighLevelStateHandler(new HighLevelStateHandler()
39 public Object get(String stateID)
41 if (stateID.equals("memory_binding"))
42 return memory.getMemory();
43 throw new IllegalArgumentException("No high level state with ID " + stateID);
47 public void set(String stateID, Object newState)
49 if (stateID.equals("memory_binding"))
51 memory.setMemory((MainMemory) newState);
52 memoryBindingListeners.forEach(l -> l.accept(newState));
54 throw new IllegalArgumentException("No high level state with ID " + stateID);
58 public void addListener(String stateID, Consumer<Object> stateChanged)
60 if (stateID.equals("memory_binding"))
61 memoryBindingListeners.add(stateChanged);
63 throw new IllegalArgumentException("No high level state with ID " + stateID);
67 public void removeListener(String stateID, Consumer<Object> stateChanged)
69 if (stateID.equals("memory_binding"))
70 memoryBindingListeners.remove(stateChanged);
72 throw new IllegalArgumentException("No high level state with ID " + stateID);
76 public String getIDForSerializing(IdentifyParams idParams)
82 public Object getParamsForSerializing(IdentifyParams idParams)
91 public MainMemoryDefinition getDefinition()
96 public Pin getAddressPin()
101 public Pin getDataPin()
106 public Pin getReadWritePin()
111 public void setCoreModelBinding(CoreWordAddressableMemory memory)
113 this.memory = memory;
116 public CoreWordAddressableMemory getCoreMemory()
123 LogicCoreAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());