944aac26c138835cc6b16491e1f6555ad1b9184b
[goodfet] / client / em260_disass.py
1 import sys
2 import em260_data as em
3
4 ##########################################
5 # Name: em260_spi_disass.py
6 # Author: Don C. Weber
7 # Usage: em260_spi_disass.py <file>
8 #
9 # Notes: This tool will parse SPI data from a Beagle
10 #        dump file taken while monitoring the EM250/EM260.
11 #        You can pass it either MOSI, MISO, 
12 #        or Bidirectional data.  This will output to
13 #        STDOUT.
14 ##########################################
15
16 #debug = 0   # 0 == off, 1 == on
17 debug = 1   # 0 == off, 1 == on
18
19 def process_data(ascii_data):
20     byte = 0
21
22     while byte != len(ascii_data):
23         outLine = []
24         spi_byte = int(ascii_data[byte:byte+2],16)    # process this byte
25         byte += 2       # next byte
26
27         # ORDER IS IMPORTANT HERE SO LIMIT TABLE ASSOCIATED
28         # WITH SPI BYTE VALUES
29     
30         # Continuation Test
31         # Do this first to save on processing
32         if spi_byte == 0xFF:
33             #if debug: print ": ".join(outLine)
34             continue
35     
36         # SPI Error Test
37         # Do this second to save on processing
38         if spi_byte <= 0x04:
39             # Grab byte error
40             #outLine.append("Error Byte")
41             outLine.append(ascii_data[byte:byte+2])
42             byte += 2       # next byte
43             # Termination Test
44             if int(ascii_data[byte:byte+2],16) != 0xA7:
45                 outLine.append("Frame Termination Error")
46                 outLine.append(ascii_data[byte:byte+2])
47             print ":    ".join(outLine)
48             byte += 2       # next byte
49             continue
50
51         # SPI Version Response Test
52         # Do this third to keep it from the truncated SPI table
53         # FIXME: A mask should work here!!!
54         #if ((spi_byte & em.spi_version_mask) == 0x80):
55         if ((spi_byte >= 0x81) and (spi_byte <= 0xBF)):
56             outLine.append("SPI Version")
57             outLine.append(hex(spi_byte))
58             # Termination Test
59             if int(ascii_data[byte:byte+2],16) != 0xA7:
60                 outLine.append("Frame Termination Error")
61                 outLine.append(ascii_data[byte:byte+2])
62             print ":    ".join(outLine)
63             byte += 2       # next byte
64             continue
65
66         # SPI Byte Value
67         try:        # Test for value else error
68             outLine.append(em.spi_values[em.spi_bytes.index(spi_byte)])
69         except:
70             outLine.append("Unknown SPI Byte Value")
71             outLine.append(repr(hex(spi_byte)))
72             print ":    ".join(outLine)
73             continue
74     
75         # SPI Protocol Version and Status Test
76         if spi_byte in em.spi_pro_stat:
77             # Termination Test
78             if int(ascii_data[byte:byte+2],16) != 0xA7:
79                 outLine.append("Frame Termination Error")
80                 outLine.append(ascii_data[byte:byte+2])
81             print ":    ".join(outLine)
82             byte += 2       # next byte
83             continue
84     
85         # EZSP Frame Test
86         # Process the Frame
87         if spi_byte == 0xFE:
88             # Get Frame Length
89             data_length = int(ascii_data[byte:byte+2],16)
90             data_length *= 2     # For ASCII the length must be doubled
91             if debug: 
92                 outLine.append("Len")
93                 outLine.append(str(data_length))
94             byte += 2       # next byte
95     
96             # Use Length to get Frame Data
97             data_frame = ascii_data[byte:byte+data_length]
98             if debug: 
99                 outLine.append("Data")
100                 outLine.append(str(data_length))
101             byte += data_length       # skip frames
102     
103             # Test for Frame Terminator before processing
104             if int(ascii_data[byte:byte+2],16) != 0xA7:
105                 outLine.append("Frame Termination Error")
106                 outLine.append(ascii_data[byte:byte+2])
107                 # Carrying on to next input
108                 print ":        ".join(outLine)
109                 byte += 2       # next byte
110                 continue
111     
112             # Process Frame Data
113             dfdata = 0
114     
115             # Handle Frame Counter
116             outLine.insert(0, data_frame[dfdata:dfdata+2])
117     
118             # Handle Data
119             dfdata += 2       # next byte
120             cmd_data = int(data_frame[dfdata:dfdata+2],16)
121     
122             # Response Test
123             if (cmd_data & em.cmd_mask):
124                 outLine.append("RESP")
125                 # Overflow Test
126                 if (cmd_data & em.resp_over_mask):
127                     outLine.append(em.resp_overflow)
128                 # Truncated Data Test
129                 if (cmd_data & em.resp_trun_mask):
130                     outLine.append(em.resp_truncated)
131             # Command Test
132             else:
133                 # Try in case reserved bits set take em.cmd_control out of range
134                 try:
135                     outLine.append(em.cmd_control[cmd_data])
136                 except:
137                     outLine.append("Unknown Control Command")
138                     # Carrying on to next input
139                     byte += 2       # next byte
140                     continue
141     
142             # Frame Identification (A.K.A.: VERB)
143             dfdata += 2       # next byte
144             frame_id_data = int(data_frame[dfdata:dfdata+2],16)
145             try:
146                 outLine.append(em.frame_ids[frame_id_data])
147             except:
148                 outLine.append("Unknown Frame Id")
149     
150             # Frame Data
151             # Not Implemented but we can tack it onto the end
152             # for manual checking
153             dfdata += 2       # next byte
154             if dfdata < len(data_frame):
155                 outLine.append("Frame Data")
156                 outLine.append(data_frame[dfdata:])
157     
158             # Carrying on to next input
159             print outLine
160             print ":    ".join(outLine)
161             byte += 2       # next byte
162             continue
163
164 #################
165 # Main
166 #################
167
168 # Get Input - Remove whitespaces just in case
169 #ascii_data = sys.argv[1].replace(' ','')
170 if __name__ == "__main__":
171         INF = open(sys.argv[1],'r')
172
173         for line in INF.readlines():
174             indata = line.replace(' ','').strip()
175             process_data(indata)