1 package net.mograsim.plugin.launch;
3 import java.util.ArrayList;
5 import java.util.function.Consumer;
7 import org.eclipse.core.resources.IMarkerDelta;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.PlatformObject;
10 import org.eclipse.core.runtime.Status;
11 import org.eclipse.debug.core.DebugEvent;
12 import org.eclipse.debug.core.DebugException;
13 import org.eclipse.debug.core.DebugPlugin;
14 import org.eclipse.debug.core.ILaunch;
15 import org.eclipse.debug.core.ILaunchConfiguration;
16 import org.eclipse.debug.core.model.IBreakpoint;
17 import org.eclipse.debug.core.model.IDebugElement;
18 import org.eclipse.debug.core.model.IDebugTarget;
19 import org.eclipse.debug.core.model.IMemoryBlock;
20 import org.eclipse.debug.core.model.IMemoryBlockExtension;
21 import org.eclipse.debug.core.model.IMemoryBlockRetrievalExtension;
22 import org.eclipse.debug.core.model.IProcess;
23 import org.eclipse.debug.core.model.IStepFilters;
24 import org.eclipse.debug.core.model.IThread;
26 import net.mograsim.logic.model.LogicExecuter;
27 import net.mograsim.machine.Machine;
28 import net.mograsim.machine.MachineDefinition;
29 import net.mograsim.plugin.MograsimActivator;
31 public class MachineDebugTarget extends PlatformObject implements IDebugTarget, IMemoryBlockRetrievalExtension
33 private final static boolean USE_PSEUDO_THREAD = true;
35 private final ILaunch launch;
36 private final Machine machine;
37 private final LogicExecuter exec;
38 private final MachineThread thread;
40 private boolean running;
42 private final List<Consumer<Double>> executionSpeedListeners;
44 public MachineDebugTarget(ILaunch launch, MachineDefinition machineDefinition)
47 this.machine = machineDefinition.createNew();
48 this.exec = new LogicExecuter(machine.getTimeline());
50 this.executionSpeedListeners = new ArrayList<>();
52 exec.startLiveExecution();
55 getLaunch().addDebugTarget(this);
58 // create after creating ourself
59 this.thread = USE_PSEUDO_THREAD ? new MachineThread(this) : null;
62 public Machine getMachine()
68 public String getName() throws DebugException
70 return "Mograsim machine \"" + machine.getDefinition().getId() + '"';
74 public String getModelIdentifier()
76 return MograsimActivator.PLUGIN_ID;
80 public IDebugTarget getDebugTarget()
86 public ILaunch getLaunch()
91 public double getExecutionSpeed()
93 return exec.getSpeedFactor();
96 public void setExecutionSpeed(double speed)
98 if (getExecutionSpeed() != speed)
100 exec.setSpeedFactor(speed);
101 callExecutionSpeedListener(speed);
106 public boolean isSuspended()
108 return exec.isPaused();
112 public boolean canSuspend()
114 return !isTerminated() && !isSuspended();
118 public void suspend() throws DebugException
121 throwDebugException("Can't suspend a terminated MachineProcess");
123 throwDebugException("Can't suspend a suspended MachineProcess");
125 exec.pauseLiveExecution();
126 fireSuspendEvent(DebugEvent.CLIENT_REQUEST);
130 public boolean canResume()
132 return !isTerminated() && isSuspended();
136 public void resume() throws DebugException
139 throwDebugException("Can't resume a terminated MachineProcess");
141 throwDebugException("Can't resume a non-suspended MachineProcess");
143 exec.unpauseLiveExecution();
144 fireResumeEvent(DebugEvent.CLIENT_REQUEST);
148 public boolean isTerminated()
154 public boolean canTerminate()
156 return !isTerminated();
160 public void terminate() throws DebugException
165 exec.stopLiveExecution();
167 fireTerminateEvent();
171 public boolean supportsBreakpoint(IBreakpoint breakpoint)
177 public void breakpointAdded(IBreakpoint breakpoint)
179 // ignore; we don't support breakpoints
183 public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta)
185 // ignore; we don't support breakpoints
189 public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta)
191 // ignore; we don't support breakpoints
195 public boolean isDisconnected()
201 public boolean canDisconnect()
207 public void disconnect() throws DebugException
209 throw new DebugException(new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.NOT_SUPPORTED,
210 "Can't disconnect from a MachineDebugTarget", null));
214 public boolean supportsStorageRetrieval()
219 @SuppressWarnings("deprecation") // TODO can we throw a DebugException instead?
221 public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException
223 return new MainMemoryBlock(this, startAddress, length);
227 public IMemoryBlockExtension getExtendedMemoryBlock(String expression, Object context) throws DebugException
229 return new MainMemoryBlockExtension(this, expression, context);
233 public IProcess getProcess()
239 public boolean hasThreads() throws DebugException
241 return USE_PSEUDO_THREAD;
245 public IThread[] getThreads() throws DebugException
247 return thread == null ? new IThread[0] : new IThread[] { thread };
250 public void addExecutionSpeedListener(Consumer<Double> executionSpeedListener)
252 executionSpeedListeners.add(executionSpeedListener);
255 public void removeExecutionSpeedListener(Consumer<Double> executionSpeedListener)
257 executionSpeedListeners.remove(executionSpeedListener);
260 private void callExecutionSpeedListener(double executionSpeed)
262 executionSpeedListeners.forEach(l -> l.accept(executionSpeed));
265 @SuppressWarnings("unchecked")
267 public <T> T getAdapter(Class<T> adapter)
269 if (adapter == IDebugElement.class)
272 // leave this here; maybe we implement IStepFilters someday
273 if (adapter == IStepFilters.class)
274 if (this instanceof IStepFilters)
275 return (T) getDebugTarget();
277 if (adapter == IDebugTarget.class)
278 return (T) getDebugTarget();
280 if (adapter == ILaunch.class)
281 return (T) getLaunch();
284 if (adapter == ILaunchConfiguration.class)
285 return (T) getLaunch().getLaunchConfiguration();
287 return super.getAdapter(adapter);
291 * Fires a creation event for this debug element.
293 private void fireCreationEvent()
295 fireEvent(new DebugEvent(this, DebugEvent.CREATE));
299 * Fires a resume for this debug element with the specified detail code.
301 * @param detail detail code for the resume event, such as <code>DebugEvent.STEP_OVER</code>
303 private void fireResumeEvent(int detail)
305 fireEvent(new DebugEvent(this, DebugEvent.RESUME, detail));
309 * Fires a suspend event for this debug element with the specified detail code.
311 * @param detail detail code for the suspend event, such as <code>DebugEvent.BREAKPOINT</code>
313 private void fireSuspendEvent(int detail)
315 fireEvent(new DebugEvent(this, DebugEvent.SUSPEND, detail));
319 * Fires a terminate event for this debug element.
321 private void fireTerminateEvent()
323 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
327 * Fires a debug event.
329 * @param event debug event to fire
331 private static void fireEvent(DebugEvent event)
333 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event });
336 private static void throwDebugException(String message) throws DebugException
338 throw new DebugException(
339 new Status(IStatus.ERROR, MograsimActivator.PLUGIN_ID, DebugException.TARGET_REQUEST_FAILED, message, null));