Added a reset button to SimulationViewEditor
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / editors / SimulationViewEditor.java
index 2dcbaff..05016cc 100644 (file)
@@ -33,6 +33,7 @@ import net.mograsim.machine.Machine;
 import net.mograsim.machine.Memory.MemoryCellModifiedListener;
 import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
 import net.mograsim.plugin.nature.MachineContext;
+import net.mograsim.plugin.nature.MachineContext.ActiveMachineListener;
 import net.mograsim.plugin.nature.ProjectMachineContext;
 import net.mograsim.plugin.tables.DisplaySettings;
 import net.mograsim.plugin.tables.mi.ActiveInstructionPreviewContentProvider;
@@ -48,6 +49,7 @@ public class SimulationViewEditor extends EditorPart
        private Machine machine;
 
        private Composite parent;
+       private Button resetButton;
        private Button sbseButton;
        private Button pauseButton;
        private Slider simSpeedSlider;
@@ -56,8 +58,29 @@ public class SimulationViewEditor extends EditorPart
        private InstructionTable instPreview;
        private Label noMachineLabel;
 
-       private MemoryCellModifiedListener currentRegisteredCellListener;
-       private LogicObserver currentClockObserver;
+       private ActiveMachineListener activeMNachineListener;
+       private MemoryCellModifiedListener memCellListener;
+       private LogicObserver clockObserver;
+
+       public SimulationViewEditor()
+       {
+               activeMNachineListener = m -> recreateContextDependentControls();
+               memCellListener = a -> instPreview.refresh();
+               clockObserver = o ->
+               {
+                       if (((CoreClock) o).isOn())
+                       {
+                               exec.pauseLiveExecution();
+                               if (!pauseButton.isDisposed())
+                                       Display.getDefault().asyncExec(() ->
+                                       {
+                                               if (!pauseButton.isDisposed())
+                                                       pauseButton.setSelection(false);
+                                               setPauseText(pauseButton, false);
+                                       });
+                       }
+               };
+       }
 
        @Override
        public void createPartControl(Composite parent)
@@ -85,6 +108,7 @@ public class SimulationViewEditor extends EditorPart
                double offX;
                double offY;
                double zoom;
+               stopExecAndDeregisterContextDependentListeners();
                if (canvas != null)
                {
                        offX = canvas.getOffX();
@@ -97,19 +121,12 @@ public class SimulationViewEditor extends EditorPart
                        offY = 0;
                        zoom = -1;
                }
-               if (exec != null)
-                       exec.stopLiveExecution();
-
-               if (machine != null)
-               {
-                       machine.getMicroInstructionMemory().deregisterCellModifiedListener(currentRegisteredCellListener);
-                       machine.getClock().deregisterObserver(currentClockObserver);
-               }
 
                Optional<Machine> machineOptional;
                if (context != null && (machineOptional = context.getActiveMachine()).isPresent())
                {
                        noMachineLabel.setVisible(false);
+                       resetButton.setEnabled(true);
                        sbseButton.setEnabled(true);
                        pauseButton.setEnabled(true);
                        simSpeedSlider.setEnabled(true);
@@ -128,8 +145,7 @@ public class SimulationViewEditor extends EditorPart
 
                        AssignableMicroInstructionMemory mIMemory = machine.getMicroInstructionMemory();
                        instPreview.bindMicroInstructionMemory(mIMemory);
-                       currentRegisteredCellListener = a -> instPreview.refresh();
-                       mIMemory.registerCellModifiedListener(currentRegisteredCellListener);
+                       mIMemory.registerCellModifiedListener(memCellListener);
 
                        canvasParent.layout();
 
@@ -139,42 +155,44 @@ public class SimulationViewEditor extends EditorPart
                } else
                {
                        noMachineLabel.setVisible(true);
+                       resetButton.setEnabled(false);
                        sbseButton.setEnabled(false);
                        pauseButton.setEnabled(false);
                        simSpeedSlider.setEnabled(false);
                }
        }
 
+       private void stopExecAndDeregisterContextDependentListeners()
+       {
+               if (exec != null)
+                       exec.stopLiveExecution();
+               if (machine != null)
+               {
+                       machine.getMicroInstructionMemory().deregisterCellModifiedListener(memCellListener);
+                       machine.getClock().deregisterObserver(clockObserver);
+               }
+       }
+
        private void addSimulationControlWidgets(Composite parent)
        {
                Composite c = new Composite(parent, SWT.NONE);
                c.setLayout(new GridLayout(7, false));
 
+               resetButton = new Button(c, SWT.PUSH);
+               resetButton.setText("Reset machine");
+               resetButton.addListener(SWT.Selection, e -> context.getActiveMachine().get().reset());
+
                sbseButton = new Button(c, SWT.CHECK);
                pauseButton = new Button(c, SWT.TOGGLE);
-               currentClockObserver = o ->
-               {
-                       if (((CoreClock) o).isOn())
-                       {
-                               exec.pauseLiveExecution();
-                               if (!pauseButton.isDisposed())
-                                       Display.getDefault().asyncExec(() ->
-                                       {
-                                               if (!pauseButton.isDisposed())
-                                                       pauseButton.setSelection(false);
-                                               setPauseText(pauseButton, false);
-                                       });
-                       }
-               };
 
                sbseButton.setText("Step by step execution");
                sbseButton.addListener(SWT.Selection, e ->
                {
                        CoreClock cl = machine.getClock();
                        if (sbseButton.getSelection())
-                               cl.registerObserver(currentClockObserver);
+                               cl.registerObserver(clockObserver);
                        else
-                               cl.deregisterObserver(currentClockObserver);
+                               cl.deregisterObserver(clockObserver);
                });
                sbseButton.setSelection(false);
 
@@ -264,7 +282,8 @@ public class SimulationViewEditor extends EditorPart
                {
                        IFileEditorInput fileInput = (IFileEditorInput) input;
                        context = ProjectMachineContext.getMachineContextOf(fileInput.getFile().getProject());
-                       context.registerObserver(m -> recreateContextDependentControls());
+                       context.activateMachine();
+                       context.addActiveMachineListener(activeMNachineListener);
                        recreateContextDependentControls();
 
                        setPartName(fileInput.getName());
@@ -323,7 +342,8 @@ public class SimulationViewEditor extends EditorPart
        @Override
        public void dispose()
        {
-               exec.stopLiveExecution();
+               stopExecAndDeregisterContextDependentListeners();
+               context.removeActiveMachineListener(activeMNachineListener);
                super.dispose();
        }
 }
\ No newline at end of file