jtagarm: a few cleanup things...
[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 cries for help.
55         if(str[0]=="?"):  
56             print "Term Commands:"
57             print commands
58             return;
59         #Lines beginning with # are comments.
60         if(str[0]=="#"):  return;
61         #Lines beginning with ! are Python.
62         if(str[0]=="!"):
63             try:
64                 exec(str.lstrip('!'));
65             except:
66                 print sys.exc_info()[0];
67             return;
68         #Backtick (`) indicates shell commands.
69         if(str[0]=='`'):
70             os.system(str.lstrip('`'));
71             return;
72         #By this point, we're looking at a GoodFET command.
73         args=str.split();
74         if len(args)==0:
75             return;
76         try:
77             eval("self.CMD%s(args)" % args[0])
78         except:
79             print sys.exc_info()[0];
80             #print "Unknown command '%s'." % args[0];
81     def CMDinfo(self,args):
82         print self.client.infostring()
83     def CMDlock(self,args):
84         print "Locking.";
85         self.client.lock();
86     def CMDerase(self,args):
87         print "Erasing.";
88         self.client.erase();
89     def CMDtest(self,args):
90         self.client.test();
91         return;
92     def CMDstatus(self,args):
93         print self.client.status();
94         return;
95     def CMDhalt(self,args):
96         print self.client.halt();
97     def CMDresume(self,args):
98         print self.client.resume();
99     def CMDpeek(self,args):
100         adr=args[1];
101         memory="vn";
102         if(len(args)>2):
103             memory=args[2];
104         adr= self.client.name2adr(adr);
105         #print "%i" % adr;
106         print "0x%08x:= 0x%04x" % (
107             adr, self.client.peek16(adr,
108                                   memory));
109     def CMDflash(self,args):
110         file=args[1];
111         self.client.flash(self.expandfilename(file));
112     def CMDdump(self,args):
113         file=args[1];
114         self.client.dump(self.expandfilename(file));
115     def CMDwhere(self,args):
116         pc=self.client.getpc();
117         print "PC=0x%04X" % pc;
118     def CMDchip(self,args):
119         cmd="self.client.CMD%s()" % args[1];
120         print cmd;
121         try:
122             eval(cmd);
123         except:
124             print sys.exc_info()[0];
125             print "Chip-specific command failed.";
126     def expandfilename(self,filename):
127         if(filename[0]=='~'):
128             return "%s%s" % (os.environ.get("HOME"),filename.lstrip('~'));
129         return filename;
130     
131