jtag: retire jtag_get_error()
[openocd/cortex.git] / tools / xsvf_tools / xsvfdump.py
blobe65f8d5b273263fdc26daacaa4009aa157e18f91
1 #!/usr/bin/python3.0
3 # Copyright 2008, SoftPLC Corporation http://softplc.com
4 # Dick Hollenbeck dick@softplc.com
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.
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.
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
23 # Dump an Xilinx XSVF file to stdout
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.
30 import sys
31 import struct
34 LABEL = "A script to dump an XSVF file to stdout"
37 Xsdrsize = 0
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,
43 LCOUNT,LDELAY,LSDR,XTRST) = range(29)
46 (RESET,IDLE,
47 DRSELECT,DRCAPTURE,DRSHIFT,DREXIT1,DRPAUSE,DREXIT2,DRUPDATE,
48 IRSELECT,IRCAPTURE,IRSHIFT,IREXIT1,IRPAUSE,IREXIT2,IRUPDATE) = range(16)
51 State = ("RESET","IDLE",
52 "DRSELECT","DRCAPTURE","DRSHIFT","DREXIT1","DRPAUSE","DREXIT2","DRUPDATE",
53 "IRSELECT","IRCAPTURE","IRSHIFT","IREXIT1","IRPAUSE","IREXIT2","IRUPDATE")
56 trst_mode_allowed = ('ON', 'OFF', 'Z', 'ABSENT')
59 Setsdrmasks = 0
60 SetsdrmasksOnesCount = 0
62 def ReadSDRMASKS( f, len ):
63 global Setsdrmasks, SetsdrmasksOnesCount
64 byteCount = (len+7)//8
65 Setsdrmasks = f.read( byteCount )
66 ls = []
67 SetsdrmasksOnesCount = 0
68 for b in Setsdrmasks:
69 ls.append( "%x" % ((b & 0xf0) >> 4) )
70 ls.append( "%x" % ( b & 0x0f ) )
71 for i in range(8):
72 if b & (1<<i):
73 SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
74 return ''.join(ls)
77 def bytes2hexString( f, len ):
78 byteCount = (len+7)//8
79 bytebuf = f.read( byteCount )
80 ls = []
81 for b in bytebuf:
82 ls.append( "%x" % ((b & 0xf0) >> 4) )
83 ls.append( "%x" % ( b & 0x0f ) )
84 return ''.join(ls)
87 def ReadByte( f ):
88 """Read a byte from a file and return it as an int in least significant 8 bits"""
89 b = f.read(1)
90 if b:
91 return 0xff & b[0];
92 else:
93 return -1
96 def ShowState( state ):
97 """return the given state int as a state string"""
98 #return "0x%02x" % state # comment this out to get textual state form
99 global State
100 if 0 <= state <= IRUPDATE:
101 return State[state]
102 else:
103 return "Unknown state 0x%02x" % state
106 def ShowOpcode( op, f ):
107 """return the given byte as an opcode string"""
108 global Xsdrsize
109 if op == XCOMPLETE:
110 print("XCOMPLETE")
112 elif op == XTDOMASK:
113 buf = bytes2hexString( f, Xsdrsize )
114 print("XTDOMASK 0x%s" % buf)
116 elif op == XSIR:
117 len = ReadByte( f )
118 buf = bytes2hexString( f, len )
119 print("XSIR 0x%02X 0x%s" % (len, buf))
121 elif op == XSDR:
122 tdi = bytes2hexString( f, Xsdrsize )
123 print("XSDR 0x%s" % tdi)
125 elif op == XRUNTEST:
126 len = struct.unpack( '>i', f.read(4) )[0]
127 print("XRUNTEST 0x%08X" % len)
129 elif op == XREPEAT:
130 len = ReadByte( f )
131 print("XREPEAT 0x%02X" % len)
133 elif op == XSDRSIZE:
134 Xsdrsize = struct.unpack( '>i', f.read(4) )[0]
135 #print("XSDRSIZE 0x%08X" % Xsdrsize, file=sys.stderr )
136 print("XSDRSIZE 0x%08X %d" % (Xsdrsize, Xsdrsize) )
138 elif op == XSDRTDO:
139 tdi = bytes2hexString( f, Xsdrsize )
140 tdo = bytes2hexString( f, Xsdrsize )
141 print("XSDRTDO 0x%s 0x%s" % (tdi, tdo) )
143 elif op == XSETSDRMASKS:
144 addrmask = bytes2hexString( f, Xsdrsize )
145 datamask = ReadSDRMASKS( f, Xsdrsize )
146 print("XSETSDRMASKS 0x%s 0x%s" % (addrmask, datamask) )
148 elif op == XSDRINC:
149 startaddr = bytes2hexString( f, Xsdrsize )
150 len = ReadByte(f)
151 print("XSDRINC 0x%s 0x%02X" % (startaddr, len), end='' )
152 for numTimes in range(len):
153 data = bytes2hexString( f, SetsdrmasksOnesCount)
154 print(" 0x%s" % data )
155 print() # newline
157 elif op == XSDRB:
158 tdi = bytes2hexString( f, Xsdrsize )
159 print("XSDRB 0x%s" % tdi )
161 elif op == XSDRC:
162 tdi = bytes2hexString( f, Xsdrsize )
163 print("XSDRC 0x%s" % tdi )
165 elif op == XSDRE:
166 tdi = bytes2hexString( f, Xsdrsize )
167 print("XSDRE 0x%s" % tdi )
169 elif op == XSDRTDOB:
170 tdo = bytes2hexString( f, Xsdrsize )
171 print("XSDRTDOB 0x%s" % tdo )
173 elif op == XSDRTDOC:
174 tdi = bytes2hexString( f, Xsdrsize )
175 tdo = bytes2hexString( f, Xsdrsize )
176 print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
178 elif op == XSDRTDOE:
179 tdi = bytes2hexString( f, Xsdrsize )
180 tdo = bytes2hexString( f, Xsdrsize )
181 print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
183 elif op == XSTATE:
184 b = ReadByte(f)
185 print("XSTATE %s" % ShowState(b))
187 elif op == XENDIR:
188 b = ReadByte( f )
189 print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
191 elif op == XENDDR:
192 b = ReadByte( f )
193 print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
195 elif op == XSIR2:
196 len = struct.unpack( '>H', f.read(2) )[0]
197 buf = bytes2hexString( f, len )
198 print("XSIR2 0x%04X 0x%s" % (len, buf))
200 elif op == XCOMMENT:
201 cmt = []
202 while 1:
203 b = ReadByte(f)
204 if b == 0: # terminating nul
205 break;
206 cmt.append( chr(b) )
207 print("XCOMMENT \"%s\"" % ''.join(cmt) )
209 elif op == XWAIT:
210 run_state = ReadByte(f)
211 end_state = ReadByte(f)
212 useconds = struct.unpack( '>i', f.read(4) )[0]
213 print("XWAIT %s %s" % (ShowState(run_state), ShowState(end_state)), useconds)
215 elif op == XWAITSTATE:
216 run_state = ReadByte(f)
217 end_state = ReadByte(f)
218 clocks = struct.unpack( '>i', f.read(4) )[0]
219 useconds = struct.unpack( '>i', f.read(4) )[0]
220 print("XWAITSTATE %s %s CLOCKS=%d USECS=%d" % (ShowState(run_state), ShowState(end_state), clocks, useconds) )
222 elif op == LCOUNT:
223 loop_count = struct.unpack( '>i', f.read(4) )[0]
224 print("LCOUNT", loop_count )
226 elif op == LDELAY:
227 run_state = ReadByte(f)
228 clocks = struct.unpack( '>i', f.read(4) )[0]
229 useconds = struct.unpack( '>i', f.read(4) )[0]
230 print("LDELAY %s CLOCKS=%d USECS=%d" % (ShowState(run_state), clocks, useconds) )
232 elif op == LSDR:
233 tdi = bytes2hexString( f, Xsdrsize )
234 tdo = bytes2hexString( f, Xsdrsize )
235 print("LSDR 0x%s 0x%s" % (tdi, tdo) )
237 elif op == XTRST:
238 # the argument is a single byte and it is the index into "trst_mode_allowed"
239 trst_mode = ReadByte(f)
240 if trst_mode <= 3:
241 print("TRST %s" % trst_mode_allowed[trst_mode] )
242 else:
243 print("TRST 0x%02X" % trst_mode );
245 else:
246 print("UNKNOWN op 0x%02X %d" % (op, op))
247 exit(1)
250 def main():
252 if len( sys.argv ) < 2:
253 print("usage %s <xsvf_filename>" % sys.argv[0])
254 exit(1)
256 f = open( sys.argv[1], 'rb' )
258 opcode = ReadByte( f )
259 while opcode != -1:
260 # print the position within the file, then the command
261 print( "%d: " % f.tell(), end='' )
262 ShowOpcode( opcode, f )
263 opcode = ReadByte(f)
266 if __name__ == "__main__":
267 main()