Added active instruction preview to LogicUIPart
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / mi / StandardMicroInstructionMemory.java
index 7ef53d9..94905a5 100644 (file)
@@ -1,15 +1,17 @@
 package net.mograsim.machine.mi;
 
 import java.util.HashSet;
+import java.util.Set;
 
-import net.mograsim.machine.MemoryObserver;
 import net.mograsim.machine.standard.memory.MemoryException;
 
 public class StandardMicroInstructionMemory implements MicroInstructionMemory
 {
        private MicroInstruction[] data;
        private MicroInstructionMemoryDefinition definition;
-       private HashSet<MemoryObserver> observers = new HashSet<>();
+       private HashSet<MemoryCellModifiedListener> observers = new HashSet<>();
+       private Set<ActiveMicroInstructionChangedListener> activeInstructionListeners = new HashSet<>();
+       private long activeInstruction = -1;
 
        public StandardMicroInstructionMemory(MicroInstructionMemoryDefinition definition)
        {
@@ -38,31 +40,57 @@ public class StandardMicroInstructionMemory implements MicroInstructionMemory
        public void setCell(long address, MicroInstruction data)
        {
                this.data[translate(address)] = data;
-               notifyObservers(address);
+               notifyMemoryChanged(address);
        }
 
        @Override
-       public void registerObserver(MemoryObserver ob)
+       public void registerCellModifiedListener(MemoryCellModifiedListener ob)
        {
                observers.add(ob);
        }
 
        @Override
-       public void deregisterObserver(MemoryObserver ob)
+       public void deregisterCellModifiedListener(MemoryCellModifiedListener ob)
        {
                observers.remove(ob);
        }
 
        @Override
-       public void notifyObservers(long address)
+       public void registerActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener ob)
+       {
+               activeInstructionListeners.add(ob);
+       }
+
+       @Override
+       public void deregisterActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener ob)
+       {
+               activeInstructionListeners.remove(ob);
+       }
+
+       private void notifyMemoryChanged(long address)
        {
                observers.forEach(ob -> ob.update(address));
        }
 
+       private void notifyActiveInstructionChanged(long address)
+       {
+               activeInstructionListeners.forEach(o -> o.activeMicroInstructionChanged(address));
+       }
+
        @Override
        public MicroInstructionMemoryDefinition getDefinition()
        {
                return definition;
        }
 
+       @Override
+       public void setActiveInstruction(long address)
+       {
+               if (address != activeInstruction)
+               {
+                       activeInstruction = address;
+                       notifyActiveInstructionChanged(address);
+               }
+       }
+
 }