Updated MainMemory interface
authorFabian Stemmler <stemmler@in.tum.de>
Sat, 24 Aug 2019 11:12:18 +0000 (13:12 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Sat, 24 Aug 2019 11:12:18 +0000 (13:12 +0200)
added getDefinition() to access MainMemoryDefinition
updated the existing memory components to be initialized with a
MainMemoryDefinition

net.mograsim.machine/src/net/mograsim/machine/DefaultMainMemoryDefinition.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/MainMemory.java
net.mograsim.machine/src/net/mograsim/machine/standard/memory/GUIMemoryWA.java
net.mograsim.machine/src/net/mograsim/machine/standard/memory/WordAddressableMemory.java
net.mograsim.machine/src/net/mograsim/machine/standard/memory/WordAddressableMemoryAdapter.java
net.mograsim.machine/src/net/mograsim/machine/standard/memory/WordAddressableMemoryComponent.java

diff --git a/net.mograsim.machine/src/net/mograsim/machine/DefaultMainMemoryDefinition.java b/net.mograsim.machine/src/net/mograsim/machine/DefaultMainMemoryDefinition.java
new file mode 100644 (file)
index 0000000..2431b65
--- /dev/null
@@ -0,0 +1,45 @@
+package net.mograsim.machine;
+
+public class DefaultMainMemoryDefinition implements MainMemoryDefinition {
+       private final int memoryAddressBits, cellWidth;
+       private final long minimalAddress, maximalAddress;
+       
+       public DefaultMainMemoryDefinition(int memoryAddressBits, int cellWidth, long minimalAddress, long maximalAddress)
+       {
+               super();
+               this.memoryAddressBits = memoryAddressBits;
+               this.cellWidth = cellWidth;
+               this.minimalAddress = minimalAddress;
+               this.maximalAddress = maximalAddress;
+       }
+
+       public DefaultMainMemoryDefinition(MainMemoryDefinition definition)
+       {
+               this(definition.getMemoryAddressBits(), definition.getCellWidth(), definition.getMinimalAddress(), definition.getMaximalAddress());
+       }
+
+       @Override
+       public int getMemoryAddressBits()
+       {
+               return memoryAddressBits;
+       }
+
+       @Override
+       public int getCellWidth()
+       {
+               return cellWidth;
+       }
+
+       @Override
+       public long getMinimalAddress()
+       {
+               return minimalAddress;
+       }
+
+       @Override
+       public long getMaximalAddress()
+       {
+               return maximalAddress;
+       }
+       
+}
index 78c0ca0..f1827ef 100644 (file)
@@ -1,5 +1,10 @@
 package net.mograsim.machine;
 
+import net.mograsim.logic.core.types.BitVector;
+
 public interface MainMemory {
        
+       public BitVector getCell(long address);
+       public void setCell(long address, BitVector word);
+       public MainMemoryDefinition getDefinition();
 }
index 3d63b47..1632c8e 100644 (file)
@@ -2,8 +2,8 @@ package net.mograsim.machine.standard.memory;
 
 import org.eclipse.swt.graphics.Color;
 
+import com.google.gson.Gson;
 import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
 
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
@@ -16,26 +16,25 @@ import net.mograsim.logic.model.model.wires.Pin;
 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.model.serializing.IdentifierGetter;
 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
+import net.mograsim.machine.DefaultMainMemoryDefinition;
+import net.mograsim.machine.MainMemoryDefinition;
+import net.mograsim.machine.standard.memory.WordAddressableMemoryComponent;
 import net.mograsim.preferences.Preferences;
 
 public class GUIMemoryWA extends GUIComponent
 {
-       private final static String paramAddr = "addrBits", paramWordWidth = "wordWidth", paramMaxAddr = "maxAddr", paramMinAddr = "minAddr";
-       private final int addressBits, wordWidth;
-       public final long maximalAddress, minimalAddress;
+       private final MainMemoryDefinition definition;
        private final Pin addrPin, dataPin, rWPin;
+       private WordAddressableMemoryComponent memory;
        private final static int width = 100, height = 300;
 
-       public GUIMemoryWA(ViewModelModifiable model, int addressBits, int wordWidth, long maximalAddress, long minimalAddress, String name)
+       public GUIMemoryWA(ViewModelModifiable model, MainMemoryDefinition definition, String name)
        {
                super(model, name);
-               this.addressBits = addressBits;
-               this.wordWidth = wordWidth;
-               this.maximalAddress = maximalAddress;
-               this.minimalAddress = minimalAddress;
+               this.definition = definition;
                setSize(width, height);
-               addPin(addrPin = new Pin(this, "A", addressBits, 0, 10));
-               addPin(dataPin = new Pin(this, "D", wordWidth, 0, 30));
+               addPin(addrPin = new Pin(this, "A", definition.getMemoryAddressBits(), 0, 10));
+               addPin(dataPin = new Pin(this, "D", definition.getCellWidth(), 0, 30));
                addPin(rWPin = new Pin(this, "RW", 1, 0, 50));
        }
 
@@ -53,6 +52,21 @@ public class GUIMemoryWA extends GUIComponent
        {
                return rWPin;
        }
+       
+       public void setLogicModelBinding(WordAddressableMemoryComponent memory)
+       {
+               this.memory = memory;
+       }
+       
+       public MainMemoryDefinition getDefinition()
+       {
+               return definition;
+       }
+       
+       public WordAddressableMemoryComponent getMemory()
+       {
+               return memory;
+       }
 
        @Override
        public void render(GeneralGC gc, Rectangle visibleRegion)
@@ -77,12 +91,7 @@ public class GUIMemoryWA extends GUIComponent
        @Override
        public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
        {
-               JsonObject obj = new JsonObject();
-               obj.addProperty(paramAddr, addressBits);
-               obj.addProperty(paramWordWidth, wordWidth);
-               obj.addProperty(paramMaxAddr, maximalAddress);
-               obj.addProperty(paramMinAddr, minimalAddress);
-               return obj;
+               return new Gson().toJsonTree(new DefaultMainMemoryDefinition(definition));
        }
 
        static
@@ -90,9 +99,7 @@ public class GUIMemoryWA extends GUIComponent
                ViewLogicModelAdapter.addComponentAdapter(new WordAddressableMemoryAdapter());
                IndirectGUIComponentCreator.setComponentSupplier(GUIAndGate.class.getCanonicalName(), (m, p, n) ->
                {
-                       JsonObject obj = p.getAsJsonObject();
-                       return new GUIMemoryWA(m, obj.get(paramAddr).getAsInt(), obj.get(paramWordWidth).getAsInt(), obj.get(paramMaxAddr).getAsLong(),
-                                       obj.get(paramMinAddr).getAsLong(), n);
+                       return new GUIMemoryWA(m, new Gson().fromJson(p, DefaultMainMemoryDefinition.class), n);
                });
        }
 }
