Added hot replace functionality for modified mpm files
[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                 mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
140
141                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
142                 MainMemory mem;
143                 if (!"".equals(initialRAMFileName))
144                 {
145                         IFile initialRAMFile = project.getFile(initialRAMFileName);
146                         try (InputStream initialRAMStream = initialRAMFile.getContents())
147                         {
148                                 mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
149                         }
150                         catch (IOException e)
151                         {
152                                 throw new CoreException(
153                                                 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
154                         }
155                 } else
156                         mem = null;
157
158                 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, machineDefinition);
159                 debugTarget.suspend();
160                 debugTarget.setExecutionSpeed(1);
161                 machine = debugTarget.getMachine();
162                 assignMicroInstructionMemory();
163                 if (mem != null)
164                         machine.getMainMemory().bind(mem);
165                 machine.reset();
166         }
167
168         private void assignMicroInstructionMemory() throws CoreException
169         {
170                 try (InputStream mpmStream = mpmFile.getContents())
171                 {
172                         MicroInstructionMemory mpm = MicroInstructionMemoryParser
173                                         .parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream);
174                         machine.getMicroInstructionMemory().bind(mpm);
175                 }
176                 catch (IOException e)
177                 {
178                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
179                 }
180         }
181
182         private void resourceChanged(IResourceChangeEvent event)
183         {
184                 // TODO remove Sysout
185                 int type = event.getType();
186                 String typeStr;
187                 switch (type)
188                 {
189                 case IResourceChangeEvent.POST_BUILD:
190                         typeStr = "POST_BUILD";
191                         break;
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
196                                         && mpmFile.exists())
197                         {
198                                 AtomicBoolean doHotReplace = new AtomicBoolean();
199                                 PlatformUI.getWorkbench().getDisplay().syncExec(() ->
200                                 {
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?",
203                                                                         mpmFile.getName())))
204                                                 doHotReplace.set(true);
205                                 });
206                                 if (doHotReplace.get())
207                                 {
208                                         try
209                                         {
210                                                 assignMicroInstructionMemory();
211                                         }
212                                         catch (CoreException e)
213                                         {
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()));
218                                         }
219                                 }
220                         }
221                         break;
222                 case IResourceChangeEvent.PRE_BUILD:
223                         typeStr = "PRE_BUILD";
224                         break;
225                 case IResourceChangeEvent.PRE_CLOSE:
226                         typeStr = "PRE_CLOSE";
227                         break;
228                 case IResourceChangeEvent.PRE_DELETE:
229                         typeStr = "PRE_DELETE";
230                         break;
231                 case IResourceChangeEvent.PRE_REFRESH:
232                         typeStr = "PRE_REFRESH";
233                         break;
234                 default:
235                         typeStr = "<unknown: " + type + ">";
236                 }
237                 System.out.println(typeStr + ": " + event);
238         }
239 }