Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / standard / memory / CoreWordAddressableMemory.java
index f7dd982..ca557a6 100644 (file)
@@ -4,9 +4,12 @@ import java.util.List;
 
 import net.mograsim.logic.core.components.BasicCoreComponent;
 import net.mograsim.logic.core.timeline.Timeline;
+import net.mograsim.logic.core.timeline.TimelineEventHandler;
 import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
+import net.mograsim.machine.MainMemory;
 import net.mograsim.machine.MainMemoryDefinition;
 
 /**
@@ -14,11 +17,11 @@ import net.mograsim.machine.MainMemoryDefinition;
  */
 public class CoreWordAddressableMemory extends BasicCoreComponent
 {
-       private final WordAddressableMemory memory;
+       private final MainMemory memory;
        private final static Bit read = Bit.ONE;
 
        private ReadWriteEnd data;
-       private ReadEnd rWBit, address, clock;
+       private ReadEnd rWBit, address;
 
        /**
         * @param data    The bits of this ReadEnd are the value that is written to/read from memory; The bit width of this wire is the width of
@@ -26,49 +29,55 @@ public class CoreWordAddressableMemory extends BasicCoreComponent
         * @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 CoreWordAddressableMemory(Timeline timeline, int processTime, MainMemoryDefinition definition, ReadWriteEnd data,
-                       ReadEnd rWBit, ReadEnd address, ReadEnd clock)
+       public CoreWordAddressableMemory(Timeline timeline, int processTime, MainMemory memory, ReadWriteEnd data, ReadEnd rWBit,
+                       ReadEnd address)
        {
                super(timeline, processTime);
-               if(data.width() != 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.width()));
-               if(rWBit.width() != 1)
-                       throw new IllegalArgumentException(String.format("Bit width of read/write mode select wire is unexpected. Expected: 1 Actual: %d", rWBit.width()));
-               if(address.width() != 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.width()));
+               MainMemoryDefinition definition = memory.getDefinition();
+               if (data.width() != 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.width()));
+               if (rWBit.width() != 1)
+                       throw new IllegalArgumentException(
+                                       String.format("Bit width of read/write mode select wire is unexpected. Expected: 1 Actual: %d", rWBit.width()));
+               if (address.width() != 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.width()));
+               this.memory = memory;
                this.data = data;
                this.rWBit = rWBit;
                this.address = address;
-               this.clock = clock;
+               memory.registerObserver(a -> update());
                data.registerObserver(this);
                rWBit.registerObserver(this);
                address.registerObserver(this);
-               clock.registerObserver(this);
-               
-               memory = new WordAddressableMemory(definition);
        }
 
        @Override
-       protected void compute()
+       protected TimelineEventHandler compute()
        {
-               if(clock.getValue() != Bit.ONE)
-                       return;
-               if (!address.hasNumericValue())
+               if (!address.getValues().isBinary())
                {
                        if (read.equals(rWBit.getValue()))
-                               data.feedSignals(Bit.U.toVector(data.width()));
-                       else
-                               data.clearSignals();
-                       return;
+                               return e -> data.feedSignals(Bit.U.toVector(data.width()));// TODO don't always feed U, but decide to feed X or U.
+                       return e -> data.clearSignals();
                }
-               long addressed = address.getUnsignedValue();
+               long addressed = address.getValues().getUnsignedValueLong();
                if (read.equals(rWBit.getValue()))
-                       data.feedSignals(memory.getCell(addressed));
-               else
                {
-                       data.clearSignals();
-                       memory.setCell(addressed, data.getValues());
+                       BitVector storedData = memory.getCell(addressed);
+                       return e -> data.feedSignals(storedData);
                }
+               BitVector transData = data.getValues();
+               if (transData.equals(memory.getCell(addressed)))
+                       return null;
+               return e ->
+               {
+                       data.clearSignals();
+                       memory.setCell(addressed, transData);
+               };
        }
 
        @Override