1 package net.mograsim.plugin.launch;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Optional;
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.ResourcesPlugin;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.core.runtime.Status;
14 import org.eclipse.debug.core.ILaunch;
15 import org.eclipse.debug.core.ILaunchConfiguration;
16 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
17 import org.eclipse.ui.statushandlers.StatusManager;
19 import net.mograsim.machine.Machine;
20 import net.mograsim.machine.MachineDefinition;
21 import net.mograsim.machine.MainMemoryDefinition;
22 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
23 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
24 import net.mograsim.machine.standard.memory.MainMemoryParser;
25 import net.mograsim.plugin.MograsimActivator;
26 import net.mograsim.plugin.nature.MachineContext;
27 import net.mograsim.plugin.nature.MograsimNature;
28 import net.mograsim.plugin.nature.ProjectMachineContext;
30 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
32 public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + ".project";
33 public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
34 public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
36 private IFile mpmFile;
37 private Machine machine;
40 public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
42 String projName = configuration.getAttribute(PROJECT_ATTR, "");
43 if ("".equals(projName))
44 return showErrorAndReturnFalse("No project specified");
46 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
47 if (!project.isAccessible())
48 return showErrorAndReturnFalse("Project not accessible");
49 if (!project.hasNature(MograsimNature.NATURE_ID))
50 return showErrorAndReturnFalse("Project is not a Mograsim project");
52 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
53 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
54 if (machDefOptional.isEmpty())
55 return showErrorAndReturnFalse("No machine definition set");
57 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
58 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
59 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
61 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
62 if ("".equals(mpmFileName))
63 return showErrorAndReturnFalse("No MPM file specified");
65 IFile mpmFile = project.getFile(mpmFileName);
66 if (mpmFile == null || !mpmFile.isAccessible())
67 return showErrorAndReturnFalse("MPM file not accessible");
69 try (InputStream mpmStream = mpmFile.getContents())
71 MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
75 throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
78 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
79 if (!"".equals(initialRAMFileName))
81 IFile initialRAMFile = project.getFile(initialRAMFileName);
82 if (initialRAMFile == null || !initialRAMFile.isAccessible())
83 return showErrorAndReturnFalse("Initial RAM file not accessible");
85 try (InputStream initialRAMStream = initialRAMFile.getContents())
87 MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
91 throw new CoreException(
92 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
96 return super.preLaunchCheck(configuration, mode, monitor);
99 private static boolean showErrorAndReturnFalse(String message)
101 StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW);
106 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException
108 String projName = configuration.getAttribute(PROJECT_ATTR, "");
109 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
111 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
112 MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
114 mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
116 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
117 Optional<IFile> memFile = Optional.empty();
118 if (!"".equals(initialRAMFileName))
120 memFile = Optional.of(project.getFile(initialRAMFileName));
122 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, mpmFile, memFile, machineDefinition);
123 debugTarget.suspend();
124 debugTarget.setExecutionSpeed(1);
125 machine = debugTarget.getMachine();