Finished MPROM support. Fixes #10
[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.DebugPlugin;
15 import org.eclipse.debug.core.ILaunch;
16 import org.eclipse.debug.core.ILaunchConfiguration;
17 import org.eclipse.debug.core.model.IMemoryBlock;
18 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
19 import org.eclipse.ui.statushandlers.StatusManager;
20
21 import net.mograsim.machine.BitVectorMemoryDefinition;
22 import net.mograsim.machine.Machine;
23 import net.mograsim.machine.MachineDefinition;
24 import net.mograsim.machine.MainMemoryDefinition;
25 import net.mograsim.machine.mi.MPROMDefinition;
26 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
27 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
28 import net.mograsim.machine.standard.memory.BitVectorBasedMemoryParser;
29 import net.mograsim.machine.standard.memory.StandardBitVectorMemory;
30 import net.mograsim.plugin.MograsimActivator;
31 import net.mograsim.plugin.nature.MachineContext;
32 import net.mograsim.plugin.nature.MograsimNature;
33 import net.mograsim.plugin.nature.ProjectMachineContext;
34
35 public class MachineLaunchConfigType extends LaunchConfigurationDelegate
36 {
37         public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + ".project";
38         public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
39         public static final String MPROM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mprom";
40         public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
41
42         @Override
43         public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
44         {
45                 String projName = configuration.getAttribute(PROJECT_ATTR, "");
46                 if ("".equals(projName))
47                         return showErrorAndReturnFalse("No project specified");
48
49                 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projName);
50                 if (!project.isAccessible())
51                         return showErrorAndReturnFalse("Project not accessible");
52                 if (!project.hasNature(MograsimNature.NATURE_ID))
53                         return showErrorAndReturnFalse("Project is not a Mograsim project");
54
55                 MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
56                 Optional<MachineDefinition> machDefOptional = machineContext.getMachineDefinition();
57                 if (machDefOptional.isEmpty())
58                         return showErrorAndReturnFalse("No machine definition set");
59
60                 MachineDefinition machineDefinition = machDefOptional.orElseThrow();
61                 MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
62                 MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
63                 MPROMDefinition mpromDef = machineDefinition.getMPROMDefinition();
64
65                 String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
66                 if ("".equals(mpmFileName))
67                         return showErrorAndReturnFalse("No MPM file specified");
68
69                 IFile mpmFile = project.getFile(mpmFileName);
70                 if (mpmFile == null || !mpmFile.isAccessible())
71                         return showErrorAndReturnFalse("MPM file not accessible");
72
73                 try (InputStream mpmStream = mpmFile.getContents())
74                 {
75                         MicroInstructionMemoryParser.parseMemory(miMemDef, mpmStream);
76                 }
77                 catch (IOException e)
78                 {
79                         throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
80                 }
81
82                 String mpromFileName = configuration.getAttribute(MPROM_FILE_ATTR, "");
83                 if (!checkMemoryFileReadable(project, mpromDef, mpromFileName, "MPROM"))
84                         return false;
85
86                 String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
87                 if (!checkMemoryFileReadable(project, mainMemDef, initialRAMFileName, "initial RAM"))
88                         return false;
89
90                 return super.preLaunchCheck(configuration, mode, monitor);
91         }
92
93         private static boolean checkMemoryFileReadable(IProject project, BitVectorMemoryDefinition mainMemDef, String fileName, String fileType)
94                         throws CoreException
95         {
96                 if (!"".equals(fileName))
97                 {
98                         IFile initialRAMFile = project.getFile(fileName);
99                         if (initialRAMFile == null || !initialRAMFile.isAccessible())
100                                 return showErrorAndReturnFalse(fileType + " file not accessible");
101
102                         try (InputStream initialRAMStream = initialRAMFile.getContents())
103                         {
104                                 BitVectorBasedMemoryParser.parseMemory(new StandardBitVectorMemory<>(mainMemDef), initialRAMStream);
105                         }
106                         catch (IOException e)
107                         {
108                                 throw new CoreException(
109                                                 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading " + fileType + " file", e));
110                         }
111                 }
112                 return true;
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
130                 IFile mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
131                 Optional<IFile> mpromFile = fileOptional(project, configuration.getAttribute(MPROM_FILE_ATTR, ""));
132                 Optional<IFile> memFile = fileOptional(project, configuration.getAttribute(INITIAL_RAM_FILE_ATTR, ""));
133                 MachineDebugTarget debugTarget = new MachineDebugTarget(launch, mpmFile, mpromFile, memFile, machineDefinition);
134                 // TODO make selectable whether the machine starts paused or not
135                 debugTarget.suspend();
136                 debugTarget.setExecutionSpeed(1);
137                 Machine machine = debugTarget.getMachine();
138                 machine.reset();
139
140                 // Add the default Mograsim memory block to make it less confusing and more comfortable.
141                 DebugPlugin.getDefault().getMemoryBlockManager()
142                                 .addMemoryBlocks(new IMemoryBlock[] { new MainMemoryBlockExtension(debugTarget, "0", null) });
143         }
144
145         private static Optional<IFile> fileOptional(IProject project, String filename)
146         {
147                 if ("".equals(filename))
148                         return Optional.empty();
149                 return Optional.of(project.getFile(filename));
150         }
151
152 }