tcl/target/bl702: implement full software reset
[openocd.git] / contrib / xsvf_tools / xsvfdump.py
blob3ed4009bdf5eae07b91b37826bc577f321422703
1 #!/usr/bin/python3.0
2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Copyright 2008, SoftPLC Corporation http://softplc.com
5 # Dick Hollenbeck dick@softplc.com
7 # Dump an Xilinx XSVF file to stdout
9 # This program is written for python 3.0, and it is not easy to change this
10 # back to 2.x. You may find it easier to use python 3.x even if that means
11 # building it.
14 import sys
15 import struct
18 LABEL = "A script to dump an XSVF file to stdout"
21 Xsdrsize = 0
24 (XCOMPLETE,XTDOMASK,XSIR,XSDR,XRUNTEST,hole0,hole1,XREPEAT,XSDRSIZE,XSDRTDO,
25 XSETSDRMASKS,XSDRINC,XSDRB,XSDRC,XSDRE,XSDRTDOB,XSDRTDOC,
26 XSDRTDOE,XSTATE,XENDIR,XENDDR,XSIR2,XCOMMENT,XWAIT,XWAITSTATE,
27 LCOUNT,LDELAY,LSDR,XTRST) = range(29)
30 (RESET,IDLE,
31 DRSELECT,DRCAPTURE,DRSHIFT,DREXIT1,DRPAUSE,DREXIT2,DRUPDATE,
32 IRSELECT,IRCAPTURE,IRSHIFT,IREXIT1,IRPAUSE,IREXIT2,IRUPDATE) = range(16)
35 State = ("RESET","IDLE",
36 "DRSELECT","DRCAPTURE","DRSHIFT","DREXIT1","DRPAUSE","DREXIT2","DRUPDATE",
37 "IRSELECT","IRCAPTURE","IRSHIFT","IREXIT1","IRPAUSE","IREXIT2","IRUPDATE")
40 trst_mode_allowed = ('ON', 'OFF', 'Z', 'ABSENT')
43 Setsdrmasks = 0
44 SetsdrmasksOnesCount = 0
46 def ReadSDRMASKS( f, len ):
47 global Setsdrmasks, SetsdrmasksOnesCount
48 byteCount = (len+7)//8
49 Setsdrmasks = f.read( byteCount )
50 ls = []
51 SetsdrmasksOnesCount = 0
52 for b in Setsdrmasks:
53 ls.append( "%x" % ((b & 0xf0) >> 4) )
54 ls.append( "%x" % ( b & 0x0f ) )
55 for i in range(8):
56 if b & (1<<i):
57 SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
58 return ''.join(ls)
61 def bytes2hexString( f, len ):
62 byteCount = (len+7)//8
63 bytebuf = f.read( byteCount )
64 ls = []
65 for b in bytebuf:
66 ls.append( "%x" % ((b & 0xf0) >> 4) )
67 ls.append( "%x" % ( b & 0x0f ) )
68 return ''.join(ls)
71 def ReadByte( f ):
72 """Read a byte from a file and return it as an int in least significant 8 bits"""
73 b = f.read(1)
74 if b:
75 return 0xff & b[0];
76 else:
77 return -1
80 def ShowState( state ):
81 """return the given state int as a state string"""
82 #return "0x%02x" % state # comment this out to get textual state form
83 global State
84 if 0 <= state <= IRUPDATE:
85 return State[state]
86 else:
87 return "Unknown state 0x%02x" % state
90 def ShowOpcode( op, f ):
91 """return the given byte as an opcode string"""
92 global Xsdrsize
93 if op == XCOMPLETE:
94 print("XCOMPLETE")
96 elif op == XTDOMASK:
97 buf = bytes2hexString( f, Xsdrsize )
98 print("XTDOMASK 0x%s" % buf)
100 elif op == XSIR:
101 len = ReadByte( f )
102 buf = bytes2hexString( f, len )
103 print("XSIR 0x%02X 0x%s" % (len, buf))
105 elif op == XSDR:
106 tdi = bytes2hexString( f, Xsdrsize )
107 print("XSDR 0x%s" % tdi)
109 elif op == XRUNTEST:
110 len = struct.unpack( '>i', f.read(4) )[0]
111 print("XRUNTEST 0x%08X" % len)
113 elif op == XREPEAT:
114 len = ReadByte( f )
115 print("XREPEAT 0x%02X" % len)
117 elif op == XSDRSIZE:
118 Xsdrsize = struct.unpack( '>i', f.read(4) )[0]
119 #print("XSDRSIZE 0x%08X" % Xsdrsize, file=sys.stderr )
120 print("XSDRSIZE 0x%08X %d" % (Xsdrsize, Xsdrsize) )
122 elif op == XSDRTDO:
123 tdi = bytes2hexString( f, Xsdrsize )
124 tdo = bytes2hexString( f, Xsdrsize )
125 print("XSDRTDO 0x%s 0x%s" % (tdi, tdo) )
127 elif op == XSETSDRMASKS:
128 addrmask = bytes2hexString( f, Xsdrsize )
129 datamask = ReadSDRMASKS( f, Xsdrsize )
130 print("XSETSDRMASKS 0x%s 0x%s" % (addrmask, datamask) )
132 elif op == XSDRINC:
133 startaddr = bytes2hexString( f, Xsdrsize )
134 len = ReadByte(f)
135 print("XSDRINC 0x%s 0x%02X" % (startaddr, len), end='' )
136 for numTimes in range(len):
137 data = bytes2hexString( f, SetsdrmasksOnesCount)
138 print(" 0x%s" % data )
139 print() # newline
141 elif op == XSDRB:
142 tdi = bytes2hexString( f, Xsdrsize )
143 print("XSDRB 0x%s" % tdi )
145 elif op == XSDRC:
146 tdi = bytes2hexString( f, Xsdrsize )
147 print("XSDRC 0x%s" % tdi )
149 elif op == XSDRE:
150 tdi = bytes2hexString( f, Xsdrsize )
151 print("XSDRE 0x%s" % tdi )
153 elif op == XSDRTDOB:
154 tdo = bytes2hexString( f, Xsdrsize )
155 print("XSDRTDOB 0x%s" % tdo )
157 elif op == XSDRTDOC:
158 tdi = bytes2hexString( f, Xsdrsize )
159 tdo = bytes2hexString( f, Xsdrsize )
160 print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
162 elif op == XSDRTDOE:
163 tdi = bytes2hexString( f, Xsdrsize )
164 tdo = bytes2hexString( f, Xsdrsize )
165 print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
167 elif op == XSTATE:
168 b = ReadByte(f)
169 print("XSTATE %s" % ShowState(b))
171 elif op == XENDIR:
172 b = ReadByte( f )
173 print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
175 elif op == XENDDR:
176 b = ReadByte( f )
177 print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
179 elif op == XSIR2:
180 len = struct.unpack( '>H', f.read(2) )[0]
181 buf = bytes2hexString( f, len )
182 print("XSIR2 0x%04X 0x%s" % (len, buf))
184 elif op == XCOMMENT:
185 cmt = []
186 while 1:
187 b = ReadByte(f)
188 if b == 0: # terminating nul
189 break;
190 cmt.append( chr(b) )
191 print("XCOMMENT \"%s\"" % ''.join(cmt) )
193 elif op == XWAIT:
194 run_state = ReadByte(f)
195 end_state = ReadByte(f)
196 useconds = struct.unpack( '>i', f.read(4) )[0]
197 print("XWAIT %s %s" % (ShowState(run_state), ShowState(end_state)), useconds)
199 elif op == XWAITSTATE:
200 run_state = ReadByte(f)
201 end_state = ReadByte(f)
202 clocks = struct.unpack( '>i', f.read(4) )[0]
203 useconds = struct.unpack( '>i', f.read(4) )[0]
204 print("XWAITSTATE %s %s CLOCKS=%d USECS=%d" % (ShowState(run_state), ShowState(end_state), clocks, useconds) )
206 elif op == LCOUNT:
207 loop_count = struct.unpack( '>i', f.read(4) )[0]
208 print("LCOUNT", loop_count )
210 elif op == LDELAY:
211 run_state = ReadByte(f)
212 clocks = struct.unpack( '>i', f.read(4) )[0]
213 useconds = struct.unpack( '>i', f.read(4) )[0]
214 print("LDELAY %s CLOCKS=%d USECS=%d" % (ShowState(run_state), clocks, useconds) )
216 elif op == LSDR:
217 tdi = bytes2hexString( f, Xsdrsize )
218 tdo = bytes2hexString( f, Xsdrsize )
219 print("LSDR 0x%s 0x%s" % (tdi, tdo) )
221 elif op == XTRST:
222 # the argument is a single byte and it is the index into "trst_mode_allowed"
223 trst_mode = ReadByte(f)
224 if trst_mode <= 3:
225 print("TRST %s" % trst_mode_allowed[trst_mode] )
226 else:
227 print("TRST 0x%02X" % trst_mode );
229 else:
230 print("UNKNOWN op 0x%02X %d" % (op, op))
231 exit(1)
234 def main():
236 if len( sys.argv ) < 2:
237 print("usage %s <xsvf_filename>" % sys.argv[0])
238 exit(1)
240 f = open( sys.argv[1], 'rb' )
242 opcode = ReadByte( f )
243 while opcode != -1:
244 # print the position within the file, then the command
245 print( "%d: " % f.tell(), end='' )
246 ShowOpcode( opcode, f )
247 opcode = ReadByte(f)
250 if __name__ == "__main__":
251 main()