Finished MPROM support. Fixes #10
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MachineDebugTarget.java
index 03fe7fd..5bb87a8 100644 (file)
@@ -1,10 +1,23 @@
 package net.mograsim.plugin.launch;
 
+import static org.eclipse.core.resources.IResourceDelta.CHANGED;
+
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
+import java.util.function.Function;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IMarkerDelta;
+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.IStatus;
 import org.eclipse.core.runtime.PlatformObject;
 import org.eclipse.core.runtime.Status;
@@ -22,39 +35,66 @@ import org.eclipse.debug.core.model.IMemoryBlockRetrievalExtension;
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.debug.core.model.IStepFilters;
 import org.eclipse.debug.core.model.IThread;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.ui.PlatformUI;
 
 import net.mograsim.logic.model.LogicExecuter;
+import net.mograsim.machine.BitVectorMemory;
+import net.mograsim.machine.BitVectorMemoryDefinition;
 import net.mograsim.machine.Machine;
 import net.mograsim.machine.MachineDefinition;
+import net.mograsim.machine.StandardMainMemory;
+import net.mograsim.machine.mi.MicroInstructionMemoryParser;
+import net.mograsim.machine.mi.StandardMPROM;
+import net.mograsim.machine.standard.memory.AbstractAssignableBitVectorMemory;
+import net.mograsim.machine.standard.memory.BitVectorBasedMemoryParser;
 import net.mograsim.plugin.MograsimActivator;
-import net.mograsim.plugin.launch.MachineLaunchConfigType.MachineLaunchParams;
 
 public class MachineDebugTarget extends PlatformObject implements IDebugTarget, IMemoryBlockRetrievalExtension
 {
+       private final static boolean USE_PSEUDO_THREAD = true;
+
        private final ILaunch launch;
        private final Machine machine;
        private final LogicExecuter exec;
+       private final MachineThread thread;
+       private final IFile mpmFile;
+       private final Optional<IFile> mpromFile;
+       private final Optional<IFile> memFile;
 
        private boolean running;
 
        private final List<Consumer<Double>> executionSpeedListeners;
 
-       private final MachineLaunchParams launchParams;
+       private final IResourceChangeListener resChangedListener;
 
-       public MachineDebugTarget(ILaunch launch, MachineLaunchParams launchParams, MachineDefinition machineDefinition)
+       public MachineDebugTarget(ILaunch launch, IFile mpmFile, Optional<IFile> mpromFile, Optional<IFile> memFile,
+                       MachineDefinition machineDefinition) throws CoreException
        {
                this.launch = launch;
                this.machine = machineDefinition.createNew();
                this.exec = new LogicExecuter(machine.getTimeline());
 
                this.executionSpeedListeners = new ArrayList<>();
-               this.launchParams = launchParams;
+               this.mpmFile = mpmFile;
+               this.mpromFile = mpromFile;
+               this.memFile = memFile;
+
+               assignMicroInstructionMemory();
+               assignMPROM();
+               assignMainMemory();
 
                exec.startLiveExecution();
                running = true;
 
                getLaunch().addDebugTarget(this);
                fireCreationEvent();
+
+               this.resChangedListener = this::resourceChanged;
+               ResourcesPlugin.getWorkspace().addResourceChangeListener(resChangedListener, IResourceChangeEvent.POST_CHANGE);
+
+               // create after creating ourself
+               this.thread = USE_PSEUDO_THREAD ? new MachineThread(this) : null;
        }
 
        public Machine getMachine()
@@ -86,11 +126,6 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
                return launch;
        }
 
-       public MachineLaunchParams getLaunchParams()
-       {
-               return launchParams;
-       }
-
        public double getExecutionSpeed()
        {
                return exec.getSpeedFactor();
@@ -165,6 +200,7 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
                if (isTerminated())
                        return;
 
+               ResourcesPlugin.getWorkspace().removeResourceChangeListener(resChangedListener);
                exec.stopLiveExecution();
                running = false;
                fireTerminateEvent();
@@ -241,13 +277,13 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
        @Override
        public boolean hasThreads() throws DebugException
        {
-               return false;
+               return USE_PSEUDO_THREAD;
        }
 
        @Override
        public IThread[] getThreads() throws DebugException
        {
-               return new IThread[0];
+               return thread == null ? new IThread[0] : new IThread[] { thread };
        }
 
        public void addExecutionSpeedListener(Consumer<Double> executionSpeedListener)
@@ -341,4 +377,111 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget,
                throw new DebugException(
                                new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED, message, null));
        }
