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
18 LABEL = "A script to dump an XSVF file to stdout"
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)
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')
44 SetsdrmasksOnesCount = 0
46 def ReadSDRMASKS( f, len ):
47 global Setsdrmasks, SetsdrmasksOnesCount
48 byteCount = (len+7)//8
49 Setsdrmasks = f.read( byteCount )
51 SetsdrmasksOnesCount = 0
53 ls.append( "%x" % ((b & 0xf0) >> 4) )
54 ls.append( "%x" % ( b & 0x0f ) )
57 SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
61 def bytes2hexString( f, len ):
62 byteCount = (len+7)//8
63 bytebuf = f.read( byteCount )
66 ls.append( "%x" % ((b & 0xf0) >> 4) )
67 ls.append( "%x" % ( b & 0x0f ) )
72 """Read a byte from a file and return it as an int in least significant 8 bits"""
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
84 if 0 <= state <= IRUPDATE:
87 return "Unknown state 0x%02x" % state
90 def ShowOpcode( op, f ):
91 """return the given byte as an opcode string"""
97 buf = bytes2hexString( f, Xsdrsize )
98 print("XTDOMASK 0x%s" % buf)
102 buf = bytes2hexString( f, len )
103 print("XSIR 0x%02X 0x%s" % (len, buf))
106 tdi = bytes2hexString( f, Xsdrsize )
107 print("XSDR 0x%s" % tdi)
110 len = struct.unpack( '>i', f.read(4) )[0]
111 print("XRUNTEST 0x%08X" % len)
115 print("XREPEAT 0x%02X" % len)
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) )
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) )
133 startaddr = bytes2hexString( f, Xsdrsize )
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 )
142 tdi = bytes2hexString( f, Xsdrsize )
143 print("XSDRB 0x%s" % tdi )
146 tdi = bytes2hexString( f, Xsdrsize )
147 print("XSDRC 0x%s" % tdi )
150 tdi = bytes2hexString( f, Xsdrsize )
151 print("XSDRE 0x%s" % tdi )
154 tdo = bytes2hexString( f, Xsdrsize )
155 print("XSDRTDOB 0x%s" % tdo )
158 tdi = bytes2hexString( f, Xsdrsize )
159 tdo = bytes2hexString( f, Xsdrsize )
160 print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
163 tdi = bytes2hexString( f, Xsdrsize )
164 tdo = bytes2hexString( f, Xsdrsize )
165 print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
169 print("XSTATE %s" % ShowState(b))
173 print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
177 print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
180 len = struct.unpack( '>H', f.read(2) )[0]
181 buf = bytes2hexString( f, len )
182 print("XSIR2 0x%04X 0x%s" % (len, buf))
188 if b == 0: # terminating nul
191 print("XCOMMENT \"%s\"" % ''.join(cmt) )
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) )
207 loop_count = struct.unpack( '>i', f.read(4) )[0]
208 print("LCOUNT", loop_count )
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) )
217 tdi = bytes2hexString( f, Xsdrsize )
218 tdo = bytes2hexString( f, Xsdrsize )
219 print("LSDR 0x%s 0x%s" % (tdi, tdo) )
222 # the argument is a single byte and it is the index into "trst_mode_allowed"
223 trst_mode = ReadByte(f)
225 print("TRST %s" % trst_mode_allowed[trst_mode] )
227 print("TRST 0x%02X" % trst_mode );
230 print("UNKNOWN op 0x%02X %d" % (op, op))
236 if len( sys.argv ) < 2:
237 print("usage %s <xsvf_filename>" % sys.argv[0])
240 f = open( sys.argv[1], 'rb' )
242 opcode = ReadByte( f )
244 # print the position within the file, then the command
245 print( "%d: " % f.tell(), end='' )
246 ShowOpcode( opcode, f )
250 if __name__ == "__main__":