cf74cf9677dfa2644d99d6cb904c7d5f5c11546b
[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 class GoodFETConsole():
15     """An interactive goodfet driver."""
16     
17     def __init__(self, client):
18         self.client=client;
19         client.serInit();
20         client.setup();
21         client.start();
22     def prompt(self):
23         sys.stdout.write("gf% ");
24         sys.stdout.flush();
25     def run(self):
26         self.prompt();
27         #for cmd in sys.stdin:
28         while 1:
29             cmd=sys.stdin.readline();
30             if not cmd: break;
31             if(cmd.strip()!=""):
32                 self.handle(cmd);
33             self.prompt();
34     def handle(self, str):
35         """Handle a command string.  First word is command."""
36         #Lines beginning with # are comments.
37         if(str[0]=="#"):  return;
38         #Lines beginning with ! are Python.
39         if(str[0]=="!"):
40             try:
41                 exec(str.lstrip('!'));
42             except:
43                 print sys.exc_info()[0];
44             return;
45         #Backtick (`) indicates shell commands.
46         if(str[0]=='`'):
47             os.system(str.lstrip('`'));
48             return;
49         #By this point, we're looking at a GoodFET command.
50         args=str.split();
51         if len(args)==0:
52             return;
53         try:
54             eval("self.CMD%s(args)" % args[0])
55         except:
56             print sys.exc_info()[0];
57             #print "Unknown command '%s'." % args[0];
58     def CMDinfo(self,args):
59         print self.client.infostring()
60     def CMDlock(self,args):
61         print "Locking.";
62         self.client.lock();
63     def CMDerase(self,args):
64         print "Erasing.";
65         self.client.erase();
66     def CMDtest(self,args):
67         self.client.test();
68         return;
69     def CMDstatus(self,args):
70         print self.client.status();
71         return;
72     def CMDpeek(self,args):
73         adr=eval(args[1]);
74         memory="vn";
75         if(len(args)>2):
76             memory=args[2];
77         print "0x%08x:= 0x%04x" % (adr, self.client.peek16(adr,memory));
78     def CMDflash(self,args):
79         file=args[1];
80         self.client.flash(self.expandfilename(file));
81     def CMDchip(self,args):
82         cmd="self.client.CMD%s()" % args[1];
83         print cmd;
84         try:
85             eval(cmd);
86         except:
87             print sys.exc_info()[0];
88             print "Chip-specific command failed.";
89     def expandfilename(self,filename):
90         if(filename[0]=='~'):
91             return "%s%s" % (os.environ.get("HOME"),filename.lstrip('~'));
92         return filename;
93     
94