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();
139 mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
141 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
143 if (!"".equals(initialRAMFileName))
145 IFile initialRAMFile = project.getFile(initialRAMFileName);
146 try (InputStream initialRAMStream = initialRAMFile.getContents())
148 mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
150 catch (IOException e)
152 throw new CoreException(
153 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
158 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, machineDefinition);
159 debugTarget.suspend();
160 debugTarget.setExecutionSpeed(1);
161 machine = debugTarget.getMachine();
162 assignMicroInstructionMemory();
164 machine.getMainMemory().bind(mem);
168 private void assignMicroInstructionMemory() throws CoreException
170 try (InputStream mpmStream = mpmFile.getContents())
172 MicroInstructionMemory mpm = MicroInstructionMemoryParser
173 .parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream);
174 machine.getMicroInstructionMemory().bind(mpm);
176 catch (IOException e)
178 throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
182 private void resourceChanged(IResourceChangeEvent event)
184 // TODO remove Sysout
185 int type = event.getType();
189 case IResourceChangeEvent.POST_BUILD:
190 typeStr = "POST_BUILD";
192 case IResourceChangeEvent.POST_CHANGE:
193 typeStr = "POST_CHANGE";
194 IResourceDelta mpmDelta;
195 if ((mpmDelta = event.getDelta().findMember(mpmFile.getFullPath())) != null && (mpmDelta.getKind() & CHANGED) == CHANGED
198 AtomicBoolean doHotReplace = new AtomicBoolean();
199 PlatformUI.getWorkbench().getDisplay().syncExec(() ->
201 if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace MPM?",
202 String.format("The MPM %s has been modified on the file system. Replace simulated MPM with modified contents?",
204 doHotReplace.set(true);
206 if (doHotReplace.get())
210 assignMicroInstructionMemory();
212 catch (CoreException e)
214 PlatformUI.getWorkbench().getDisplay()
215 .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
216 "Failed Hot Replace!",
217 "An error occurred trying to read the modified MPM from the file system: " + e.getMessage()));
222 case IResourceChangeEvent.PRE_BUILD:
223 typeStr = "PRE_BUILD";
225 case IResourceChangeEvent.PRE_CLOSE:
226 typeStr = "PRE_CLOSE";
228 case IResourceChangeEvent.PRE_DELETE:
229 typeStr = "PRE_DELETE";
231 case IResourceChangeEvent.PRE_REFRESH:
232 typeStr = "PRE_REFRESH";
235 typeStr = "<unknown: " + type + ">";
237 System.out.println(typeStr + ": " + event);