goodfet.ccspi added txpipscapy in an alpha version, starting to generate pip/pop...
[goodfet] / client / GoodFETConsole.py
1 #!/usr/bin/env python
2 # GoodFET Client Library
3
4 # (C) 2009 Travis Goodspeed <travis at radiantmachines.com>
5 #
6 # This code is being rewritten and refactored.  You've been warned!
7
8 import sys, os;
9 import binascii;
10
11 from GoodFET import GoodFET;
12 from intelhex import IntelHex;
13
14
15 #grep CMD GoodFETConsole.py | grep def | sed s/\(sel.\*// | sed 's/def CMD//'
16 commands="""
17     info
18     lock
19     erase
20     test
21     status
22     halt
23     resume
24     peek
25     flash
26     dump
27     where
28     chip
29 """
30
31 class GoodFETConsole():
32     """An interactive goodfet driver."""
33     
34     def __init__(self, client):
35         self.client=client;
36         client.serInit();
37         client.setup();
38         client.start();
39         client.loadsymbols();
40     def prompt(self):
41         sys.stdout.write("gf% ");
42         sys.stdout.flush();
43     def run(self):
44         self.prompt();
45         #for cmd in sys.stdin:
46         while 1:
47             cmd=sys.stdin.readline();
48             if not cmd: break;
49             if(cmd.strip()!=""):
50                 self.handle(cmd);
51             self.prompt();
52     def handle(self, str):
53         """Handle a command string.  First word is command."""
54         #Lines beginning with # are comments.
55         if(str[0]=="#"):  return;
56         #Lines beginning with ! are Python.
57         if(str[0]=="!"):
58             try:
59                 exec(str.lstrip('!'));
60             except:
61                 print sys.exc_info()[0];
62             return;
63         #Backtick (`) indicates shell commands.
64         if(str[0]=='`'):
65             os.system(str.lstrip('`'));
66             return;
67         #By this point, we're looking at a GoodFET command.
68         args=str.split();
69         if len(args)==0:
70             return;
71         try:
72             eval("self.CMD%s(args)" % args[0])
73         except:
74             print sys.exc_info()[0];
75             #print "Unknown command '%s'." % args[0];
76     def CMDinfo(self,args):
77         print self.client.infostring()
78     def CMDlock(self,args):
79         print "Locking.";
80         self.client.lock();
81     def CMDerase(self,args):
82         print "Erasing.";
83         self.client.erase();
84     def CMDtest(self,args):
85         self.client.test();
86         return;
87     def CMDstatus(self,args):
88         print self.client.status();
89         return;
90     def CMDhalt(self,args):
91         print self.client.halt();
92     def CMDresume(self,args):
93         print self.client.resume();
94     def CMDpeek(self,args):
95         adr=args[1];
96         memory="vn";
97         if(len(args)>2):
98             memory=args[2];
99         adr= self.client.name2adr(adr);
100         #print "%i" % adr;
101         print "0x%08x:= 0x%04x" % (
102             adr, self.client.peek16(adr,
103                                   memory));
104     def CMDflash(self,args):
105         file=args[1];
106         self.client.flash(self.expandfilename(file));
107     def CMDdump(self,args):
108         file=args[1];
109         self.client.dump(self.expandfilename(file));
110     def CMDwhere(self,args):
111         pc=self.client.getpc();
112         print "PC=0x%04X" % pc;
113     def CMDchip(self,args):
114         cmd="self.client.CMD%s()" % args[1];
115         print cmd;
116         try:
117             eval(cmd);
118         except:
119             print sys.exc_info()[0];
120             print "Chip-specific command failed.";
121     def expandfilename(self,filename):
122         if(filename[0]=='~'):
123             return "%s%s" % (os.environ.get("HOME"),filename.lstrip('~'));
124         return filename;
125     
126