The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / standard / memory / WordAddressableMemory.java
diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/WordAddressableMemory.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/standard/memory/WordAddressableMemory.java
new file mode 100644 (file)
index 0000000..4a54ad9
--- /dev/null
@@ -0,0 +1,48 @@
+package net.mograsim.machine.standard.memory;
+
+import java.math.BigInteger;
+
+import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
+import net.mograsim.machine.GenericMemory;
+import net.mograsim.machine.MainMemory;
+import net.mograsim.machine.MainMemoryDefinition;
+
+public class WordAddressableMemory extends GenericMemory<BitVector> implements MainMemory
+{
+       private final int cellWidth;
+       private final MainMemoryDefinition definition;
+
+       public WordAddressableMemory(MainMemoryDefinition definition)
+       {
+               super(definition);
+               this.cellWidth = definition.getCellWidth();
+               this.definition = definition;
+       }
+
+       @Override
+       public BitVector getCell(long address)
+       {
+               BitVector data = super.getCell(address);
+               return data == null ? BitVector.of(Bit.ZERO, cellWidth) : data;
+       }
+
+       @Override
+       public BigInteger getCellAsBigInteger(long address)
+       {
+               BitVector data = getCell(address);
+               return data == null ? BigInteger.valueOf(0) : data.getUnsignedValue();
+       }
+
+       @Override
+       public void setCellAsBigInteger(long address, BigInteger data)
+       {
+               setCell(address, BitVector.from(data, cellWidth));
+       }
+
+       @Override
+       public MainMemoryDefinition getDefinition()
+       {
+               return definition;
+       }
+}