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.BitVectorMemory;
14 import net.mograsim.machine.BitVectorMemoryDefinition;
15 import net.mograsim.machine.ModelMemory;
17 public abstract class AbstractModelBitVectorMemory<M extends BitVectorMemory, D extends BitVectorMemoryDefinition> extends ModelMemory
19 private final Pin addrPin, dataPin, rWPin;
20 private final D definition;
21 private final boolean readonly;
23 private CoreBitVectorMemory<M> memory;
25 private final List<Consumer<Object>> memoryBindingListeners;
27 public AbstractModelBitVectorMemory(LogicModelModifiable model, int width, int height, int aPinX, int dPinX, int rwPinX,
28 boolean readonly, String centerText, D definition, String name)
30 super(model, width, height, name, centerText, false);
31 this.definition = definition;
32 this.readonly = readonly;
34 addPin(addrPin = new Pin(model, this, "A", definition.getMemoryAddressBits(), PinUsage.INPUT, width, aPinX));
35 addPin(dataPin = new Pin(model, this, "D", definition.getCellWidth(), PinUsage.TRISTATE, width, dPinX));
39 addPin(rWPin = new Pin(model, this, "RW", 1, PinUsage.INPUT, width, rwPinX));
41 memoryBindingListeners = new ArrayList<>();
43 setHighLevelStateHandler(new HighLevelStateHandler()
46 public Object get(String stateID)
48 if (stateID.equals("memory_binding"))
49 return memory.getMemory();
50 throw new IllegalArgumentException("No high level state with ID " + stateID);
54 public void set(String stateID, Object newState)
56 if (stateID.equals("memory_binding"))
58 @SuppressWarnings("unchecked")
60 memory.setMemory(mem);
61 memoryBindingListeners.forEach(l -> l.accept(newState));
63 throw new IllegalArgumentException("No high level state with ID " + stateID);
67 public void addListener(String stateID, Consumer<Object> stateChanged)
69 if (stateID.equals("memory_binding"))
70 memoryBindingListeners.add(stateChanged);
72 throw new IllegalArgumentException("No high level state with ID " + stateID);
76 public void removeListener(String stateID, Consumer<Object> stateChanged)
78 if (stateID.equals("memory_binding"))
79 memoryBindingListeners.remove(stateChanged);
81 throw new IllegalArgumentException("No high level state with ID " + stateID);
85 public String getIDForSerializing(IdentifyParams idParams)
91 public Object getParamsForSerializing(IdentifyParams idParams)
100 public D getDefinition()
105 public Pin getAddressPin()
110 public Pin getDataPin()
115 public Pin getReadWritePin()
118 throw new IllegalArgumentException("This AbstractModelBitVectorMemory has no RW pin; it is readonly");
122 public boolean isReadonly()
127 public void setCoreModelBinding(CoreBitVectorMemory<M> memory)
129 this.memory = memory;
132 public CoreBitVectorMemory<M> getCoreMemory()
139 LogicCoreAdapter.addComponentAdapter(new BitVectorMemoryAdapter());