Re-added instruction preview to SimulationViewEditor
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / editors / SimulationViewEditor.java
index e6d05ea..4dd346b 100644 (file)
@@ -14,6 +14,7 @@ import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Slider;
 import org.eclipse.ui.IEditorInput;
@@ -23,11 +24,18 @@ import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.part.EditorPart;
 
 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
+import net.mograsim.logic.core.LogicObserver;
+import net.mograsim.logic.core.components.CoreClock;
 import net.mograsim.logic.model.LogicExecuter;
 import net.mograsim.logic.model.LogicUICanvas;
 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.ProjectMachineContext;
+import net.mograsim.plugin.tables.DisplaySettings;
+import net.mograsim.plugin.tables.mi.ActiveInstructionPreviewContentProvider;
+import net.mograsim.plugin.tables.mi.InstructionTable;
 
 //TODO what if we open multiple editors?
 //TODO actually save / load register and latch states
@@ -36,11 +44,17 @@ public class SimulationViewEditor extends EditorPart
        private MachineContext context;
 
        private LogicExecuter exec;
+       private Machine machine;
 
        private Composite parent;
+       private Button sbseButton;
+       private Button pauseButton;
+       private Slider simSpeedSlider;
        private LogicUICanvas canvas;
        private Label noMachineLabel;
 
+       private MemoryCellModifiedListener currentRegisteredCellListener;
+
        @Override
        public void createPartControl(Composite parent)
        {
@@ -69,13 +83,30 @@ public class SimulationViewEditor extends EditorPart
                if (context != null && (machineOptional = context.getActiveMachine()).isPresent())
                {
                        noMachineLabel.setVisible(false);
-                       Machine machine = machineOptional.get();
+                       sbseButton.setEnabled(true);
+                       pauseButton.setEnabled(true);
+                       simSpeedSlider.setEnabled(true);
+
+                       if (machine != null)
+                               machine.getMicroInstructionMemory().deregisterCellModifiedListener(currentRegisteredCellListener);
+
+                       machine = machineOptional.get();
                        canvas = new LogicUICanvas(parent, SWT.NONE, machine.getModel());
+                       canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
                        ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(canvas);
                        userInput.buttonDrag = 3;
                        userInput.buttonZoom = 2;
                        userInput.enableUserInput();
 
+                       // initialize Instruction preview
+                       InstructionTable instPreview = new InstructionTable(parent, new DisplaySettings());
+                       instPreview.getTableViewer().getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+                       instPreview.setContentProvider(new ActiveInstructionPreviewContentProvider(instPreview.getTableViewer()));
+                       AssignableMicroInstructionMemory mIMemory = machine.getMicroInstructionMemory();
+                       instPreview.bindMicroInstructionMemory(mIMemory);
+                       currentRegisteredCellListener = a -> instPreview.refresh();
+                       mIMemory.registerCellModifiedListener(currentRegisteredCellListener);
+
                        GridData uiData = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.FILL_BOTH);
                        canvas.setLayoutData(uiData);
 
@@ -83,15 +114,47 @@ public class SimulationViewEditor extends EditorPart
                        exec = new LogicExecuter(machine.getTimeline());
                        exec.startLiveExecution();
                } else
+               {
                        noMachineLabel.setVisible(true);
+                       sbseButton.setEnabled(false);
+                       pauseButton.setEnabled(false);
+                       simSpeedSlider.setEnabled(false);
+               }
        }
 
        private void addSimulationControlWidgets(Composite parent)
        {
                Composite c = new Composite(parent, SWT.NONE);
-               c.setLayout(new GridLayout(6, false));
+               c.setLayout(new GridLayout(7, false));
+
+               sbseButton = new Button(c, SWT.CHECK);
+               pauseButton = new Button(c, SWT.TOGGLE);
+               LogicObserver clockObserver = 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(clockObserver);
+                       else
+                               cl.deregisterObserver(clockObserver);
+               });
+               sbseButton.setSelection(false);
 
-               Button pauseButton = new Button(c, SWT.TOGGLE);
                pauseButton.setSelection(true);
                setPauseText(pauseButton, false);
 
@@ -130,26 +193,25 @@ public class SimulationViewEditor extends EditorPart
                Label speedLabel = new Label(c, SWT.NONE);
                speedLabel.setText("Simulation Speed: ");
 
-               Slider slider = new Slider(c, SWT.NONE);
-               slider.setMinimum(1);
-               slider.setMaximum(100 + slider.getThumb());
-               slider.setIncrement(1);
+               simSpeedSlider = new Slider(c, SWT.NONE);
+               simSpeedSlider.setMinimum(1);
+               simSpeedSlider.setMaximum(100 + simSpeedSlider.getThumb());
+               simSpeedSlider.setIncrement(1);
 
                Label speedPercentageLabel = new Label(c, SWT.NONE);
                speedPercentageLabel.setText("100%");
 
-               slider.addListener(SWT.Selection, e ->
+               simSpeedSlider.addListener(SWT.Selection, e ->
                {
-                       int selection = slider.getSelection();
+                       int selection = simSpeedSlider.getSelection();
                        speedPercentageLabel.setText(selection + "%");
 
-                       exec.setSpeedPercentage(slider.getSelection());
+                       exec.setSpeedPercentage(simSpeedSlider.getSelection());
                });
-               slider.setSelection(100);
+               simSpeedSlider.setSelection(100);
 
                c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
                c.pack();
-               c.setVisible(true);
        }
 
        private static void setPauseText(Button pauseButton, boolean hovered)