1 package net.mograsim.plugin.launch;
3 import java.math.BigInteger;
5 import org.eclipse.core.runtime.PlatformObject;
6 import org.eclipse.debug.core.DebugEvent;
7 import org.eclipse.debug.core.DebugException;
8 import org.eclipse.debug.core.DebugPlugin;
9 import org.eclipse.debug.core.ILaunch;
10 import org.eclipse.debug.core.model.IDebugTarget;
11 import org.eclipse.debug.core.model.IMemoryBlock;
13 import net.mograsim.machine.MainMemory;
14 import net.mograsim.plugin.MograsimActivator;
17 public class MainMemoryBlock extends PlatformObject implements IMemoryBlock
19 private final MachineDebugTarget debugTarget;
20 private final MainMemory mem;
21 private final long startAddress;
22 private final int length;
24 public MainMemoryBlock(MachineDebugTarget debugTarget, long startAddress, long length)
26 MainMemory mem = debugTarget.getMachine().getMainMemory();
29 throw new IllegalArgumentException("Negative size");
31 throw new IllegalArgumentException("Negative start address");
32 if ((startAddress + length) / 2 > mem.size())
33 throw new IllegalArgumentException("End address higher than memory size");
34 if (length > Integer.MAX_VALUE)
35 throw new IllegalArgumentException("Memory block bigger than Integer.MAX_VALUE (" + Integer.MAX_VALUE + "\"");
36 if (startAddress % 2 != 0 || length % 2 != 0)
37 throw new IllegalArgumentException("Unaligned memory block");
39 this.debugTarget = debugTarget;
41 this.startAddress = startAddress;
42 this.length = (int) length;
44 mem.registerCellModifiedListener(e ->
46 if (e >= startAddress / 2 && e < (startAddress + length) / 2)
47 fireContentChangeEvent();
52 public String getModelIdentifier()
54 return MograsimActivator.PLUGIN_ID;
58 public IDebugTarget getDebugTarget()
64 public ILaunch getLaunch()
66 return debugTarget.getLaunch();
70 public long getStartAddress()
76 public long getLength()
82 public byte[] getBytes() throws DebugException
84 byte[] bs = new byte[length];
87 for (i = 0, j = startAddress / 2; i < length; i += 2, j++)
89 short word = mem.getCellAsBigInteger(j).shortValue();
90 bs[i + 0] = (byte) (word & 0xFF);
91 bs[i + 1] = (byte) (word >>> 8);
97 public boolean supportsValueModification()
103 public void setValue(long offset, byte[] bytes) throws DebugException
105 if (offset % 2 != 0 || bytes.length % 2 != 0)
106 throw new IllegalArgumentException("Can't write unaligned to a memory block");
109 for (i = 0, j = (startAddress + offset) / 2; i < bytes.length; i += 2, j++)
112 word |= bytes[i + 0] & 0xFF;
113 word |= bytes[i + 1] << 8;
114 mem.setCellAsBigInteger(j, BigInteger.valueOf(word));
119 * Fires a terminate event for this debug element.
121 private void fireContentChangeEvent()
123 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
127 * Fires a debug event.
129 * @param event debug event to fire
131 private static void fireEvent(DebugEvent event)
133 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event });