286975a6f1d7109ee0655fb16e6bc07f2e199ed5
[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.ResourcesPlugin;
10 import org.eclipse.core.runtime.CoreException;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.core.runtime.IStatus;
13 import org.eclipse.core.runtime.Status;
14 import org.eclipse.debug.core.ILaunch;
15 import org.eclipse.debug.core.ILaunchConfiguration;
16 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
17 import org.eclipse.ui.statushandlers.StatusManager;
18
19 import net.mograsim.machine.Machine;
20 import net.mograsim.machine.MachineDefinition;
21 import net.mograsim.machine.MainMemoryDefinition;
22 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
23 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
24 import net.mograsim.machine.standard.memory.MainMemoryParser;
25 import net.mograsim.plugin.MograsimActivator;
26 import net.mograsim.plugin.nature.MachineContext;
27 import net.mograsim.plugin.nature.MograsimNature;
28 import net.mograsim.plugin.nature.ProjectMachineContext;
29
30 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
31 {
32         public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + ".project";
33         public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
34         public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
35
36         private IFile mpmFile;
37         private Machine machine;
38
39         @Override
40         public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
41         {
42                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
43                 if ("".equals(projName))
44                         return showErrorAndReturnFalse("No project specified");
45
46                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
47                 if (!project.isAccessible())
48                         return showErrorAndReturnFalse("Project not accessible");
49                 if (!project.hasNature(MograsimNature.NATURE_ID))
50                         return showErrorAndReturnFalse("Project is not a Mograsim project");
51
52                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
53                 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
54                 if (machDefOptional.isEmpty())
55                         return showErrorAndReturnFalse("No machine definition set");
56
57                 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
58                 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
59                 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
60
61                 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
62                 if ("".equals(mpmFileName))
63                         return showErrorAndReturnFalse("No MPM file specified");
64
65                 IFile mpmFile = project.getFile(mpmFileName);
66                 if (mpmFile == null || !mpmFile.isAccessible())
67                         return showErrorAndReturnFalse("MPM file not accessible");
68
69                 try (InputStream mpmStream = mpmFile.getContents())
70                 {
71                         MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
72                 }
73                 catch (IOException e)
74                 {
75                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
76                 }
77
78                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
79                 if (!"".equals(initialRAMFileName))
80                 {
81                         IFile initialRAMFile = project.getFile(initialRAMFileName);
82                         if (initialRAMFile == null || !initialRAMFile.isAccessible())
83                                 return showErrorAndReturnFalse("Initial RAM file not accessible");
84
85                         try (InputStream initialRAMStream = initialRAMFile.getContents())
86                         {
87                                 MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
88                         }
89                         catch (IOException e)
90                         {
91                                 throw new CoreException(
92                                                 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
93                         }
94                 }
95
96                 return super.preLaunchCheck(configuration, mode, monitor);
97         }
98
99         private static boolean showErrorAndReturnFalse(String message)
100         {
101                 StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW);
102                 return false;
103         }
104
105         @Override
106         public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException
107         {
108                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
109                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
110
111                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
112                 MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
113
114                 mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
115
116                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
117                 Optional<IFile> memFile = Optional.empty();
118                 if (!"".equals(initialRAMFileName))
119                 {
120                         memFile = Optional.of(project.getFile(initialRAMFileName));
121                 }
122                 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, mpmFile, memFile, machineDefinition);
123                 // TODO make selectable whether the machine starts paused or not
124                 debugTarget.suspend();
125                 debugTarget.setExecutionSpeed(1);
126                 machine = debugTarget.getMachine();
127                 machine.reset();
128         }
129
130 }