X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=plugins%2Fnet.mograsim.plugin.core%2Fsrc%2Fnet%2Fmograsim%2Fplugin%2Flaunch%2FMachineDebugTarget.java;h=5bb87a8d4f74ee39a117612bd289760bb3d980eb;hb=648fc6e69e09fe4467cb6bac47934be1a7dcf0d6;hp=5270c767195b8cc024e17fc30338a0fbb34673ec;hpb=a873ef940ba160f284ba6fa3fee1b9704bf68858;p=Mograsim.git diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineDebugTarget.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineDebugTarget.java index 5270c767..5bb87a8d 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineDebugTarget.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineDebugTarget.java @@ -1,8 +1,23 @@ package net.mograsim.plugin.launch; -import java.util.Arrays; - +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; @@ -15,52 +30,82 @@ import org.eclipse.debug.core.model.IBreakpoint; import org.eclipse.debug.core.model.IDebugElement; import org.eclipse.debug.core.model.IDebugTarget; import org.eclipse.debug.core.model.IMemoryBlock; +import org.eclipse.debug.core.model.IMemoryBlockExtension; +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; -public class MachineDebugTarget extends PlatformObject implements IDebugTarget +public class MachineDebugTarget extends PlatformObject implements IDebugTarget, IMemoryBlockRetrievalExtension { - private final MachineProcess process; + 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 mpromFile; + private final Optional memFile; + + private boolean running; - public MachineDebugTarget(MachineProcess process) + private final List> executionSpeedListeners; + + private final IResourceChangeListener resChangedListener; + + public MachineDebugTarget(ILaunch launch, IFile mpmFile, Optional mpromFile, Optional memFile, + MachineDefinition machineDefinition) throws CoreException { - this.process = process; + this.launch = launch; + this.machine = machineDefinition.createNew(); + this.exec = new LogicExecuter(machine.getTimeline()); - DebugPlugin.getDefault().addDebugEventListener(es -> Arrays.stream(es).filter(e -> e.getSource() == process).forEach(e -> - { - switch (e.getKind()) - { - case DebugEvent.RESUME: - fireResumeEvent(e.getDetail()); - break; - case DebugEvent.SUSPEND: - fireSuspendEvent(e.getDetail()); - break; - case DebugEvent.TERMINATE: - fireTerminateEvent(); - break; - default: - // ignore - } - })); + this.executionSpeedListeners = new ArrayList<>(); + 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() { - return process.getMachine(); + return machine; } @Override public String getName() throws DebugException { - return process.getName(); + return "Mograsim machine \"" + machine.getDefinition().getId() + '"'; } @Override @@ -78,60 +123,87 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget @Override public ILaunch getLaunch() { - return process.getLaunch(); + return launch; + } + + public double getExecutionSpeed() + { + return exec.getSpeedFactor(); } public void setExecutionSpeed(double speed) { - process.setExecutionSpeed(speed); + if (getExecutionSpeed() != speed) + { + exec.setSpeedFactor(speed); + callExecutionSpeedListener(speed); + } } @Override public boolean isSuspended() { - return process.isSuspended(); + return exec.isPaused(); } @Override public boolean canSuspend() { - return process.canSuspend(); + return !isTerminated() && !isSuspended(); } @Override public void suspend() throws DebugException { - process.suspend(); + if (isTerminated()) + throwDebugException("Can't suspend a terminated MachineProcess"); + if (isSuspended()) + throwDebugException("Can't suspend a suspended MachineProcess"); + + exec.pauseLiveExecution(); + fireSuspendEvent(DebugEvent.CLIENT_REQUEST); } @Override public boolean canResume() { - return process.canResume(); + return !isTerminated() && isSuspended(); } @Override public void resume() throws DebugException { - process.resume(); + if (isTerminated()) + throwDebugException("Can't resume a terminated MachineProcess"); + if (!isSuspended()) + throwDebugException("Can't resume a non-suspended MachineProcess"); + + exec.unpauseLiveExecution(); + fireResumeEvent(DebugEvent.CLIENT_REQUEST); } @Override public boolean isTerminated() { - return process.isTerminated(); + return !running; } @Override public boolean canTerminate() { - return process.canTerminate(); + return !isTerminated(); } @Override public void terminate() throws DebugException { - process.terminate(); + if (isTerminated()) + return; + + ResourcesPlugin.getWorkspace().removeResourceChangeListener(resChangedListener); + exec.stopLiveExecution(); + running = false; + fireTerminateEvent(); } @Override @@ -183,6 +255,7 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget return true; } + @SuppressWarnings("deprecation") // TODO can we throw a DebugException instead? @Override public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException { @@ -190,21 +263,42 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget } @Override - public MachineProcess getProcess() + public IMemoryBlockExtension getExtendedMemoryBlock(String expression, Object context) throws DebugException { - return process; + return new MainMemoryBlockExtension(this, expression, context); + } + + @Override + public IProcess getProcess() + { + return null; } @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 executionSpeedListener) + { + executionSpeedListeners.add(executionSpeedListener); + } + + public void removeExecutionSpeedListener(Consumer executionSpeedListener) + { + executionSpeedListeners.remove(executionSpeedListener); + } + + private void callExecutionSpeedListener(double executionSpeed) + { + executionSpeedListeners.forEach(l -> l.accept(executionSpeed)); } @SuppressWarnings("unchecked") @@ -225,9 +319,6 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget if (adapter == ILaunch.class) return (T) getLaunch(); - if (adapter == IProcess.class) - return (T) getProcess(); - // CONTEXTLAUNCHING if (adapter == ILaunchConfiguration.class) return (T) getLaunch().getLaunchConfiguration(); @@ -280,4 +371,117 @@ public class MachineDebugTarget extends PlatformObject implements IDebugTarget { DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event }); } + + private static void throwDebugException(String message) throws DebugException + { + 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 void assignMemory(Optional memFile, + AbstractAssignableBitVectorMemory memoryToAssign, D definition, Function 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 getMPROMFile() + { + return mpromFile; + } + + public Optional getMEMFile() + { + return memFile; + } } \ No newline at end of file