hi=ord(self.data[0]);
lo=ord(self.data[1]);
return (hi<<8)+lo;
+ def CCdebuginstr(self,instr):
+ self.writecmd(0x30,0x88,len(instr),instr);
+ return ord(self.data[0]);
def MSP430peek(self,adr):
"""Read the contents of memory at an address."""
self.data=[adr&0xff, (adr&0xff00)>>8];
self.writecmd(0x11,0x02,2,self.data);
return ord(self.data[0])+(ord(self.data[1])<<8);
+ def CCpeekcodebyte(self,adr):
+ """Read the contents of code memory at an address."""
+ self.data=[adr&0xff, (adr&0xff00)>>8];
+ self.writecmd(0x30,0x90,2,self.data);
+ return ord(self.data[0]);
+ def CCpeekdatabyte(self,adr):
+ """Read the contents of data memory at an address."""
+ self.data=[adr&0xff, (adr&0xff00)>>8];
+ self.writecmd(0x30,0x91, 2, self.data);
+ return ord(self.data[0]);
+ def CCpokedatabyte(self,adr,val):
+ """Write a byte to data memory."""
+ self.data=[adr&0xff, (adr&0xff00)>>8, val];
+ self.writecmd(0x30, 0x92, 3, self.data);
+ return ord(self.data[0]);
+ def CCchiperase(self):
+ """Erase all of the target's memory."""
+ self.writecmd(0x30,0x80,0,None);
+ def CCstatus(self):
+ """Check the status."""
+ self.writecmd(0x30,0x84,0,None);
+ return ord(self.data[0])
+ CCstatusbits={0x80 : "erased",
+ 0x40 : "pcon_idle",
+ 0x20 : "halted",
+ 0x10 : "pm0",
+ 0x08 : "halted",
+ 0x04 : "locked",
+ 0x02 : "oscstable",
+ 0x01 : "overflow"};
+ def CCstatusstr(self):
+ """Check the status as a string."""
+ status=self.CCstatus();
+ str="";
+ i=1;
+ while i<0x100:
+ if(status&i):
+ str="%s %s" %(self.CCstatusbits[i],str);
+ i*=2;
+ return str;
def MSP430poke(self,adr,val):
"""Read the contents of memory at an address."""
self.data=[adr&0xff, (adr&0xff00)>>8, val&0xff, (val&0xff00)>>8];
self.writecmd(0x30,0x20,0,self.data);
ident=self.CCidentstr();
print "Target identifies as %s." % ident;
+ print "Status: %s." % self.CCstatusstr();
+ self.CCreleasecpu();
+ self.CChaltcpu();
+ print "Status: %s." % self.CCstatusstr();
def CCstop(self):
"""Stop debugging."""
self.writecmd(0x30,0x21,0,self.data);
def MSP430releasecpu(self):
"""Resume the CPU."""
self.writecmd(0x11,0xA1,0,self.data);
-
+ def CChaltcpu(self):
+ """Halt the CPU."""
+ self.writecmd(0x30,0x86,0,self.data);
+ def CCreleasecpu(self):
+ """Resume the CPU."""
+ self.writecmd(0x30,0x87,0,self.data);
def MSP430shiftir8(self,ins):
"""Shift the 8-bit Instruction Register."""
data=[ins];
print "%04x %04x" % (i, self.MSP430peek(i));
i+=2;
def CCtest(self):
+ self.CCreleasecpu();
+ self.CChaltcpu();
+ print "Status: %s" % self.CCstatusstr();
+
#Grab ident three times, should be equal.
ident1=self.CCident();
ident2=self.CCident();
print "%04x, %04x, %04x" % (ident1, ident2, ident3);
#Single step, printing PC.
- print "Tracing execution at startup."
+ #print "Tracing execution at startup."
for i in range(1,15):
- print "PC=%04x" % self.CCgetPC();
+ pc=self.CCgetPC();
+ byte=self.CCpeekcodebyte(i);
+ print "PC=%04x, %02x" % (pc, byte);
self.CCstep_instr();
+
+ #print "Verifying that debugging a NOP doesn't affect the PC."
+ for i in range(1,15):
+ pc=self.CCgetPC();
+ self.CCdebuginstr([0x00]);
+ if(pc!=self.CCgetPC()):
+ print "ERROR: PC changed during CCdebuginstr([NOP])!";
+ for i in range(0xE500,0xE600):
+ byte=self.CCpeekdatabyte(i);
+ print "data %04x: %02x" % (i,byte);
+ self.CCpokedatabyte(i,i&0xFF);
+ byte=self.CCpeekdatabyte(i);
+ print "data %04x: %02x" % (i,byte);
+ print "Status: %s." % self.CCstatusstr();
#Exit debugger
self.CCstop();
print "Done.";
txdata(app,verb,1);
break;
case CC_DEBUG_INSTR:
- txdata(app,NOK,0);//TODO add me
+ cc_debug_instr(len);
+ txdata(app,verb,1);
break;
case CC_STEP_INSTR:
cc_step_instr();
//Macro commands
case CC_READ_CODE_MEMORY:
+ cmddata[0]=peekcodebyte(cmddataword[0]);
+ txdata(app,verb,1);
+ break;
case CC_READ_XDATA_MEMORY:
+ cmddata[0]=peekdatabyte(cmddataword[0]);
+ txdata(app,verb,1);
+ break;
case CC_WRITE_XDATA_MEMORY:
+ cmddata[0]=pokedatabyte(cmddataword[0], cmddata[2]);
+ txdata(app,verb,1);
+ break;
case CC_SET_PC:
case CC_CLOCK_INIT:
case CC_WRITE_FLASH_PAGE:
ccread(1);
return;
}
+
+//! Debug an instruction.
+void cc_debug_instr(unsigned char len){
+ //Bottom two bits of command indicate length.
+ unsigned char cmd=0x54+(len&0x3);
+ CCWRITE;
+ cctrans8(cmd); //Second command code
+ cccmd(len&0x3); //Command itself.
+ ccread(1);
+ return;
+}
+
+//! Debug an instruction, for local use.
+unsigned char cc_debug(unsigned char len,
+ unsigned char a,
+ unsigned char b,
+ unsigned char c){
+ unsigned char cmd=0x54+0x3;//(len&0x3);
+ CCWRITE;
+ cctrans8(cmd);
+ /*if(len--)*/ cctrans8(a);
+ if(len--) cctrans8(b);
+ if(len--) cctrans8(c);
+ CCREAD;
+ return cctrans8(0x00);
+}
+
+//! Fetch a byte of code memory.
+unsigned char peekcodebyte(unsigned long adr){
+ /** See page 9 of SWRA124 */
+ unsigned char bank=adr>>15,
+ lb=adr&0xFF,
+ hb=(adr>>8)&0x7F,
+ toret=0;
+ adr&=0x7FFF;
+
+ //MOV MEMCTR, (bank*16)+1
+ cc_debug(3, 0x75, 0xC7, 0);//(bank<<4) + 1);
+ //MOV DPTR, address
+ cc_debug(3, 0x90, hb, lb);
+
+ //for each byte
+ //CLR A
+ cc_debug(1, 0xE4, 0, 0);
+ //MOVC A, @A+DPTR;
+ toret=cc_debug(1, 0x93, 0, 0);
+ //INC DPTR
+ //cc_debug(1, 0xA3, 0, 0);
+
+ return toret;
+}
+
+
+//! Set a byte of data memory.
+unsigned char pokedatabyte(unsigned int adr,
+ unsigned char val){
+ unsigned char
+ hb=(adr&0xFF00)>>8,
+ lb=adr&0xFF;
+
+ //MOV DPTR, adr
+ cc_debug(3, 0x90, hb, lb);
+ //MOV A, val
+ cc_debug(2, 0x74, val, 0);
+ //MOVX @DPTR, A
+ cc_debug(1, 0xF0, 0, 0);
+
+ return;
+ /*
+DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
+for (n = 0; n < count; n++) {
+ DEBUG_INSTR(IN: 0x74, inputArray[n], OUT: Discard);
+ DEBUG_INSTR(IN: 0xF0, OUT: Discard);
+ DEBUG_INSTR(IN: 0xA3, OUT: Discard);
+}
+ */
+}
+
+//! Fetch a byte of data memory.
+unsigned char peekdatabyte(unsigned int adr){
+ unsigned char
+ hb=(adr&0xFF00)>>8,
+ lb=adr&0xFF,
+ toret;
+
+ //MOV DPTR, adr
+ cc_debug(3, 0x90, hb, lb);
+ //MOVX A, @DPTR
+ //Must be 2, perhaps for clocking?
+ toret=cc_debug(3, 0xE0, 0, 0);
+ return toret;
+
+ /*
+DEBUG_INSTR(IN: 0x90, HIBYTE(address), LOBYTE(address), OUT: Discard);
+for (n = 0; n < count; n++) {
+ DEBUG_INSTR(IN: 0xE0, OUT: outputArray[n]);
+ DEBUG_INSTR(IN: 0xA3, OUT: Discard);
+}
+ */
+}