1 package net.mograsim.plugin.launch;
3 import static org.eclipse.core.resources.IResourceDelta.CHANGED;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Optional;
8 import java.util.concurrent.atomic.AtomicBoolean;
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.resources.IProject;
12 import org.eclipse.core.resources.IResourceChangeEvent;
13 import org.eclipse.core.resources.IResourceChangeListener;
14 import org.eclipse.core.resources.IResourceDelta;
15 import org.eclipse.core.resources.ResourcesPlugin;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.debug.core.ILaunch;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.statushandlers.StatusManager;
27 import net.mograsim.machine.Machine;
28 import net.mograsim.machine.MachineDefinition;
29 import net.mograsim.machine.MainMemory;
30 import net.mograsim.machine.MainMemoryDefinition;
31 import net.mograsim.machine.mi.MicroInstructionMemory;
32 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
33 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
34 import net.mograsim.machine.standard.memory.MainMemoryParser;
35 import net.mograsim.plugin.MograsimActivator;
36 import net.mograsim.plugin.nature.MachineContext;
37 import net.mograsim.plugin.nature.MograsimNature;
38 import net.mograsim.plugin.nature.ProjectMachineContext;
40 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
42 public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + ".project";
43 public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
44 public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
46 private final IResourceChangeListener resChangedListener;
47 private IFile mpmFile;
48 private Machine machine;
50 public MachineLaunchConfigType()
52 this.resChangedListener = this::resourceChanged;
53 ResourcesPlugin.getWorkspace().addResourceChangeListener(resChangedListener,
54 // IResourceChangeEvent.POST_BUILD |
55 IResourceChangeEvent.POST_CHANGE |
56 // IResourceChangeEvent.PRE_BUILD |
57 // IResourceChangeEvent.PRE_CLOSE |
58 // IResourceChangeEvent.PRE_DELETE |
59 // IResourceChangeEvent.PRE_REFRESH |
64 public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
66 String projName = configuration.getAttribute(PROJECT_ATTR, "");
67 if ("".equals(projName))
68 return showErrorAndReturnFalse("No project specified");
70 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
71 if (!project.isAccessible())
72 return showErrorAndReturnFalse("Project not accessible");
73 if (!project.hasNature(MograsimNature.NATURE_ID))
74 return showErrorAndReturnFalse("Project is not a Mograsim project");
76 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
77 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
78 if (machDefOptional.isEmpty())
79 return showErrorAndReturnFalse("No machine definition set");
81 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
82 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
83 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
85 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
86 if ("".equals(mpmFileName))
87 return showErrorAndReturnFalse("No MPM file specified");
89 IFile mpmFile = project.getFile(mpmFileName);
90 if (mpmFile == null || !mpmFile.isAccessible())
91 return showErrorAndReturnFalse("MPM file not accessible");
93 try (InputStream mpmStream = mpmFile.getContents())
95 MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
99 throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
102 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
103 if (!"".equals(initialRAMFileName))
105 IFile initialRAMFile = project.getFile(initialRAMFileName);
106 if (initialRAMFile == null || !initialRAMFile.isAccessible())
107 return showErrorAndReturnFalse("Initial RAM file not accessible");
109 try (InputStream initialRAMStream = initialRAMFile.getContents())
111 MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
113 catch (IOException e)
115 throw new CoreException(
116 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
120 return super.preLaunchCheck(configuration, mode, monitor);
123 private static boolean showErrorAndReturnFalse(String message)
125 StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW);
130 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException
132 String projName = configuration.getAttribute(PROJECT_ATTR, "");
133 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
135 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
136 MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
137 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
140 mpmFile = project.getFile(mpmName = configuration.getAttribute(MPM_FILE_ATTR, ""));
142 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
144 if (!"".equals(initialRAMFileName))
146 IFile initialRAMFile = project.getFile(initialRAMFileName);
147 try (InputStream initialRAMStream = initialRAMFile.getContents())
149 mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
151 catch (IOException e)
153 throw new CoreException(
154 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
159 MachineLaunchParams params = new MachineLaunchParams(projName, mpmName, initialRAMFileName);
160 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, params, machineDefinition);
161 debugTarget.suspend();
162 debugTarget.setExecutionSpeed(1);
163 machine = debugTarget.getMachine();
164 assignMicroInstructionMemory();
166 machine.getMainMemory().bind(mem);
170 private void assignMicroInstructionMemory() throws CoreException
172 try (InputStream mpmStream = mpmFile.getContents())
174 MicroInstructionMemory mpm = MicroInstructionMemoryParser
175 .parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream);
176 machine.getMicroInstructionMemory().bind(mpm);
178 catch (IOException e)
180 throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
184 private void resourceChanged(IResourceChangeEvent event)
186 // TODO remove Sysout
187 int type = event.getType();
191 case IResourceChangeEvent.POST_BUILD:
192 typeStr = "POST_BUILD";
194 case IResourceChangeEvent.POST_CHANGE:
195 typeStr = "POST_CHANGE";
196 IResourceDelta mpmDelta;
197 if ((mpmDelta = event.getDelta().findMember(mpmFile.getFullPath())) != null && (mpmDelta.getKind() & CHANGED) == CHANGED
200 AtomicBoolean doHotReplace = new AtomicBoolean();
201 PlatformUI.getWorkbench().getDisplay().syncExec(() ->
203 if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace MPM?",
204 String.format("The MPM %s has been modified on the file system. Replace simulated MPM with modified contents?",
206 doHotReplace.set(true);
208 if (doHotReplace.get())
212 assignMicroInstructionMemory();
214 catch (CoreException e)
216 PlatformUI.getWorkbench().getDisplay()
217 .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
218 "Failed Hot Replace!",
219 "An error occurred trying to read the modified MPM from the file system: " + e.getMessage()));
224 case IResourceChangeEvent.PRE_BUILD:
225 typeStr = "PRE_BUILD";
227 case IResourceChangeEvent.PRE_CLOSE:
228 typeStr = "PRE_CLOSE";
230 case IResourceChangeEvent.PRE_DELETE:
231 typeStr = "PRE_DELETE";
233 case IResourceChangeEvent.PRE_REFRESH:
234 typeStr = "PRE_REFRESH";
237 typeStr = "<unknown: " + type + ">";
239 System.out.println(typeStr + ": " + event);
242 public static class MachineLaunchParams
244 public final String projectPath, mpmPath, ramPath;
246 MachineLaunchParams(String projectPath, String mpmPath, String ramPath)
248 this.projectPath = projectPath;
249 this.mpmPath = mpmPath;
250 this.ramPath = ramPath;
253 public String getProjectPath()
258 public String getMpmPath()
263 public String getRamPath()