1 package net.mograsim.plugin.launch;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Optional;
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;
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;
34 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
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";
40 private final IResourceChangeListener resChangedListener;
42 public MachineLaunchConfigType()
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 |
56 public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
58 String projName = configuration.getAttribute(PROJECT_ATTR, "");
59 if ("".equals(projName))
60 return showErrorAndReturnFalse("No project specified");
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");
68 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
69 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
70 if (machDefOptional.isEmpty())
71 return showErrorAndReturnFalse("No machine definition set");
73 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
74 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
75 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
77 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
78 if ("".equals(mpmFileName))
79 return showErrorAndReturnFalse("No MPM file specified");
81 IFile mpmFile = project.getFile(mpmFileName);
82 if (mpmFile == null || !mpmFile.isAccessible())
83 return showErrorAndReturnFalse("MPM file not accessible");
85 try (InputStream mpmStream = mpmFile.getContents())
87 MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
91 throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
94 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
95 if (!"".equals(initialRAMFileName))
97 IFile initialRAMFile = project.getFile(initialRAMFileName);
98 if (initialRAMFile == null || !initialRAMFile.isAccessible())
99 return showErrorAndReturnFalse("Initial RAM file not accessible");
101 try (InputStream initialRAMStream = initialRAMFile.getContents())
103 MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
105 catch (IOException e)
107 throw new CoreException(
108 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
112 return super.preLaunchCheck(configuration, mode, monitor);
115 private static boolean showErrorAndReturnFalse(String message)
117 StatusManager.getManager().handle(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, message, null), StatusManager.SHOW);
122 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException
124 String projName = configuration.getAttribute(PROJECT_ATTR, "");
125 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
127 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
128 MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
129 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
130 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
132 IFile mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
134 MicroInstructionMemory mpm;
135 try (InputStream mpmStream = mpmFile.getContents())
137 mpm = MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
139 catch (IOException e)
141 throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
144 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
146 if (!"".equals(initialRAMFileName))
148 IFile initialRAMFile = project.getFile(initialRAMFileName);
149 try (InputStream initialRAMStream = initialRAMFile.getContents())
151 mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
153 catch (IOException e)
155 throw new CoreException(
156 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
161 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, machineDefinition);
162 debugTarget.suspend();
163 debugTarget.setExecutionSpeed(1);
164 Machine machine = debugTarget.getMachine();
165 machine.getMicroInstructionMemory().bind(mpm);
167 machine.getMainMemory().bind(mem);
171 private void resourceChanged(IResourceChangeEvent event)
173 // TODO react to MPM changes
174 int type = event.getType();
178 case IResourceChangeEvent.POST_BUILD:
179 typeStr = "POST_BUILD";
181 case IResourceChangeEvent.POST_CHANGE:
182 typeStr = "POST_CHANGE";
184 case IResourceChangeEvent.PRE_BUILD:
185 typeStr = "PRE_BUILD";
187 case IResourceChangeEvent.PRE_CLOSE:
188 typeStr = "PRE_CLOSE";
190 case IResourceChangeEvent.PRE_DELETE:
191 typeStr = "PRE_DELETE";
193 case IResourceChangeEvent.PRE_REFRESH:
194 typeStr = "PRE_REFRESH";
197 typeStr = "<unknown: " + type + ">";
199 System.out.println(typeStr + ": " + event);