+
+       private void resourceChanged(IResourceChangeEvent event)
+       {
+               if (event.getType() == IResourceChangeEvent.POST_CHANGE)
+               {
+                       tryHotReplaceIfChanged(event, mpmFile, this::assignMicroInstructionMemory, "MPM");
+
+                       if (mpromFile.isPresent())
+                               tryHotReplaceIfChanged(event, mpromFile.get(), this::assignMPROM, "MPROM");
+               }
+       }
+
+       private static void tryHotReplaceIfChanged(IResourceChangeEvent event, IFile memFile, RunnableThrowingCoreException assign, String type)
+       {
+               IResourceDelta mpmDelta = event.getDelta().findMember(memFile.getFullPath());
+               if (mpmDelta != null && (mpmDelta.getKind() & CHANGED) == CHANGED && memFile.exists())
+                       tryHotReplace(memFile, assign, type);
+       }
+
+       private static void tryHotReplace(IFile memFile, RunnableThrowingCoreException assign, String type)
+       {
+               AtomicBoolean doHotReplace = new AtomicBoolean();
+               PlatformUI.getWorkbench().getDisplay().syncExec(() ->
+               {
+                       if (MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Hot Replace " + type + "?",
+                                       String.format("The " + type + " %s has been modified on the file system. Replace simulated " + type
+                                                       + " with modified contents?", memFile.getName())))
+                               doHotReplace.set(true);
+               });
+               if (doHotReplace.get())
+               {
+                       try
+                       {
+                               assign.run();
+                       }
+                       catch (CoreException e)
+                       {
+                               PlatformUI.getWorkbench().getDisplay()
+                                               .asyncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
+                                                               "Failed Hot Replace!",
+                                                               "An error occurred trying to read the modified " + type + " from the file system: " + e.getMessage()));
+                       }
+               }
+       }
+
+       private static interface RunnableThrowingCoreException
+       {
+               public void run() throws CoreException;
+       }
+
+       private void assignMicroInstructionMemory() throws CoreException
+       {
+               try (InputStream mpmStream = mpmFile.getContents())
+               {
+                       machine.getMicroInstructionMemory().bind(
+                                       MicroInstructionMemoryParser.parseMemory(machine.getDefinition().getMicroInstructionMemoryDefinition(), mpmStream));
+               }
+               catch (IOException e)
+               {
+                       throw new CoreException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading MPM file", e));
+               }
+       }
+
+       private void assignMPROM() throws CoreException
+       {
+               assignMemory(mpromFile, machine.getMPROM(), machine.getDefinition().getMPROMDefinition(), StandardMPROM::new, "MPROM");
+       }
+
+       private void assignMainMemory() throws CoreException
+       {
+               assignMemory(memFile, machine.getMainMemory(), machine.getDefinition().getMainMemoryDefinition(), StandardMainMemory::new,
+                               "initial RAM");
+       }
+
+       private static <D extends BitVectorMemoryDefinition, M extends BitVectorMemory> void assignMemory(Optional<IFile> memFile,
+                       AbstractAssignableBitVectorMemory<M> memoryToAssign, D definition, Function<D, M> newMemory, String type) throws CoreException
+       {
+               if (memFile.isPresent())
+               {
+                       try (InputStream initialRAMStream = memFile.get().getContents())
+                       {
+                               M mem = newMemory.apply(definition);
+                               BitVectorBasedMemoryParser.parseMemory(mem, initialRAMStream);
+                               memoryToAssign.bind(mem);
+                       }
+                       catch (IOException e)
+                       {
+                               throw new CoreException(
+                                               new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, "Unexpected IO exception reading " + type + " file", e));
+                       }
+               }
+       }
+
+       public IFile getMPMFile()
+       {
+               return mpmFile;
+       }
+
+       public Optional<IFile> getMPROMFile()
+       {
+               return mpromFile;
+       }
+
+       public Optional<IFile> getMEMFile()
+       {
+               return memFile;
+       }
 }
\ No newline at end of file