msp mote memory now contains map entries instead of name->address hash

This commit is contained in:
fros4943 2008-02-11 14:07:38 +00:00
parent 2beb497c35
commit 36f70eff7c
2 changed files with 79 additions and 77 deletions

View File

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: MspMote.java,v 1.1 2008/02/07 14:53:29 fros4943 Exp $ * $Id: MspMote.java,v 1.2 2008/02/11 14:07:38 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote; package se.sics.cooja.mspmote;
@ -39,8 +39,9 @@ import org.jdom.Element;
import se.sics.cooja.*; import se.sics.cooja.*;
import se.sics.cooja.mspmote.interfaces.TR1001Radio; import se.sics.cooja.mspmote.interfaces.TR1001Radio;
import se.sics.mspsim.core.MSP430; import se.sics.mspsim.core.MSP430;
import se.sics.mspsim.core.MapTable;
import se.sics.mspsim.util.ELF; import se.sics.mspsim.util.ELF;
import se.sics.mspsim.util.MapEntry;
import se.sics.mspsim.util.MapTable;
/** /**
* @author Fredrik Osterlind * @author Fredrik Osterlind
@ -179,13 +180,8 @@ public abstract class MspMote implements Mote {
/* TODO Need new memory type including size and type as well */ /* TODO Need new memory type including size and type as well */
/* Create mote address memory */ /* Create mote address memory */
Properties varAddresses = new Properties(); ArrayList<MapEntry> allEntries = map.getAllEntries();
for (int i=0; i < map.functionNames.length; i++) { myMemory = new MspMoteMemory(allEntries, myCpu);
if (map.functionNames[i] != null) {
varAddresses.put(map.functionNames[i], new Integer(i));
}
}
myMemory = new MspMoteMemory(varAddresses, myCpu);
myCpu.reset(); myCpu.reset();
} }
@ -249,7 +245,9 @@ public abstract class MspMote implements Mote {
if (monitorStackUsage) { if (monitorStackUsage) {
// CPU loop with stack observer // CPU loop with stack observer
int newStack; int newStack;
while (!stopNextInstruction && cpu.step() < cycleCounter) { while (!stopNextInstruction && cpu.cycles < cycleCounter) {
cpu.step(cycleCounter);
/* Check if radio has pending incoming bytes */ /* Check if radio has pending incoming bytes */
if (myRadio != null && myRadio.hasPendingBytes()) { if (myRadio != null && myRadio.hasPendingBytes()) {
myRadio.tryDeliverNextByte(cpu.cycles); myRadio.tryDeliverNextByte(cpu.cycles);
@ -267,12 +265,14 @@ public abstract class MspMote implements Mote {
} }
} }
} else { /* Fast CPU loop */ } else { /* Fast CPU loop */
do { while (!stopNextInstruction && cpu.cycles < cycleCounter) {
cpu.step(cycleCounter);
/* Check if radio has pending incoming bytes */ /* Check if radio has pending incoming bytes */
if (myRadio != null && myRadio.hasPendingBytes()) { if (myRadio != null && myRadio.hasPendingBytes()) {
myRadio.tryDeliverNextByte(cpu.cycles); myRadio.tryDeliverNextByte(cpu.cycles);
} }
} while (!stopNextInstruction && (cpu.step() < cycleCounter) ); }
} }
// Reset abort flag // Reset abort flag

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: MspMoteMemory.java,v 1.1 2008/02/07 14:53:29 fros4943 Exp $ * $Id: MspMoteMemory.java,v 1.2 2008/02/11 14:07:38 fros4943 Exp $
*/ */
package se.sics.cooja.mspmote; package se.sics.cooja.mspmote;
@ -35,35 +35,50 @@ import org.apache.log4j.Logger;
import se.sics.cooja.AddressMemory; import se.sics.cooja.AddressMemory;
import se.sics.cooja.MoteMemory; import se.sics.cooja.MoteMemory;
import se.sics.mspsim.core.MSP430; import se.sics.mspsim.core.MSP430;
import se.sics.mspsim.util.MapEntry;
public class MspMoteMemory implements MoteMemory, AddressMemory { public class MspMoteMemory implements MoteMemory, AddressMemory {
private static Logger logger = Logger.getLogger(MspMoteMemory.class); private static Logger logger = Logger.getLogger(MspMoteMemory.class);
private final Properties variableAddresses; private final ArrayList<MapEntry> mapEntries;
private int TEMP_MEM_START;
private MSP430 cpu; private MSP430 cpu;
public MspMoteMemory(Properties variableAddresses, MSP430 cpu) { public MspMoteMemory(ArrayList<MapEntry> allEntries, MSP430 cpu) {
this.variableAddresses = variableAddresses; this.mapEntries = new ArrayList<MapEntry>();
for (MapEntry entry: allEntries) {
if (entry.getType() == MapEntry.TYPE.variable) {
mapEntries.add(entry);
}
}
this.cpu = cpu; this.cpu = cpu;
TEMP_MEM_START = 0;
} }
public String[] getVariableNames() { public String[] getVariableNames() {
String[] names = new String[variableAddresses.size()]; String[] names = new String[mapEntries.size()];
Enumeration nameEnum = variableAddresses.keys(); for (int i = 0; i < mapEntries.size(); i++) {
for (int i = 0; i < variableAddresses.size(); i++) { names[i] = mapEntries.get(i).getName();
names[i] = (String) nameEnum.nextElement();
} }
return names; return names;
} }
public int getVariableAddress(String varName) { private MapEntry getMapEntry(String varName) throws UnknownVariableException {
if (!variableAddresses.containsKey(varName)) { for (MapEntry entry: mapEntries) {
return -1; if (entry.getName().equals(varName)) {
return entry;
}
} }
return ((Integer) variableAddresses.get(varName)).intValue(); throw new UnknownVariableException(varName);
}
public int getVariableAddress(String varName) throws UnknownVariableException {
MapEntry entry = getMapEntry(varName);
return entry.getAddress();
}
public int getIntegerLength() {
return 2;
} }
public void clearMemory() { public void clearMemory() {
@ -71,27 +86,27 @@ public class MspMoteMemory implements MoteMemory, AddressMemory {
} }
public byte[] getMemorySegment(int address, int size) { public byte[] getMemorySegment(int address, int size) {
int[] ret = new int[size]; int[] memInts = new int[size];
System.arraycopy(cpu.memory, address - TEMP_MEM_START, ret, 0, size); System.arraycopy(cpu.memory, address, memInts, 0, size);
// TODO XXX Slow method /* Convert to byte array */
byte[] ret2 = new byte[size]; byte[] memBytes = new byte[size];
for (int i=0; i < size; i++) { for (int i=0; i < size; i++) {
ret2[i] = (byte) ret[i]; memBytes[i] = (byte) memInts[i];
} }
return ret2; return memBytes;
} }
public void setMemorySegment(int address, byte[] data) { public void setMemorySegment(int address, byte[] data) {
// TODO XXX Slow method /* Convert to int array */
int[] intArr = new int[data.length]; int[] memInts = new int[data.length];
for (int i=0; i < data.length; i++) { for (int i=0; i < data.length; i++) {
intArr[i] = data[i]; memInts[i] = data[i];
} }
System.arraycopy(intArr, 0, cpu.memory, address - TEMP_MEM_START, data.length); System.arraycopy(memInts, 0, cpu.memory, address, data.length);
} }
public int getTotalSize() { public int getTotalSize() {
@ -99,16 +114,21 @@ public class MspMoteMemory implements MoteMemory, AddressMemory {
} }
public boolean variableExists(String varName) { public boolean variableExists(String varName) {
return variableAddresses.containsKey(varName); for (MapEntry entry: mapEntries) {
if (entry.getName().equals(varName)) {
return true;
}
}
return false;
} }
public int getIntValueOf(String varName) { /* TODO Check correct variable size in below methods */
// Get start address of variable
if (!variableAddresses.containsKey(varName)) {
return -1;
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
public int getIntValueOf(String varName) throws UnknownVariableException {
MapEntry entry = getMapEntry(varName);
int varAddr = entry.getAddress();
byte[] varData = getMemorySegment(varAddr, 2); byte[] varData = getMemorySegment(varAddr, 2);
int retVal = 0; int retVal = 0;
@ -119,46 +139,34 @@ public class MspMoteMemory implements MoteMemory, AddressMemory {
return Integer.reverseBytes(retVal) >> 16; // Crop two bytes return Integer.reverseBytes(retVal) >> 16; // Crop two bytes
} }
public void setIntValueOf(String varName, int newVal) { public void setIntValueOf(String varName, int newVal) throws UnknownVariableException {
// Get start address of variable MapEntry entry = getMapEntry(varName);
if (!variableAddresses.containsKey(varName)) { int varAddr = entry.getAddress();
return;
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
// TODO Check if small/big-endian when coming from JNI?
int newValToSet = Integer.reverseBytes(newVal); int newValToSet = Integer.reverseBytes(newVal);
// Create byte array // Create byte array
int pos = 0; int pos = 0;
byte[] varData = new byte[4]; byte[] varData = new byte[2];
varData[pos++] = (byte) ((newValToSet & 0xFF000000) >> 24); varData[pos++] = (byte) ((newValToSet & 0xFF000000) >> 24);
varData[pos++] = (byte) ((newValToSet & 0xFF0000) >> 16); varData[pos++] = (byte) ((newValToSet & 0xFF0000) >> 16);
varData[pos++] = (byte) ((newValToSet & 0xFF00) >> 8);
varData[pos++] = (byte) ((newValToSet & 0xFF) >> 0);
setMemorySegment(varAddr, varData); setMemorySegment(varAddr, varData);
} }
public byte getByteValueOf(String varName) { public byte getByteValueOf(String varName) throws UnknownVariableException {
// Get start address of variable MapEntry entry = getMapEntry(varName);
if (!variableAddresses.containsKey(varName)) { int varAddr = entry.getAddress();
return -1;
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
byte[] varData = getMemorySegment(varAddr, 1); byte[] varData = getMemorySegment(varAddr, 1);
return varData[0]; return varData[0];
} }
public void setByteValueOf(String varName, byte newVal) { public void setByteValueOf(String varName, byte newVal) throws UnknownVariableException {
// Get start address of variable MapEntry entry = getMapEntry(varName);
if (!variableAddresses.containsKey(varName)) { int varAddr = entry.getAddress();
return;
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
byte[] varData = new byte[1]; byte[] varData = new byte[1];
@ -167,23 +175,17 @@ public class MspMoteMemory implements MoteMemory, AddressMemory {
setMemorySegment(varAddr, varData); setMemorySegment(varAddr, varData);
} }
public byte[] getByteArray(String varName, int length) { public byte[] getByteArray(String varName, int length) throws UnknownVariableException {
// Get start address of variable MapEntry entry = getMapEntry(varName);
if (!variableAddresses.containsKey(varName)) { int varAddr = entry.getAddress();
return null;
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
// TODO Check if small/big-endian when coming from JNI? // TODO Check if small/big-endian when coming from JNI?
return getMemorySegment(varAddr, length); return getMemorySegment(varAddr, length);
} }
public void setByteArray(String varName, byte[] data) { public void setByteArray(String varName, byte[] data) throws UnknownVariableException {
// Get start address of variable MapEntry entry = getMapEntry(varName);
if (!variableAddresses.containsKey(varName)) { int varAddr = entry.getAddress();
return;
}
int varAddr = ((Integer) variableAddresses.get(varName)).intValue();
// TODO Check if small/big-endian when coming from JNI? // TODO Check if small/big-endian when coming from JNI?
setMemorySegment(varAddr, data); setMemorySegment(varAddr, data);