X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Flaunch%2FMachineLaunchConfigType.java;h=32ea832596651cb261bfb8db76db0e64e90502fb;hb=da1a7df25582ae1fe277762c2ba5640d4697a71c;hp=27aca7e18311bbca780dc63630a7749aa116b744;hpb=a873ef940ba160f284ba6fa3fee1b9704bf68858;p=Mograsim.git 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 27aca7e1..32ea8325 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,23 +1,37 @@ package net.mograsim.plugin.launch; +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; + +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.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +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.ui.statushandlers.StatusManager; +import net.mograsim.machine.Machine; import net.mograsim.machine.MachineDefinition; +import net.mograsim.machine.mi.MicroInstructionMemory; +import net.mograsim.machine.mi.MicroInstructionMemoryDefinition; +import net.mograsim.machine.mi.MicroInstructionMemoryParser; import net.mograsim.plugin.MograsimActivator; import net.mograsim.plugin.nature.MachineContext; +import net.mograsim.plugin.nature.MograsimNature; import net.mograsim.plugin.nature.ProjectMachineContext; public class MachineLaunchConfigType extends LaunchConfigurationDelegate { public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + "project"; + public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + "mpm"; private final IResourceChangeListener resChangedListener; @@ -34,16 +48,80 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate 0); } + @Override + public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException + { + String projName = configuration.getAttribute(PROJECT_ATTR, ""); + if ("".equals(projName)) + return showErrorAndReturnFalse("No project specified"); + + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName); + if (!project.isAccessible()) + return showErrorAndReturnFalse("Project not accessible"); + if (!project.hasNature(MograsimNature.NATURE_ID)) + return showErrorAndReturnFalse("Project is not a Mograsim project"); + + MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project); + Optional machDefOptional = machineContext.getMachineDefinition(); + if (machDefOptional.isEmpty()) + return showErrorAndReturnFalse("No machine definition set"); + + MachineDefinition machineDefinition = machDefOptional.orElseThrow(); + MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition(); + + String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, ""); + if ("".equals(mpmFileName)) + return showErrorAndReturnFalse("No MPM file specified"); + + IFile mpmFile = project.getFile(mpmFileName); + if (!mpmFile.isAccessible()) + return showErrorAndReturnFalse("MPM file not accessible"); + + try (InputStream mpmStream = mpmFile.getContents()) + { + MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream); + } + catch (IOException e) + { + throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e)); + } + + return super.preLaunchCheck(configuration, mode, monitor); + } + + private static boolean showErrorAndReturnFalse(String message) + { + StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW); + return false; + } + @Override public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { String projName = configuration.getAttribute(PROJECT_ATTR, ""); IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName); + MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project); MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow(); + MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition(); + + 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)); + } MachineDebugTarget debugTarget = new MachineDebugTarget(new MachineProcess(launch, machineDefinition)); - debugTarget.setExecutionSpeed(10d); + debugTarget.setExecutionSpeed(1); + Machine machine = debugTarget.getMachine(); + machine.getMicroInstructionMemory().bind(mpm); + machine.reset(); } private void resourceChanged(IResourceChangeEvent event)