ab3650833be19c9954ec6d3802e8008865ced35d
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MachineLaunchConfigType.java
1 package net.mograsim.plugin.launch;
2
3 import static org.eclipse.core.resources.IResourceDelta.CHANGED;
4
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Optional;
8 import java.util.concurrent.atomic.AtomicBoolean;
9
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;
26
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;
39
40 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
41 {
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";
45
46         private final IResourceChangeListener resChangedListener;
47         private IFile mpmFile;
48         private Machine machine;
49
50         public MachineLaunchConfigType()
51         {
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 |
60                                                 0);
61         }
62
63         @Override
64         public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
65         {
66                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
67                 if ("".equals(projName))
68                         return showErrorAndReturnFalse("No project specified");
69
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");
75
76                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
77                 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
78                 if (machDefOptional.isEmpty())
79                         return showErrorAndReturnFalse("No machine definition set");
80
81                 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
82                 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
83                 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
84
85                 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
86                 if ("".equals(mpmFileName))
87                         return showErrorAndReturnFalse("No MPM file specified");
88
89                 IFile mpmFile = project.getFile(mpmFileName);
90                 if (mpmFile == null || !mpmFile.isAccessible())
91                         return showErrorAndReturnFalse("MPM file not accessible");
92
93                 try (InputStream mpmStream = mpmFile.getContents())
94                 {
95                         MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
96                 }
97                 catch (IOException e)
98                 {
99                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
100                 }
101
102                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
103                 if (!"".equals(initialRAMFileName))
104                 {
105                         IFile initialRAMFile = project.getFile(initialRAMFileName);
106                         if (initialRAMFile == null || !initialRAMFile.isAccessible())
107                                 return showErrorAndReturnFalse("Initial RAM file not accessible");
108
109                         try (InputStream initialRAMStream = initialRAMFile.getContents())
110                         {
111                                 MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
112                         }
113                         catch (IOException e)
114                         {
115                                 throw new CoreException(
116                                                 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
117                         }
118                 }
119
120                 return super.preLaunchCheck(configuration, mode, monitor);
121         }
122
123         private static boolean showErrorAndReturnFalse(String message)
124         {
125                 StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW);
126                 return false;
127         }
128
129         @Override
130         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException
131         {
132                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
133                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
134
135                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
136                 MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
137                 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
138
139                 String mpmName;
140                 mpmFile = project.getFile(mpmName = configuration.getAttribute(MPM_FILE_ATTR, ""));
141
142                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
143                 MainMemory mem;
144                 if (!"".equals(initialRAMFileName))
145                 {
146                         IFile initialRAMFile = project.getFile(initialRAMFileName);
147                         try (InputStream initialRAMStream = initialRAMFile.getContents())
148                         {
149                                 mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
150                         }
151                         catch (IOException e)
152                         {
153                                 throw new CoreException(
154                                                 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
155                         }
156                 } else
157                         mem = null;
158
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();
165                 if (mem != null)
166                         machine.getMainMemory().bind(mem);
167                 machine.reset();
168         }
169
170         private void assignMicroInstructionMemory() throws CoreException
171         {
172                 try (InputStream mpmStream = mpmFile.getContents())
173                 {
174                         MicroInstructionMemory mpm = MicroInstructionMemoryParser
175                                         .parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream);
176                         machine.getMicroInstructionMemory().bind(mpm);
177                 }
178                 catch (IOException e)
179                 {
180                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
181                 }
182         }
183
184         private void resourceChanged(IResourceChangeEvent event)
185         {
186                 // TODO remove Sysout
187                 int type = event.getType();
188                 String typeStr;
189                 switch (type)
190                 {
191                 case IResourceChangeEvent.POST_BUILD:
192                         typeStr = "POST_BUILD";
193                         break;
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
198                                         && mpmFile.exists())
199                         {
200                                 AtomicBoolean doHotReplace = new AtomicBoolean();
201                                 PlatformUI.getWorkbench().getDisplay().syncExec(() ->
202                                 {
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?",
205                                                                         mpmFile.getName())))
206                                                 doHotReplace.set(true);
207                                 });
208                                 if (doHotReplace.get())
209                                 {
210                                         try
211                                         {
212                                                 assignMicroInstructionMemory();
213                                         }
214                                         catch (CoreException e)
215                                         {
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()));
220                                         }
221                                 }
222                         }
223                         break;
224                 case IResourceChangeEvent.PRE_BUILD:
225                         typeStr = "PRE_BUILD";
226                         break;
227                 case IResourceChangeEvent.PRE_CLOSE:
228                         typeStr = "PRE_CLOSE";
229                         break;
230                 case IResourceChangeEvent.PRE_DELETE:
231                         typeStr = "PRE_DELETE";
232                         break;
233                 case IResourceChangeEvent.PRE_REFRESH:
234                         typeStr = "PRE_REFRESH";
235                         break;
236                 default:
237                         typeStr = "<unknown: " + type + ">";
238                 }
239                 System.out.println(typeStr + ": " + event);
240         }
241
242         public static class MachineLaunchParams
243         {
244                 public final String projectPath, mpmPath, ramPath;
245
246                 MachineLaunchParams(String projectPath, String mpmPath, String ramPath)
247                 {
248                         this.projectPath = projectPath;
249                         this.mpmPath = mpmPath;
250                         this.ramPath = ramPath;
251                 }
252
253                 public String getProjectPath()
254                 {
255                         return projectPath;
256                 }
257
258                 public String getMpmPath()
259                 {
260                         return mpmPath;
261                 }
262
263                 public String getRamPath()
264                 {
265                         return ramPath;
266                 }
267         }
268 }