Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / standard / memory / WordAddressableMemory.java
index 719c993..4a54ad9 100644 (file)
@@ -1,80 +1,48 @@
 package net.mograsim.machine.standard.memory;
 
-import java.util.Arrays;
-import java.util.HashMap;
+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
+public class WordAddressableMemory extends GenericMemory<BitVector> implements MainMemory
 {
        private final int cellWidth;
-       private final long minimalAddress, maximalAddress;
-       private final int pageSize = 64;
+       private final MainMemoryDefinition definition;
 
-       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.pages = new HashMap<>();
+               super(definition);
+               this.cellWidth = definition.getCellWidth();
+               this.definition = definition;
        }
 
-       public void setCell(long address, BitVector b)
+       @Override
+       public BitVector getCell(long address)
        {
-               if (address < minimalAddress || address > maximalAddress)
-                       throw new IndexOutOfBoundsException(String.format("Memory address out of bounds! Minimum: %d Maximum: %d Actual: %d",
-                                       minimalAddress, maximalAddress, address));
-               long page = address / pageSize;
-               int offset = (int) (address % pageSize);
-               Page p = pages.get(Long.valueOf(page));
-               if (p == null)
-                       pages.put(page, p = new Page());
-               p.setCell(offset, b);
+               BitVector data = super.getCell(address);
+               return data == null ? BitVector.of(Bit.ZERO, cellWidth) : data;
        }
 
-       public BitVector getCell(long address)
+       @Override
+       public BigInteger getCellAsBigInteger(long address)
        {
-               long page = address / pageSize;
-               int offset = (int) (address % pageSize);
-               Page p = pages.get(Long.valueOf(page));
-               if (p == null)
-                       return BitVector.of(Bit.U, cellWidth);
-               return p.getCell(offset);
+               BitVector data = getCell(address);
+               return data == null ? BigInteger.valueOf(0) : data.getUnsignedValue();
        }
 
-       private class Page
+       @Override
+       public void setCellAsBigInteger(long address, BigInteger data)
        {
-               private BitVector[] memory;
-
-               public Page()
-               {
-                       memory = new BitVector[pageSize];
-               }
-
-               public BitVector getCell(int index)
-               {
-                       BitVector b = memory[index];
-                       if (b == null)
-                               return BitVector.of(Bit.U, cellWidth);
-                       return memory[index];
-               }
-
-               public void setCell(int index, BitVector bits)
-               {
-                       if (bits.length() != cellWidth)
-                               throw new IllegalArgumentException(String.format(
-                                               "BitVector to be saved in memory cell has unexpected length. Expected: %d Actual: %d", cellWidth, bits.length()));
-                       memory[index] = bits;
-               }
+               setCell(address, BitVector.from(data, cellWidth));
+       }
 
-               @Override
-               public String toString()
-               {
-                       return Arrays.deepToString(memory);
-               }
+       @Override
+       public MainMemoryDefinition getDefinition()
+       {
+               return definition;
        }
 }