From: Fabian Stemmler Date: Wed, 2 Oct 2019 01:14:04 +0000 (+0200) Subject: Added hot replace functionality for modified mpm files X-Git-Url: https://mograsim.net/gitweb/?p=Mograsim.git;a=commitdiff_plain;h=609d5558cc109214ec2ee94ff617c1c82ed1a673 Added hot replace functionality for modified mpm files --- diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineLaunchConfigType.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineLaunchConfigType.java index 82d7e8be..a97d2c6f 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineLaunchConfigType.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineLaunchConfigType.java @@ -1,13 +1,17 @@ package net.mograsim.plugin.launch; +import static org.eclipse.core.resources.IResourceDelta.CHANGED; + import java.io.IOException; import java.io.InputStream; import java.util.Optional; +import java.util.concurrent.atomic.AtomicBoolean; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResourceChangeEvent; import org.eclipse.core.resources.IResourceChangeListener; +import org.eclipse.core.resources.IResourceDelta; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; @@ -16,6 +20,8 @@ import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.model.LaunchConfigurationDelegate; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.statushandlers.StatusManager; import net.mograsim.machine.Machine; @@ -38,6 +44,8 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram"; private final IResourceChangeListener resChangedListener; + private IFile mpmFile; + private Machine machine; public MachineLaunchConfigType() { @@ -126,20 +134,9 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project); MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow(); - MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition(); MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition(); - IFile mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, "")); - - MicroInstructionMemory mpm; - try (InputStream mpmStream = mpmFile.getContents()) - { - mpm = MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream); - } - catch (IOException e) - { - throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e)); - } + mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, "")); String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, ""); MainMemory mem; @@ -161,16 +158,30 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate MachineDebugTarget debugTarget = new MachineDebugTarget(launch, machineDefinition); debugTarget.suspend(); debugTarget.setExecutionSpeed(1); - Machine machine = debugTarget.getMachine(); - machine.getMicroInstructionMemory().bind(mpm); + machine = debugTarget.getMachine(); + assignMicroInstructionMemory(); if (mem != null) machine.getMainMemory().bind(mem); machine.reset(); } + private void assignMicroInstructionMemory() throws CoreException + { + try (InputStream mpmStream = mpmFile.getContents()) + { + MicroInstructionMemory mpm = MicroInstructionMemoryParser + .parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream); + machine.getMicroInstructionMemory().bind(mpm); + } + catch (IOException e) + { + throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e)); + } + } + private void resourceChanged(IResourceChangeEvent event) { - // TODO react to MPM changes + // TODO remove Sysout int type = event.getType(); String typeStr; switch (type) @@ -180,6 +191,33 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate break; case IResourceChangeEvent.POST_CHANGE: typeStr = "POST_CHANGE"; + IResourceDelta mpmDelta; + if ((mpmDelta = event.getDelta().findMember(mpmFile.getFullPath())) != null && (mpmDelta.getKind() & CHANGED) == CHANGED + && mpmFile.exists()) + { + AtomicBoolean doHotReplace = new AtomicBoolean(); + PlatformUI.getWorkbench().getDisplay().syncExec(() -> + { + if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace MPM?", + String.format("The MPM %s has been modified on the file system. Replace simulated MPM with modified contents?", + mpmFile.getName()))) + doHotReplace.set(true); + }); + if (doHotReplace.get()) + { + try + { + assignMicroInstructionMemory(); + } + catch (CoreException e) + { + PlatformUI.getWorkbench().getDisplay() + .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), + "Failed Hot Replace!", + "An error occurred trying to read the modified MPM from the file system: " + e.getMessage())); + } + } + } break; case IResourceChangeEvent.PRE_BUILD: typeStr = "PRE_BUILD";