]> git.sur5r.net Git - openocd/blob - tools/xsvf_tools/xsvfdump.py
Dick Hollenbeck <dick@softplc.com> SVF to XSVF converter and the XSVF dumper take #2
[openocd] / tools / xsvf_tools / xsvfdump.py
1 #!/usr/bin/python3.0
2
3 # Copyright 2008, SoftPLC Corporation  http://softplc.com
4 # Dick Hollenbeck dick@softplc.com
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, you may find one here:
18 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 # or you may search the http://www.gnu.org website for the version 2 license,
20 # or you may write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22
23 # Dump an Xilinx XSVF file to stdout
24
25 # This program is written for python 3.0, and it is not easy to change this
26 # back to 2.x.  You may find it easier to use python 3.x even if that means
27 # building it.
28
29
30 import sys
31 import struct
32
33
34 LABEL = "A script to dump an XSVF file to stdout"
35
36
37 Xsdrsize = 0
38
39
40 (XCOMPLETE,XTDOMASK,XSIR,XSDR,XRUNTEST,hole0,hole1,XREPEAT,XSDRSIZE,XSDRTDO,
41     XSETSDRMASKS,XSDRINC,XSDRB,XSDRC,XSDRE,XSDRTDOB,XSDRTDOC,
42     XSDRTDOE,XSTATE,XENDIR,XENDDR,XSIR2,XCOMMENT,XWAIT,XWAITSTATE,LCOUNT,LDELAY,LSDR) = range(28)
43
44
45 (RESET,IDLE,
46     DRSELECT,DRCAPTURE,DRSHIFT,DREXIT1,DRPAUSE,DREXIT2,DRUPDATE,
47     IRSELECT,IRCAPTURE,IRSHIFT,IREXIT1,IRPAUSE,IREXIT2,IRUPDATE) = range(16)
48
49
50 State = ("RESET","IDLE",
51     "DRSELECT","DRCAPTURE","DRSHIFT","DREXIT1","DRPAUSE","DREXIT2","DRUPDATE",
52     "IRSELECT","IRCAPTURE","IRSHIFT","IREXIT1","IRPAUSE","IREXIT2","IRUPDATE")
53
54 Setsdrmasks = 0
55 SetsdrmasksOnesCount = 0
56
57 def ReadSDRMASKS( f, len ):
58     global Setsdrmasks, SetsdrmasksOnesCount
59     byteCount = (len+7)//8
60     Setsdrmasks = f.read( byteCount )
61     ls = []
62     SetsdrmasksOnesCount = 0
63     for b in Setsdrmasks:
64         ls.append( "%x" % ((b & 0xf0) >> 4) )
65         ls.append( "%x" % ( b & 0x0f ) )
66         for i in range(8):
67             if b & (1<<i):
68                 SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
69     return ''.join(ls)
70
71
72 def bytes2hexString( f, len ):
73     byteCount = (len+7)//8
74     bytebuf = f.read( byteCount )
75     ls = []
76     for b in bytebuf:
77         ls.append( "%x" % ((b & 0xf0) >> 4) )
78         ls.append( "%x" % ( b & 0x0f ) )
79     return ''.join(ls)
80
81
82 def ReadByte( f ):
83     """Read a byte from a file and return it as an int in least significant 8 bits"""
84     b = f.read(1)
85     if b:
86         return 0xff & b[0];
87     else:
88         return -1
89
90
91 def ShowState( state ):
92     """return the given state int as a state string"""
93     #return "0x%02x" % state # comment this out to get textual state form
94     global State
95     if 0 <= state <= IRUPDATE:
96         return State[state]
97     else:
98         return "Unknown state 0x%02x" % state
99
100
101 def ShowOpcode( op, f ):
102     """return the given byte as an opcode string"""
103     global Xsdrsize
104     if op == XCOMPLETE:
105         print("XCOMPLETE")
106
107     elif op == XTDOMASK:
108         buf = bytes2hexString( f, Xsdrsize )
109         print("XTDOMASK 0x%s" % buf)
110
111     elif op == XSIR:
112         len = ReadByte( f )
113         buf = bytes2hexString( f, len )
114         print("XSIR 0x%02X 0x%s" % (len, buf))
115
116     elif op == XSDR:
117         tdi = bytes2hexString( f, Xsdrsize )
118         print("XSDR 0x%s" % tdi)
119
120     elif op == XRUNTEST:
121         len = struct.unpack( '>i', f.read(4) )[0]
122         print("XRUNTEST 0x%08X" % len)
123
124     elif op == XREPEAT:
125         len = ReadByte( f )
126         print("XREPEAT 0x%02X" % len)
127
128     elif op == XSDRSIZE:
129         Xsdrsize = struct.unpack( '>i', f.read(4) )[0]
130         #print("XSDRSIZE 0x%08X" % Xsdrsize, file=sys.stderr )
131         print("XSDRSIZE 0x%08X %d" % (Xsdrsize, Xsdrsize) )
132
133     elif op == XSDRTDO:
134         tdi = bytes2hexString( f, Xsdrsize )
135         tdo = bytes2hexString( f, Xsdrsize )
136         print("XSDRTDO 0x%s 0x%s" % (tdi, tdo) )
137
138     elif op == XSETSDRMASKS:
139         addrmask = bytes2hexString( f, Xsdrsize )
140         datamask = ReadSDRMASKS( f, Xsdrsize )
141         print("XSETSDRMASKS 0x%s 0x%s" % (addrmask, datamask) )
142
143     elif op == XSDRINC:
144         startaddr = bytes2hexString( f, Xsdrsize )
145         len = ReadByte(f)
146         print("XSDRINC 0x%s 0x%02X" % (startaddr, len), end='' )
147         for numTimes in range(len):
148             data = bytes2hexString( f, SetsdrmasksOnesCount)
149             print(" 0x%s" % data )
150         print() # newline
151
152     elif op == XSDRB:
153         tdi = bytes2hexString( f, Xsdrsize )
154         print("XSDRB 0x%s" % tdi )
155
156     elif op == XSDRC:
157         tdi = bytes2hexString( f, Xsdrsize )
158         print("XSDRC 0x%s" % tdi )
159
160     elif op == XSDRE:
161         tdi = bytes2hexString( f, Xsdrsize )
162         print("XSDRE 0x%s" % tdi )
163
164     elif op == XSDRTDOB:
165         tdo = bytes2hexString( f, Xsdrsize )
166         print("XSDRTDOB 0x%s" % tdo )
167
168     elif op == XSDRTDOC:
169         tdi = bytes2hexString( f, Xsdrsize )
170         tdo = bytes2hexString( f, Xsdrsize )
171         print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
172
173     elif op == XSDRTDOE:
174         tdi = bytes2hexString( f, Xsdrsize )
175         tdo = bytes2hexString( f, Xsdrsize )
176         print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
177
178     elif op == XSTATE:
179         b = ReadByte(f)
180         print("XSTATE %s" % ShowState(b))
181
182     elif op == XENDIR:
183         b = ReadByte( f )
184         print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
185
186     elif op == XENDDR:
187         b = ReadByte( f )
188         print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
189
190     elif op == XSIR2:
191         len = struct.unpack( '>H', f.read(2) )[0]
192         buf = bytes2hexString( f, len )
193         print("XSIR2 0x%04X 0x%s" % (len, buf))
194
195     elif op == XCOMMENT:
196         cmt = []
197         while 1:
198             b = ReadByte(f)
199             if b == 0:          # terminating nul
200                 break;
201             cmt.append( chr(b) )
202         print("XCOMMENT \"%s\"" % ''.join(cmt)  )
203
204     elif op == XWAIT:
205         run_state = ReadByte(f)
206         end_state = ReadByte(f)
207         useconds  = struct.unpack( '>i', f.read(4) )[0]
208         print("XWAIT %s %s" % (ShowState(run_state), ShowState(end_state)), useconds)
209
210     elif op == XWAITSTATE:
211         run_state = ReadByte(f)
212         end_state = ReadByte(f)
213         clocks    = struct.unpack( '>i', f.read(4) )[0]
214         useconds  = struct.unpack( '>i', f.read(4) )[0]
215         print("XWAITSTATE %s %s CLOCKS=%d USECS=%d" % (ShowState(run_state), ShowState(end_state), clocks, useconds) )
216
217     elif op == LCOUNT:
218         loop_count = struct.unpack( '>i', f.read(4) )[0]
219         print("LCOUNT", loop_count )
220
221     elif op == LDELAY:
222         run_state = ReadByte(f)
223         clocks    = struct.unpack( '>i', f.read(4) )[0]
224         useconds  = struct.unpack( '>i', f.read(4) )[0]
225         print("LDELAY %s CLOCKS=%d USECS=%d" % (ShowState(run_state), clocks, useconds) )
226
227     elif op == LSDR:
228         tdi = bytes2hexString( f, Xsdrsize )
229         tdo = bytes2hexString( f, Xsdrsize )
230         print("LSDR 0x%s 0x%s" % (tdi, tdo) )
231
232     else:
233         print("UNKNOWN op 0x%02X %d" % (op, op))
234         exit(1)
235
236
237 def main():
238
239     if len( sys.argv ) < 2:
240         print("usage %s <xsvf_filename>" % sys.argv[0])
241         exit(1)
242
243     f = open( sys.argv[1], 'rb' )
244
245     opcode = ReadByte( f )
246     while opcode != -1:
247         # print the position within the file, then the command
248         print( "%d: " % f.tell(), end='' )
249         ShowOpcode( opcode, f )
250         opcode = ReadByte(f)
251
252
253 if __name__ == "__main__":
254     main()
255