index 719c993..037b17a 100644 (file)
@@ -5,24 +5,29 @@ import java.util.HashMap;
 
 import net.mograsim.logic.core.types.Bit;
 import net.mograsim.logic.core.types.BitVector;
+import net.mograsim.machine.MainMemory;
+import net.mograsim.machine.MainMemoryDefinition;
 
-public class WordAddressableMemory
+public class WordAddressableMemory implements MainMemory
 {
        private final int cellWidth;
        private final long minimalAddress, maximalAddress;
+       private final MainMemoryDefinition definition;
        private final int pageSize = 64;
 
        private HashMap<Long, Page> pages;
 
-       public WordAddressableMemory(int cellWidth, long minimalAddress, long maximalAddress)
+       public WordAddressableMemory(MainMemoryDefinition definition)
        {
                super();
-               this.cellWidth = cellWidth;
-               this.minimalAddress = minimalAddress;
-               this.maximalAddress = maximalAddress;
+               this.cellWidth = definition.getCellWidth();
+               this.minimalAddress = definition.getMinimalAddress();
+               this.maximalAddress = definition.getMaximalAddress();
+               this.definition = definition;
                this.pages = new HashMap<>();
        }
 
+       @Override
        public void setCell(long address, BitVector b)
        {
                if (address < minimalAddress || address > maximalAddress)
@@ -36,6 +41,7 @@ public class WordAddressableMemory
                p.setCell(offset, b);
        }
 
+       @Override
        public BitVector getCell(long address)
        {
                long page = address / pageSize;
@@ -77,4 +83,10 @@ public class WordAddressableMemory
                        return Arrays.deepToString(memory);
                }
        }
+
+       @Override
+       public MainMemoryDefinition getDefinition()
+       {
+               return definition;
+       }
 }
index bc979dc..ffacb74 100644 (file)
@@ -27,7 +27,7 @@ public class WordAddressableMemoryAdapter implements ComponentAdapter<GUIMemoryW
                ReadWriteEnd data = logicWiresPerPin.get(guiComponent.getDataPin()).createReadWriteEnd();
                ReadEnd address = logicWiresPerPin.get(guiComponent.getAddressPin()).createReadOnlyEnd();
                ReadEnd mode = logicWiresPerPin.get(guiComponent.getReadWritePin()).createReadOnlyEnd();
-               new WordAddressableMemoryComponent(timeline, 2, guiComponent.maximalAddress, guiComponent.minimalAddress, data, mode, address);
+               new WordAddressableMemoryComponent(timeline, 2, guiComponent.getDefinition(), data, mode, address);
        }
 
 }
index 4a3edde..e781d3d 100644 (file)
@@ -8,6 +8,7 @@ import net.mograsim.logic.core.types.Bit;
 import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.wires.Wire.ReadEnd;
 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
+import net.mograsim.machine.MainMemoryDefinition;
 
 /**
  * A memory component that only allows access to words of a specific length
@@ -26,10 +27,16 @@ public class WordAddressableMemoryComponent extends BasicComponent
         * @param rWBit   The value of the 0th bit dictates the mode: 0: Write, 1: Read
         * @param address The bits of this ReadEnd address the memory cell to read/write
         */
-       public WordAddressableMemoryComponent(Timeline timeline, int processTime, long minimalAddress, long maximalAddress, ReadWriteEnd data,
+       public WordAddressableMemoryComponent(Timeline timeline, int processTime, MainMemoryDefinition definition, ReadWriteEnd data,
                        ReadEnd rWBit, ReadEnd address)
        {
                super(timeline, processTime);
+               if(data.length() != definition.getCellWidth())
+                       throw new IllegalArgumentException(String.format("Bit width of data wire does not match main memory definition. Expected: %d Actual: %d", definition.getCellWidth(), data.length()));
+               if(rWBit.length() != 1)
+                       throw new IllegalArgumentException(String.format("Bit width of read/write mode select wire is unexpected. Expected: 1 Actual: %d", rWBit.length()));
+               if(address.length() != definition.getMemoryAddressBits())
+                       throw new IllegalArgumentException(String.format("Bit width of address wire does not match main memory definition. Expected: %d Actual: %d", definition.getMemoryAddressBits(), address.length()));
                this.data = data;
                this.rWBit = rWBit;
                this.address = address;
@@ -37,7 +44,7 @@ public class WordAddressableMemoryComponent extends BasicComponent
                rWBit.registerObserver(this);
                address.registerObserver(this);
 
-               memory = new WordAddressableMemory(data.length(), minimalAddress, maximalAddress);
+               memory = new WordAddressableMemory(definition);
        }
 
        @Override