Finished MPROM support. Fixes #10
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MachineLaunchConfigType.java
index ab36508..a09ff23 100644 (file)
@@ -1,37 +1,32 @@
 package net.mograsim.plugin.launch;
 
-import static org.eclipse.core.resources.IResourceDelta.CHANGED;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Optional;
-import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResourceChangeEvent;
-import org.eclipse.core.resources.IResourceChangeListener;
-import org.eclipse.core.resources.IResourceDelta;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.model.IMemoryBlock;
 import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.statushandlers.StatusManager;
 
+import net.mograsim.machine.BitVectorMemoryDefinition;
 import net.mograsim.machine.Machine;
 import net.mograsim.machine.MachineDefinition;
-import net.mograsim.machine.MainMemory;
 import net.mograsim.machine.MainMemoryDefinition;
-import net.mograsim.machine.mi.MicroInstructionMemory;
+import net.mograsim.machine.mi.MPROMDefinition;
 import net.mograsim.machine.mi.MicroInstructionMemoryDefinition;
 import net.mograsim.machine.mi.MicroInstructionMemoryParser;
-import net.mograsim.machine.standard.memory.MainMemoryParser;
+import net.mograsim.machine.standard.memory.BitVectorBasedMemoryParser;
+import net.mograsim.machine.standard.memory.StandardBitVectorMemory;
 import net.mograsim.plugin.MograsimActivator;
 import net.mograsim.plugin.nature.MachineContext;
 import net.mograsim.plugin.nature.MograsimNature;
@@ -41,25 +36,9 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate
 {
        public static final String PROJECT_ATTR = MograsimActivator.PLUGIN_ID + ".project";
        public static final String MPM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mpm";
+       public static final String MPROM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".mprom";
        public static final String INITIAL_RAM_FILE_ATTR = MograsimActivator.PLUGIN_ID + ".initialram";
 
-       private final IResourceChangeListener resChangedListener;
-       private IFile mpmFile;
-       private Machine machine;
-
-       public MachineLaunchConfigType()
-       {
-               this.resChangedListener = this::resourceChanged;
-               ResourcesPlugin.getWorkspace().addResourceChangeListener(resChangedListener,
-                               // IResourceChangeEvent.POST_BUILD |
-                               IResourceChangeEvent.POST_CHANGE |
-                               // IResourceChangeEvent.PRE_BUILD |
-                               // IResourceChangeEvent.PRE_CLOSE |
-                               // IResourceChangeEvent.PRE_DELETE |
-                               // IResourceChangeEvent.PRE_REFRESH |
-                                               0);
-       }
-
        @Override
        public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException
        {
@@ -81,6 +60,7 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate
                MachineDefinition machineDefinition = machDefOptional.orElseThrow();
                MicroInstructionMemoryDefinition miMemDef = machineDefinition.getMicroInstructionMemoryDefinition();
                MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
+               MPROMDefinition mpromDef = machineDefinition.getMPROMDefinition();
 
                String mpmFileName = configuration.getAttribute(MPM_FILE_ATTR, "");
                if ("".equals(mpmFileName))
@@ -99,25 +79,37 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate
                        throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
                }
 
+               String mpromFileName = configuration.getAttribute(MPROM_FILE_ATTR, "");
+               if (!checkMemoryFileReadable(project, mpromDef, mpromFileName, "MPROM"))
+                       return false;
+
                String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
-               if (!"".equals(initialRAMFileName))
+               if (!checkMemoryFileReadable(project, mainMemDef, initialRAMFileName, "initial RAM"))
+                       return false;
+
+               return super.preLaunchCheck(configuration, mode, monitor);
+       }
+
+       private static boolean checkMemoryFileReadable(IProject project, BitVectorMemoryDefinition mainMemDef, String fileName, String fileType)
+                       throws CoreException
+       {
+               if (!"".equals(fileName))
                {
-                       IFile initialRAMFile = project.getFile(initialRAMFileName);
+                       IFile initialRAMFile = project.getFile(fileName);
                        if (initialRAMFile == null || !initialRAMFile.isAccessible())
-                               return showErrorAndReturnFalse("Initial RAM file not accessible");
+                               return showErrorAndReturnFalse(fileType + " file not accessible");
 
                        try (InputStream initialRAMStream = initialRAMFile.getContents())
                        {
-                               MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
+                               BitVectorBasedMemoryParser.parseMemory(new StandardBitVectorMemory<>(mainMemDef), initialRAMStream);
                        }
                        catch (IOException e)
                        {
                                throw new CoreException(
-                                               new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
+                                               new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading " + fileType + " file", e));
                        }
                }
-
-               return super.preLaunchCheck(configuration, mode, monitor);
+               return true;
        }
 
        private static boolean showErrorAndReturnFalse(String message)
@@ -134,135 +126,27 @@ public class MachineLaunchConfigType extends LaunchConfigurationDelegate
 
                MachineContext machineContext = ProjectMachineContext.getMachineContextOf(project);
                MachineDefinition machineDefinition = machineContext.getMachineDefinition().orElseThrow();
-               MainMemoryDefinition mainMemDef = machineDefinition.getMainMemoryDefinition();
-
-               String mpmName;
-               mpmFile = project.getFile(mpmName = configuration.getAttribute(MPM_FILE_ATTR, ""));
 
-               String initialRAMFileName = configuration.getAttribute(INITIAL_RAM_FILE_ATTR, "");
-               MainMemory mem;
-               if (!"".equals(initialRAMFileName))
-               {
-                       IFile initialRAMFile = project.getFile(initialRAMFileName);
-                       try (InputStream initialRAMStream = initialRAMFile.getContents())
-                       {
-                               mem = MainMemoryParser.parseMemory(mainMemDef, initialRAMStream);
-                       }
-                       catch (IOException e)
-                       {
-                               throw new CoreException(
-                                               new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading initial RAM file", e));
-                       }
-               } else
-                       mem = null;
-
-               MachineLaunchParams params = new MachineLaunchParams(projName, mpmName, initialRAMFileName);
-               MachineDebugTarget debugTarget = new MachineDebugTarget(launch, params, machineDefinition);
+               IFile mpmFile = project.getFile(configuration.getAttribute(MPM_FILE_ATTR, ""));
+               Optional<IFile> mpromFile = fileOptional(project, configuration.getAttribute(MPROM_FILE_ATTR, ""));
+               Optional<IFile> memFile = fileOptional(project, configuration.getAttribute(INITIAL_RAM_FILE_ATTR, ""));
+               MachineDebugTarget debugTarget = new MachineDebugTarget(launch, mpmFile, mpromFile, memFile, machineDefinition);
+               // TODO make selectable whether the machine starts paused or not
                debugTarget.suspend();
                debugTarget.setExecutionSpeed(1);
-               machine = debugTarget.getMachine();
-               assignMicroInstructionMemory();
-               if (mem != null)
-                       machine.getMainMemory().bind(mem);
+               Machine machine = debugTarget.getMachine();
                machine.reset();
-       }
 
-       private void assignMicroInstructionMemory() throws CoreException
-       {
-               try (InputStream mpmStream = mpmFile.getContents())
-               {
-                       MicroInstructionMemory mpm = MicroInstructionMemoryParser
-                                       .parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream);
-                       machine.getMicroInstructionMemory().bind(mpm);
-               }
-               catch (IOException e)
-               {
-                       throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
-               }
+               // Add the default Mograsim memory block to make it less confusing and more comfortable.
+               DebugPlugin.getDefault().getMemoryBlockManager()
+                               .addMemoryBlocks(new IMemoryBlock[] { new MainMemoryBlockExtension(debugTarget, "0", null) });
        }
 
