Further work on machine launch configs
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / launch / MainMemoryBlock.java
1 package net.mograsim.plugin.launch;
2
3 import org.eclipse.debug.core.DebugException;
4 import org.eclipse.debug.core.ILaunch;
5 import org.eclipse.debug.core.model.IDebugTarget;
6 import org.eclipse.debug.core.model.IMemoryBlock;
7
8 import net.mograsim.machine.MainMemory;
9 import net.mograsim.plugin.MograsimActivator;
10
11 public class MainMemoryBlock implements IMemoryBlock
12 {
13         private final MachineDebugTarget debugTarget;
14         private final MainMemory mem;
15         private final long startAddress;
16         private final int length;
17
18         public MainMemoryBlock(MachineDebugTarget debugTarget, long startAddress, long length)
19         {
20                 if (length < 0)
21                         throw new IllegalArgumentException("Can't create a memory block of negative size");
22                 if (length > Integer.MAX_VALUE)
23                         throw new IllegalArgumentException("Can't create a memory block bigger than Integer.MAX_VALUE (" + Integer.MAX_VALUE + "\"");
24                 if (startAddress % 2 != 0)
25                         throw new IllegalArgumentException("Can't create an unaligned memory block");
26
27                 this.debugTarget = debugTarget;
28                 this.mem = debugTarget.getMachine().getMainMemory();
29                 this.startAddress = startAddress;
30                 this.length = (int) length;
31         }
32
33         @Override
34         public String getModelIdentifier()
35         {
36                 return MograsimActivator.PLUGIN_ID;
37         }
38
39         @Override
40         public IDebugTarget getDebugTarget()
41         {
42                 return debugTarget;
43         }
44
45         @Override
46         public ILaunch getLaunch()
47         {
48                 return debugTarget.getLaunch();
49         }
50
51         @Override
52         public <T> T getAdapter(Class<T> adapter)
53         {
54                 // TODO
55                 return null;
56         }
57
58         @Override
59         public long getStartAddress()
60         {
61                 return startAddress;
62         }
63
64         @Override
65         public long getLength()
66         {
67                 return length;
68         }
69
70         @Override
71         public byte[] getBytes() throws DebugException
72         {
73                 // TODO speedup. Maybe make a method for this in MainMemory?
74                 byte[] bs = new byte[length];
75                 int i;
76                 long j;
77                 for (i = 0, j = startAddress / 2; i < length; i++)
78                 {
79                         short word = mem.getCellAsBigInteger(j).shortValueExact();
80                         bs[i + 0] = (byte) (word & 0xFF);
81                         bs[i + 1] = (byte) (word >>> 8);
82                 }
83                 return bs;
84         }
85
86         @Override
87         public boolean supportsValueModification()
88         {
89                 // TODO
90                 return false;
91         }
92
93         @Override
94         public void setValue(long offset, byte[] bytes) throws DebugException
95         {
96                 // TODO
97         }
98 }