Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / CoreDemux.java
index a5bb0fc..8f4fe42 100644 (file)
@@ -3,6 +3,8 @@ package net.mograsim.logic.core.components;
 import java.util.List;
 
 import net.mograsim.logic.core.timeline.Timeline;
+import net.mograsim.logic.core.timeline.TimelineEventHandler;
+import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.wires.CoreWire;
 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
@@ -53,19 +55,27 @@ public class CoreDemux extends BasicCoreComponent
        }
 
        @Override
-       public void compute()
+       public TimelineEventHandler compute()
        {
-               int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
+               int selectValue = select.getValues().isBinary() ? (int) select.getValues().getUnsignedValueLong() : -1;
                if (selectValue >= outputs.length)
                        selectValue = -1;
 
-               if (selected != selectValue && selected != -1)
-                       outputs[selected].clearSignals();
+               boolean hasOldSelection = selected != selectValue && selected != -1;
+               int oldSelection = selected;
+               boolean hasNewSelection = selectValue != -1;
+               int newSelection = selectValue;
+               BitVector inputValues = in.getValues();
 
                selected = selectValue;
 
-               if (selectValue != -1)
-                       outputs[selectValue].feedSignals(in.getValues());
+               return e ->
+               {
+                       if (hasOldSelection)
+                               outputs[oldSelection].clearSignals();
+                       if (hasNewSelection)
+                               outputs[newSelection].feedSignals(inputValues);
+               };
        }
 
        @Override