-       private void resourceChanged(IResourceChangeEvent event)
+       private static Optional<IFile> fileOptional(IProject project, String filename)
        {
-               // TODO remove Sysout
-               int type = event.getType();
-               String typeStr;
-               switch (type)
-               {
-               case IResourceChangeEvent.POST_BUILD:
-                       typeStr = "POST_BUILD";
-                       break;
-               case IResourceChangeEvent.POST_CHANGE:
-                       typeStr = "POST_CHANGE";
-                       IResourceDelta mpmDelta;
-                       if ((mpmDelta = event.getDelta().findMember(mpmFile.getFullPath())) != null && (mpmDelta.getKind() & CHANGED) == CHANGED
-                                       && mpmFile.exists())
-                       {
-                               AtomicBoolean doHotReplace = new AtomicBoolean();
-                               PlatformUI.getWorkbench().getDisplay().syncExec(() ->
-                               {
-                                       if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace MPM?",
-                                                       String.format("The MPM %s has been modified on the file system. Replace simulated MPM with modified contents?",
-                                                                       mpmFile.getName())))
-                                               doHotReplace.set(true);
-                               });
-                               if (doHotReplace.get())
-                               {
-                                       try
-                                       {
-                                               assignMicroInstructionMemory();
-                                       }
-                                       catch (CoreException e)
-                                       {
-                                               PlatformUI.getWorkbench().getDisplay()
-                                                               .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
-                                                                               "Failed Hot Replace!",
-                                                                               "An error occurred trying to read the modified MPM from the file system: " + e.getMessage()));
-                                       }
-                               }
-                       }
-                       break;
-               case IResourceChangeEvent.PRE_BUILD:
-                       typeStr = "PRE_BUILD";
-                       break;
-               case IResourceChangeEvent.PRE_CLOSE:
-                       typeStr = "PRE_CLOSE";
-                       break;
-               case IResourceChangeEvent.PRE_DELETE:
-                       typeStr = "PRE_DELETE";
-                       break;
-               case IResourceChangeEvent.PRE_REFRESH:
-                       typeStr = "PRE_REFRESH";
-                       break;
-               default:
-                       typeStr = "<unknown: " + type + ">";
-               }
-               System.out.println(typeStr + ": " + event);
+               if ("".equals(filename))
+                       return Optional.empty();
+               return Optional.of(project.getFile(filename));
        }
 
-       public static class MachineLaunchParams
-       {
-               public final String projectPath, mpmPath, ramPath;
-
-               MachineLaunchParams(String projectPath, String mpmPath, String ramPath)
-               {
-                       this.projectPath = projectPath;
-                       this.mpmPath = mpmPath;
-                       this.ramPath = ramPath;
-               }
-
-               public String getProjectPath()
-               {
-                       return projectPath;
-               }
-
-               public String getMpmPath()
-               {
-                       return mpmPath;
-               }
-
-               public String getRamPath()
-               {
-                       return ramPath;
-               }
-       }
 }
\ No newline at end of file