Added String preferences
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MainMemoryBlock.java
1 package net.mograsim.plugin.launch;
2
3 import java.math.BigInteger;
4
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;
12
13 import net.mograsim.machine.MainMemory;
14 import net.mograsim.plugin.MograsimActivator;
15
16 @Deprecated
17 public class MainMemoryBlock extends PlatformObject implements IMemoryBlock
18 {
19         private final MachineDebugTarget debugTarget;
20         private final MainMemory mem;
21         private final long startAddress;
22         private final int length;
23
24         public MainMemoryBlock(MachineDebugTarget debugTarget, long startAddress, long length)
25         {
26                 MainMemory mem = debugTarget.getMachine().getMainMemory();
27
28                 if (length < 0)
29                         throw new IllegalArgumentException("Negative size");
30                 if (startAddress < 0)
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");
38
39                 this.debugTarget = debugTarget;
40                 this.mem = mem;
41                 this.startAddress = startAddress;
42                 this.length = (int) length;
43
44                 mem.registerCellModifiedListener(e ->
45                 {
46                         if (e >= startAddress / 2 && e < (startAddress + length) / 2)
47                                 fireContentChangeEvent();
48                 });
49         }
50
51         @Override
52         public String getModelIdentifier()
53         {
54                 return MograsimActivator.PLUGIN_ID;
55         }
56
57         @Override
58         public IDebugTarget getDebugTarget()
59         {
60                 return debugTarget;
61         }
62
63         @Override
64         public ILaunch getLaunch()
65         {
66                 return debugTarget.getLaunch();
67         }
68
69         @Override
70         public long getStartAddress()
71         {
72                 return startAddress;
73         }
74
75         @Override
76         public long getLength()
77         {
78                 return length;
79         }
80
81         @Override
82         public byte[] getBytes() throws DebugException
83         {
84                 byte[] bs = new byte[length];
85                 int i;
86                 long j;
87                 for (i = 0, j = startAddress / 2; i < length; i += 2, j++)
88                 {
89                         short word = mem.getCellAsBigInteger(j).shortValue();
90                         bs[i + 0] = (byte) (word & 0xFF);
91                         bs[i + 1] = (byte) (word >>> 8);
92                 }
93                 return bs;
94         }
95
96         @Override
97         public boolean supportsValueModification()
98         {
99                 return true;
100         }
101
102         @Override
103         public void setValue(long offset, byte[] bytes) throws DebugException
104         {
105                 if (offset % 2 != 0 || bytes.length % 2 != 0)
106                         throw new IllegalArgumentException("Can't write unaligned to a memory block");
107                 int i;
108                 long j;
109                 for (i = 0, j = (startAddress + offset) / 2; i < bytes.length; i += 2, j++)
110                 {
111                         short word = 0;
112                         word |= bytes[i + 0] & 0xFF;
113                         word |= bytes[i + 1] << 8;
114                         mem.setCellAsBigInteger(j, BigInteger.valueOf(word));
115                 }
116         }
117
118         /**
119          * Fires a terminate event for this debug element.
120          */
121         private void fireContentChangeEvent()
122         {
123                 fireEvent(new DebugEvent(this, DebugEvent.TERMINATE));
124         }
125
126         /**
127          * Fires a debug event.
128          *
129          * @param event debug event to fire
130          */
131         private static void fireEvent(DebugEvent event)
132         {
133                 DebugPlugin.getDefault().fireDebugEventSet(new DebugEvent[] { event });
134         }
135 }