e9b6531bc537171fb7f37716c10bb7141c5c2a82
[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.mi.MicroInstructionMemory;
24 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
25 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
26 import net.mograsim.plugin.MograsimActivator;
27 import net.mograsim.plugin.nature.MachineContext;
28 import net.mograsim.plugin.nature.MograsimNature;
29 import net.mograsim.plugin.nature.ProjectMachineContext;
30
31 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
32 {
33         public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + ".project";
34         public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
35         public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
36
37         private final IResourceChangeListener resChangedListener;
38
39         public MachineLaunchConfigType()
40         {
41                 this.resChangedListener = this::resourceChanged;
42                 ResourcesPlugin.getWorkspace().addResourceChangeListener(resChangedListener,
43                                 // IResourceChangeEvent.POST_BUILD |
44                                 IResourceChangeEvent.POST_CHANGE |
45                                 // IResourceChangeEvent.PRE_BUILD |
46                                 // IResourceChangeEvent.PRE_CLOSE |
47                                 // IResourceChangeEvent.PRE_DELETE |
48                                 // IResourceChangeEvent.PRE_REFRESH |
49                                                 0);
50         }
51
52         @Override
53         public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
54         {
55                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
56                 if ("".equals(projName))
57                         return showErrorAndReturnFalse("No project specified");
58
59                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
60                 if (!project.isAccessible())
61                         return showErrorAndReturnFalse("Project not accessible");
62                 if (!project.hasNature(MograsimNature.NATURE_ID))
63                         return showErrorAndReturnFalse("Project is not a Mograsim project");
64
65                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
66                 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
67                 if (machDefOptional.isEmpty())
68                         return showErrorAndReturnFalse("No machine definition set");
69
70                 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
71                 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
72
73                 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
74                 if ("".equals(mpmFileName))
75                         return showErrorAndReturnFalse("No MPM file specified");
76
77                 IFile mpmFile = project.getFile(mpmFileName);
78                 if (!mpmFile.isAccessible())
79                         return showErrorAndReturnFalse("MPM file not accessible");
80
81                 try (InputStream mpmStream = mpmFile.getContents())
82                 {
83                         MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
84                 }
85                 catch (IOException e)
86                 {
87                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
88                 }
89
90                 // TODO parse RAM
91
92                 return super.preLaunchCheck(configuration, mode, monitor);
93         }
94
95         private static boolean showErrorAndReturnFalse(String message)
96         {
97                 StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW);
98                 return false;
99         }
100
101         @Override
102         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException
103         {
104                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
105                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
106
107                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
108                 MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
109                 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
110
111                 IFile mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
112
113                 MicroInstructionMemory mpm;
114                 try (InputStream mpmStream = mpmFile.getContents())
115                 {
116                         mpm = MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
117                 }
118                 catch (IOException e)
119                 {
120                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
121                 }
122
123                 // TODO parse RAM
124
125                 MachineDebugTarget debugTarget = new MachineDebugTarget(new MachineProcess(launch, machineDefinition));
126                 debugTarget.setExecutionSpeed(1);
127                 Machine machine = debugTarget.getMachine();
128                 machine.getMicroInstructionMemory().bind(mpm);
129                 // TODO bind RAM
130                 machine.reset();
131         }
132
133         private void resourceChanged(IResourceChangeEvent event)
134         {
135                 // TODO react to MPM changes
136                 int type = event.getType();
137                 String typeStr;
138                 switch (type)
139                 {
140                 case IResourceChangeEvent.POST_BUILD:
141                         typeStr = "POST_BUILD";
142                         break;
143                 case IResourceChangeEvent.POST_CHANGE:
144                         typeStr = "POST_CHANGE";
145                         break;
146                 case IResourceChangeEvent.PRE_BUILD:
147                         typeStr = "PRE_BUILD";
148                         break;
149                 case IResourceChangeEvent.PRE_CLOSE:
150                         typeStr = "PRE_CLOSE";
151                         break;
152                 case IResourceChangeEvent.PRE_DELETE:
153                         typeStr = "PRE_DELETE";
154                         break;
155                 case IResourceChangeEvent.PRE_REFRESH:
156                         typeStr = "PRE_REFRESH";
157                         break;
158                 default:
159                         typeStr = "<unknown: " + type + ">";
160                 }
161                 System.out.println(typeStr + ": " + event);
162         }
163 }