82d7e8be8d96d7f1aa0b8165638aed0e6d2cca32
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MachineLaunchConfigType.java
1 package net.mograsim.plugin.launch;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Optional;
6
7 import org.eclipse.core.resources.IFile;
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.resources.IResourceChangeEvent;
10 import org.eclipse.core.resources.IResourceChangeListener;
11 import org.eclipse.core.resources.ResourcesPlugin;
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.debug.core.ILaunch;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
19 import org.eclipse.ui.statushandlers.StatusManager;
20
21 import net.mograsim.machine.Machine;
22 import net.mograsim.machine.MachineDefinition;
23 import net.mograsim.machine.MainMemory;
24 import net.mograsim.machine.MainMemoryDefinition;
25 import net.mograsim.machine.mi.MicroInstructionMemory;
26 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
27 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
28 import net.mograsim.machine.standard.memory.MainMemoryParser;
29 import net.mograsim.plugin.MograsimActivator;
30 import net.mograsim.plugin.nature.MachineContext;
31 import net.mograsim.plugin.nature.MograsimNature;
32 import net.mograsim.plugin.nature.ProjectMachineContext;
33
34 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
35 {
36         public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + ".project";
37         public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
38         public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
39
40         private final IResourceChangeListener resChangedListener;
41
42         public MachineLaunchConfigType()
43         {
44                 this.resChangedListener = this::resourceChanged;
45                 ResourcesPlugin.getWorkspace().addResourceChangeListener(resChangedListener,
46                                 // IResourceChangeEvent.POST_BUILD |
47                                 IResourceChangeEvent.POST_CHANGE |
48                                 // IResourceChangeEvent.PRE_BUILD |
49                                 // IResourceChangeEvent.PRE_CLOSE |
50                                 // IResourceChangeEvent.PRE_DELETE |
51                                 // IResourceChangeEvent.PRE_REFRESH |
52                                                 0);
53         }
54
55         @Override
56         public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
57         {
58                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
59                 if ("".equals(projName))
60                         return showErrorAndReturnFalse("No project specified");
61
62                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
63                 if (!project.isAccessible())
64                         return showErrorAndReturnFalse("Project not accessible");
65                 if (!project.hasNature(MograsimNature.NATURE_ID))
66                         return showErrorAndReturnFalse("Project is not a Mograsim project");
67
68                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
69                 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
70                 if (machDefOptional.isEmpty())
71                         return showErrorAndReturnFalse("No machine definition set");
72
73                 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
74                 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
75                 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
76
77                 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
78                 if ("".equals(mpmFileName))
79                         return showErrorAndReturnFalse("No MPM file specified");
80
81                 IFile mpmFile = project.getFile(mpmFileName);
82                 if (mpmFile == null || !mpmFile.isAccessible())
83                         return showErrorAndReturnFalse("MPM file not accessible");
84
85                 try (InputStream mpmStream = mpmFile.getContents())
86                 {
87                         MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
88                 }
89                 catch (IOException e)
90                 {
91                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
92                 }
93
94                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
95                 if (!"".equals(initialRAMFileName))
96                 {
97                         IFile initialRAMFile = project.getFile(initialRAMFileName);
98                         if (initialRAMFile == null || !initialRAMFile.isAccessible())
99                                 return showErrorAndReturnFalse("Initial RAM file not accessible");
100
101                         try (InputStream initialRAMStream = initialRAMFile.getContents())
102                         {
103                                 MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
104                         }
105                         catch (IOException e)
106                         {
107                                 throw new CoreException(
108                                                 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
109                         }
110                 }
111
112                 return super.preLaunchCheck(configuration, mode, monitor);
113         }
114
115         private static boolean showErrorAndReturnFalse(String message)
116         {
117                 StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW);
118                 return false;
119         }
120
121         @Override
122         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException
123         {
124                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
125                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
126
127                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
128                 MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
129                 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
130                 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
131
132                 IFile mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
133
134                 MicroInstructionMemory mpm;
135                 try (InputStream mpmStream = mpmFile.getContents())
136                 {
137                         mpm = MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
138                 }
139                 catch (IOException e)
140                 {
141                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
142                 }
143
144                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
145                 MainMemory mem;
146                 if (!"".equals(initialRAMFileName))
147                 {
148                         IFile initialRAMFile = project.getFile(initialRAMFileName);
149                         try (InputStream initialRAMStream = initialRAMFile.getContents())
150                         {
151                                 mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
152                         }
153                         catch (IOException e)
154                         {
155                                 throw new CoreException(
156                                                 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
157                         }
158                 } else
159                         mem = null;
160
161                 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, machineDefinition);
162                 debugTarget.suspend();
163                 debugTarget.setExecutionSpeed(1);
164                 Machine machine = debugTarget.getMachine();
165                 machine.getMicroInstructionMemory().bind(mpm);
166                 if (mem != null)
167                         machine.getMainMemory().bind(mem);
168                 machine.reset();
169         }
170
171         private void resourceChanged(IResourceChangeEvent event)
172         {
173                 // TODO react to MPM changes
174                 int type = event.getType();
175                 String typeStr;
176                 switch (type)
177                 {
178                 case IResourceChangeEvent.POST_BUILD:
179                         typeStr = "POST_BUILD";
180                         break;
181                 case IResourceChangeEvent.POST_CHANGE:
182                         typeStr = "POST_CHANGE";
183                         break;
184                 case IResourceChangeEvent.PRE_BUILD:
185                         typeStr = "PRE_BUILD";
186                         break;
187                 case IResourceChangeEvent.PRE_CLOSE:
188                         typeStr = "PRE_CLOSE";
189                         break;
190                 case IResourceChangeEvent.PRE_DELETE:
191                         typeStr = "PRE_DELETE";
192                         break;
193                 case IResourceChangeEvent.PRE_REFRESH:
194                         typeStr = "PRE_REFRESH";
195                         break;
196                 default:
197                         typeStr = "<unknown: " + type + ">";
198                 }
199                 System.out.println(typeStr + ": " + event);
200         }
201 }