X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Feditors%2FSimulationViewEditor.java;h=06fadc001fc880876b2867bf7f3aa0925e5e2440;hb=59e4f0d9447e3c764c83e7edabfcbf5c3c24a97b;hp=0420334fa260442e66e943c032f0a91a9281d144;hpb=c303ab90cb0fb61acde8a1e0ddc7a5ed8855ab82;p=Mograsim.git diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java index 0420334f..06fadc00 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/editors/SimulationViewEditor.java @@ -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,16 +49,39 @@ public class SimulationViewEditor extends EditorPart private Machine machine; private Composite parent; + private Button resetButton; private Button sbseButton; private Button pauseButton; + private Label speedFactorLabel; private Slider simSpeedSlider; private Composite canvasParent; private LogicUICanvas canvas; 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,13 +109,7 @@ public class SimulationViewEditor extends EditorPart double offX; double offY; double zoom; - if (exec != null) - exec.stopLiveExecution(); - if (machine != null) - { - machine.getMicroInstructionMemory().deregisterCellModifiedListener(currentRegisteredCellListener); - machine.getClock().deregisterObserver(currentClockObserver); - } + stopExecAndDeregisterContextDependentListeners(); if (canvas != null) { offX = canvas.getOffX(); @@ -109,6 +127,7 @@ public class SimulationViewEditor extends EditorPart if (context != null && (machineOptional = context.getActiveMachine()).isPresent()) { noMachineLabel.setVisible(false); + resetButton.setEnabled(true); sbseButton.setEnabled(true); pauseButton.setEnabled(true); simSpeedSlider.setEnabled(true); @@ -127,70 +146,64 @@ public class SimulationViewEditor extends EditorPart AssignableMicroInstructionMemory mIMemory = machine.getMicroInstructionMemory(); instPreview.bindMicroInstructionMemory(mIMemory); - currentRegisteredCellListener = a -> instPreview.refresh(); - mIMemory.registerCellModifiedListener(currentRegisteredCellListener); + mIMemory.registerCellModifiedListener(memCellListener); canvasParent.layout(); // initialize executer exec = new LogicExecuter(machine.getTimeline()); + updateSpeedFactor(); + updatePausedState(); exec.startLiveExecution(); } 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.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); 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); pauseButton.setSelection(true); setPauseText(pauseButton, false); - pauseButton.addListener(SWT.Selection, e -> - { - setPauseText(pauseButton, false); - if (pauseButton.getSelection()) - { - exec.unpauseLiveExecution(); - } else - { - exec.pauseLiveExecution(); - } - }); + pauseButton.addListener(SWT.Selection, e -> updatePausedState()); pauseButton.addMouseTrackListener(new MouseTrackListener() { @Override @@ -212,28 +225,39 @@ public class SimulationViewEditor extends EditorPart } }); - Label speedLabel = new Label(c, SWT.NONE); - speedLabel.setText("Simulation Speed: "); + new Label(c, SWT.NONE).setText("Simulation Speed: "); simSpeedSlider = new Slider(c, SWT.NONE); - simSpeedSlider.setMinimum(1); - simSpeedSlider.setMaximum(100 + simSpeedSlider.getThumb()); + simSpeedSlider.setMinimum(0); + simSpeedSlider.setMaximum(50 + simSpeedSlider.getThumb()); simSpeedSlider.setIncrement(1); + simSpeedSlider.setSelection(0); - Label speedPercentageLabel = new Label(c, SWT.NONE); - speedPercentageLabel.setText("100%"); + speedFactorLabel = new Label(c, SWT.NONE); + speedFactorLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); - simSpeedSlider.addListener(SWT.Selection, e -> - { - int selection = simSpeedSlider.getSelection(); - speedPercentageLabel.setText(selection + "%"); + simSpeedSlider.addListener(SWT.Selection, e -> updateSpeedFactor()); + updateSpeedFactor(); - exec.setSpeedPercentage(simSpeedSlider.getSelection()); - }); - simSpeedSlider.setSelection(100); + c.layout(); + } + + private void updatePausedState() + { + setPauseText(pauseButton, false); + if (exec != null) + if (pauseButton.getSelection()) + exec.unpauseLiveExecution(); + else + exec.pauseLiveExecution(); + } - c.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL)); - c.pack(); + private void updateSpeedFactor() + { + double factor = Math.pow(1.32, simSpeedSlider.getSelection() - 50); + speedFactorLabel.setText(String.format("%f", factor)); + if (exec != null) + exec.setSpeedFactor(factor); } private void addInstructionPreviewControlWidgets(Composite parent) @@ -263,7 +287,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()); @@ -322,7 +347,8 @@ public class SimulationViewEditor extends EditorPart @Override public void dispose() { - exec.stopLiveExecution(); + stopExecAndDeregisterContextDependentListeners(); + context.removeActiveMachineListener(activeMNachineListener); super.dispose(); } } \ No newline at end of file